0

I'm trying to receive a series of messages I sent with msgsnd in a series of fork()s. I can tell the messages sent ok, but receiving them has been a problem. Here's the problem part of the code, using three messages as an example:

for(j=1;j<4;j++)
{
    if(!(pid_A = fork()))
    {
        msgLen = msgrcv(msqid, &rec, sizeof(struct u_msgbuf) - sizeof(long), 0,0);
        if(msgLen == -1)
        {
            printf("%i - %s.\n", j, strerror(errno));
            exit(0);
        }
        else
        {
            printf("%i - %s.\n", j, rec.word);
            exit(0);
        }
    }
}

If I write code similar to this without a fork(), it works fine, so I know the fork() is messing with my message queue in a way I don't understand. Could someone tell me why this won't work in a fork(), and how I could code this in such a way that my fork() will receive messages properly?

Edit: What I am getting is this: first fork to process runs fine, second rarely does, third always fails. The errno is getting set to 22, which is Invalid Argument.


Update (declarations used):

struct u_msgbuf
{
  long mtype;
  char word[15];
};

struct u_msgbuf rec; 
alk
  • 69,737
  • 10
  • 105
  • 255
  • post you child process code. – Lidong Guo Aug 08 '13 at 00:28
  • There is no child code at the moment; I'm worried about getting this solved before I write it. – Michael Wyatt Aug 08 '13 at 00:36
  • The code executed when `fork()` returns 0 **IS** child process code. Do you have a `msgsnd()` in your code? What is the behavior you expect, what do you observe? – TheCodeArtist Aug 08 '13 at 03:15
  • Yes, I've got the msgsnd()s working properly, which I can verify by removing the fork()s from the code and just checking through the messages one by one. As for what I get vs. what I expect, I'll update OP in a moment to clarify. – Michael Wyatt Aug 08 '13 at 03:26
  • What system are you on? – jxh Aug 08 '13 at 03:51
  • Trying to get this running on a Linux box. – Michael Wyatt Aug 08 '13 at 03:56
  • What does your call to `msgsnd()` look like? – jxh Aug 08 '13 at 03:57
  • [Here](http://ideone.com/gow4GB) is a little example I implemented. – jxh Aug 08 '13 at 04:17
  • My sent messages are just a pretty basic msgsnd(msqid,&outgoing,msize,0);. The returns and my tests both indicate the sends are working properly, and the msgrcv()s working properly when not in a fork(). It's that fork() that's throwing things off. – Michael Wyatt Aug 08 '13 at 04:30
  • You don't seem very willing to share information that would be useful to help resolve your problem. I provided you with a working example. Hopefully you can use it to figure out where you are going wrong. – jxh Aug 08 '13 at 06:11
  • 1
    I'm not sure what you've asked to see that I haven't shown? I'd be happy to share anything that will help me nail this problem down. And I'm tinkering with your sample code now. I'll let you know how it goes, though to be honest, I'm more interested in what's going wrong and why than just finishing up the code. – Michael Wyatt Aug 08 '13 at 06:17
  • Please show us how `rec` is declared. – alk Aug 08 '13 at 06:29
  • struct u_msgbuf rec; A u_msgbuf defined as: struct u_msgbuf{long mtype;char word[15];}; – Michael Wyatt Aug 08 '13 at 06:32
  • @jxh, what is the purpose of your ack_type struct? I can see that the code requires it, and without it returns behavior similar to what my code is doing, but I don't understand what exactly it is doing. Perhaps that is my problem. – Michael Wyatt Aug 08 '13 at 06:38
  • @MichaelWyatt: The `ack_type` is used so that the parent can be sure all the children received the message before it deletes the message queue. – jxh Aug 08 '13 at 06:43
  • @MichaelWyatt: Basically, there is no problem in the code that you show, except that the message size seems odd. So, the problem is somewhere else, and we are all currently on a fishing expedition. If you provide a [complete example that demonstrates the problem](http://sscce.org/), we could probably find it in a matter of minutes. – jxh Aug 08 '13 at 06:54
  • @jxh That gives me a working theory to tackle tomorrow after some sleep. I think my queue may be getting deleted sooner than I intend it to. – Michael Wyatt Aug 08 '13 at 06:58
  • 1
    @MichaelWyatt: Early deletion of the queue was my initial thought, given the nature of the error, but I was not able to reproduce that behavior. Get some sleep, fresh eyes are better than tired eyes. – jxh Aug 08 '13 at 06:59
  • @jxh I didn't want to leave you hanging. Yes, it's a queue deletion problem. I guess the problem is in my wait function. That's the last place I thought to look! Thanks for your help. Tomorrow there shall be progress. – Michael Wyatt Aug 08 '13 at 07:03

0 Answers0