- For individual users
- Instant access to solutions
- Ask your tech questions
- Start your 30-day Free Trial
Main Topics
Browse All TopicsHi I'm working with some shared memory where different processes read and write data. I'm using a message queue to store messages of when data has changed between read and write operations.
/* struct that defines a message */
struct msgbuf{
long mtype; /* must be positive */
int childId; //ID of child sending message
int bufferChanged; //Buffer at which value was changed
int beforeValue; //Value before child sleeps
int afterValue; //Value after child sleeps
};
So while reading and writing and checking for changes the processes store messages the following way
struct msgbuf msg = {BUFFER_CHANGED, id, position, read, bufferArr[position]};
if(msgsnd(msqid, &msg, sizeof(msg), 0)== -1){
perror("msgsnd in read.write");
}
This is working fine. Oh by the way here's how I create the message queue.
#define BUFFER_CHANGED 1
qKey = ftok("./", 'A');
msqid = msgget(qKey, (IPC_CREAT | 0666));
/*Perform the following if the call is unsuccessful.*/
if(msqid == -1){
printf ("\nThe msgget call failed, error number = %d\n", errno);
}
/*Return the msqid upon successful completion.*/
else{
printf ("\nMessage queue successful. The msqid = %d\n", msqid);
//exit(0);
}
So my problem is that I'm not quite sure how to retrieve messages from the queue and display them on the screen. I've been reading up on msgrcv system call, but it's not very clear to me.
rc = msgrcv(msqid, &msg, sizeof(msg), BUFFER_CHANGED, IPC_NOWAIT);
rc is an int since msgrcv retuns an int. How do i point this int to the actual message? How to I read contents from the message so I can display them? I'm assuming this should be done in some sort of loop.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: jkrPosted on 2009-11-05 at 05:31:48ID: 25749189
>>So my problem is that I'm not quite sure how to retrieve messages from the
du/~beej/g uide/ipc/m q.html ("Message Queues") which illutrates that quite nicely and also comes with sample code. The idea (also with a loop) would be to
>>queue and display them on the screen.
Take a look at http://www.ecst.csuchico.e
Select allOpen in new window