Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/wait.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- enum
- {
- BASE = 10,
- NAMELEN = 20
- };
- int
- main(int argc, char *argv[])
- {
- if (argc < 2) {
- return 0;
- }
- int count = strtol(argv[1], NULL, BASE);
- int semid = semget(IPC_PRIVATE, count + 1, 0666|IPC_CREAT);
- int i;
- for (i = 0; i < count; i++) {
- if (!fork()) {
- semop(semid, (struct sembuf[]){{count, 1, 0}}, 1);
- int num;
- while (semop(semid, (struct sembuf[]){{i, -1, 0}}, 1) >= 0) {
- if (scanf("%d", &num) <= 0) {
- semctl(semid, 0, IPC_RMID, 0);
- break;
- }
- printf("%d %d\n", i, num);
- fflush(stdout);
- num = num >= 0 ? num % count : num % count + count;
- semop(semid, (struct sembuf[]){{num % count, 1, 0}}, 1);
- }
- _exit(0);
- }
- }
- semop(semid, (struct sembuf[]){{count, -count, 0}}, 1);
- semop(semid, (struct sembuf[]){{0, 1, 0}}, 1);
- while (wait(NULL) >= 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement