Producer and consumer - segmentation fault - c

I'm a beginner in concurrent programming and I want to implement a consumer and producer. I want to send after 3 seconds from producer two integers and I want to print the sum of numbers from producer.
After running the code I have Segmentation fault :(.
Thank you for help :D
This is my code for producer:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define VECTOR_SIZE 2
struct msgbuf
{
long mtype;
int vector[VECTOR_SIZE];
};
int main() {
int msqid;
// int msgflg = IPC_CREAT | 0666;
key_t key;
struct msgbuf sbuf;
size_t buflen;
int i;
key = ftok(".", 'g');
//Get the message queue ID for the given key
if ((msqid = msgget(key, IPC_CREAT | 0666 )) < 0) {
perror("Could not get message queue\n");
exit(1);
}
printf("MSQID value: %d\n", msqid);
//Message Type
sbuf.mtype = 1;
while(1){
for(i = 0; i < 2; i++){
printf("Enter a message to add to message queue : ");
scanf("%d",sbuf.vector[i]);
buflen = strlen(sbuf.vector[i]) + 1 ;
}
sleep(3);
}
// getchar();
if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0)
{
printf ("%d, %ld, %d, %d\n", msqid, sbuf.mtype, sbuf.vector[0], sbuf.vector[1], (int)buflen);
perror("Could not send message!\n");
exit(1);
}
else
printf("Message Sent\n");
exit(0);
}
And here I have the code for consumer:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 128
#define VECTOR_SIZE 2
struct msgbuf
{
long mtype;
char vector[VECTOR_SIZE];
};
int main() {
int msqid;
key_t key;
struct msgbuf rcvbuffer;
// key = 1234;
key = ftok(".", 'g');
if ((msqid = msgget(key, 0666)) < 0) {
perror("Could not get message queue\n");
exit(1);
}
printf("MSQID value: %d\n", msqid);
// Receive an answer of message type 1.
if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0) {
perror("Could not receive message!\n");
exit(1);
}
printf("%d\n", rcvbuffer.vector[0] + rcvbuffer.vector[1]);
return 0;
}

Related

How to block with semaphore on worker master file?

Hy guys i have one problem. My teacher he ask me to do a worker master program.The first conection is about to give with the msg queues ,then i msg from master to worker the key for shared memory and the key from semaphore ( here i need to creat a semaphore with key and intiliaze ). The problem is here , before write on shared memory the structure i have (file_entry) i need with the semaphore to block the worker.c and when the master has done to write the strucure on shared memory the worker.c just open again. Aslo my teacher he want my to do with no posix but with system V the semaphore .
master.c file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <sys/sem.h>
#include <sys/types.h>
#include<unistd.h>
#include <fcntl.h>
#include <semaphore.h>
#include <errno.h>
#define size_array 8
#define id 60
// structure for message queue
struct mesg_buffer {
long mesg_type;
int shm_key;
int sem_key;
} message;
typedef struct file
{
int zero;
int p_pid;
int array_length;
int array;
} file_entry;
int main(int argc, char *argv[])
{
key_t msg_key, shm_key, sem_key;
int msgid, shmid, semid, i;
sem_t *sem;
pid_t procces_id ;
// ftok to generate unique key for msg queue
msg_key = ftok("progfile.txt", id);
if( msg_key < 0 ){
printf(" error on generate key msg queue !!\n ");
}
// msgget creates a message queue
// and returns identifier
msgid = msgget(msg_key, 0666 | IPC_CREAT );
if ( msgid < 0 ){
printf("error to identifier on msg queue !!\n");
}
shm_key = shm_key/2;
// shmget returns an identifier in shmid
shmid = shmget( shm_key, sizeof(file_entry) * 8, IPC_CREAT | 0666 );
if( shmid < 0 ){
printf("error to identifier on shm !!\n");
}
sem_key = ftok("semfile.txt", id);
if( sem_key < 0 ){
printf(" error on generate key semaphore !!\n ");
}
// create a new semaphore
sem_key=sem_key+1;
semid = semget(sem_key, 1, IPC_CREAT);
if( semid < 0 ){
printf("error to identifier on semaphore !!\n");
}
struct sembuf sbuf;
sbuf.sem_flg = 0;
sbuf.sem_num = 0;
sbuf.sem_op = 1;
int ret=semop(semid, &sbuf, 1);
printf("safas %d \n",ret);
if( ret < 0 ){
printf("error to intialize !!\n");
}
message.mesg_type = 1;
message.shm_key=shm_key;
message.sem_key=sem_key;
// msgsnd to send message
if( msgsnd(msgid, &message, sizeof(message), 0) < 0 ){
printf("errro to send !!\n");
}
// display the message
printf("Data send is : %d %d \n", message.shm_key, message.sem_key);
file_entry *entries;
entries = (file_entry *) shmat(shmid, 0, 0);
if ( entries == NULL ) {
printf("error on attachment !!\n");
}
entries->p_pid = 40022;
entries->zero = 0;
entries->array_length = 8;
for( int i = 0; i < entries->array_length; i++) {
entries[i].array = rand()%2;
}
return 0;
}
worker.c file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <semaphore.h>
#include <unistd.h>
#define size_array 8
#define id 60
// structure for message queue
struct mesg_buffer {
long mesg_type;
int shm_key;
int sem_key;
} message;
typedef struct file
{
int zero;
int p_pid;
int array_length;
int array;
} file_entry;
int main()
{
key_t msg_key,shm_key,sem_key;
int ID, msgid, shmid, semid, i=1;
sem_t *sem;
// ftok to generate unique key
msg_key = ftok("progfile.txt", id);
if( msg_key < 0 ){
printf(" error on generate key msg queue !!\n ");
}
// msgget creates a message queue
// and returns identifier
msgid = msgget(msg_key, 0666 | IPC_CREAT);
if( msgid < 0) {
printf("error to identifier on msg queue !!\n");
}
// msgrcv to receive message
if ( msgrcv(msgid, &message, sizeof(message), 1, 0) < 0 ) {
printf("error to receive !! \n");
}
// display the message
printf("Data Received is : %d %d \n",message.shm_key,message.sem_key);
shmid = shmget( message.shm_key, sizeof(file_entry) *8, IPC_CREAT | 0666);
if( shmid < 0 ){
printf("error to identifier on shm !!\n");
}
file_entry *entries;
entries = (file_entry *) shmat(shmid, 0, 0);
if ( entries == NULL ) {
printf("error on attachment !!\n");
}
printf("%d\n", entries->p_pid);
printf("%d\n", entries->zero);
printf("%d\n\n", entries->array_length);
for( int i = 0; i < entries->array_length; i++) {
printf("%d\n",entries[i].array);
}
// create a new semaphore
semid = semget(message.sem_key, 1, IPC_CREAT);
if( semid < 0 ){
printf(" error to identifier on semaphore !!\n");
}
// to destroy the message queue
msgctl(msgid, IPC_RMID, NULL);
return 0;
}

Failing in message queue read from a file

I have add to some data into a text file and read out that in the 2. I have the first code to write some stuff into the text file, but in the 2. code i can't reach it. I get message error: No such file or directory. What do i miss in these? ( i have to use message queue to solve this problem)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define PERMS 0644
struct my_msgbuf {
long mtype;
char mtext[200];
};
int main(void) {
struct my_msgbuf buf;
int msqid;
int len;
key_t key;
system("touch msgq.txt");
if ((key = ftok("msgq.txt", 'B')) == -1) {
perror("ftok");
exit(1);
}
if ((msqid = msgget(key, PERMS | IPC_CREAT)) == -1) {
perror("msgget");
exit(1);
}
printf("message queue: ready to send messages.\n");
printf("Enter lines of text, ^D to quit:\n");
buf.mtype = 1;
while(fgets(buf.mtext, sizeof(buf.mtext), stdin) != NULL) {
len = strlen(buf.mtext);
/* remove newline at end, if it exists */
if (buf.mtext[len-1] == '\n') buf.mtext[len-1] = '\0';
if (msgsnd(msqid, &buf, len+1, 0) == -1) /* +1 for '\0' */
perror("msgsnd");
}
strcpy(buf.mtext, "end");
len = strlen(buf.mtext);
if (msgsnd(msqid, &buf, len+1, 0) == -1) /* +1 for '\0' */
perror("msgsnd");
if (msgctl(msqid, IPC_RMID, NULL) == -1) {
perror("msgctl");
exit(1);
}
printf("message queue: done sending messages.\n");
return 0;
}
Code to read from message que
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define PERMS 0644
struct my_msgbuf {
long mtype;
char mtext[200];
};
int main(void) {
struct my_msgbuf buf;
int msqid;
int toend;
key_t key;
if ((key = ftok("msgq.txt", 'B')) == -1) {
perror("ftok");
exit(1);
}
if ((msqid = msgget(key, PERMS)) == -1) {
perror("msgget");
exit(1);
}
printf("message queue: ready to receive messages.\n");
for(;;) {
if (msgrcv(msqid, &buf, sizeof(buf.mtext), 0, 0) == -1) {
perror("msgrcv");
exit(1);
}
printf("recvd: \"%s\"\n", buf.mtext);
toend = strcmp(buf.mtext,"end");
if (toend == 0)
break;
}
printf("message queue: done receiving messages.\n");
system("rm msgq.txt");
return 0;
}
Your programs work. You must first start the writer, so that the message queue is created, then start the reader, then in the writer Enter lines of text, which it will send to the reader, which will receive them.

How to use the same fifo for more consumers and producers?

I created the two producers and two consumers, producer1 sent to consumer1 two integer number and consumer1 print the sum of numbers, and I have another consumer and producer, producer2 sent to consumer2 the path for folder and consumer2 print the all files from directory (ls command from linux). An now I want to merge together this, for example I want as all producers and consumer to use the same message queue.
This is my code for producer1:
//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
struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
int main() {
int msqid;
// int msgflg = IPC_CREAT | 0666;
key_t key;
struct msgbuf sbuf;
size_t buflen;
key = ftok(".", 'g');
//Get the message queue ID for the given key
if ((msqid = msgget(key, IPC_CREAT | 0666 )) < 0) {
perror("Could not get message queue\n");
exit(1);
}
printf("MSQID value: %d\n", msqid);
//Message Type
sbuf.mtype = 1;
printf("Enter a path for a folder : ");
scanf("%[^\n]",sbuf.mtext);
// getchar();
buflen = strlen(sbuf.mtext) + 1 ;
if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0)
{
printf ("%d, %ld, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, (int)buflen);
perror("Could not send message!\n");
exit(1);
}
else
printf("Message Sent\n");
exit(0);
}
Consumer1:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 128
struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
int main() {
int msqid;
key_t key;
struct msgbuf rcvbuffer;
char pathForPrint[1024] = "";
// key = 1234;
key = ftok(".", 'g');
if ((msqid = msgget(key, 0666)) < 0) {
perror("Could not get message queue\n");
exit(1);
}
printf("MSQID value: %d\n", msqid);
// Receive an answer of message type 1.
if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0) {
perror("Could not receive message!\n");
exit(1);
}
//printf("%s\n", rcvbuffer.mtext);
strcat(pathForPrint, "ls ");
strcat(pathForPrint, rcvbuffer.mtext);
system(pathForPrint);
return 0;
}
Producer2:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define VECTOR_SIZE 2
struct msgbuf
{
long mtype;
int vector[VECTOR_SIZE];
};
int main() {
int msqid;
// int msgflg = IPC_CREAT | 0666;
key_t key;
struct msgbuf sbuf;
size_t buflen;
int i;
key = ftok(".", 'g');
//Get the message queue ID for the given key
if ((msqid = msgget(key, IPC_CREAT | 0666 )) < 0) {
perror("Could not get message queue\n");
exit(1);
}
printf("MSQID value: %d\n", msqid);
//Message Type
sbuf.mtype = 1;
while(1){
printf("Enter a message to add to message queue : ");
for(i = 0; i < 2; i++){
scanf("%d",&(sbuf.vector[i]));
buflen = sizeof(sbuf.vector[i]) + 1 ;
}
// getchar();
if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0)
{
printf ("%d, %ld, %d, %d, %d\n", msqid, sbuf.mtype, sbuf.vector[0], sbuf.vector[1], (int)buflen);
perror("Could not send message!\n");
exit(1);
}
else
printf("Message Sent\n");
sleep(3);
}
exit(0);
}
Consumer2:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 128
#define VECTOR_SIZE 2
struct msgbuf
{
long mtype;
int vector[VECTOR_SIZE];
};
int main() {
int msqid;
key_t key;
struct msgbuf rcvbuffer;
// key = 1234;
key = ftok(".", 'g');
while(1){
if ((msqid = msgget(key, 0666)) < 0) {
perror("Could not get message queue\n");
exit(1);
}
printf("MSQID value: %d\n", msqid);
// Receive an answer of message type 1.
if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0) {
perror("Could not receive message!\n");
exit(1);
}
printf("%d\n", (rcvbuffer.vector[0] + rcvbuffer.vector[1]));
}
return 0;
}
For use the same queue is necessary to have the same key, but how to make it ?
Thank you!
The key is a randomly selectable integer. If you generate the key with ftok(), you get the same key as long you refer to the same path and the same id, cf. the man page. If you want the same key, it is a bad idea to use a relative path (as you did), since your programs may have different directories. Use an absolute path.
However, please consider the remark of Aconcagua. If you use a common queue, you need different type ids (you use id=1 for both).

