LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   msgsnd() function hangs (https://www.linuxquestions.org/questions/linux-newbie-8/msgsnd-function-hangs-4175579510/)

Vikrant.ere 05-11-2016 06:58 AM

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

rtmistler 05-11-2016 07:23 AM

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

Vikrant.ere 05-12-2016 12:29 AM

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


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