-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsg-send-test.c
More file actions
59 lines (48 loc) · 1.34 KB
/
msg-send-test.c
File metadata and controls
59 lines (48 loc) · 1.34 KB
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
53
54
55
56
57
58
59
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MSGSZ 128
/*
* Declare the message structure.
*/
typedef struct msgbuf {
long mtype;
char mtext[MSGSZ];
} msg_buf;
int main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
msg_buf sbuf;
size_t buf_length;
/*
* Get the message queue id for the "name" 9527, which was created by
* by the server.
*/
key = 9527;
if ((msqid = msgget(key, msgflg)) < 0) {
perror("msgget");
return 1;
} else {
(void) fprintf(stderr, "msgget: msgget succeeded: msqid = %d\n",
msqid);
}
sbuf.mtype = 1;
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
(void) strcpy(sbuf.mtext, "Hello message receiver...");
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
buf_length = strlen(sbuf.mtext) + 1 ;
/*
* Send a message.
*/
if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {
perror("msgsnd");
return 1;
} else {
printf("Message: \"%s\" Sent\n", sbuf.mtext);
}
return 0;
}