while (!finished)
fd = accept(....)
pid = fork();
if (pid != 0) {
handle_request(fd);
}
}
The fork system call:
Fork is a very weird system call. It is the only system call that returns
TWICE every time you call it!
You can tell the parent from the child because the parent gets a non-zero return from fork, and the child gets a zero.
After a fork, both copies have the same file descriptors open.
Any new file descriptors are not shared, however.
The Fork BOMB:
If your program is buggy, it might go into an infinite loop. If this
loop contains a fork(), then it will clone itself infinite times.
It only takes a few infinite loops to bring down the whole system.
Just a 0.1 seconds worth of forking will fire off hundreds of
infinite loops. The computer will become so bogged down it doesn't
notice you pressing ^C. The server will need to be rebooted, and
I'll need to yell at you. So don't do that.
Requirements
| Points | Requirement |
| 4 | Program uses fork to handle multiple connections |
| 2 | Server prints "waiting" every five seconds |
| +1 more | Server only prints "waiting" if there is NO client connected |
| 2 | Client quits after five connections |
| 2 | Any client who types "q" on a line by itself kills ALL copies of the server |