-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgq.c
116 lines (82 loc) · 1.88 KB
/
msgq.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>//key_t
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
struct my_msgbuf {
long mtype;
char mtext[20];
};
int initializeMessageQueue()
{
key_t key;
int msgqid;
if ((msgqid = msgget(key, 0666 | IPC_CREAT)) == -1) {
perror("msgget");
exit(1);
}
return msgqid;
}
void killMessageQueue(int msgqid)
{
if (msgctl(msgqid, IPC_RMID, NULL) == -1) {
perror("msgctl");
exit(1);
}
}
void int_send(int num, int msgqid, int type)
{
printf("Converting num %d to string \n", num);
struct my_msgbuf buf;
buf.mtype = type;
sprintf(buf.mtext,"%d",num); //converting interger to string
size_t size = sizeof(buf.mtext);
if (msgsnd(msgqid, &buf, size, 0) == -1)
perror("msgsnd");
}
void int_recv(int msgqid, int type)
{
struct my_msgbuf buf;
if (msgrcv(msgqid, &buf, sizeof (buf.mtext), type, 0) == -1) {
perror("msgrcv");
exit(1);
}
printf("Converting \"%s\" to integer \n",buf.mtext);
int num = atoi(buf.mtext); //converting string to interger
printf("num retrieved from queue: %d\n",num);
}
int main(void)
{
int msgqid = initializeMessageQueue();
printf("Created a queue with msqid: %d\n", msgqid);
int status;
pid_t pid, wpid;
pid = fork();
switch (pid) {
case -1:
printf("fork failed");
exit(-1);
case 0:
int_recv(msgqid, getpid());
sleep(2);
exit(0);
default:
do
{
printf("Parent: pid=%d ", getpid());
printf("my child is %d \n", pid);
int num;
printf("Enter a number :");
scanf("%d",&num);
int_send(num, msgqid, pid);
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
printf("Exiting ... killing message queue \n");
killMessageQueue(msgqid);
}
}