public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* fork() fails if it is called recursively from a child thread.
@ 2017-03-09 11:40 Takashi Yano
  2017-03-09 13:53 ` Eliot Moss
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Takashi Yano @ 2017-03-09 11:40 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 1017 bytes --]

Hello,

I found fork() fails if it is called recursively from a child thread.

Simple test case, attached (fk.c), reproduces this problem.

Expected result:
Parent 0 [22034] exit.
Child 0 [22036] works.
Parent 1 [22036] exit.
Child 1 [22038] works.
Parent 2 [22038] exit.
Child 2 [22039] works.
Parent 3 [22039] exit.
Child 3 [22040] works.
Parent 4 [22040] exit.
Child 4 [22041] works.

Result in cygwin 2.7.0:
Child 0 [4668] works.
Parent 0 [7188] exit.
      0 [main] a 4668 fork: child -1 - forked process 8456 died unexpectedly, retry 0, exit code 0xC0000142, errno 11
fork(): Resource temporarily unavailable

Strictly speaking, the test case is not safe because it calls functions
which are not async-signal-safe from forked child process, i.e. printf()
and perror(), in spite of multi-thread. However the same happens even
without printf() and perror().

This is the cause of which iperf 2.0.5 with option -s -D fails to start
as daemon.

Is this the known issue?

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

[-- Attachment #2: fk.c --]
[-- Type: text/x-csrc, Size: 550 bytes --]

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>

void MoveToChild(int n)
{
	pid_t pid;

	if ( (pid = fork()) == -1 ) {
		perror("fork()");
		_exit(1);
	} else if ( pid != 0 ) {
		printf("Parent %d [%d] exit.\n", n, getpid());
		_exit(0);
	}

	printf("Child %d [%d] works.\n", n, getpid());
}

void *thread_main(void *args)
{
	int i;
	for (i=0; i<5; i++) MoveToChild(i);
	return NULL;
}

int main()
{
	pthread_t t;
	pthread_create(&t, NULL, thread_main, NULL);
	pthread_join(t, NULL);
	return 0;
}


[-- Attachment #3: Type: text/plain, Size: 219 bytes --]


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

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

end of thread, other threads:[~2017-03-21 15:05 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-09 11:40 fork() fails if it is called recursively from a child thread Takashi Yano
2017-03-09 13:53 ` Eliot Moss
2017-03-09 16:16   ` Takashi Yano
2017-03-09 17:06 ` Achim Gratz
2017-03-09 17:33   ` Takashi Yano
2017-03-09 19:48     ` cyg Simple
2017-03-10 20:10 ` Corinna Vinschen
2017-03-11  1:29   ` Takashi Yano
2017-03-21 15:05     ` Erik Bray

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 read-only IMAP folder(s) and NNTP newsgroup(s).