LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-11-2016, 06:58 AM   #1
Vikrant.ere
LQ Newbie
 
Registered: May 2016
Posts: 2

Rep: Reputation: Disabled
msgsnd() function hangs


Hi,

I found this topic has been discussed previously by many. But my issue is quite unique. I am trying with a simple sender receiver program using message queue. Below is the sample code -

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/msg.h>

#define MAX_TEXT 512

struct my_msg_st
{
long int my_msg_type;
char some_text[BUFSIZ];
};


int main()
{
int running = 1;
struct my_msg_st some_data;
int msgid;
char buffer[BUFSIZ];

msgid = msgget((key_t)1234, 0666|IPC_CREAT);

if(msgid == -1)
{
fprintf(stderr,"msgget failed with error: %d\n",errno);
exit(EXIT_FAILURE);
}

while(running)
{
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
some_data.my_msg_type = 1;
strcpy(some_data.some_text,buffer);

if(msgsnd(msgid, (void *)&some_data, sizeof(some_data.some_text), 0) == -1)
{
fprintf(stderr, "msgsnd failed\n");
exit(EXIT_FAILURE);
}

if(strncmp(buffer,"end",3) == 0)
{
running = 0;
}
}

exit(EXIT_SUCCESS);
}

I found that the code get hang exactly at msgsnd() function. If I am using IPC_NOWAIT flag it will throw error. If I am restarting my system this program work fine for 1st time. And then for 2nd time and onwards the same issue persist.

Please suggest any solution.

Thanks
 
Old 05-11-2016, 07:23 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Welcome to LQ.

Please use [code] tags to enclose your code. I have a link in my signature which may help describe.

If you're saying that this works sometimes and not others, then within your fprintf() statement of the failure, also output the errno value which msgsnd() has returned.

There are a LOT of possibilities here, see the man page for msgsnd(2):
Code:
RETURN VALUE
       On failure both functions return -1 with errno indicating the error, otherwise  msgsnd()  returns  0  and
       msgrcv() returns the number of bytes actually copied into the mtext array.

ERRORS
       When msgsnd() fails, errno will be set to one among the following values:

       EACCES The  calling  process  does  not have write permission on the message queue, and does not have the
              CAP_IPC_OWNER capability.

       EAGAIN The message can't be sent due to the msg_qbytes limit for the queue and IPC_NOWAIT  was  specified
              in msgflg.

       EFAULT The address pointed to by msgp isn't accessible.

       EIDRM  The message queue was removed.

       EINTR  Sleeping on a full message queue condition, the process caught a signal.

       EINVAL Invalid  msqid  value,  or nonpositive mtype value, or invalid msgsz value (less than 0 or greater
              than the system value MSGMAX).

       ENOMEM The system does not have enough memory to make a copy of the message pointed to by msgp.

       When msgrcv() fails, errno will be set to one among the following values:

       E2BIG  The message text length is greater than msgsz and MSG_NOERROR isn't specified in msgflg.

       EACCES The calling process does not have read permission on the message queue,  and  does  not  have  the
              CAP_IPC_OWNER capability.

       EAGAIN No message was available in the queue and IPC_NOWAIT was specified in msgflg.

       EFAULT The address pointed to by msgp isn't accessible.

       EIDRM  While the process was sleeping to receive a message, the message queue was removed.

       EINTR  While the process was sleeping to receive a message, the process caught a signal; see signal(7).

       EINVAL msgqid was invalid, or msgsz was less than 0.

       ENOMSG IPC_NOWAIT  was  specified  in  msgflg and no message of the requested type existed on the message
              queue.
My money is on EACCESS or EFAULT
 
Old 05-12-2016, 12:29 AM   #3
Vikrant.ere
LQ Newbie
 
Registered: May 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
Hi,

Thanks for your reply. I had checked the errno when it hangs. The errno belongs to 11 which is EAGAIN i.e. buffer is full. Why the same is not happening for the 1st time when I run the program ??


Thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Hanging while using msgsnd and msgrcv sanddune Programming 9 01-19-2016 05:37 AM
msgget, msgsnd and msgrcv stupendus Programming 3 08-07-2007 04:18 AM
IPC V message queue --msgsnd() call jasdeep.js Linux - Kernel 1 01-02-2007 06:59 PM
gdb hangs on function call astropirhana Programming 2 09-29-2006 08:01 AM
msgsnd (sys_msgsnd) symbol unresolved yhus Programming 0 09-06-2005 01:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration