-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsgReceive.c
More file actions
46 lines (39 loc) · 1.04 KB
/
msgReceive.c
File metadata and controls
46 lines (39 loc) · 1.04 KB
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
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>
#define MSGKEY 1024
struct msgst {
long msgtype;
char msgtext[2048];
};
void childproc(){
struct msgst msgs;
int msgid,ret;
char str[512];
while(1){
msgid = msgget(MSGKEY, IPC_CREAT | 0660);
if(msgid < 0) {
printf("msq not existed! errno=%d [%s]\n", errno, strerror(errno));
sleep(2);
continue;
}
ret = msgrcv(msgid, &msgs, sizeof(struct msgst), 0, 0);
printf("text=[%s] pid=[%d]\n", msgs.msgtext, getpid());
}
return;
}
int
main() {
int i, cpid;
for (i=0; i<5; i++){
cpid = fork();
if (cpid < 0)
printf("fork failed\n");
else if (cpid ==0) /*child process*/
childproc();
}
return 0;
}