473,394 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Problem with message queues / msgrcv

Hi netties,

posted this in a different group already, which seems to be
inappropriate.

I create a message queue with msgget(), sent a 5-byte message to it
with msgsnd() and try to receive the queue's content with msgrcv().
Everything seems to work fine, apart from the fact that I get back
only the last byte back from the original message (after sending the
message ipcs -qA shows clearly that there are 5 Bytes waiting in the
queue).
I am running Solaris 10 8/07 with gcc 3.4.3 as the compiler.
As being not such an experienced C-programmer it could be possible
hat
I am something doing wrong when fiddling around with structures,
pointers etc., but I cannot find it.
The source code (see below) was compiled in the following way:
gcc -fpic msgqueue_tst.c -o mgsqueue_tst
The output is:
Hi there, here is msgget!
msqid: 16777338
msgget: msgget succeeded (msqid: 16777338)
Hi there, here is msgsnd!
msgsnd: message was successfully placed into message queue 16777338.
Hi there, here is msgrcv!
msgflg: 3600
msgtyp: 0
msgrcv: message was successfully read from message queue 16777338.
We finally made it past the invocation ( msgactsz = 1, mtext: o )!
The source:
#include <stdio.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int main() {
printf("Hi there, here is msgget!\n") ;
/* Parameters */
key_t key = 0 ;
int msgflg = 03600 ;
/* Return value */
int msqid ;
/* Create the message queue */
msqid = msgget( key, msgflg ) ;
printf( "msqid: %i\n", msqid) ;
if ( msqid == -1 )
{
printf("msgget: initializing message queue failed!\n") ;
perror("errno:") ;
return -1 ;
} else
{
printf("msgget: msgget succeeded (msqid: %i)\n", msqid) ;
}
printf("Hi there, here is msgsnd!\n") ;
/* Parameters */
size_t msgsz = 5 ;
struct message {
long mtype ;
const char *mpointer ;
} ;
struct message msg ;
msg.mtype = 0 ;
msg.mpointer = "hello" ;
if ( msgsnd( msqid, msg.mpointer, msgsz, msgflg ) == 0 ) {
printf("msgsnd: message was successfully placed into message
queue %i.\n", msqid) ;
} else
{
printf("msgsnd: sending message to message queue %i failed.
\n", msqid) ;
return -1 ;
}
printf("Hi there, here is msgrcv!\n") ;
/* Parameters */
int msgtyp = 0 ;
struct message_rcv {
long mtype ;
char messtxt[msgsz] ;
} *msg_rcv ;
msg_rcv = malloc( msgsz*sizeof(char) + sizeof(long) ) ;
/* Receive the message */
printf("msgflg: %5o\n", msgflg) ;
printf("msgtyp: %d\n", msgtyp) ;
long msgactsz ; // Actual number of bytes placed in the structure
if ( msgactsz = msgrcv( msqid, msg_rcv, msgsz, msgtyp,
IPC_NOWAIT ) != -1 ) {
printf("msgrcv: message was successfully read from message
queue
%i.\n", msqid) ;
} else
{
printf("msgrcv: receiving message from message queue %i
failed.
\n", msqid) ;
fprintf( stderr, "errno: %i\n", errno ) ;
return -1 ;
}
printf("We finally made it past the invocation ( msgactsz = %d,
mtext: %s )!\n", msgactsz, msg_rcv->messtxt ) ;
return 0 ;

}
Hope somebody can help!

Cheers
Bernd
Jun 27 '08 #1
2 6449
bernd wrote:
Hi netties,

posted this in a different group already, which seems to be
inappropriate.
unfortunatly you're wrong here too as messages queue are not part of the C
standard, try comp.unix.programmer.

Bye, Jojo
Jun 27 '08 #2
On 27 May 2008 at 10:59, bernd wrote:
I create a message queue with msgget(), sent a 5-byte message to it
with msgsnd() and try to receive the queue's content with msgrcv().
Everything seems to work fine, apart from the fact that I get back
only the last byte back from the original message (after sending the
message ipcs -qA shows clearly that there are 5 Bytes waiting in the
queue).
First up, there are several minor problems with your code (a missing
header, wrong format specifier for printf, an assignment needing
parentheses to do what you want it to do...), all of which you can find
by turning up the warning level on gcc. Another fairly serious problem
is that you declare an array of length msgsz, where msgsz is a variable.
You can't do that, even with C99's VLAs, since your array is a member of
a structure.

For now, let's replace
size_t msgsz = 5 ;
with
#define msgsz 6
which will also give us space for the null-terminator at the end of the
"hello" string.

As to your actual problem, it's in the sending stage.
struct message {
long mtype ;
const char *mpointer ;
} ;
struct message msg ;

msg.mtype = 0 ;
msg.mpointer = "hello" ;

if ( msgsnd( msqid, msg.mpointer, msgsz, msgflg ) == 0 ) {
[snip]

The first thing to note is that msg.mtype must be 0.

The second thing is that the second argument to msgsnd needs to be a
pointer to a struct message: use &msg instead of msg.mpointer.

But the really slippery point is that this is one of those occasions
when arrays and pointers aren't interchangeable. Let's suppose for the
sake of argument that msg.mpointer is at address 0xDEADBEEF, and think
about what msgsnd is going to do when you pass it &msg. It will try to
read msgsz (i.e. 6) characters from memory locations 0xDEADBEEF,
0xDEADBEF0, ..., 0xDEADBEF4. That's unfortunate, because 0xDEADBEEF
through 0xDEADBEF2 (on a 32-bit system) contain a pointer to the string
literal "hello" in the data segment of your program, while the last two
bytes are garbage.

So you should change the sending code to look like this:

struct message {
long mtype ;
char mtext[msgsz];
} ;
struct message msg;

msg.mtype = 1;
strcpy(msg.mtext, "hello");

if ( msgsnd( msqid, &msg, msgsz, msgflg ) == 0 ) {
...

Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Aki Niimura | last post by:
Hello everyone. I'm trying to control a program from a Python program using IPC. Although using a socket is a common choice for such applications, I would like to use SysV message queues...
4
by: Tingo | last post by:
Hi all, Is it possible to create a message queue with a specific ID? I want to do this because I'm trying to write a piece of software which restores communicating processes (which communicate...
3
by: Gerhard Swart | last post by:
Hi all. I'm writing a queue browser that reads queues from a specified machine and then display the data that's on the queue. I am using the MessageQueue Class in .Net(C#). I get the problem...
3
by: Brian Keating | last post by:
hello, doesn anyone know how to deploy message queues? I've got a program which uses message queues and i wish to deploy it, one click deployment in vs2005 doesn't allow me to specify message...
0
by: sam | last post by:
Hi, I have windows services appliaction. Application reads message from MS message queuing. I have short code example below of my application. I have different process points these point have...
8
by: Vmz | last post by:
HI, I got two queues, I need to write void function that would check both queues and remove from first queue all nodes that matches second queue. Each node has two pointers that shows to previous...
12
by: whitehatmiracle | last post by:
Hello group I got the code of this classic elevator simulation. Im having some problem foloowing it. And i feel it can be simplified... can u people help me? here's the code: class button {...
2
by: Ramon | last post by:
Hi, I'm currently writing a program that tries to read (from a message queue) either a message of type 123 or a one of type 111. I've tried the following code but it seems that it is not...
3
by: Ahmad Jalil Qarshi | last post by:
Hi, I have developed an application in C on IBM AIX 5.2. Its using multiple Message Queues to share data between different processes. I am using following functions to send/receive messages...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.