Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/msg.h>
- struct m
- {
- long type;
- long end;
- };
- void doson(int key, int son, int brother)
- {
- struct m msg;
- int val;
- if(!fork()) {
- int msqid = msgget(key, 0);
- while(1) {
- msgrcv(msqid, &msg, sizeof(msg.end), son, 0);
- if(msg.end == 1) {
- _exit(0);
- }
- if(scanf("%d", &val) < 0) {
- msg.end = 1;
- msg.type = brother;
- msgsnd(msqid, &msg, sizeof(msg.end), 0);
- _exit(0);
- }
- printf("%d %d\n",son, val);
- fflush(stdout);
- msg.type = brother;
- msgsnd(msqid, &msg, sizeof(msg.end), 0);
- }
- }
- }
- int
- main(void)
- {
- struct m msg;
- key_t key = ftok("file1.txt", 0);
- int msqid = msgget(key, IPC_CREAT | 0644);
- msg.type = 1;
- msg.end = 0;
- msgsnd(msqid, &msg, sizeof(msg.end), 0);
- doson(key, 1, 2);
- doson(key, 2, 1);
- wait(NULL);
- wait(NULL);
- msgctl(msqid, IPC_RMID, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement