Different input/output stream for every forked process
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.
精彩评论