C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-15-2009, 03:15 AM   #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..
smitsaboo is offline   Reply With Quote
Old 08-15-2009, 09:41 AM   #2
pwning noobs
 
Zlatko's Avatar
 
Join Date: Jun 2009
Location: The Great White North
Posts: 125
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.
__________________
Sun Certified Java Programmer / Developer
IEEE CSDP
Zlatko is offline   Reply With Quote
Old 08-15-2009, 11:45 AM   #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
smitsaboo is offline   Reply With Quote
Old 08-15-2009, 01:09 PM   #4
pwning noobs
 
Zlatko's Avatar
 
Join Date: Jun 2009
Location: The Great White North
Posts: 125
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.
__________________
Sun Certified Java Programmer / Developer
IEEE CSDP
Zlatko is offline   Reply With Quote
Old 08-15-2009, 11:33 PM   #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....
smitsaboo is offline   Reply With Quote
Reply

Tags
message queue, pointers, strutures

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:31 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22