The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
the given code goes in infinite loop and does not increment variable i mrityunjay22 Shell Programming and Scripting 6 12-26-2007 01:20 AM
Infinite Loop in Autosys while running a shell script, Manual run is fine sharmagaurav_2k Shell Programming and Scripting 2 09-04-2007 08:20 AM
pick the bug the server enters an infinite loop arjunjag High Level Programming 3 07-19-2007 01:53 AM
ls command in infinite Loop umakant SUN Solaris 3 07-17-2007 01:25 AM
high priority thread contains an infinite loop rvan High Level Programming 0 02-14-2007 08:30 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-20-2008
Registered User
 

Join Date: Oct 2007
Posts: 25
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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-20-2008
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,042
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.
Reply With Quote
  #3 (permalink)  
Old 05-20-2008
Registered User
 

Join Date: Oct 2007
Posts: 25
Quote:
Originally Posted by Perderabo View Post
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.
Reply With Quote
  #4 (permalink)  
Old 05-21-2008
Registered User
 

Join Date: Mar 2006
Location: Ahmedabad
Posts: 125
Further you can impose individual/group specific limit using pam_limit and configurations written in /etc/security/limits.conf file.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Bookmarks

Tags
None

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:




All times are GMT -4. The time now is 12:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66