Link to home
Start Free TrialLog in
Avatar of johnduff
johnduff

asked on

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;
      
}


ASKER CERTIFIED SOLUTION
Avatar of cjjclifford
cjjclifford

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of johnduff
johnduff

ASKER

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.
Tried to do just the msgget to create the message Q and it dosn't do anyhting, returns -1, please help if you can
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
From the author's first comment it looks like my answer did help a bit....