Received: from PACIFIC-CARRIER-ANNEX.MIT.EDU by po7.MIT.EDU (5.61/4.7) id AA07304; Thu, 18 Jan 96 05:12:32 EST
Received: from ns.dkfz-heidelberg.de by MIT.EDU with SMTP
	id AA05786; Thu, 18 Jan 96 05:12:29 EST
Received: from mbp-sun5.inet.dkfz-heidelberg.de (mbp-sun5.inet.dkfz-heidelberg.de [193.174.54.166]) 
          by ns.DKFZ-Heidelberg.de (8.6.10/DKFZ-8.6.4) with SMTP
          id LAA07136; Thu, 18 Jan 1996 11:17:08 +0100
Received: by mbp-sun5.inet.dkfz-heidelberg.de (5.x/SMI-SVR4)
	id AA26675; Thu, 18 Jan 1996 11:10:20 +0100
Date: Thu, 18 Jan 1996 11:10:20 +0100
Message-Id: <9601181010.AA26675@mbp-sun5.inet.dkfz-heidelberg.de>
From: Jonathan Karges <J.Karges@dkfz-heidelberg.de>
To: proven@MIT.EDU
Cc: J.Karges@dkfz-heidelberg.de, proven@MIT.EDU
In-Reply-To: <9601160320.AA13404@jimi.MIT.EDU> (message from Christopher
	Provenzano on Mon, 15 Jan 1996 22:20:24 EST)
Subject: Re: PTHREADS AND FORK...

-> Is there anyway you can use fork()/exec() to do the correct thing? I know
-> that combination should work on my implementation.

The problem in my case is that the initial process has several threads
operating on some open files. Between fork() and exec() the threads of
the newly created process can change the state of the files without
the threads of the parent process knowing about it.

Based on your fork() version in your pthread distribution I would suggest
the following. However, I am not sure if everything is right since I don't
know your source code in all details.


#include <sys/types.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <config.h>
#include <stdio.h>


pid_t fork1()
{
        pid_t ret;
	pthread_t next, current;

        pthread_sched_prevent();

        fd_kern_fork();
        if (ret = machdep_sys_fork()) { /* Parent or error */
	  pthread_sched_resume();
        } else { /* Child */

	  /* kill all threads except the current one */
	  current = pthread_self();
/* What about locking here ? */
	  for (next = pthread_link_list; next; next = next->pll, count++)
	    if (!(pthread_equal(next, current)))
	      pthread_kill(next, SIGKILL);

	  machdep_unset_thread_timer(NULL);
	  machdep_stop_timer(NULL);
	  fork_lock++;
        }
        return(ret);
}


Is there anything else to do? I don't know exactly how the different
thread lists are managed. Especially I am not sure, if some further
locking/unlocking is necessary, since I don't know the state in which
your original fork() version is started.

What do you think?

Regards

Jonathan
