forked from nyg0813/sysprog_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mq_client.c
52 lines (41 loc) · 871 Bytes
/
mq_client.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
// Example of message queue in C.
// For educational purposes only.
// Author: Vaclav Bohac
#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;
int msg_size = 0;
msg.mtype=2696;
strcpy(msg.mtext, "helloworld");
msg_size = sizeof(msg) - sizeof(msg.mtype);
int rtn = msgsnd(que_id, &msg, msg_size, IPC_NOWAIT);
if (rtn == -1) {
printf("msgsnd() fail\n");
return -1;
}
}