diff options
author | Tucker Evans <tuckerevans24@gmail.com> | 2017-11-26 02:16:57 -0500 |
---|---|---|
committer | Tucker Evans <tuckerevans24@gmail.com> | 2017-11-26 02:16:57 -0500 |
commit | a3da640beebd64bef1f63b2f07f639dff3ff3c09 (patch) | |
tree | 105c7927df72c1662dedac2ac0b891c261da1da1 | |
parent | 561079b325265af54c3d1d897863f21fa641e471 (diff) |
CS3871/assignments/sync: Added basic semaphore solution (no concurent readers)
P(sem)
crit
V(sem)
Removed debugging prints
-rw-r--r-- | sync/reader.c | 26 | ||||
-rw-r--r-- | sync/sync.c | 39 | ||||
-rw-r--r-- | sync/writer.c | 26 |
3 files changed, 69 insertions, 22 deletions
diff --git a/sync/reader.c b/sync/reader.c index 66ee57b..929eba1 100644 --- a/sync/reader.c +++ b/sync/reader.c @@ -4,11 +4,15 @@ #include <stdlib.h> #include <sys/ipc.h> #include <sys/select.h> +#include <sys/sem.h> #include <sys/shm.h> #include <sys/types.h> #include <time.h> #include <unistd.h> +#define NSEM 3 +#define KEY 52 + char *mem; void quit(signum) @@ -22,10 +26,10 @@ int main(argc, argv) int argc; char **argv; { -printf("\tR STARTING\n"); - int shmid, i, pid, id; + int shmid, semid, i, pid, id; char filename[50]; FILE *fd; + struct sembuf sb; if (argc < 2) { printf("usage: reader [id]\n"); @@ -33,7 +37,6 @@ printf("\tR STARTING\n"); } id = atoi(argv[1]); -printf("\tR%d. started...\n", id); if ((shmid = shmget(52, 1<<14, IPC_CREAT | 0666)) == -1){ @@ -45,12 +48,16 @@ printf("\tR%d. started...\n", id); perror("shmat"); exit(1); } + + if ((semid = semget(shmid, NSEM, 0)) == -1) { + perror("Rsemget: "); + exit(1); + } + signal(SIGQUIT, quit); -printf("\tR%d. SHMID: %d\n", id, shmid); sprintf(filename, "reader.%d", id); -printf("\tR%d. opening file \"%s\"...\n", id, filename); fd = fopen(filename, "a"); if (!fd) { @@ -60,7 +67,9 @@ printf("\tR%d. opening file \"%s\"...\n", id, filename); srand(time(NULL)); while (1) { -printf("\tR%d. reading...\n", id); + sb.sem_num = 0; sb.sem_op = -1; sb.sem_flg = 0; + semop(semid, &sb, 1); + for (i = 0; i < 1<<14; i++) { fprintf(fd, "%c", *(mem + i)); fflush(fd); @@ -68,7 +77,10 @@ printf("\tR%d. reading...\n", id); fprintf(fd, "\n"); fflush(fd); -printf("\tR%d. waiting...\n", id); + sb.sem_op = 1; + semop(semid, &sb, 1); + + sleep(rand() % (id + 1)); } } diff --git a/sync/sync.c b/sync/sync.c index 66574a1..23d6ba1 100644 --- a/sync/sync.c +++ b/sync/sync.c @@ -1,15 +1,28 @@ #include <stdio.h> #include <stdlib.h> #include <sys/ipc.h> +#include <sys/sem.h> #include <sys/shm.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> +#define NSEM 3 + +union semun { + int val; + struct semid_ds *buf; + ushort *array; +}; + + +int shmid, semid; + void quit(signum) int signum; { shmctl(shmid, IPC_RMID, NULL); + semctl(semid, IPC_RMID, 0); } @@ -17,8 +30,9 @@ int main(argc, argv) int argc; char **argv; { - int shmid, i, pid, n_read, n_write, w; + int i, pid, n_read, n_write, w; char *mem, **arg_r, **arg_w; + union semun semarg; if (argc < 2) { printf("usage: sync [number readers] [number writers]\n"); @@ -36,13 +50,27 @@ char **argv; perror("shmat"); exit(1); } +printf("Sshmid: %x\n", shmid); signal(SIGQUIT, quit); -printf("\nSHMID: %d\nsetting initial memory values...\n", shmid); for (i = 0; i < 1<<14; i++) { *(mem + i) = 0x30; } + if ((semid = semget(shmid, NSEM, 0666 | IPC_CREAT)) == -1) { + perror("Ssemget: "); + exit(1); + } + + semarg.val = 1; + for (i = 0; i < NSEM; i++) { + if ((semctl(semid, i, SETVAL, semarg)) == -1) { + perror("semctl: "); + exit(1); + } + } + + arg_r = malloc(sizeof(char*) * 3); arg_w = malloc(sizeof(char*) * 3); *arg_r = malloc(sizeof(char) * 10); @@ -57,11 +85,8 @@ printf("\nSHMID: %d\nsetting initial memory values...\n", shmid); *(arg_r + 2) = NULL; *(arg_w + 2) = NULL; -printf("forking readers...\n"); for (i = 0; i < n_read; i++){ -printf("sprintf...\n"); sprintf(*(arg_r + 1), "%d", i); -printf("\t%d\n",i); if (pid = fork()) { /* printf("starting reader %d...\n", i); */ } else { @@ -70,10 +95,8 @@ printf("\t%d\n",i); } } -printf("forking writers...\n"); for (i = 0; i < n_write; i++) { sprintf(*(arg_w + 1), "%d", i); -printf("\t%d\n", i); if (pid = fork()) { /* printf("starting writer %d...\n", i); */ } else { @@ -89,7 +112,7 @@ printf("sync done...\n"); */ for (i = 0; i < (n_write + n_read); i++) { wait(&w); - printf("\nReturned with code:%d\n", WTERMSIG(w)); + printf("\nReturned with code:%d\n", WEXITSTATUS(w)); } quit(); } diff --git a/sync/writer.c b/sync/writer.c index b1ab9ed..d38dd70 100644 --- a/sync/writer.c +++ b/sync/writer.c @@ -4,12 +4,15 @@ #include <stdlib.h> #include <sys/ipc.h> #include <sys/select.h> +#include <sys/sem.h> #include <sys/shm.h> #include <sys/types.h> #include <sys/types.h> #include <time.h> #include <unistd.h> +#define NSEM 3 + char *mem; void quit(signum) @@ -23,9 +26,9 @@ int main(argc, argv) int argc; char **argv; { -printf("\tW STARTING\n"); - int shmid, i, pid, id; + int shmid, semid, i, pid, id; char *mem; + struct sembuf sb; if (argc < 2) { printf("usage: writer [id]\n"); @@ -34,7 +37,6 @@ printf("\tW STARTING\n"); id = atoi(argv[1]); -printf("\tW%d. started...\n", id); if ((shmid = shmget(52, 1<<14, IPC_CREAT | 0666)) == -1){ perror("shmget: shmget failed"); @@ -45,20 +47,30 @@ printf("\tW%d. started...\n", id); perror("shmat"); exit(1); } +printf("Wshmid: %x\n", shmid); + + if ((semid = semget(shmid, NSEM, 0)) == -1) { + perror("Wsemget: "); + exit(1); + } signal(SIGQUIT, quit); -printf("\tW%d. SHMID: %d\n", id, shmid); srand(time(NULL)); while (1) { -printf("\tW%d. testing...\n", id); rand() % id; -printf("\tW%d. writing...\n", id); + + sb.sem_num = 0; sb.sem_op = -1; sb.sem_flg = 0; + semop(semid, &sb, 1); + for (i = 0; i < 1<<14; i++) { mem[i]= 0x30 + id; } -printf("\tW%d. waiting...\n"); + + sb.sem_op = 1; + semop(semid, &sb, 1); + sleep(rand() % ((id * 2) + 1)); } } |