|
Home > Archive > Unix Programming > April 2004 > Message Queues
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Xarky 2004-04-21, 12:34 pm |
| Hi,
I am learning Message Queues, and wrote a small program. I am
executing a sender program where it sends data to the message queue,
and a receiver program which is supposed to receive the data sent. I
have a problem when receiving messages.
The command being used is as follows:
msgrcv (qid, &buf, length, 1, 0)
The problem is that it is returning me -1, where errno is giving me
the error message "Invalid Argument". This error is being given at
different moments, ie sometimes on the first message retrieved or even
when retrieving the fourth message.
I am not sure, but my problem could be in the 4th argument.
Please can someone help me in my problem.
I hope I was clear in my problem.
Thanks in Advance.
| |
| Michael Kerrisk 2004-04-22, 3:36 am |
| On 21 Apr 2004 09:34:51 -0700, bernardpace@yahoo.com (Xarky) wrote:
>Hi,
>
> I am learning Message Queues, and wrote a small program. I am
>executing a sender program where it sends data to the message queue,
>and a receiver program which is supposed to receive the data sent. I
>have a problem when receiving messages.
>
> The command being used is as follows:
> msgrcv (qid, &buf, length, 1, 0)
>
> The problem is that it is returning me -1, where errno is giving me
>the error message "Invalid Argument". This error is being given at
>different moments, ie sometimes on the first message retrieved or even
>when retrieving the fourth message.
>
>I am not sure, but my problem could be in the 4th argument.
>
>
>Please can someone help me in my problem.
>I hope I was clear in my problem.
There is not enough information here to solve your problem (which is
unlikley to be related to the 4th arg, since you specifiy it as zero).
One possible cause of EINVAL (:invalid argument") is that the message
queue ID is invalid. If that isn't the problem, then please post the
*simplest* version of a program that demonstrates the error.
Cheers,
Michael
| |
|
| Michael Kerrisk <michael.kerrisk.at.gmx.net@nospam.com> wrote in message
news:<69qe80l0pqcog58aaiceusmpmbh9u38kh8@4ax.com>...
> There is not enough information here to solve your problem (which is
> unlikley to be related to the 4th arg, since you specifiy it as zero).
> One possible cause of EINVAL (:invalid argument") is that the message
> queue ID is invalid. If that isn't the problem, then please post the
> *simplest* version of a program that demonstrates the error.
>
> Cheers,
>
> Michael
Hi,
I am going to include the source code, which is three files.
1) RecSend.h
2) Sender.c
3) Reciever.c
I hope this way it would help more to solve my problem.
Thanks in Advance for sparing your time.
/******************************/
RecSend.h
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/ipc.h>
#include <linux/msg.h>
#include <sys/types.h>
typedef struct mymsgbuf
{
long mytype;
int num;
} mess_t; // end typedef mymsgbuf
/********************************/
* Reciever Program */
#include "RecSend.h"
void error_msg()
{
printf ("Error encountered: %s.\n", strerror(errno));
exit (EXIT_FAILURE);
} // end method error_msg
void remove_IPC (int *data)
{
if (msgctl (*data, IPC_RMID, NULL) == -1)
error_msg();
} // end method remove_IPC
int main()
{
ssize_t length;
int qid, cnt;
key_t msgkey;
mess_t buf;
system ("clear");
printf ("*** Reciever Program ***\n");
length = sizeof (mess_t) - sizeof (long);
msgkey = ftok (".", 'm');
if (msgkey == -1)
error_msg();
qid = msgget (msgkey, IPC_CREAT | 0666);
if (qid == -1)
{
remove_IPC (&qid);
error_msg();
} // end if
printf ("Reciever QID = %d.\n", qid);
for (cnt=0; cnt<5; cnt++)
{
sleep (rand() % 4);
if (msgrcv (qid, &buf, length, 1, 0) == -1)
{
remove_IPC (&qid);
error_msg();
} // end if
printf ("Reciever = %d, Message No. = %d.\n", cnt+1, buf.num);
} // end for loop
remove_IPC (&qid);
return EXIT_SUCCESS;
} // end main
/ ****************************************
*****/
/* Sender Program */
#include "RecSend.h"
void error_msg()
{
printf ("Error encountered: %s.\n", strerror(errno));
exit (EXIT_FAILURE);
} // end method error_msg
void remove_IPC (int *data)
{
if (msgctl (*data, IPC_RMID, 0) == -1)
error_msg();
} // end remove_IPC
int main()
{
ssize_t length;
int qid, cnt;
key_t msgkey;
mess_t buf;
system ("clear");
printf ("*** Sender Program ***\n");
length = sizeof (mess_t) - sizeof(long);
msgkey = ftok (".", 'm');
if (msgkey == -1)
error_msg();
qid = msgget (msgkey, IPC_CREAT | 0666);
if (qid == -1)
{
remove_IPC (&qid);
error_msg();
} // end if
printf ("Sender QID = %d.\n", qid);
srand (time(0));
for (cnt=0; cnt<5; cnt++)
{
sleep (rand() % 4);
buf.mytype = 1;
buf.num = rand() % 100;
if (msgsnd (qid, &buf, length, 0) == -1)
{
remove_IPC (&qid);
error_msg();
} // end if
printf ("Sender = %d, Message No. = %d.\n", cnt+1, buf.num);
} // end for loop
remove_IPC (&qid);
return EXIT_SUCCESS;
} // end main
| |
| Michael Kerrisk 2004-04-22, 11:36 am |
| On 22 Apr 2004 06:26:18 -0700, bernardpace@yahoo.com (Xarky) wrote:
>Michael Kerrisk <michael.kerrisk.at.gmx.net@nospam.com> wrote in message
>news:<69qe80l0pqcog58aaiceusmpmbh9u38kh8@4ax.com>...
>
>
>Hi,
> I am going to include the source code, which is three files.
> 1) RecSend.h
> 2) Sender.c
> 3) Reciever.c
>
> I hope this way it would help more to solve my problem.
The error is occuring because your sender deletes the message queue
before all the messages are read by the reader.
By the way, you fail to include very many required header files. this
sort of thing can create other potentail errors -- you should fix
these (turning on compiler warnings will help you here -- e.g., with
gcc it's "gcc -Wall".)
Cheers,
Michael
>
>/******************************/
>RecSend.h
>
>#include <errno.h>
>#include <stdio.h>
>#include <stdlib.h>
>#include <linux/ipc.h>
>#include <linux/msg.h>
>#include <sys/types.h>
>
>typedef struct mymsgbuf
>{
> long mytype;
> int num;
>} mess_t; // end typedef mymsgbuf
>
>
>/********************************/
>* Reciever Program */
>#include "RecSend.h"
>
>void error_msg()
>{
> printf ("Error encountered: %s.\n", strerror(errno));
> exit (EXIT_FAILURE);
>} // end method error_msg
>
>void remove_IPC (int *data)
>{
> if (msgctl (*data, IPC_RMID, NULL) == -1)
> error_msg();
>} // end method remove_IPC
>
>int main()
>{
> ssize_t length;
> int qid, cnt;
> key_t msgkey;
> mess_t buf;
>
> system ("clear");
> printf ("*** Reciever Program ***\n");
> length = sizeof (mess_t) - sizeof (long);
>
> msgkey = ftok (".", 'm');
> if (msgkey == -1)
> error_msg();
>
> qid = msgget (msgkey, IPC_CREAT | 0666);
> if (qid == -1)
> {
> remove_IPC (&qid);
> error_msg();
> } // end if
>
> printf ("Reciever QID = %d.\n", qid);
>
> for (cnt=0; cnt<5; cnt++)
> {
> sleep (rand() % 4);
>
> if (msgrcv (qid, &buf, length, 1, 0) == -1)
> {
> remove_IPC (&qid);
> error_msg();
> } // end if
> printf ("Reciever = %d, Message No. = %d.\n", cnt+1, buf.num);
> } // end for loop
>
> remove_IPC (&qid);
> return EXIT_SUCCESS;
>} // end main
>
>
>/ ****************************************
*****/
>/* Sender Program */
>
>#include "RecSend.h"
>
>void error_msg()
>{
> printf ("Error encountered: %s.\n", strerror(errno));
> exit (EXIT_FAILURE);
>} // end method error_msg
>
>void remove_IPC (int *data)
>{
> if (msgctl (*data, IPC_RMID, 0) == -1)
> error_msg();
>} // end remove_IPC
>
>int main()
>{
> ssize_t length;
> int qid, cnt;
> key_t msgkey;
> mess_t buf;
>
> system ("clear");
> printf ("*** Sender Program ***\n");
> length = sizeof (mess_t) - sizeof(long);
>
> msgkey = ftok (".", 'm');
> if (msgkey == -1)
> error_msg();
>
> qid = msgget (msgkey, IPC_CREAT | 0666);
> if (qid == -1)
> {
> remove_IPC (&qid);
> error_msg();
> } // end if
>
> printf ("Sender QID = %d.\n", qid);
> srand (time(0));
>
> for (cnt=0; cnt<5; cnt++)
> {
> sleep (rand() % 4);
> buf.mytype = 1;
> buf.num = rand() % 100;
>
> if (msgsnd (qid, &buf, length, 0) == -1)
> {
> remove_IPC (&qid);
> error_msg();
> } // end if
> printf ("Sender = %d, Message No. = %d.\n", cnt+1, buf.num);
> } // end for loop
>
> remove_IPC (&qid);
> return EXIT_SUCCESS;
>} // end main
|
|
|
|
|