Skip to content

Instantly share code, notes, and snippets.

@sacko87
Created August 11, 2012 21:42
Show Gist options
  • Save sacko87/3327485 to your computer and use it in GitHub Desktop.
Save sacko87/3327485 to your computer and use it in GitHub Desktop.
IPC Sender and Receiver Example
Description
===========
This is a small example of how to use IPC queues using the C
programming language. There are two separate implementations:
* the sender
this has the ability to send data (using the command line)
* the receiver
this has the ability to recieve data from the sender
COMPILATION
===========
Run the following to generate both the sender AND receiver
binaries.
make
Run the following to generate ONLY the sender
make sender
Run the following to generate ONLY the receiver
make receiver
CLEANUP
=======
Cleaning will remove the binaries from the local directory
Run:
make clean
make clean-snd
make clean-rcv
# the compiler to use
CC =gcc
# flags to send to the compiler
CFLAGS = -O2 -Wall
# executable names
SENDER_EXEC = Sender
RECEVIER_EXEC = Receiver
# compile the message queue programs
# that use IPC messages
## both sender and receiver
sndrcv: ${SENDER_EXEC} ${RECEVIER_EXEC}
## sender ONLY
sender: Sender.c
${CC} ${CFLAGS} ${SENDER_EXEC}.c -o ${SENDER_EXEC}
## receiver ONLY
receiver: Receiver.c
${CC} ${CFLAGS} ${RECEVIER_EXEC}.c -o ${RECEVIER_EXEC}
# clean the binaries that would
# be created during compilation
## both sender and receiver
clean: clean-snd clean-rcv
## sender ONLY
clean-snd:
rm -Rf ${SENDER_EXEC} ${SENDER_EXEC}.dSYM
## receiver ONLY
clean-rcv:
rm -Rf ${RECEVIER_EXEC} ${RECEVIER_EXEC}.dSYM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int
main(void)
{
int mq; /* the queue handler */
/* create a key for the queue */
key_t k = ftok("/var", 'c');
if(k != -1) { /* did we succeed? */
/* get the queue (create if it doesn't exist) */
mq = msgget(k, 0644 | IPC_CREAT);
if(mq != -1) { /* did we succeed */
char b[256] = ""; /* a message buffer */
/* keep waiting on messages */
while(1) { /* keep going */
/* attempt to receive a message */
if(msgrcv(mq, &b, sizeof(b), 0, 0) != -1) {
/* was it the `stop' instruction? */
if(strcmp(b, "stop") == 0) break;
printf("%s\n", b); /* print the message */
} else {
perror("msgrcv()");
break;
}
}
} else {
perror("msgget()");
return 2;
}
} else {
perror("ftok()");
return 1;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int
main(void)
{
int mq; /* the queue handler */
/* create a key for the queue */
key_t k = ftok("/var", 'c');
if(k != -1) { /* did we succeed? */
/* get the queue (create if it doesn't exist) */
mq = msgget(k, 0644 | IPC_CREAT);
if(mq != -1) { /* did we succeed */
char b[256] = ""; /* a message buffer */
size_t l; /* the length of the string from stdin */
/* get some data from the command line */
while(fgets(b, sizeof(b), stdin) != NULL) {
if((l = strlen(b)) > 0) { /* did we get anything? */
/* change the '\n' to a '\0' */
if(b[l - 1] == '\n') { b[l - 1] = 0; l--; }
/* is it the command to quit? */
if(strcmp(b, "quit") == 0) break;
/* send the message to the queue */
if(msgsnd(mq, &b, l, 0) == -1) {
perror("msgsnd()");
break;
}
}
}
} else {
perror("msgget()");
return 2;
}
/* close the queue */
if(msgctl(mq, 0, NULL) == -1) {
perror("msgctl()");
return 3;
}
} else {
perror("ftok()");
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment