diff options
author | Matthew Strapp <msattr@gmail.com> | 2020-09-21 08:26:21 -0500 |
---|---|---|
committer | Matthew Strapp <msattr@gmail.com> | 2020-09-21 08:26:21 -0500 |
commit | a97b8f7687cdd75be72547a20222a50a56ee7b28 (patch) | |
tree | fbecf6957fec34d0cdb1289c55f4ece4c8ee0a7e /csci4061 | |
parent | add first breakout (diff) | |
download | homework-a97b8f7687cdd75be72547a20222a50a56ee7b28.tar homework-a97b8f7687cdd75be72547a20222a50a56ee7b28.tar.gz homework-a97b8f7687cdd75be72547a20222a50a56ee7b28.tar.bz2 homework-a97b8f7687cdd75be72547a20222a50a56ee7b28.tar.lz homework-a97b8f7687cdd75be72547a20222a50a56ee7b28.tar.xz homework-a97b8f7687cdd75be72547a20222a50a56ee7b28.tar.zst homework-a97b8f7687cdd75be72547a20222a50a56ee7b28.zip |
Do second breakout
Diffstat (limited to 'csci4061')
-rw-r--r-- | csci4061/092120_breakout/main.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/csci4061/092120_breakout/main.c b/csci4061/092120_breakout/main.c new file mode 100644 index 0000000..835dd88 --- /dev/null +++ b/csci4061/092120_breakout/main.c @@ -0,0 +1,49 @@ +/* +CSci 4061 - Recitation 2 - 21st Sept 2020 +Full Name : x500 +Full Name : x500 +Full Name : x500 +Full Name : x500 +Matthew Strapp : strap012 +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include <errno.h> + +int main(int argc, char **argv) { + // take 'n' from the input and convert to integer + // hint: see cmd slide + if (argc < 2) { + printf("USAGE: ./main int\n"); + return -1; + } + int n = strtol(argv[1], NULL, 10); + // for i in n + // create child process + // print pid //hint: check week 2 slides + // call execl to print 'hello there' using 'echo' //hint: check recitation slide + pid_t pid; + for (int i=0; i<n; i++) { + pid = fork(); + if (pid != 0) { + printf("%d\n", getpid()); + execl("/bin/echo", "/bin/echo", "hello", "there", NULL); + } + } + // parent waits for all child processes to terminate + wait(NULL); + // parent create child process + // call execv on 'ptime' executable // hint: similar to 'echo' usage in slide + pid = fork(); + if (pid != 0) { + char *args[] = {"./ptime", NULL}; + execv(*args, args); + } + // parent waits for child to complete + waitpid(pid, NULL, 0); + return 0; +}
\ No newline at end of file |