how does fork() copy threads of a process in multi-processor (SMP)

Josh Cartwright joshc at linux.com
Thu Mar 10 13:29:10 EST 2011


On Thu, Mar 10, 2011 at 11:55:35PM +0700, Mulyadi Santosa wrote:
> Hi...
> 
> On Thu, Mar 10, 2011 at 22:05, lalit mohan tripathi
> <lalit.tripathi at gmail.com> wrote:
> > I've a general question:  In Multiprocessor (Multi-Core) (SMP)
> > environment how does  fork system call  (do_fork() related code)
> > maintain the synchronization in case the threads of a process are
> > running on different processors?  E.g. it can happen that the fork()
> > is called by cpu-0 thread and other thread of same process is
> > executing on cpu-1.
> 
> well, AFAIK, the newly born child, at least when they are just about
> to kick in into the run queue (in any CPU), is actually still inside
> the parent's code path. Or in simpler word, they are still bound in
> the same core/processor they are created. Therefore, there's no
> problem regarding task struct duplication etc

Also, please keep in mind that fork() does NOT cause all the threads of the
parent process to propogate to the child process.  Only the thread that called
fork() is duplicated.  From `man 2 fork':

 * The child process is created with a single thread -- the one that called
   fork().  The entire virtual address space of the parent is replicated in the
   child, including the states of mutexes, condition variables, and other pthreads
   objects; the use of pthread_atfork(3) may be helpful for dealing with problems
   that this can cause.

-- 
                                       joshc



More information about the Kernelnewbies mailing list