IPC Message queue not works with forked process

I'm trying to use IPC message queue with a forked process, passing a pointer to a dynamically allocated string, but it doesn't work.
This is a simple test that I made. It doesn't print the string received from the queue. But if I try to remove the fork() it works perfectly.
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MSGSZ 128
typedef struct msgbuf {
long mtype;
char *mtext;
} message_buf;
int
main ()
{
int msqid;
char *p;
key_t key = 129;
message_buf sbuf, rbuf;
p = (char *) malloc(sizeof(char) * MSGSZ);
if ((msqid = msgget(key, IPC_CREAT|0666)) < 0) {
perror("msgget");
exit(1);
}
if (fork() == 0) {
strcpy(p, "Did you get this?");
sbuf.mtype = 1;
sbuf.mtext = p;
if (msgsnd(msqid, &sbuf, MSGSZ, IPC_NOWAIT) < 0) {
perror("msgsnd");
exit(1);
}
}
else {
sleep(1);
if (msgrcv(msqid, &rbuf, MSGSZ, 0, 0) < 0) {
perror("msgrcv");
exit(1);
}
printf("Forked version: %s\n", rbuf.mtext);
msgctl(msqid, IPC_RMID, NULL);
}
}
The problem is that you are sending a pointer across process boundaries. Pointers are only valid within the same process and are meaningless when sent/used in another process. In fact, you are sending the pointer value followed by a whole bunch of garbage bytes as themsgbuf.mtext is in fact not MSGSZ bytes in size (so technically invoking Undefined Behaviour).
What you need to do is to declare the buffer inline in the message. That is, change the message_buf definition to be:
typedef struct msgbuf {
long mtype;
char mtext[MSGSZ];
} message_buf;
And then strcpy straight into mtext:
strcpy(sbuf.mtext, "Did you get this?");
For clarity, below is the full program with the changes described:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MSGSZ 128
typedef struct msgbuf {
long mtype;
char mtext[MSGSZ];
} message_buf;
int
main (void)
{
int msqid;
key_t key = 129;
message_buf sbuf, rbuf;
if ((msqid = msgget(key, IPC_CREAT|0666)) < 0) {
perror("msgget");
exit(1);
}
if (fork() == 0) {
strcpy(sbuf.mtext, "Did you get this?");
sbuf.mtype = 1;
if (msgsnd(msqid, &sbuf, MSGSZ, IPC_NOWAIT) < 0) {
perror("msgsnd");
exit(1);
}
}
else {
sleep(1);
if (msgrcv(msqid, &rbuf, MSGSZ, 0, 0) < 0) {
perror("msgrcv");
exit(1);
}
printf("Forked version: %s\n", rbuf.mtext);
msgctl(msqid, IPC_RMID, NULL);
}
}

How do I use mqueue in a c program on a Linux based system?

