2001-03-16 16:31:32

by Sane, Purushottam

[permalink] [raw]
Subject: fork and pthreads

I am having a strange problem.

I have a big daemon program to which I am trying to add multi-threading.

At the begining, after some sanity check, this program does a double fork to
create a deamon.

After that it listens for the client on the port. Whenever the client
connects, it creates a new thread using
pthread-create.

The problem is, the thread (main thread) calling pthread-create hangs
indefinetely in __sigsuspend. The newly created thread however, runs
normally to completion.

I wrote few test programs trying to simulate this behaviour but all of them
worked as expected.

Does anyone know, what's going on ??

Nitin Sane
[email protected]
*(508)382-7319


2001-03-16 17:24:59

by Doug McNaught

[permalink] [raw]
Subject: Re: fork and pthreads

[email protected] writes:

> I am having a strange problem.
>
> I have a big daemon program to which I am trying to add multi-threading.
>
> At the begining, after some sanity check, this program does a double fork to
> create a deamon.
>
> After that it listens for the client on the port. Whenever the client
> connects, it creates a new thread using
> pthread-create.
>
> The problem is, the thread (main thread) calling pthread-create hangs
> indefinetely in __sigsuspend. The newly created thread however, runs
> normally to completion.

Just a guess--are you calling setsid() to establish a new session?
Omitting this *might* cause signal-delivery problems in pthreads.

-Doug

2001-03-16 17:53:59

by Richard Guenther

[permalink] [raw]
Subject: Re: fork and pthreads

Hi!

Well, using pthreads and forking in them seems to trigger libc
bugs (read: SIGSEGvs) here under certain conditions (happens,
after I introduced signal handlers and using pthread_sigmask,
I think), so hangs should be definitely possible, too...

Richard.

On Fri, 16 Mar 2001 [email protected] wrote:

> I am having a strange problem.
>
> I have a big daemon program to which I am trying to add multi-threading.
>
> At the begining, after some sanity check, this program does a double fork to
> create a deamon.
>
> After that it listens for the client on the port. Whenever the client
> connects, it creates a new thread using
> pthread-create.
>
> The problem is, the thread (main thread) calling pthread-create hangs
> indefinetely in __sigsuspend. The newly created thread however, runs
> normally to completion.
>
> I wrote few test programs trying to simulate this behaviour but all of them
> worked as expected.
>
> Does anyone know, what's going on ??
>
> Nitin Sane
> [email protected]
> *(508)382-7319
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
>

--
Richard Guenther <[email protected]>
WWW: http://www.anatom.uni-tuebingen.de/~richi/
The GLAME Project: http://www.glame.de/

2001-03-16 18:04:19

by Craig Ruff

[permalink] [raw]
Subject: Re: fork and pthreads

On Fri, Mar 16, 2001 at 06:52:26PM +0100, Richard Guenther wrote:
> Well, using pthreads and forking in them seems to trigger libc
> bugs (read: SIGSEGvs) here under certain conditions (happens,
> after I introduced signal handlers and using pthread_sigmask,
> I think), so hangs should be definitely possible, too...

You must pretty much avoid using signal handlers with pthreads.
In stead, you need to carefully setup things such that most signals
are blocked in most threads and a single thread (or selected set
of threads) does a sigwait for signals of interest. Most good
pthreads books talk about this issue, as does the DCE documentation.

2001-03-16 18:06:49

by Sane, Purushottam

[permalink] [raw]
Subject: RE: fork and pthreads

I am not using any signals. All the signals are blocked with SIG_IGN

Nitin Sane
[email protected]
*(508)382-7319


-----Original Message-----
From: Craig Ruff [mailto:[email protected]]
Sent: Friday, March 16, 2001 1:03 PM
To: Richard Guenther
Cc: [email protected]; [email protected]
Subject: Re: fork and pthreads


On Fri, Mar 16, 2001 at 06:52:26PM +0100, Richard Guenther wrote:
> Well, using pthreads and forking in them seems to trigger libc
> bugs (read: SIGSEGvs) here under certain conditions (happens,
> after I introduced signal handlers and using pthread_sigmask,
> I think), so hangs should be definitely possible, too...

You must pretty much avoid using signal handlers with pthreads.
In stead, you need to carefully setup things such that most signals
are blocked in most threads and a single thread (or selected set
of threads) does a sigwait for signals of interest. Most good
pthreads books talk about this issue, as does the DCE documentation.

2001-03-16 23:05:19

by Laramie Leavitt

[permalink] [raw]
Subject: RE: fork and pthreads

> Nitin Sane Wrote:
> > On Fri, Mar 16, 2001 at 06:52:26PM +0100, Richard Guenther wrote:
> > > Well, using pthreads and forking in them seems to trigger libc
> > > bugs (read: SIGSEGvs) here under certain conditions (happens,
> > > after I introduced signal handlers and using pthread_sigmask,
> > > I think), so hangs should be definitely possible, too...
> >
> > You must pretty much avoid using signal handlers with pthreads.
> > In stead, you need to carefully setup things such that most signals
> > are blocked in most threads and a single thread (or selected set
> > of threads) does a sigwait for signals of interest. Most good
> > pthreads books talk about this issue, as does the DCE documentation.
>
> I am not using any signals. All the signals are blocked with SIG_IGN
>

You know, I have been running into that exact same problem, except that
I don't fork at all. I start up my process and nearly immediately try
and spawn a few threads. The calling thread blocks indefinately in
a __sigsuspend(), but is apparently delivered an unknown signal.
The spawned thread executes normally.

It appears to be fixed when I compile with -D_REENTRANT, but I am not
certain...

Laramie