Understanding UNIX Pipes and File Descriptors [closed]
I am new to UNIX pipe communication and in need of some help here un开发者_StackOverflow中文版derstanding this. I need to create pipes and fork off child process that communicate with the parent process via these pipes. My questions are
a) Why a pipe per child process?
b) What are file descriptors really, and do I need to create one per pipe?
c) What do the StdIn and StdOut to do with all this?
Why a pipe per child process?
Because a single pipe has only two endpoints. If you tried to share a single pipe among all the children, you wouldn't know which child process generated a given piece of output.
What are file descriptors really?
Entries in the table of open files held by the operating system.
Do I need to create one [file descriptor] per pipe?
Any process which has any file-like object open, including either end of a pipe, needs to have a file descriptor through which it can communicate with that file-like object. Indeed, having an entry in the file descriptor table pointing to that file is exactly what having a file open is.
What do stdin and stdout have to do with this?
stdin is entry 0 in the file descriptor table; stdout is entry 1. If the processes you're launching read and write their data to file descriptors 0 and 1, that may be where you want your pipes attached.
精彩评论