-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_queue.c
More file actions
77 lines (64 loc) · 1.5 KB
/
Copy pathmessage_queue.c
File metadata and controls
77 lines (64 loc) · 1.5 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*Using Message Queue - The first process sends a string to the second
process. The second process reverses the received string and sends it back
to the first process. The first process compares the original string and the
reversed string received from the second one and then prints whether the
string is a palindrome or not.
*/
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/wait.h>
#define MAX 50
struct m_buffer
{
long m_type;
char m_text[MAX];
};
int main()
{
pid_t id;
key_t key;
int msgid,l,i;
char text[MAX],temp;
struct m_buffer queue;
key = ftok(".",67);
msgid = msgget(key,0666 | IPC_CREAT);
id = fork();
if(id == 0)
{
msgrcv(msgid,&queue,sizeof(queue.m_text),1,0);
l = strlen(queue.m_text);
for(i = 0;i < (l / 2);i++)
{
temp = queue.m_text[i];
queue.m_text[i] = queue.m_text[(l - i - 1)];
queue.m_text[(l - i - 1)] = temp;
}
text[l] = '\0';
queue.m_type = 2;
msgsnd(msgid,&queue,sizeof(queue.m_text),0);
}
else
{
printf("Enter a string : ");
fgets(text,sizeof(text),stdin);
text[strcspn(text,"\n")] = '\0';
queue.m_type = 1;
strcpy(queue.m_text,text);
msgsnd(msgid,&queue,sizeof(queue.m_text),0);
msgrcv(msgid,&queue,sizeof(queue.m_text),2,0);
if(strcmp(text,queue.m_text) == 0)
{
printf("The string is a palindrome!\n");
}
else
{
printf("The string isn't a palindrome!\n");
}
wait(NULL);
msgctl(msgid, IPC_RMID, NULL);
}
return 0;
}