- Get link
- X
- Other Apps
Featured Post
Posted by
Unknown
on
- Get link
- X
- Other Apps
A simple implementation of IPC Message Queues.
IPC_msgq_send.c adds the message on the message queue .
IPC_msgq_rcv.c removes the message from the message queue.
To use this program first compile and run IPC_msgq_send.c to add a message to the message queue. To see the Message Queue type ipcs -q on your Unix/Linux Terminal.
Now compile and run IPC_msgq_rcv.c to read the message from the Message Queue.
To see that you have read the message again use ipcs -q
IPC_msgq_send.c adds the message on the message queue .
IPC_msgq_rcv.c removes the message from the message queue.
To use this program first compile and run IPC_msgq_send.c to add a message to the message queue. To see the Message Queue type ipcs -q on your Unix/Linux Terminal.
Now compile and run IPC_msgq_rcv.c to read the message from the Message Queue.
To see that you have read the message again use ipcs -q
//IPC_msgq_send.c #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXSIZE 128 void die(char *s) { perror(s); exit(1); } typedef struct msgbuf { long mtype; char mtext[MAXSIZE]; }; main() { int msqid; int msgflg = IPC_CREAT | 0666; key_t key; struct msgbuf sbuf; size_t buflen; key = 1234; if ((msqid = msgget(key, msgflg )) < 0) //Get the message queue ID for the given key die("msgget"); //Message Type sbuf.mtype = 1; printf("Enter a message to add to message queue : "); scanf("%[^\n]",sbuf.mtext); getchar(); buflen = strlen(sbuf.mtext) + 1 ; if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0) { printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buflen); die("msgsnd"); } else printf("Message Sent\n"); exit(0); }
//IPC_msgq_rcv.c #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <stdio.h> #include <stdlib.h> #define MAXSIZE 128 void die(char *s) { perror(s); exit(1); } typedef struct msgbuf { long mtype; char mtext[MAXSIZE]; } ; main() { int msqid; key_t key; struct msgbuf rcvbuffer; key = 1234; if ((msqid = msgget(key, 0666)) < 0) die("msgget()"); //Receive an answer of message type 1. if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0) die("msgrcv"); printf("%s\n", rcvbuffer.mtext); exit(0); }
Comments
very nice and simple code, helped me a lot , thanks....:)
ReplyDeletethank you bhaiya...............it is really a simplest coding............thanks again
ReplyDeleteyes the bhaiya helped us all...all hail bhaiya
Deletei helped make you
Deleteyuppyyyyy..its working buddy!!thanx
ReplyDeleteerror...in IPC_msgq_send.c
ReplyDeletemsg_que_sent.c: In function ‘main’:
msg_que_sent.c:45:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long int’ [-Wformat=]
printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buflen);
^
Hey, its not an error, its seems to be a warning. Probably our compiler versions were different. Not an issue until it's not compiling. You can use %ld if needed.
Deletecan u write another prgrm that do both things at same time means recive and send the message at same time and other program is also send and recive the message at same time means both programme send and recive...
Delete\it is possible???
if yes than plz write the code..
i think think this is possibble using two message queues.. but i didnt do it if u than plz help me...
use %ld for mtext
Deleteuse %ld for mytype and change s_size with int.theb it will successfully compile
Deletei got an error it says variable has
Deleteincomplete type 'struct msgbuf'
struct msgbuf rcvbuffer;
i got 2 errors when running the code:
Delete1) variable has incomplete type
'struct msgbuf'
struct msgbuf rcvbuffer;
Please add typedef in front of struct declaration.
DeleteI have fixed that in the code.
Deletehow to take input from the file using pipes and then display the messge using grep command??
ReplyDeleteThank you. Helped me to Understand the basics
ReplyDeletethanks a lot boss :)
ReplyDeleteThanks Dude for Short and simple explanation.
ReplyDeleteCan you throw some light on POSIX queue as well .if it is possible.
When i compile it give me error like this
ReplyDelete"msgget: Function not implemented".
Please Give me a solution of it???
How can i solve it???
have you included #include and #include headers
ReplyDeletei didnt get the out
ReplyDeletethanks for sharing simple understandable program.
ReplyDeletewhen to use msg queue and when to use shared memory in IPC ? can u pls throw some light, thanks in advance
ReplyDelete
DeleteIn short Message queues are more reliable but slower as compared to shared memory. However, shared memory has synchronization issues. They both can be combined to achieve better results.
Essentially if multiple processes need to update a chunk of data concurrently, you would go for shared memory.
If you need a message passing kind of scenario where a mailbox kind of scenario exist where a process sends messages to a message queue and different processes read it from the queue one by one.
plz program for reader writer by using message queue.... any one can interested in these program..
ReplyDeleteso plz reply me.. and provide the code
In sending part, on which basis we decided the buflen = strlen + 1 , as text length is strlen but mtype is long so why we have just added 1 byte instead of sizeof long
ReplyDeleteHow do I run message queue program on windows????
ReplyDeleteThis is not an implementation of message queue, rather this is an example app that uses the POSIX Message Queue.
ReplyDeleteHello sir, whether it will run on putty server our lab programs is done in putty server
ReplyDelete