I want to use python to create a pipe readfd, writefd = os.pipe()
. And then I create a subprocess inside my python script to run some c code where it tries to open the readfd(which is an int say 5) FILE* fp = fdopen(readfd, "r");
but it returns a nullptr. Why is this wrong? How can I open the file descriptor in my c code?
Asked
Active
Viewed 822 times
1

Herbert
- 538
- 1
- 4
- 15
-
I would recommend using a named pipe, that way you can pull the name from constants file instead of passing file descriptors. – Dan Mar 08 '17 at 21:57
-
@Dan I want it to be cross platform. But it will be hard to have named pipe in windows as it don't have os.mkfifo() function. – Herbert Mar 08 '17 at 21:59
-
Can you simply redirect stdin and stdout? – Dan Mar 08 '17 at 22:00
-
@Dan No, since I use stdin and stdout for other purpose. – Herbert Mar 08 '17 at 22:02
-
Did you check the value of `readfd`? Is t the same in the subrocess as in the python script? What is the error message reported by `fdopen`? – DYZ Mar 08 '17 at 23:14
-
@DYZ It's 5 in my case. I don't know what's the error message because it simply return NULL as `FILE* fp` is nullptr after that line. I think the problem might because the file descriptor is not real one as it is created by python? – Herbert Mar 08 '17 at 23:17
-
Please use `perror()` to retrieve the error message. – DYZ Mar 08 '17 at 23:20