handling Infinite fork


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users handling Infinite fork
# 1  
Old 05-20-2008
Question handling Infinite fork

how Unix handles as process which forks infinitely.
like .......

while(1)
fork();

........
What happens when it is executed and how to avoid it.

Thanks,
Ashish
# 2  
Old 05-20-2008
It creates as many processes as possible. Many versions of unix are configured with a kernel variable called maxuproc or something like that. It is max processes that a non-root user can create. That is really the only protection and even with that a program like this is a nuisance. As fast as you kill a process, another takes its place.

To recover, as root, su to the user who is running the "while(1) fork();". root will be allowed to switch a root process to this user even if this bumps the number of processes past maxuproc. Now you have a shell running as the user. The shell cannot fork(), but it can exec(). So enter the command:
exec /usr/bin/kill -9 -1
Killing process -1 actually signals all processes owned by the user. This is documented on the kill(2) man page and this is required by posix. If there are a lot of processes and system calls are preemptable and processes with real-time priority are running, this may not work. A second approach is:
exec /usr/bin/kill -STOP -1
The STOP signal, whose number varies from system to system, cannot be caught. It is used for job control and suspends the process. A suspended process cannot attempt to fork() but it continues to consume a process slot so no new process can take its place. Once all of the offending processes are suspended, then you can kill them off.

Shells often have a built-in kill command... if it can handle -1 are a process number, then you can use that. But shells often have built-in kills that choke on the KILLALL constant.
# 3  
Old 05-20-2008
Quote:
Originally Posted by Perderabo
It creates as many processes as possible. Many versions of unix are configured with a kernel variable called maxuproc or something like that. It is max processes that a non-root user can create. That is really the only protection and even with that a program like this is a nuisance. As fast as you kill a process, another takes its place.

To recover, as root, su to the user who is running the "while(1) fork();". root will be allowed to switch a root process to this user even if this bumps the number of processes past maxuproc. Now you have a shell running as the user. The shell cannot fork(), but it can exec(). So enter the command:
exec /usr/bin/kill -9 -1
Killing process -1 actually signals all processes owned by the user. This is documented on the kill(2) man page and this is required by posix. If there are a lot of processes and system calls are preemptable and processes with real-time priority are running, this may not work. A second approach is:
exec /usr/bin/kill -STOP -1
The STOP signal, whose number varies from system to system, cannot be caught. It is used for job control and suspends the process. A suspended process cannot attempt to fork() but it continues to consume a process slot so no new process can take its place. Once all of the offending processes are suspended, then you can kill them off.

Shells often have a built-in kill command... if it can handle -1 are a process number, then you can use that. But shells often have built-in kills that choke on the KILLALL constant.
thanks a lot.
# 4  
Old 05-21-2008
Further you can impose individual/group specific limit using pam_limit and configurations written in /etc/security/limits.conf file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Infinite loop query

I have a script script.shwhich is scheduled to run at 11 AM everyday. # script.sh Code: ./scb_script.sh & unfortunately scb_script.sh is running today in infinite loop as respective files are not available. My question, when script.sh starts running tomorrow, will the old process be... (1 Reply)
Discussion started by: JSKOBS
1 Replies

2. Shell Programming and Scripting

Infinite looping in script

we have one script which we use to send mail in our environment. If we are giving correct attachment script runs fine but if we give a attachment name which is not present on server then this script go to infinite loop and causing all memory to be used. could any one please suggest me what is wrong... (2 Replies)
Discussion started by: anshu ranjan
2 Replies

3. Homework & Coursework Questions

Help with infinite loop problem

1. The problem statement, all variables and given/known data: My problem is an infinite loop when i press any other key other then Y or y in the while loop. what i want it to do is return to the normal script outside of it if pressing N or n or keep asking the same question if its any other... (4 Replies)
Discussion started by: Ren_kun
4 Replies

4. Programming

Infinite thread

I created a thread which pings a machine for every 15 seconds. I made my thread function in infinite loop. Main process also in infinite loop and will run for years. I wonder the thread will continue as long as main process continuous or will thread terminates at some point? Is there any life... (6 Replies)
Discussion started by: satish@123
6 Replies

5. Shell Programming and Scripting

Infinite while loop

what is the difference between while:,while true and while false? (6 Replies)
Discussion started by: proactiveaditya
6 Replies

6. Shell Programming and Scripting

infinite while do loop problem

hi all, this is how my scrip looks like #!/bin/sh bindir='/opt/apps/script/bin' datadir='/opt/apps/script/data' dir='/opt/apps/script' while : ; do ls -1rt /opt/apps/script/data/check.txt*|tail -1 > /dev/null 2>&1 if ;then chmod +rwx $bindir/dummy2.sh ... (8 Replies)
Discussion started by: tententen
8 Replies

7. Programming

Is my code in an Infinite Loop?

Production C code compiled without the dash-g option is running, and seems to be in an infinite loop. Is there a way to tell? Is there a diagnostic tool that will report what objects or what lines of code or even what functions are being executed? Or is my best option to kill it with a dump? ... (5 Replies)
Discussion started by: marcus121
5 Replies

8. Shell Programming and Scripting

while infinite loop_sockets constant

Hello Every one, it's requiered to create 'n' number of sockets constant for a hour time. i had my own script to create sockets using this i was able to create sockets and sendind data but using the tool i can maintain 'n' number of sockets for 5 minutes only after sockets are getting reduced to... (1 Reply)
Discussion started by: mannam srinivas
1 Replies

9. Shell Programming and Scripting

Infinite loop not looping

Hi guys, I'm having a problem getting my infinite loop to loop. It simply reads in the users choice form the menu, executes the corresponding case statement and quits instead of looping back to the main menu again. I have a feeling it might be something with my if then statements within the case... (2 Replies)
Discussion started by: hootdocta5
2 Replies

10. Shell Programming and Scripting

way to exit a infinite process

There is a sh file called "agg.sh", this is a kind of negative scenario, this script would fail as expected, but the problem is that after executing the script the following o/p is displayed continuosly without returning the control.We have to press "crtrl+c" to exit the script. ... (3 Replies)
Discussion started by: villain41
3 Replies
Login or Register to Ask a Question