How do I use mqueue (message queue) in a c program on a Linux based system?
I'm looking for some good code examples that can show how this is done in a correct and proper way, maybe a howto.
The following is a simple example of a server that receives messages from clients until it receives an "exit" message telling it to stop.
The code for the server:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <mqueue.h>
#include "common.h"
int main(int argc, char **argv)
{
mqd_t mq;
struct mq_attr attr;
char buffer[MAX_SIZE + 1];
int must_stop = 0;
/* initialize the queue attributes */
attr.mq_flags = 0;
attr.mq_maxmsg = 10;
attr.mq_msgsize = MAX_SIZE;
attr.mq_curmsgs = 0;
/* create the message queue */
mq = mq_open(QUEUE_NAME, O_CREAT | O_RDONLY, 0644, &attr);
CHECK((mqd_t)-1 != mq);
do {
ssize_t bytes_read;
/* receive the message */
bytes_read = mq_receive(mq, buffer, MAX_SIZE, NULL);
CHECK(bytes_read >= 0);
buffer[bytes_read] = '\0';
if (! strncmp(buffer, MSG_STOP, strlen(MSG_STOP)))
{
must_stop = 1;
}
else
{
printf("Received: %s\n", buffer);
}
} while (!must_stop);
/* cleanup */
CHECK((mqd_t)-1 != mq_close(mq));
CHECK((mqd_t)-1 != mq_unlink(QUEUE_NAME));
return 0;
}
The code for the client:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <mqueue.h>
#include "common.h"
int main(int argc, char **argv)
{
mqd_t mq;
char buffer[MAX_SIZE];
/* open the mail queue */
mq = mq_open(QUEUE_NAME, O_WRONLY);
CHECK((mqd_t)-1 != mq);
printf("Send to server (enter \"exit\" to stop it):\n");
do {
printf("> ");
fflush(stdout);
memset(buffer, 0, MAX_SIZE);
fgets(buffer, MAX_SIZE, stdin);
/* send the message */
CHECK(0 <= mq_send(mq, buffer, MAX_SIZE, 0));
} while (strncmp(buffer, MSG_STOP, strlen(MSG_STOP)));
/* cleanup */
CHECK((mqd_t)-1 != mq_close(mq));
return 0;
}
The common header:
#ifndef COMMON_H_
#define COMMON_H_
#define QUEUE_NAME "/test_queue"
#define MAX_SIZE 1024
#define MSG_STOP "exit"
#define CHECK(x) \
do { \
if (!(x)) { \
fprintf(stderr, "%s:%d: ", __func__, __LINE__); \
perror(#x); \
exit(-1); \
} \
} while (0) \
#endif /* #ifndef COMMON_H_ */
Compiling:
gcc -o server server.c -lrt
gcc -o client client.c -lrt
#include <stdio.h>
#include <fcntl.h>
#include <mqueue.h>
int main(int argc, char *argv[])
{
mqd_t mq; // message queue
struct mq_attr ma; // message queue attributes
int status = 0;
int a = 5;
int b = 0;
printf("a = %d, b = %d\n", a, b);
// Specify message queue attributes.
ma.mq_flags = 0; // blocking read/write
ma.mq_maxmsg = 16; // maximum number of messages allowed in queue
ma.mq_msgsize = sizeof(int); // messages are contents of an int
ma.mq_curmsgs = 0; // number of messages currently in queue
// Create the message queue with some default settings.
mq = mq_open("/test_queue", O_RDWR | O_CREAT, 0700, &ma);
// -1 indicates an error.
if (mq == -1)
{
printf("Failed to create queue.\n");
status = 1;
}
if (status == 0)
{
status = mq_send(mq, (char *)(&a), sizeof(int), 1);
}
if (status == 0)
{
status = mq_receive(mq, (char *)(&b), sizeof(int), NULL);
}
if ((status == 0) && (mq_close(mq) == -1))
{
printf("Error closing message queue.\n");
status = 1;
}
if ((status == 0) && (mq_unlink("test_queue") == -1))
{
printf("Error deleting message queue.\n");
status = 1;
}
printf("a = %d, b = %d\n", a, b);
return status;
}
mq_send(mq, (char *)(&a), sizeof(int), 1) copies sizeof(int) bytes from buffer &a in this case, it does not carry the pointer of variable a, but carries the value of variable a from one process to another process. Implementation is right.
Code as below for your reference:
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);
}
struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
void 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);
}
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);
}
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, %ld, %s, %d \n", msqid, sbuf.mtype, sbuf.mtext, (int)buflen);
die("msgsnd");
}
else
printf("Message Sent\n");
exit(0);
}
Compile each of the source files, to get a writer-executable and reader-executable. As below::
gcc -o MQsender IPC_msgq_send.c
gcc -o MQreceiver IPC_msgq_rcv.c
Executing each of the binaries, you can send the message and read the message from the message queue. Also, try to see the message queue state, by running command (at different states of queue):
ipcs -q
For your linux system, you can know all the details of the IPC mechanisms and available queues etc, by using:
ipcs -a
Reference Blog

Resources