C++ How to gracefully exit multiprocessing

  • C/C++
  • Thread starter Bill Simpson
  • Start date
  • Tags
    C++
In summary, the conversation discusses the use of multiprocessing to handle compute-intensive code. The individual is attempting to gracefully terminate the processes while still maintaining communication through shared input/output streams. After researching, it is suggested to use lightweight processes with POSIX threads instead of heavyweight processes with fork to handle this issue. Another solution is to use a thread to print results using a mutex for synchronization.
  • #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?
 
Technology news on Phys.org
  • #2
Bill Simpson said:
Is there a really simple solution to this problem?
Bottom line: Use the lightweight processes that you get with POSIX threads instead of the heavyweight processes that you get with fork.

One solution is to make a thread that wants to print something do so as follows: Lock a mutex, print the desired results, and finally unlock the mutex. Threads are simple and switching between threads is generally faster than is switching between heavyweight processes.
 

Related to C++ How to gracefully exit multiprocessing

1. How do I terminate a C++ multiprocessing program gracefully?

To gracefully exit a C++ multiprocessing program, you can use the exit() function or the terminate() method. These will signal to the operating system that the program should end, and any resources used by the program will be released.

2. Is it important to gracefully exit a C++ multiprocessing program?

Yes, it is important to gracefully exit a C++ multiprocessing program to ensure that all resources are properly released and any unfinished tasks are completed. If a program is terminated abruptly, it can lead to memory leaks and other issues.

3. Can I use the exit() function in all situations to terminate a C++ multiprocessing program?

No, the exit() function should only be used in the main process of a multiprocessing program. If you try to use it in a child process, it will terminate the entire program, including the other child processes.

4. How can I handle errors and exceptions when exiting a C++ multiprocessing program?

You can use the try-except block to catch any errors or exceptions that may occur when exiting a C++ multiprocessing program. This will allow you to handle them gracefully and prevent the program from crashing.

5. Are there any best practices for gracefully exiting a C++ multiprocessing program?

Yes, it is recommended to use the join() method to wait for all child processes to finish before exiting the main process. This ensures that all tasks are completed before the program terminates. It is also good practice to clean up any resources used by the program before exiting.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Replies
10
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
29
Views
9K
  • Programming and Computer Science
Replies
18
Views
3K
  • Mechanical Engineering
Replies
32
Views
494
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
19
Views
5K
Back
Top