tags:

views:

257

answers:

3

I tried this experiment on my Linux desktop:

int main()
{
  while(1)
    fork();
  return 0;
}

I ran this program as normal user(not root), i was surprised to find that it brought down my system, it has become unresponsive. I had hoped that due to resource limit exhaustion my process would have been killed,but apparently this is not the case. Any ideas why?

thanks, Sid.

PS: this was my office Linux box on which i was experimenting from home, i hope everything will be okay when i restart it tomorrow ....

+1  A: 

Most likely, your system administrator didn't set up the user limits. If no user limits are set, then they can't protect anyone.

Jörg W Mittag
+12  A: 

You've re-invented a fork bomb.

I think most Linux distributions don't set per-user resource limits by default. You can configure them of course, but you probably haven't.

The machine will be fine after a reboot - unless the CPU usage has caused over-heating problems.

To prevent an ordinary user from spawning too many processes you need to add configuration to /etc/security/limits.conf

You can use ulimit to set limits that would apply to your current session if you think you're going to run a program that might start too many processes or use up too much of other resources.

Douglas Leeder
Douglas, is there a similar option that allows you to limit resources on an application (or specific process)?
Dave
@Dave: see setrlimit (http://linux.die.net/man/2/setrlimit) and the bash command ulimit (http://www.ss64.com/bash/ulimit.html)
Hasturkun
Thanks @Hasturkun for those links. I was aware of ulimit, but can't figure out a way to use it on a specific application. In my case I'm running OSX (it also has the ulimit) command, and would love to be able to limit resources per application, to avoid massive slowdowns.
Dave
I don't know of any way to limit it for a single application. Maybe you could wrap the binary in a script that does ulimit?
Douglas Leeder
setrlimit (and ulimit by extension) sets the limits for the current process and its children, so you would wrap your application with something like (setrlimit..., exec), or (ulimit, exec) for a bash script
Hasturkun
+2  A: 

you can find stuffs about that on wikipedia.

Aif