-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage_queue.c
48 lines (45 loc) · 859 Bytes
/
message_queue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <unistd.h>
void main(){
int msgid,p,i,f=0;
char buf[50];
struct mymsg{
long mtype;
char mtext[50];
}msg,msg2;
msgid=msgget(IPC_PRIVATE,0777|IPC_CREAT);
if(msgid==2){
printf("Fail");
exit(2);
}
p=fork();
if(p==-1){
printf("Fork");
exit(1);
}
while(1){
if(p==0){
sleep(1);
printf("\nChild: ");
fgets(msg.mtext,50,stdin);
msg.mtype=1;
msgsnd(msgid,&msg,50,0);
sleep(1);
msgrcv(msgid,&msg2,50,0,IPC_NOWAIT);
printf("\nCHILD->PARENT : %s\n", msg2.mtext);
sleep(1);
printf("Parent:");
fgets(msg2.mtext,50,stdin);
msg2.mtype=2;
msgsnd(msgid,&msg2,50,0);
sleep(1);
msgrcv(msgid,&msg,50,0,IPC_NOWAIT);
printf("\nParent->Child : %s",msg.mtext);
}
}
}