Last active
February 5, 2016 11:16
-
-
Save anirudh-ramesh/415631b1034bda38a594 to your computer and use it in GitHub Desktop.
Message Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Fire up the command prompt. | |
2.1. Execute: gcc send.c -o send | |
2.2. Execute: gcc recv.c -o recv | |
3. Execute: ./send <<< "Message." | |
4. Execute: ./recv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <errno.h> | |
#include <signal.h> | |
#include <stdarg.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/ipc.h> | |
#include <sys/msg.h> | |
#include <unistd.h> | |
#define MSGSZ 255 | |
#define KEY 10 | |
typedef struct msgbuf | |
{ | |
long mtype; | |
char mtext[MSGSZ+1]; | |
} message_buf; | |
static void INThandler(int); | |
int main(void) | |
{ | |
key_t key = KEY; | |
int msgflg = 0644; | |
message_buf rbuf; | |
int msqid = msgget(key, msgflg); | |
if (msqid < 0) | |
{ | |
perror("msgget"); | |
exit(1); | |
} | |
signal(SIGINT, INThandler); | |
rbuf.mtype = 1; | |
for ( ; ; ) | |
{ | |
printf("Waiting...\n"); | |
ssize_t nbytes = msgrcv(msqid, &rbuf, MSGSZ, 0, 0); | |
if (nbytes < 0) | |
{ | |
perror("msgrcv"); | |
exit(1); | |
} | |
printf("PID - %ld : Message (%d) - %s", rbuf.mtype, (int)nbytes, rbuf.mtext); | |
} | |
return 0; | |
} | |
static void INThandler(int sig) | |
{ | |
signal(sig, SIG_IGN); | |
int mask, msgid; | |
key_t key = KEY; | |
mask = 0644; | |
msgid = msgget(key, mask); | |
if (msgid == -1) | |
{ | |
printf("Message queue does not exist.\n"); | |
exit(EXIT_SUCCESS); | |
} | |
if (msgctl(msgid, IPC_RMID, NULL) == -1) | |
{ | |
fprintf(stderr, "Message queue could not be deleted.\n"); | |
exit(EXIT_FAILURE); | |
} | |
else | |
printf("Message queue was deleted.\n"); | |
exit(0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/ipc.h> | |
#include <sys/msg.h> | |
#include <unistd.h> | |
#define MSGSZ 255 | |
#define KEY 10 | |
typedef struct msgbuf | |
{ | |
long mtype; | |
char mtext[MSGSZ+1]; | |
} message_buf; | |
int main(void) | |
{ | |
message_buf sbuf; | |
sbuf.mtype = getpid(); | |
printf("The PID is %ld\n", sbuf.mtype); | |
printf("Enter word(s) (Limit: %d characters):\n", MSGSZ+1); | |
if (fgets(sbuf.mtext, sizeof(sbuf.mtext), stdin) == 0) | |
{ | |
printf("End-of-File detected!\n"); | |
exit(EXIT_FAILURE); | |
} | |
printf("\n"); | |
key_t key = KEY; | |
int mask = 0644 | IPC_CREAT; | |
int msgid = msgget(key, mask); | |
if (msgid < 0) | |
{ | |
fprintf(stderr, "Failed to create message key %d.\n", key); | |
exit(EXIT_FAILURE); | |
} | |
if (msgsnd(msgid, &sbuf, strlen(sbuf.mtext)+1, IPC_NOWAIT) < 0) | |
{ | |
perror("msgsnd"); | |
exit(EXIT_FAILURE); | |
} | |
printf("id - %ld : message - %s\n", sbuf.mtype, sbuf.mtext); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment