forked from remzi-arpacidusseau/ostep-homework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80a09e2
commit 343173d
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,70 @@ | ||
|
||
# Overview: `generator.py` | ||
|
||
This tool, `generator.py`, allows the user to create little C programs | ||
that exercise `fork` in different ways so as to gain better | ||
understanding of how `fork` works. | ||
|
||
The simplest usage is just as follows: | ||
```sh | ||
prompt> ./generator.py | ||
``` | ||
|
||
The output you will see when you run this is a randomly generated C | ||
program. In this case, you will see something like this: | ||
|
||
```c | ||
#include <assert.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/time.h> | ||
#include <sys/wait.h> | ||
#include <unistd.h> | ||
|
||
void wait_or_die() { | ||
int rc = wait(NULL); | ||
assert(rc > 0); | ||
} | ||
|
||
int fork_or_die() { | ||
int rc = fork(); | ||
assert(rc >= 0); | ||
return rc; | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
// thread a | ||
if (fork_or_die() == 0) { | ||
sleep(1); | ||
// thread b | ||
exit(0); | ||
} | ||
wait_or_die(); | ||
if (fork_or_die() == 0) { | ||
sleep(4); | ||
// thread c | ||
exit(0); | ||
} | ||
wait_or_die(); | ||
if (fork_or_die() == 0) { | ||
sleep(4); | ||
// thread d | ||
if (fork_or_die() == 0) { | ||
sleep(2); | ||
// thread e | ||
exit(0); | ||
} | ||
if (fork_or_die() == 0) { | ||
sleep(2); | ||
// thread f | ||
exit(0); | ||
} | ||
wait_or_die(); | ||
wait_or_die(); | ||
exit(0); | ||
} | ||
wait_or_die(); | ||
return 0; | ||
} | ||
``` |