How do you handle while(1) fork(); ?

Rhys Weatherley rhys at batserver.cs.uq.oz.au
Thu Jul 12 20:09:55 AEST 1990


peter at aucs.uucp (Peter Steele) writes:

>ARaman at massey.ac.nz (A.V. Raman) writes:

>>Is there any way to kill all instances of a process that has the
>>following piece of code in it without having to bring the system down?

>>   while (1)
>>      fork();

>>Any help (by email) would be appreciated.

>I think a summary of responses to this question would be appreciated.
>We've had students do this on many occasions on our Sun.

I would also be interested in a summary, but how about this one:

	while (!fork ())
	  {
	    /* some non-important code */
	  }
	/* some "clean-up" code */
	exit (0);

This will create a round-robin of processes, where each process spawns
a child, and then dies (fork() returns 0 in the child process).  Extremely 
difficult to kill these ones, because by the time you have a process-id 
to kill, that process doesn't exist usually anymore!  This is an 
over-simplification of the code to produce this effect, but both examples 
show the danger of using fork(), when you don't really know what you're doing!  

Personally I prefer the idea of having some sort of library 'spawn' function 
which takes a program name and arguments (similar to exec, without overwriting
the current process), and takes care of all the details for you when you want 
to start up a new program as a child process, allowing the experts to deal 
with fork() and its friends.  (This has no doubt been covered before :-).

Rhys.

+===============================+==============================+
||  Rhys Weatherley             |  University of Queensland,  ||
||  rhys at batserver.cs.uq.oz.au  |  Australia.  G'day!!        ||
+===============================+==============================+



More information about the Comp.unix.questions mailing list