From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fa528,689ead9f7415a732 X-Google-Attributes: gidfa528,public X-Google-ArrivalTime: 2002-08-30 09:41:45 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!demos!srcc!Gamma.RU!roma From: Roman Prokhorov Newsgroups: comp.unix.tru64 Subject: Re: fork() problem Date: Fri, 30 Aug 2002 20:41:37 +0400 Organization: Gamma NNTP server Moscow Russia Message-ID: References: NNTP-Posting-Host: roma.ttk.ru X-Trace: news.gamma.ru 1030725703 22589 195.170.229.76 (30 Aug 2002 16:41:43 GMT) X-Complaints-To: abuse@gamma.ru NNTP-Posting-Date: Fri, 30 Aug 2002 16:41:43 +0000 (UTC) Keywords: 318629621 Posted-And-Mailed: yes User-Agent: MT-NewsWatcher/3.1 (PPC) Xref: archiver1.google.com comp.unix.tru64:8531 In article , Paul Pluzhnikov wrote: > Roman Prokhorov writes: > > > Still don't beleive it. Why fork() checks the stdio locks? It doesn't > > print anything by itself. > > From Linux "man pthread_atfork": Strange, but FreeBSD doesn't have pthread_atfork. > To understand the purpose of pthread_atfork, recall that > fork(2) duplicates the whole memory space, including mutexes in > their current locking state, but only the calling thread: other > threads are not running in the child process. Thus, if a mutex > is locked by a thread other than the thread calling fork, that > mutex will remain locked forever in the child process, possibly Yes, but it becomes locked in parent process too !!! > blocking the execution of the child process. To avoid this, > install handlers with pthread_atfork as follows: the prepare handler > locks the global mutexes (in locking order), and the parent > and child handlers unlock them (in reverse order). Alternatively, > prepare and parent can be set to NULL and child to a function that > calls pthread_mutex_init on the global mutexes. > > Apparently Tru64 choose the first alternative ;-( > > Cheers, OK, what mutexes are locked by fgets? Are they documented anywhere? Try launching the program below under Tru64 and see which files are created and which are not. How to make it to create all the files _while_ it's waiting for the input? ------ #include #include #include #include #include #include void fork_handler1() { creat("forkPrepareHandler.txt",0666); } void fork_handler2() { creat("forkParentHandler.txt",0666); } void fork_handler3() { creat("forkChildHandler.txt",0666); } void func(void *pData) { pthread_atfork(fork_handler1,fork_handler2,fork_handler3); if(fork()==0) { creat("forkChild.txt",0666); _exit(0); } else { creat("forkParent.txt",0666); } } main() { char buf[10]; pthread_t theThread; if(!pthread_create(&theThread,NULL,(void *(*)(void *))&func,NULL)) { pthread_detach(theThread); } else { printf("Unable to launch a thread\n"); } creat("forkMain.txt",0666); printf("hit Enter, please\n"); fgets(buf,sizeof(buf),stdin); _exit(0); //important } --- -- Roman