* fork() problem
@ 2002-08-28 23:13 Roman Prokhorov
2002-08-29 4:04 ` Paul Pluzhnikov
0 siblings, 1 reply; 8+ messages in thread
From: Roman Prokhorov @ 2002-08-28 23:13 UTC (permalink / raw)
Hi,
I have a problem: Under Tru64 the fork() called from a thread freezes.
This doesn't happen on the other Unixes I tried. See my code below, did
I do anything wrong?
I discovered that I can unfreeze the fork() when I call sleep() from the
main thread. What sleep() realy does?
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include <sys/types.h>
#include<unistd.h>
void fork_handler1() {
printf("prepare handler called\n");
}
void fork_handler2() {
printf("parent handler called\n");
}
void fork_handler3() {
printf("child handler called\n");
}
void func(void *pData) {
pthread_atfork(fork_handler1,fork_handler2,fork_handler3);
printf("thread launched\n");
if(fork()==0) {
printf("new fork started, pid=%d\n",getpid());
exit(0);
} else {
printf("old fork running, pid=%d\n",getpid());
}
}
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");
}
printf("hit Enter, please\n");
fgets(buf,sizeof(buf),stdin);
//sleep(1);
printf("hit Enter again, please\n");
fgets(buf,sizeof(buf),stdin);
}
--
Roman
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: fork() problem
2002-08-28 23:13 fork() problem Roman Prokhorov
@ 2002-08-29 4:04 ` Paul Pluzhnikov
2002-08-29 15:00 ` Roman Prokhorov
0 siblings, 1 reply; 8+ messages in thread
From: Paul Pluzhnikov @ 2002-08-29 4:04 UTC (permalink / raw)
Roman Prokhorov <roma@stalker.gamma.ru> writes:
> I have a problem: Under Tru64 the fork() called from a thread freezes.
> This doesn't happen on the other Unixes I tried. See my code below, did
> I do anything wrong?
Yes.
Your question has been answered many times in comp.programming.threads.
Search groups.google.com for "thread fork async" ...
Here is one answer by Kaz Kylheku
Message-ID: <slrn8urucn.gqp.kaz@ashi.FootPrints.net>
Mind you, according to POSIX, if a threaded process forks a
child, that child can execute only async safe functions. It
can use exec() to load a new process image. Since fprintf()
is not async safe, calling it results in undefined behavior.
Cheers,
--
In order to understand recursion you must first understand recursion.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: fork() problem
2002-08-29 4:04 ` Paul Pluzhnikov
@ 2002-08-29 15:00 ` Roman Prokhorov
2002-08-30 0:05 ` Paul Pluzhnikov
0 siblings, 1 reply; 8+ messages in thread
From: Roman Prokhorov @ 2002-08-29 15:00 UTC (permalink / raw)
In article <m3bs7m73wa.fsf@salmon.parasoft.com>,
Paul Pluzhnikov <ppluzhnikov@earthlink.net> wrote:
> Roman Prokhorov <roma@stalker.gamma.ru> writes:
>
> > I have a problem: Under Tru64 the fork() called from a thread freezes.
> > This doesn't happen on the other Unixes I tried. See my code below, did
> > I do anything wrong?
>
> Yes.
>
> Your question has been answered many times in comp.programming.threads.
> Search groups.google.com for "thread fork async" ...
>
> Here is one answer by Kaz Kylheku
> Message-ID: <slrn8urucn.gqp.kaz@ashi.FootPrints.net>
>
> Mind you, according to POSIX, if a threaded process forks a
> child, that child can execute only async safe functions. It
> can use exec() to load a new process image. Since fprintf()
> is not async safe, calling it results in undefined behavior.
>
> Cheers,
Thanks for the tip about async safe functions. However it didn't help.
What's wrong now? I don't use printf in the thread at all, but fork()
still doesn't invoke unless I call sleep().
---------
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include <sys/types.h>
#include<unistd.h>
void func(void *pData) {
if(fork()==0) {
char *const args[]={"/usr/bin/ls","-al",NULL};
execvp("/usr/bin/ls",args);
_exit(0);
} else {
}
}
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");
}
printf("hit Enter, please\n");
fgets(buf,sizeof(buf),stdin);
//sleep(1);
printf("hit Enter again, please\n");
fgets(buf,sizeof(buf),stdin);
}
--
Roman
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: fork() problem
2002-08-29 15:00 ` Roman Prokhorov
@ 2002-08-30 0:05 ` Paul Pluzhnikov
2002-08-30 12:28 ` Roman Prokhorov
0 siblings, 1 reply; 8+ messages in thread
From: Paul Pluzhnikov @ 2002-08-30 0:05 UTC (permalink / raw)
Roman Prokhorov <roma@stalker.gamma.ru> writes:
> Thanks for the tip about async safe functions. However it didn't help.
>
> What's wrong now? I don't use printf in the thread at all, but fork()
> still doesn't invoke unless I call sleep().
Which is why fork()ing multithreaded executables is a Bad Idea (TM).
The second thread is stuck, trying to acquire the stdin lock:
>0 0x3ff805b0400 in pthread_mutex_tryrec(...) in /usr/shlib/libpthread.so
#1 0x3ff805afe1c in __pthread_mutex_trylock(...) in /usr/shlib/libpthread.so
#2 0x3ff801bf1b4 in __flockall(...) in /usr/shlib/libc.so
#3 0x3ff801467ec in __libc_locks_reinit(...) in /usr/shlib/libc.so
#4 0x3ff800cf740 in __fork(...) in /usr/shlib/libc.so
#5 0x120001978 in func(pData=0x0) "forkthread.c":10
#6 0x3ff805c7c78 in __thdBase(...) in /usr/shlib/libpthread.so
Who is holding the stdio lock? But the initial thread, of course:
>0 0x3ff800cc9c8 in __read(...) in /usr/shlib/libc.so
#1 0x3ff8014287c in __read_nc(...) in /usr/shlib/libc.so
#2 0x3ff800d05cc in __filbuf(...) in /usr/shlib/libc.so
#3 0x3ff800d0780 in fgets(...) in /usr/shlib/libc.so ## this is where the lock was
## most likely acquired
#4 0x120001a70 in main() "forkthread.c":30
#5 0x120001888 in __start(...) in a.out
So, your choices are:
- avoid forking [save yourself and future maintainers some grief]
- postpone doing anything in the main thread until you know that the
other thread *has already forked* [this is easy to do in your
test program, using condition variable; but I suspect in your
real app you just want to be able to fork, while other threads
are fread()ing]
- fork a "slave process" before creating any threads, open a pipe
to it. When the main process wants to fork(), have it write a
message to the pipe to slave instead. The slave [which does
not create any threads ever], can fork any time it wants to.
Cheers,
--
In order to understand recursion you must first understand recursion.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: fork() problem
2002-08-30 0:05 ` Paul Pluzhnikov
@ 2002-08-30 12:28 ` Roman Prokhorov
2002-08-30 15:04 ` Paul Pluzhnikov
0 siblings, 1 reply; 8+ messages in thread
From: Roman Prokhorov @ 2002-08-30 12:28 UTC (permalink / raw)
In article <m33csxmeu0.fsf@salmon.parasoft.com>,
Paul Pluzhnikov <ppluzhnikov@earthlink.net> wrote:
> Roman Prokhorov <roma@stalker.gamma.ru> writes:
>
> > Thanks for the tip about async safe functions. However it didn't help.
> >
> > What's wrong now? I don't use printf in the thread at all, but fork()
> > still doesn't invoke unless I call sleep().
>
> Which is why fork()ing multithreaded executables is a Bad Idea (TM).
>
> The second thread is stuck, trying to acquire the stdin lock:
>
> >0 0x3ff805b0400 in pthread_mutex_tryrec(...) in
> >/usr/shlib/libpthread.so
> #1 0x3ff805afe1c in __pthread_mutex_trylock(...) in
> /usr/shlib/libpthread.so
> #2 0x3ff801bf1b4 in __flockall(...) in /usr/shlib/libc.so
> #3 0x3ff801467ec in __libc_locks_reinit(...) in /usr/shlib/libc.so
> #4 0x3ff800cf740 in __fork(...) in /usr/shlib/libc.so
> #5 0x120001978 in func(pData=0x0) "forkthread.c":10
> #6 0x3ff805c7c78 in __thdBase(...) in /usr/shlib/libpthread.so
>
> Who is holding the stdio lock? But the initial thread, of course:
>
> >0 0x3ff800cc9c8 in __read(...) in /usr/shlib/libc.so
> #1 0x3ff8014287c in __read_nc(...) in /usr/shlib/libc.so
> #2 0x3ff800d05cc in __filbuf(...) in /usr/shlib/libc.so
> #3 0x3ff800d0780 in fgets(...) in /usr/shlib/libc.so ## this is where
> the lock was
> ## most likely
> acquired
Still don't beleive it. Why fork() checks the stdio locks? It doesn't
print anything by itself.
> #4 0x120001a70 in main() "forkthread.c":30
> #5 0x120001888 in __start(...) in a.out
>
> So, your choices are:
>
> - avoid forking [save yourself and future maintainers some grief]
>
> - postpone doing anything in the main thread until you know that the
> other thread *has already forked* [this is easy to do in your
> test program, using condition variable; but I suspect in your
> real app you just want to be able to fork, while other threads
> are fread()ing]
>
> - fork a "slave process" before creating any threads, open a pipe
> to it. When the main process wants to fork(), have it write a
> message to the pipe to slave instead. The slave [which does
> not create any threads ever], can fork any time it wants to.
My initial task was launching an external program from a thread while
the main thread stays in fgets(). I uses system() call but it didn't
work under Tru64. So I tried o emulate its behaviour via fork/exec and
found that fork() is the source of the problem.
--
Roman
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: fork() problem
2002-08-30 12:28 ` Roman Prokhorov
@ 2002-08-30 15:04 ` Paul Pluzhnikov
2002-08-30 16:41 ` Roman Prokhorov
0 siblings, 1 reply; 8+ messages in thread
From: Paul Pluzhnikov @ 2002-08-30 15:04 UTC (permalink / raw)
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":
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
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,
--
In order to understand recursion you must first understand recursion.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: fork() problem
2002-08-30 15:04 ` Paul Pluzhnikov
@ 2002-08-30 16:41 ` Roman Prokhorov
2002-08-30 18:04 ` Paul Pluzhnikov
0 siblings, 1 reply; 8+ messages in thread
From: Roman Prokhorov @ 2002-08-30 16:41 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: fork() problem
2002-08-30 16:41 ` Roman Prokhorov
@ 2002-08-30 18:04 ` Paul Pluzhnikov
0 siblings, 0 replies; 8+ messages in thread
From: Paul Pluzhnikov @ 2002-08-30 18:04 UTC (permalink / raw)
Roman Prokhorov <roma@stalker.gamma.ru> writes:
> > 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 !!!
It will be unlocked in the parent process as soon as
fork() is done.
>
> > 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?
It is unlikely that fgets needs other mutexes (it may need malloc
mutex at times). It is also unlikely that these mutexes are
documented anywhere.
Even if they were, you are missing the point:
> How to make it to create all the files
> _while_ it's waiting for the input?
which is that you *can't* reliably fork() multithreaded programs,
unless you wrote *all* the code [including libc]. Since you
did not write libc [and I hope you don't plan on it ;-], you just
can't fork().
Accept this fact, and move on to one of the other solutions.
Cheers,
--
In order to understand recursion you must first understand recursion.
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2002-08-30 18:04 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2002-08-30 18:04 ` Paul Pluzhnikov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox