diff options
Diffstat (limited to 'pintos-progos/examples/recursor.c')
| -rw-r--r-- | pintos-progos/examples/recursor.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/pintos-progos/examples/recursor.c b/pintos-progos/examples/recursor.c new file mode 100644 index 0000000..79c784a --- /dev/null +++ b/pintos-progos/examples/recursor.c | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <syscall.h> | ||
| 4 | |||
| 5 | int | ||
| 6 | main (int argc, char *argv[]) | ||
| 7 | { | ||
| 8 | char buffer[128]; | ||
| 9 | pid_t pid; | ||
| 10 | int retval = 0; | ||
| 11 | |||
| 12 | if (argc != 4) | ||
| 13 | { | ||
| 14 | printf ("usage: recursor <string> <depth> <waitp>\n"); | ||
| 15 | exit (1); | ||
| 16 | } | ||
| 17 | |||
| 18 | /* Print args. */ | ||
| 19 | printf ("%s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]); | ||
| 20 | |||
| 21 | /* Execute child and wait for it to finish if requested. */ | ||
| 22 | if (atoi (argv[2]) != 0) | ||
| 23 | { | ||
| 24 | snprintf (buffer, sizeof buffer, | ||
| 25 | "recursor %s %d %s", argv[1], atoi (argv[2]) - 1, argv[3]); | ||
| 26 | pid = exec (buffer); | ||
| 27 | if (atoi (argv[3])) | ||
| 28 | retval = wait (pid); | ||
| 29 | } | ||
| 30 | |||
| 31 | /* Done. */ | ||
| 32 | printf ("%s %s: dying, retval=%d\n", argv[1], argv[2], retval); | ||
| 33 | exit (retval); | ||
| 34 | } | ||
