www.delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2010/08/16/10:58:17

X-Recipient: archive-cygwin AT delorie DOT com
X-Spam-Check-By: sourceware.org
Date: Mon, 16 Aug 2010 10:58:00 -0400
From: Christopher Faylor <cgf-use-the-mailinglist-please AT cygwin DOT com>
To: cygwin AT cygwin DOT com
Subject: Re: Bug in fork() while in a thread
Message-ID: <20100816145759.GA32478@ednor.casa.cgf.cx>
Reply-To: cygwin AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
References: <i498t9$tot$1 AT dough DOT gmane DOT org> <20100815185353 DOT GA26993 AT ednor DOT casa DOT cgf DOT cx> <20100816090552 DOT GA11340 AT calimero DOT vinschen DOT de>
MIME-Version: 1.0
In-Reply-To: <20100816090552.GA11340@calimero.vinschen.de>
User-Agent: Mutt/1.5.20 (2009-06-14)
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

On Mon, Aug 16, 2010 at 11:05:52AM +0200, Corinna Vinschen wrote:
>On Aug 15 14:53, Christopher Faylor wrote:
>> On Sun, Aug 15, 2010 at 07:42:01PM +0200, Jason Curl wrote:
>> >Is it allowed to issue the fork() system call while not in the main 
>> >thread? When I read the OpenGroup specifications I don't seem to find 
>> >anything against allowing this.
>> >
>> >In particular, if I create a thread, then issue a fork(), data that 
>> >exists on the stack is corrupted after the fork() is in the child. Using 
>> >data on the heap doesn't show any issues (and is currently my 
>> >workaround, in case this is a bug).
>> 
>> If I'm reading this correctly then "the stack" in this case is the stack
>> associated with the main thread.  Cygwin only duplicates the stack in
>> the executing thread.  In your example, env (or presumably env2) from
>> the main thread is passed to another thread which then calls fork.  In
>> that scenario, the forked process is going to see garbage in env since
>> the array has never been initialized.
>> 
>> It is theoretically possible to duplicate the stack of the main thread
>> and other threads in the forked process [...]
>
>I guess I'm missing something here.  Here's an excerpt from the SUSv4
>fork man page:
>
>  The fork() function shall create a new process. The new process (child
>  process) shall be an exact copy of the calling process (parent process)
>  except as detailed below:
>
>  [...]
>
>  o A process shall be created with a single thread. If a
>    multi-threaded process calls fork(), the new process shall contain
>    a replica of the calling thread and its entire address space,
>    possibly including the states of mutexes and other resources.
>    [...]
>
>Is the stack of another thread, which is not executed in the forked
>process anymore, a part of the calling's thread "entire address space"?

I saw that too but I can confirm that linux duplicates the main thread.
That isn't too surprising since it probably just replicates the entire
address space of the process including all of the stacks of all of the
threads.

Cygwin is more "efficient" than that.

cgf

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019