-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgSender.c
47 lines (41 loc) · 1.11 KB
/
msgSender.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
#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];
};
int
main() {
struct msgst msgs;
int msg_type;
char str[256];
int ret;
int msgid;
msgid = msgget(MSGKEY, IPC_EXCL);
if (msgid < 0) {
printf("Failed to create msq\n");
return -1;
}
while (1) {
printf("Input Message type: ");
scanf("%d", &msg_type);
if (msg_type == 0)
break;
printf("Input Message to be sent: ");
scanf("%s", str);
msgs.msgtype = msg_type;
strcpy(msgs.msgtext, str);
ret = msgsnd(msgid, &msgs, sizeof(struct msgst), IPC_NOWAIT);
if (ret < 0) {
printf("msgsnd() write message failed...\n");
return -1;
}
}
msgctl(msgid, IPC_RMID, 0);
return 0;
}