diff options
author | Tucker Evans <tuckerevans24@gmail.com> | 2017-11-24 18:12:18 -0500 |
---|---|---|
committer | Tucker Evans <tuckerevans24@gmail.com> | 2017-11-24 18:12:18 -0500 |
commit | 2177f0712fb47a95b2970c6838a4a4d978174b3b (patch) | |
tree | fe66e6a05d978e9fb91560d03774ee88c862db4c /sync/sync.c | |
parent | 0e15942312c9c20fcac5ac9f2368d94b80fda230 (diff) |
CS3871/assignments/sync: Initial Commit
Diffstat (limited to 'sync/sync.c')
-rw-r--r-- | sync/sync.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/sync/sync.c b/sync/sync.c new file mode 100644 index 0000000..e1fbc4c --- /dev/null +++ b/sync/sync.c @@ -0,0 +1,61 @@ +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/shm.h> +#include <stdlib.h> +#include <unistd.h> +#include <stdio.h> + + +int main(argc, argv) +int argc; +char **argv; +{ + int shmid, i, pid, n_read, n_write; + char *mem, **arg_r, **arg_w; + + if (argc < 2) { + printf("usage: sync [number readers] [number writers]\n"); + exit(1); + } + n_read = atoi(argv[1]); + n_write = atoi(argv[2]); + + if ((shmid = shmget(52, 1<<14, IPC_CREAT | 0666)) == -1){ + perror("shmget: shmget failed"); + exit(1); + } + + if ((mem = shmat(shmid, NULL, 0)) == (char *) -1) { + perror("shmat"); + exit(1); + } + + for (i = 0; i < 1<<14; i++) { + *(mem + i) = 0x30; + } + + arg_r = malloc(sizeof(char*) * 2); + arg_w = malloc(sizeof(char*) * 2); + + *(arg_r + 1) = NULL; + *(arg_w + 1) = NULL; + + for (i = 0; i < n_read; i++){ + sprintf(*arg_r, "%d", i); + if (pid = fork()) { + printf("starting reader %d...\n", i); + } else { + execvp("./reader", arg_r); + } + } + + for (i = 0; i < n_write; i++) { + sprintf(*arg_w, "%d", i); + if (pid = fork()) { + printf("starting writer %d...\n", i); + } else { + execvp("./writer", arg_w); + } + + } +} |