Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threads and fork(). How can I deal with that? [duplicate]

Possible Duplicate:
fork in multi-threaded program

If I have an application which employs fork() and might be developed as multithreaded, what are the thumb rules/guidelines to consider to safely program this kind of applications?

like image 260
Emanuele Avatar asked Oct 13 '12 00:10

Emanuele


1 Answers

The basic thumb rules, according to various internet articles like ( http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them , fork in multi-threaded program ) are:

  1. (Main) Process[0] Monothread --> fork() --> (Child) Process[1] Multithreaded: OK!
    If Process[1] crashes or messes around with memory it won't touch address space of Process[0] (unless you use shared R/W memory... but this is another topic of its own).
    In Linux by default all fork()ed memory is Copy On Write. Given that Process[0] is monothreaded, when we invoke fork() all possible mutual exclusion primitives should be generally in an unlocked state.

  2. (Main) Process[0] Multithreaded --> fork() --> (Child) Process[1] Mono/Multithread: BAD!
    If you fork() a Multithreaded process your mutexes and many other thread synchronization primitives will likely be in an undefined state in Process[1]. You can work around with pthread_atfork() but if you use libraries you might as well roll a dice and hope to be lucky. Because generally you don't (want to) know the implementation details of libraries.

The advantages of fork() into a multithreaded process are that you could manipulate/read/aggregate your data quicker (in the Child process), without having to care about stability of the process you fork() from (Main). This is useful if your main process has a dataset of a lot of memory and you don't want to duplicate/reload it to safely process the data in another process (Child). This way the original process is stable and independent from the data aggregation/manipulation process (fork()ed).

Of course this means that the original process will generally be slower than it might be if developed in multithreaded fashion. But again, this is the price you might want to be paying for more stability.

If instead your main process is multithreaded, refrain from using fork(). It's going to be a proper mess to implement it in a stable way.

Cheers

like image 99
Emanuele Avatar answered Oct 14 '22 04:10

Emanuele