5

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 ....

Al pacino
  • 1,250
  • 1
  • 10
  • 8

3 Answers3

11

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
  • 52,368
  • 9
  • 94
  • 137
  • Douglas, is there a similar option that allows you to limit resources on an application (or specific process)? –  Jan 18 '09 at 15:25
  • @Dave: see setrlimit (http://linux.die.net/man/2/setrlimit) and the bash command ulimit (http://www.ss64.com/bash/ulimit.html) – Hasturkun Jan 18 '09 at 17:06
  • 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. –  Jan 19 '09 at 04:51
  • 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 Jan 20 '09 at 13:07
  • 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 Jan 20 '09 at 17:23
2

you can find stuffs about that on wikipedia.

Aif
  • 11,015
  • 1
  • 30
  • 44
1

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
  • 363,080
  • 75
  • 446
  • 653