kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* how does fork() copy threads of a process in multi-processor (SMP)
       [not found] ` <AANLkTi=zUBAAXQaPedsPiL3DDrqEPH7+0M=T2e8shEAX@mail.gmail.com>
@ 2011-03-10 15:05   ` lalit mohan tripathi
  2011-03-10 16:55     ` Mulyadi Santosa
  0 siblings, 1 reply; 4+ messages in thread
From: lalit mohan tripathi @ 2011-03-10 15:05 UTC (permalink / raw)
  To: kernelnewbies

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.

I can see that copy of the process is made by do_fork() code in
fork.c. ?Can anyone give more details how the concurrency issue gets
handled e.g. how ?PC and Regs are of cpu-1 thread get copied without
any race condition in SMP / multiprocessor system?

Thanks in advance

^ permalink raw reply	[flat|nested] 4+ messages in thread

* how does fork() copy threads of a process in multi-processor (SMP)
  2011-03-10 15:05   ` how does fork() copy threads of a process in multi-processor (SMP) lalit mohan tripathi
@ 2011-03-10 16:55     ` Mulyadi Santosa
  2011-03-10 18:29       ` Josh Cartwright
  2011-03-11  6:58       ` lalit mohan tripathi
  0 siblings, 2 replies; 4+ messages in thread
From: Mulyadi Santosa @ 2011-03-10 16:55 UTC (permalink / raw)
  To: kernelnewbies

Hi...

On Thu, Mar 10, 2011 at 22:05, lalit mohan tripathi
<lalit.tripathi@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

is that what you mean?

-- 
regards,

Mulyadi Santosa
Freelance Linux trainer and consultant

blog: the-hydra.blogspot.com
training: mulyaditraining.blogspot.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

* how does fork() copy threads of a process in multi-processor (SMP)
  2011-03-10 16:55     ` Mulyadi Santosa
@ 2011-03-10 18:29       ` Josh Cartwright
  2011-03-11  6:58       ` lalit mohan tripathi
  1 sibling, 0 replies; 4+ messages in thread
From: Josh Cartwright @ 2011-03-10 18:29 UTC (permalink / raw)
  To: kernelnewbies

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@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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* how does fork() copy threads of a process in multi-processor (SMP)
  2011-03-10 16:55     ` Mulyadi Santosa
  2011-03-10 18:29       ` Josh Cartwright
@ 2011-03-11  6:58       ` lalit mohan tripathi
  1 sibling, 0 replies; 4+ messages in thread
From: lalit mohan tripathi @ 2011-03-11  6:58 UTC (permalink / raw)
  To: kernelnewbies

On Thu, Mar 10, 2011 at 10:25 PM, Mulyadi Santosa
<mulyadi.santosa@gmail.com> wrote:
> Hi...
>
> On Thu, Mar 10, 2011 at 22:05, lalit mohan tripathi
> <lalit.tripathi@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
>
> is that what you mean?
>
> --
> regards,
>
> Mulyadi Santosa
> Freelance Linux trainer and consultant
>
> blog: the-hydra.blogspot.com
> training: mulyaditraining.blogspot.com
>

Ok, I got the answer: Only the thread calling fork() gets copied.  Thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-03-11  6:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <AANLkTimxR44eJOmGxwXKGN6QhHG5UdrAcs5fCUqNHf45@mail.gmail.com>
     [not found] ` <AANLkTi=zUBAAXQaPedsPiL3DDrqEPH7+0M=T2e8shEAX@mail.gmail.com>
2011-03-10 15:05   ` how does fork() copy threads of a process in multi-processor (SMP) lalit mohan tripathi
2011-03-10 16:55     ` Mulyadi Santosa
2011-03-10 18:29       ` Josh Cartwright
2011-03-11  6:58       ` lalit mohan tripathi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).