-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_queue.c
More file actions
42 lines (34 loc) · 854 Bytes
/
message_queue.c
File metadata and controls
42 lines (34 loc) · 854 Bytes
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
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <stdio.h>
#include "message_queue.h"
int get_message_queue() {
int msgqid;
msgqid = msgget(IPC_PRIVATE, IPC_CREAT | 0666);
if (msgqid == -1) {
perror("msgget");
exit(1);
}
return msgqid;
}
void receive_msg(int msgqid, struct msgbuf* mbuf, int mtype) {
if (msgrcv(msgqid, mbuf, sizeof(mbuf->mtext), mtype, 0) == -1) {
perror("msgrcv");
exit(1);
}
}
void send_msg(int msgqid, struct msgbuf* mbuf, int mtype) {
mbuf->mtype = mtype;
if (msgsnd(msgqid, mbuf, sizeof(mbuf->mtext), IPC_NOWAIT) < 0) {
perror("msgsnd");
exit(1);
}
}
void remove_message_queue(int msgqid) {
if (msgctl(msgqid, IPC_RMID, NULL) == -1) {
perror("msgctl");
exit(1);
}
}