Solved

Message Q error

Posted on 2004-09-28
7
177 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
  • 3
  • 2
7 Comments
 
LVL 11

Accepted Solution

by:
cjjclifford earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
From the author's first comment it looks like my answer did help a bit....
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now