- #1
Bill Simpson
- 1,077
- 33
Not a homework problem.
I've written a little compute-intensive code. That only keeps one core busy. So I run four copies. But compute time is variable so I thought I'd try my hand at multiprocessing.
I can fork to create four processes. No problem.
I/O is from cin, cout and the processes need to share these to get the work stream and report results. So create a pipe holding a magic cookie. When any process has completed crunching and needs to do I/O it waits on the pipe until it gets the cookie, does the I/O, puts the cookie back in the pipe and goes back to crunching. No problem.
Now the problem. When each process finally discovers the input stream of work has been exhausted I need to gracefully terminate the process. But a few Google searches tell me that if the process just exits it will tear down the shared cin, cout and the other processes that are busy working won't be able to report their final results.
Is there a really simple solution to this problem?
I've written a little compute-intensive code. That only keeps one core busy. So I run four copies. But compute time is variable so I thought I'd try my hand at multiprocessing.
I can fork to create four processes. No problem.
I/O is from cin, cout and the processes need to share these to get the work stream and report results. So create a pipe holding a magic cookie. When any process has completed crunching and needs to do I/O it waits on the pipe until it gets the cookie, does the I/O, puts the cookie back in the pipe and goes back to crunching. No problem.
Now the problem. When each process finally discovers the input stream of work has been exhausted I need to gracefully terminate the process. But a few Google searches tell me that if the process just exits it will tear down the shared cin, cout and the other processes that are busy working won't be able to report their final results.
Is there a really simple solution to this problem?