Solved

Message Q error

Posted on 2004-09-28
7
188 Views
Last Modified: 2010-04-21
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
Comment
Question by:johnduff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 11

Accepted Solution

by:
cjjclifford earned 200 total points
ID: 12171297
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
 
LVL 2

Author Comment

by:johnduff
ID: 12172245
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
 
LVL 2

Author Comment

by:johnduff
ID: 12172647
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
 
LVL 2

Author Comment

by:johnduff
ID: 12173831
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
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12419170
From the author's first comment it looks like my answer did help a bit....
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question