Path: utzoo!attcan!uunet!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!dkuug!freja.diku.dk!skinfaxe.diku.dk!thorinn From: thorinn@skinfaxe.diku.dk (Lars Henrik Mathiesen) Newsgroups: comp.unix.questions Subject: Re: How do you handle while(1) fork(); ? Message-ID: <1990Jul14.164513.24595@diku.dk> Date: 14 Jul 90 16:45:13 GMT References: <841@massey.ac.nz> <671@mtune.ATT.COM> Sender: news@diku.dk (The Netnews System) Organization: Department Of Computer Science, University Of Copenhagen Lines: 44 jrw@mtune.ATT.COM (Jim Webb) writes: >> while (1) >> fork(); >Under System V, running "kill -9 -1" will send the kill to all processes >belonging to the invoking user. So, to stop the above, you could do that >as the user (if s/he has any processes left) or by becoming root and then >entering: > su pest -c "kill -9 -1" There are two problems with this (at least in BSD): Firstly, su will use the user's shell, and csh will not accept the command, I think. More seriously, race conditions in the kernel will usually allow a few of these processes to survive: If a process is inside the fork system call but the new process slot hasn't been assigned yet, the kill signal will only be posted for the parent. When the system call completes, the parent is killed, but the child survives. You have to repeat the call to kill, and rapidly, otherwise the user's process limit will be reached again in milliseconds. I have had success with code like this: ------killall.c --------- /* Call (as root): killall numeric-uid */ main(argc, argv) char **argv; { nice(-40); setuid(atoi(argv[1])); if (getuid() <= 0) /* Something's wrong */ exit(1); while(kill(-1,9) == 0) /* There's someone out there, repeat */ ; exit(0); } ------------------------- Add error messages, name-to-uid translation etc. as you like. And don't run this when you're not root (exercise: why not?). -- Lars Mathiesen, DIKU, U of Copenhagen, Denmark [uunet!]mcsun!diku!thorinn Institute of Datalogy -- we're scientists, not engineers. thorinn@diku.dk