Thread: How to pass pointer to a structure in a message queue

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    How to pass pointer to a structure in a message queue

    Hi,

    I have a structure whose one member is a pointer to another structure .

    Code:
    struct first{
    int i;
    }fir;
    
    struct second{
    char name[10];
    struct first * point;
    }sec;
    I have assigned the following values to structure member

    Code:
     sec.point= (struct first*)malloc(sizeof(struct first)); 
      printf("Enter name\n");
      scanf("%s",sec.name);
      printf("Enter number\n");
      scanf("%d",&sec.point->i);
    I want to pass second structure to a another process using message queues.

    Code:
    msgid= msgget((key_t) 10,IPC_CREAT|0666);
    
    if(msgid<0)
      {
       perror("Msg Queue not created");
       exit(1);
      }
    
    if(msgsnd(msgid,&sec,sizeof(struct second),0)<0)      
      {
       perror("Message not sent");
       exit(1);
      }
    I then receive the message in another process
    Code:
    struct first{
    int i;
    }fir;
    
    struct second{
    char name[10];
    struct first * point;
    }var;
    
    main()
    {
    
     int msgid;
      var.point= (struct first*)malloc(sizeof(struct first)); 
     
     
      msgid=msgget((key_t)10 ,IPC_CREAT|0666);	/*Creating Message Queue*/
    
     if(msgid<0)
      {
       perror("Msg Queue not created");
       exit(1);
      }
    
    
     if(msgrcv(msgid,&var,sizeof(struct second),0,0|MSG_NOERROR)<0)
     {
      perror("Message not recieved");
      exit(1);
     }
     printf("name was\n");
     printf("%s",var.name);
     printf("nuber was\n");
     printf("%d",var.point->i);
     
     
     }
    It displays the character value but shows "segmentation fault" for the interger value...Any idea if a pointer of one process can work in another...I could have used a normal variable for the first structure but i have some specifications to follow which has defined structure like these. However if i copy individual members into a character array (using memcpy) and pass the array in queue it do works.... If pointer is passed through an array why not struture..

  2. #2
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Processes cannot share pointers because they are in different address spaces. The closest you could come to doing this is to have shared memory between two processes, and pass offsets to the start of shared memory via the queues.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    Hi,

    many thanks for your reply...so my initial doubts were true- i cant pass pointers between process... just to understand your comment better, why should queues be used with shared memeory...Shouldnt i just write data from process into the memory and have the other process read from it?? I thinks queus in this case would be redudant...

    Well i am still bit naive in C and IPCs so many explaination would be highly appriciated

  4. #4
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Quote Originally Posted by smitsaboo View Post
    Hi,

    why should queues be used with shared memeory...Shouldnt i just write data from process into the memory and have the other process read from it??
    Yes, there is no special reason to use both, unless your solution needs it. For example, if one process wants to tell another that a certain area in shared memory has changed, queues could be one way for the second process to be notified.

    Also, remember that if you're using shared memory, you will probably not want one process to read a region (a struct for example) while another process is writing it because you'd get a mixture of new and old data. Ususally, semaphores are used for mutual exclusion.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    Thanks for your reply

    Yea!!!! using queues for notification purpose do make sense. Another way could have been to have the reader process in constant reading mode, of course it would need semaphores for that

    But yea using queues would be lot more safer n clearer....


    Well thanks agian for your replies....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Passing function pointer using message queues
    By LxRz in forum Linux Programming
    Replies: 4
    Last Post: 05-05-2008, 10:41 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM

Tags for this Thread