[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

Message Q error

working on message Q with a parent and child process and the Q is not being created, getting an Error # 38 on it.  This is the program:

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


struct myMessage
      {
            long int message_type;
            char some_text[3];
      };
      
int main()
{
      int messageQ;
      pid_t processNum= fork();            
      
      // create messageQ
      
      if(processNum==0)
      {   printf("child: create and send messQ, process=0 ");
            messageQ=getmsg((key_t)1234, 0666|IPC_CREAT);
            fprintf(stderr,"messageerror: %d\n",errno);
            printf("creatQ = %d ",messageQ);
            //create message
      
            struct myMessage message;
            message.message_type=0;
            message.some_text[0]='1';
            message.some_text[1]='2';
            message.some_text[2]='3';
      
            //send message
            msgsnd(messageQ,(void *)&message,3,0);
            printf("MSG sent ");
            printf("sentQ = %d ",messageQ);
      
      }
      
      if(processNum!=0)
      {      printf("Waiting ");
            printf("%d ",processNum);
            wait(10);
      }

      
      
      // create struct pointer to return results into
      int success = 0;
      if(processNum!=0)
      {      struct myMessage recievedMsg;
            success=msgrcv(messageQ, (void *)&recievedMsg,3, 0 ,0);
            printf("recQ = %d ",messageQ);
              printf("success: %d",success);

      }
      

      
      return 0;
      
}


0
johnduff
Asked:
johnduff
  • 3
  • 2
1 Solution
 
cjjcliffordCommented:
I would guess that the messageQ would have to be created before the fork(), or at least the line:

messageQ=getmsg((key_t)1234, 0666|IPC_CREAT);

should be also in the parent block, before the msgrcv() call...

Also, the child and parent code blocks shouldn't flow into each other... currently, the child code runs straight into the parent logic - put in an else { } for the parent logic, rather than continually checking if this is the parent, just do it once...

what's happening now is the variable messageQ is only getting assigned to in the child, so the parent doesn't have a Q to connect to...
0
 
johnduffAuthor Commented:
when the getmsg() is called for the first time the messeageQ that is returned is -1, which I'm taking it to mean that the messageQ is not being created at all.  Tried moving that line before the fork and the only thing it fixes is that when the parent calls msgrcv() it's getting -1 returned as well, before it was getting a random int value.
0
 
johnduffAuthor Commented:
Tried to do just the msgget to create the message Q and it dosn't do anyhting, returns -1, please help if you can
0
 
johnduffAuthor Commented:
okay, i created a new file and just did the msgget() and for some reason now when i create a messageQ it returns 0 instead of -1, which means it was created.  so this is solved for now, i will post to this again if i come across a real problem and not just the computer being stupid.

John
0
 
cjjcliffordCommented:
From the author's first comment it looks like my answer did help a bit....
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now