开发者

Different input/output stream for every forked process

开发者 https://www.devze.com 2023-02-11 22:28 出处:网络
I have some code, where several processes are created by forking. Every process have popen() function to execute some shell command. Problem is that all of these processes use same input/output stream

I have some code, where several processes are created by forking. Every process have popen() function to execute some shell command. Problem is that all of these processes use same input/output stream. This cause situation, when collision occurs because of processes writ开发者_如何学编程e to one stream simultaneously.

Is there any way to resolve that problem, so that every forked process used it's own stream? It is not allowed to do anything with forking in my case.


You'll have to close and reopen your stdin and stdout before or, if possible, right after the fork, in the child process.


When you call fork(), you inherit the file descriptors (stdin, stdout, etc) from the parent process. When you popen it's going to take the shared stdin/stdout and pipe it into the popened process. It sounds like you want to close any open file descriptors after forking, and reopen them.

0

精彩评论

暂无评论...
验证码 换一张
取 消