in reply to Process file simultaneously after forking

It would be simpler for the parent to fork two child processes that do the processing. The parent would just read the input, divide it among the children, and write lines to the children pipes. The children processes would read lines from stdin and process them. This way all the components are simple and you don't have to worry about synchronization because the operating system handle it through the pipes. Another advantage is that it scales to any number of children.

This system does have a starvation problem. The parent will block on writing to the slowest child process and all the other children will wait on reading their next line. If this is a problem, you can try using non-blocking IO in the parent. The parent can then select an available child to write the line too.

  • Comment on Re: Process file simultaneously after forking