memologue (<- good article but in Japanese)
- Thread-1 and thread-2 are running in parent process before forking.
- The thread-1 calls doit function.
- The doit function locks own mutex.
- The thread-1 sleeps by nanosleep.
- Then, the current context switches to thread-2.
- Thread-2 calls fork function.
- A child process is created.
- 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!
- The child process starts processing.
- In the child process, doit function is called.
- The doit attempts to lock the mutex already locked, which causes dead-lock in the child process.
Solutions
- Exit all other threads before forking.
- Use exec after forking soon in new child processes.
- Use only fork-safe procedures in all the other threads.
- Use callback functions before and after forking with pthread_atfork.
- No forking in multi-thread program.
No comments:
Post a Comment