Tuesday, December 18, 2012

fork() explosion!

Quick summary: I discovered that an infinite loop calling only fork() is a terrible idea. I hope I don't get an angry email from a system administrator any time soon!

I've been home for two days, and my goal has been to finish the Unix Shell assignment for my programming systems class.

Just a few minutes ago, I learned how important it is to be careful when working with system calls like fork(). I wrote a program (on the Princeton server) which consisted of an infinite loop that called fork() once for each iteration. It seemed like a pretty harmless program. After, all I could always use Ctrl-C to terminate the infinite loop, right?

Wrong. And it should have been clear from the start that this was a terrible idea. Each child process created by the fork() is another infinite loop that spawns infinitely many more child processes! And this continues recursively! So my original process and all its children were soon taking over the entire system! Ctrl-C didn't work, so I closed the terminal window and logged back in.

Issuing the top command, I could see hundreds of copies of my program running, and taking up over 90% of the system CPU. After unsuccessful attempts to kill all these processes, a system administrator must have realized that something was wrong and terminated everything for me. Now, things are back to normal (or they seem to be at least).

Oops. I hope I don't get an angry email from a system administrator any time soon!

No comments:

Post a Comment