From: Roman Prokhorov <roma@stalker.gamma.ru>
Subject: Re: fork() problem
Date: Fri, 30 Aug 2002 20:41:37 +0400 [thread overview]
Message-ID: <roma-DF91D2.20413730082002@news.gamma.ru> (raw)
In-Reply-To: m3adn45s4k.fsf@salmon.parasoft.com
In article <m3adn45s4k.fsf@salmon.parasoft.com>,
Paul Pluzhnikov <ppluzhnikov@earthlink.net> wrote:
> Roman Prokhorov <roma@stalker.gamma.ru> 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<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include <sys/types.h>
#include<unistd.h>
#include<fcntl.h>
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
next prev parent reply other threads:[~2002-08-30 16:41 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-08-28 23:13 fork() problem Roman Prokhorov
2002-08-29 4:04 ` Paul Pluzhnikov
2002-08-29 15:00 ` Roman Prokhorov
2002-08-30 0:05 ` Paul Pluzhnikov
2002-08-30 12:28 ` Roman Prokhorov
2002-08-30 15:04 ` Paul Pluzhnikov
2002-08-30 16:41 ` Roman Prokhorov [this message]
2002-08-30 18:04 ` Paul Pluzhnikov
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox