-
Notifications
You must be signed in to change notification settings - Fork 44
/
msg_send.cpp
50 lines (41 loc) · 872 Bytes
/
msg_send.cpp
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
//
// Created by jxq on 19-8-12.
//
// p25 system v消息队列(二)
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while (0);
int main(int argc, char** argv)
{
if (argc != 3)
{
fprintf(stderr, "Usage: %s <bytes> <type>\n", argv[1]);
exit(EXIT_FAILURE);
}
int len = atoi(argv[1]);
int type = atoi(argv[2]);
int msgid;
msgid = msgget(1234, 0);
if (msgid == -1)
{
ERR_EXIT("msgget");
}
struct msgbuf *ptr;
ptr = (struct msgbuf*) malloc(sizeof(long) + len);
ptr->mtype = type;
if (msgsnd(msgid, ptr, len, 0) < 0)
{
ERR_EXIT("msgsnd");
}
return 0;
}