forked from nyg0813/sysprog_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mq_server.c
53 lines (49 loc) · 929 Bytes
/
mq_server.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
49
50
51
52
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
typedef struct {
long mtype; /* message type, must be > 0 */
char mtext[16]; /* message data */
} MsgType;
int main ( void )
{
key_t key = 4071;
int que_id = msgget(key,0666);
if(que_id==-1)
{
que_id = msgget(key, IPC_CREAT|0666);
if(que_id==-1)
{
printf("msgget error\n");
return -1;
}
}
MsgType msg;
ssize_t nbytes = 0;
int msg_size = 0;
msg_size = sizeof(msg) - sizeof(msg.mtype);
nbytes = msgrcv(que_id, &msg, msg_size, 2696, IPC_NOWAIT);
if (nbytes > 0)
{
printf("recv msg form msgque, bytes(%lu)\n", nbytes);
printf("%s\n", msg.mtext);
}
else
{
if (errno == ENOMSG)
printf("empty queue\n");
else
{
printf("msgrcv() error\n");
return -1;
}
}
return 0;
}