2010/02/14

fork-exec problem

memologue (<- good article but in Japanese)
  1. Thread-1 and thread-2 are running in parent process before forking.
  2. The thread-1 calls doit function.
  3. The doit function locks own mutex.
  4. The thread-1 sleeps by nanosleep.
  5. Then, the current context switches to thread-2.
  6. Thread-2 calls fork function.
  7. A child process is created.
  8. In this time, doit's mutex in the child process has been locked (because it was copied from locking parent's); however, there is not any thread to unlock the mutex in the child process!
  9. The child process starts processing.
  10. In the child process, doit function is called.
  11. The doit attempts to lock the mutex already locked, which causes dead-lock in the child process.

Solutions

  1. Exit all other threads before forking.
  2. Use exec after forking soon in new child processes.
  3. Use only fork-safe procedures in all the other threads.
  4. Use callback functions before and after forking with pthread_atfork.
  5. No forking in multi-thread program.

No comments:

How to set parameters to debugging program on Visual Studio 2019 with CMake

Solution: MSDN Sometimes the "Debug and Launch Settings for CMake" bottun is disabled. In this case, change to the target view. ...