Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/msg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/ipc.h>
- #include <sys/types.h>
- #include <sys/sem.h>
- #include <sys/shm.h>
- #include <sys/wait.h>
- #include <math.h>
- int main(int argc, char *argv[]) {
- char *eptr;
- int count = strtol(argv[1], &eptr, 10);
- int sem = semget(IPC_PRIVATE, count, IPC_CREAT | 0666);
- setbuf(stdin, 0);
- struct sembuf up = {0, 1, 0};
- semop(sem, &up, 1);
- for (int i = 0; i < count; i++) {
- if (!fork()) {
- struct sembuf down = {i, -1, 0};
- while(semop(sem, &down, 1) != -1) {
- int num;
- if (scanf("%d", &num) > 0) {
- printf("%d %d\n", i, num);
- fflush(stdout);
- up.sem_num = abs(num) % count;
- semop(sem, &up, 1);
- } else {
- semctl(sem, 0, IPC_RMID, 0);
- exit(0);
- }
- }
- }
- }
- semop(sem, &up, 1);
- while(wait(NULL) != -1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement