Path: utzoo!attcan!uunet!aplcen!haven!udel!wuarchive!zaphod.mps.ohio-state.edu!samsung!munnari.oz.au!bunyip!brolga!uqcspe!batserver.cs.uq.oz.au!rhys From: rhys@batserver.cs.uq.oz.au (Rhys Weatherley) Newsgroups: comp.unix.questions Subject: Re: How do you handle while(1) fork(); ? Message-ID: <4261@uqcspe.cs.uq.oz.au> Date: 12 Jul 90 10:09:55 GMT References: <841@massey.ac.nz> <1990Jul11.115612.2155@aucs.uucp> Sender: news@uqcspe.cs.uq.oz.au Reply-To: rhys@batserver.cs.uq.oz.au Lines: 43 peter@aucs.uucp (Peter Steele) writes: >ARaman@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@batserver.cs.uq.oz.au | Australia. G'day!! || +===============================+==============================+