-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsgq_write.c
41 lines (31 loc) · 838 Bytes
/
msgq_write.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
// Program to demo Message Queue - Write
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#define MAX 10
#define BUF_SIZE 100
// structure for message queue
struct mesg_buffer
{
long mesg_type;
char mesg_text[BUF_SIZE];
} message;
int main()
{
key_t key;
int msgid;
printf("Enter message to be passed :\n");
//fgets(message.mesg_text, sizeof(message), stdin);
read(0, message.mesg_text, BUF_SIZE);
printf("Message being sent: %s\n", message.mesg_text);
// ftok to generate unique key
key = ftok("progfile", 65);
// msgget creates a message queue and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);
message.mesg_type = 1;
// msgsnd to send message
msgsnd(msgid, &message, sizeof(message), 0);
sleep(60);
return 0;
}