How do you handle while(1) fork(); ?
Lars Henrik Mathiesen
thorinn at skinfaxe.diku.dk
Sun Jul 15 02:45:13 AEST 1990
jrw at 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 at diku.dk
More information about the Comp.unix.questions
mailing list