Message Queues
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Message Queues




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Message Queues  
Xarky


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-21-04 05: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.





[ Post a follow-up to this message ]



    Re: Message Queues  
Michael Kerrisk


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-22-04 08: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





[ Post a follow-up to this message ]



    Re: Message Queues  
Xarky


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-22-04 02:37 PM

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





[ Post a follow-up to this message ]



    Re: Message Queues  
Michael Kerrisk


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-22-04 04:36 PM

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






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:25 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register