diff options
Diffstat (limited to 'sync/writer.c')
-rw-r--r-- | sync/writer.c | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/sync/writer.c b/sync/writer.c index deaa8d0..b1ab9ed 100644 --- a/sync/writer.c +++ b/sync/writer.c @@ -1,33 +1,40 @@ -#include <sys/types.h> +#include <errno.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> #include <sys/ipc.h> +#include <sys/select.h> #include <sys/shm.h> -#include <stdlib.h> -#include <unistd.h> -#include <stdio.h> #include <sys/types.h> -#include <sys/select.h> +#include <sys/types.h> #include <time.h> +#include <unistd.h> +char *mem; + +void quit(signum) +int signum; +{ + shmdt(mem); + exit(1); +} int main(argc, argv) int argc; char **argv; { +printf("\tW STARTING\n"); int shmid, i, pid, id; char *mem; - struct timeval *s; - if (argc != 1) { - printf("usage: reader [id]\n"); + if (argc < 2) { + printf("usage: writer [id]\n"); exit(1); } id = atoi(argv[1]); - if (argc < 2) { - printf("usage: sync [number readers] [number writers]\n"); - exit(1); - } +printf("\tW%d. started...\n", id); if ((shmid = shmget(52, 1<<14, IPC_CREAT | 0666)) == -1){ perror("shmget: shmget failed"); @@ -38,15 +45,20 @@ char **argv; perror("shmat"); exit(1); } + + signal(SIGQUIT, quit); +printf("\tW%d. SHMID: %d\n", id, shmid); srand(time(NULL)); - while (1) { - s->tv_sec = rand() % (id * 2); +printf("\tW%d. testing...\n", id); + rand() % id; +printf("\tW%d. writing...\n", id); for (i = 0; i < 1<<14; i++) { mem[i]= 0x30 + id; - select(0, NULL, NULL, NULL, s); } +printf("\tW%d. waiting...\n"); + sleep(rand() % ((id * 2) + 1)); } } |