X-Recipient: archive-cygwin AT delorie DOT com X-Spam-Check-By: sourceware.org Message-ID: <48DF3317.5060300@sh.cvut.cz> Date: Sun, 28 Sep 2008 09:32:39 +0200 From: =?UTF-8?B?VsOhY2xhdiBIYWlzbWFu?= User-Agent: Thunderbird 2.0.0.17 (Windows/20080914) MIME-Version: 1.0 To: cygwin AT cygwin DOT com Subject: Re: crash if fork(2) from another thread References: <003301c92110$336cee20$0200a8c0 AT whiterabc2znlh> In-Reply-To: <003301c92110$336cee20$0200a8c0@whiterabc2znlh> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Reply-To: cygwin AT cygwin DOT com Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 Hirokazu Yamamoto wrote, On 28.9.2008 4:16: > # I've post mail, but it didn't show up in > http://www.nabble.com/Cygwin-f12165.html. > # Maybe it was not good to attach a file. So try again... > > I'm not familiar with pthread & fork, but I think following code should not > crash. Is this expected behavior? > > #include > #include > #include > #include > > void *thread_func(void* args) > { > int ret; > pid_t pid; > pthread_t thread; > > puts("thread_func"); > > pid = fork(); > > assert(pid != (pid_t)-1); > > if (pid != 0) /* parent process */ > { > int status; > > printf("parent process (child pid = %d)\n", pid); > > waitpid(pid, &status, 0); > > puts("parent process end"); > } > else /* child process */ > { > puts("child process"); /* crash here */ I think this is not allowed in here. You can only do async-signal-safe stuff in the child. IO is AFAIK not in that category. Basically, the only thing you can safely do in the child process is to call exec(). > } > > return args; > } > > int main() > { > int ret; > pthread_t thread; > > ret = pthread_create(&thread, NULL, thread_func, NULL); > assert(ret == 0); > > ret = pthread_join(thread, NULL); > assert(ret == 0); > } > > // built with "gcc main.c -o main.exe" > > Thank you. - -- VH -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (MingW32) iFYEAREIAAYFAkjfMxcACgkQhQBMvHf/WHkrOgDgjwH6nF9WnksWQtjZDKOutx2d Sk/irkVq2NjjzADeJOjUN3LB+WkRPYGHGubEVHVT7H5DEh2NcZtPyg== =/9e7 -----END PGP SIGNATURE----- -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/