multithreaded fork()

Hi:
The fork() man page says the following. Is this for 6.2? It seems to be a
very constraining caveat!!
Any idea about how one can achieve it: fork when there are multiple threads
(actually only 2 threads) present?


Caveats:
Currently, fork() is supported only in single-threaded applications. If you
create a thread and then call fork(), the function returns -1 and sets errno
to ENOSYS.


Thanks,

Rommel

Nope. You have to call fork() before any threads are created. If you
want to fork()/exec() use spawn*() instead.

chris


Rommel Dongre <rdongre@pillardata.com> wrote:

Hi:
The fork() man page says the following. Is this for 6.2? It seems to be a
very constraining caveat!!
Any idea about how one can achieve it: fork when there are multiple threads
(actually only 2 threads) present?


Caveats:
Currently, fork() is supported only in single-threaded applications. If you
create a thread and then call fork(), the function returns -1 and sets errno
to ENOSYS.


Thanks,

Rommel




\


Chris McKillop <cdm@qnx.com> “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

Is it possible to kill all the threads except the main and then fork()
successfully?

“Chris McKillop” <cdm@qnx.com> wrote in message
news:augvm6$cpb$1@nntp.qnx.com

Nope. You have to call fork() before any threads are created. If you
want to fork()/exec() use spawn*() instead.

chris


Rommel Dongre <> rdongre@pillardata.com> > wrote:
Hi:
The fork() man page says the following. Is this for 6.2? It seems to be
a
very constraining caveat!!
Any idea about how one can achieve it: fork when there are multiple
threads
(actually only 2 threads) present?


Caveats:
Currently, fork() is supported only in single-threaded applications. If
you
create a thread and then call fork(), the function returns -1 and sets
errno
to ENOSYS.


Thanks,

Rommel







\

Chris McKillop <> cdm@qnx.com> > “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

Rommel Dongre <rdongre@pillardata.com> wrote:

Is it possible to kill all the threads except the main and then fork()
successfully?

Offically, no. The caveat is accurate – the use of fork() and pthreads
are mutually exclusive, in the same process. The state of libc’s
mutexes/condvars are undefined in the child process.

If you want to be dirty and nasty, the code for fork() from libc will give
you a hint of how you could accomplish this. I really don’t recommend it
though.

:wink:

Kris

pid_t fork(void) {
#if 1 // @@@ TEMPORARY until libc is fork safe wrt mutexs
extern int _Multi_threaded;

if(_Multi_threaded) {
errno = ENOSYS;
return -1;
}
#endif
return _fork(_FORK_ASPACE, 0);
}


“kirk” <kirussel@NOSPAMrogers.com> wrote in message
news:auleo6$ik3$1@inn.qnx.com

Rommel Dongre <> rdongre@pillardata.com> > wrote:
Is it possible to kill all the threads except the main and then fork()
successfully?

Offically, no. The caveat is accurate – the use of fork() and pthreads
are mutually exclusive, in the same process. The state of libc’s
mutexes/condvars are undefined in the child process.

Kris Warkentin <kewarken@qnx.com> wrote:

If you want to be dirty and nasty, the code for fork() from libc will give
you a hint of how you could accomplish this. I really don’t recommend it
though.

BUYER BEWARE! THIS IS MOST CERTAINLY GUARANTEED TO BREAK IN
THE FUTURE. THIS IS NOT RECOMMENDED.

Thanks,
Thomas

pid_t fork(void) {
#if 1 // @@@ TEMPORARY until libc is fork safe wrt mutexs
extern int _Multi_threaded;

if(_Multi_threaded) {
errno = ENOSYS;
return -1;
}
#endif
return _fork(_FORK_ASPACE, 0);
}



“kirk” <> kirussel@NOSPAMrogers.com> > wrote in message
news:auleo6$ik3$> 1@inn.qnx.com> …
Rommel Dongre <> rdongre@pillardata.com> > wrote:
Is it possible to kill all the threads except the main and then fork()
successfully?

Offically, no. The caveat is accurate – the use of fork() and pthreads
are mutually exclusive, in the same process. The state of libc’s
mutexes/condvars are undefined in the child process.

Thomas (toe-mah) Fletcher QNX Software Systems
thomasf@qnx.com Core OS Technology Group
(613)-591-0931 http://www.qnx.com/

Hi:
Is it possible to spawn() from a multi-threaded app?

<thomasf@qnx.com> wrote in message news:av1klo$hqs$1@nntp.qnx.com

Kris Warkentin <> kewarken@qnx.com> > wrote:
If you want to be dirty and nasty, the code for fork() from libc will
give
you a hint of how you could accomplish this. I really don’t recommend
it
though.

BUYER BEWARE! THIS IS MOST CERTAINLY GUARANTEED TO BREAK IN
THE FUTURE. THIS IS NOT RECOMMENDED.

Thanks,
Thomas

pid_t fork(void) {
#if 1 // @@@ TEMPORARY until libc is fork safe wrt mutexs
extern int _Multi_threaded;

if(_Multi_threaded) {
errno = ENOSYS;
return -1;
}
#endif
return _fork(_FORK_ASPACE, 0);
}


“kirk” <> kirussel@NOSPAMrogers.com> > wrote in message
news:auleo6$ik3$> 1@inn.qnx.com> …
Rommel Dongre <> rdongre@pillardata.com> > wrote:
Is it possible to kill all the threads except the main and then
fork()
successfully?

Offically, no. The caveat is accurate – the use of fork() and
pthreads
are mutually exclusive, in the same process. The state of libc’s
mutexes/condvars are undefined in the child process.



\


Thomas (toe-mah) Fletcher QNX Software Systems
thomasf@qnx.com > Core OS Technology Group
(613)-591-0931 > http://www.qnx.com/

Rommel Dongre <rdongre@pillardata.com> wrote:

Hi:
Is it possible to spawn() from a multi-threaded app?

Yes, it is. That is the usual solution.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

A pointer to spawn on the fork() page that talked about the caveat would
have been really helpful on the online documentation!!

“David Gibbs” <dagibbs@qnx.com> wrote in message
news:av2bcc$1ts$1@nntp.qnx.com

Rommel Dongre <> rdongre@pillardata.com> > wrote:
Hi:
Is it possible to spawn() from a multi-threaded app?

Yes, it is. That is the usual solution.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Rommel Dongre <rdongre@pillardata.com> wrote:
: A pointer to spawn on the fork() page that talked about the caveat would
: have been really helpful on the online documentation!!

I’ll pass this on to the writer who looks after this book. Thanks for the
suggestion.

: “David Gibbs” <dagibbs@qnx.com> wrote in message
: news:av2bcc$1ts$1@nntp.qnx.com
:> Rommel Dongre <rdongre@pillardata.com> wrote:
:> > Hi:
:> > Is it possible to spawn() from a multi-threaded app?
:>
:> Yes, it is. That is the usual solution.


Steve Reid stever@qnx.com
TechPubs (Technical Publications)
QNX Software Systems