Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

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;[/code]

I have assigned following values to the structure variable


[code]sec.point= (struct first*)malloc(sizeof(struct first));
printf("Enter name
");
scanf("%s",sec.name);
printf("Enter number
");
scanf("%d",&sec.point->i);
[/code]


Iwant to pass second structure to another process using message queue

[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);
}[/code]

I then receive the strutcure 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
");
printf("%s",var.name);
printf("nuber was
");
printf("%d",var.point->i);


}[/code]

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..

Comments

  • LundinLundin Member Posts: 3,711
    I'm no unix guru but the documentation over the struct looks like it expects a long as first member:

    http://opengroup.org/onlinepubs/007908775/xsh/msgrcv.html

    Also, that function likely uses memcpy() or similar, and is that case you can't use pointers as struct members.
  • smitsaboosmitsaboo Member Posts: 3
    Hi,

    many thanks for your valuable comments. I dont think long int is a paramount requirement. Yea you do need it if you have priority scheme being used. The above code should work fine if the variable for the first function was a normal variable and not a pointer....


    any ways Cheers for your help
  • ccarverccarver Member Posts: 1
    Bad News: You can't directly pass pointers between processes. Well you can with unexpected results. The issue is the pointer is passed pointing to a virtual address that is specific to that process. The other process will get the pointer, but it will think that pointer is to an address space inside of it.

    Good News: You can pass pointers between processes using shared memory. All it takes is a small bit of common sense wizardry. See I ran into the same problem where I had multiple threads tied into a shared memory segment and that was fine a dandy, because I used a mutex in shared memory and threads share the same process address space. But then once I added a separate process to use the same shared memory everything fell apart. Problem is you can't share mutexes across processes and many of the shared memory resources were pointers. So even if the other process gained access to shared memory, the pointer were still invalid.

    What you will want to do is allocate a section of shared memory for the structure second. Now the pointer in the second structure will now be a separate pointer to another shared memory structure containing the structure first. Use semaphores to condition signal between processes and BAM you are done.

    Unfortunately I can not give you the code. ) C :" My work owns the code, but I can point you in the right direction.
  • smitsaboosmitsaboo Member Posts: 3
    hi!!!


    many thanks for your reply...


    It has been a while since i posted my query and since then i have figured out how pointers can be used in shared memory...


    But again thanks a lot for your concern and a nice easy description...


Sign In or Register to comment.