![]() |
| | #1 |
| Registered User Join Date: Aug 2009
Posts: 3
| How to pass pointer to a structure in a message queue 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: 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);
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: 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);
}
|
| smitsaboo is offline | |
| | #2 |
| pwning noobs 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 | |
| | #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 | |
| | #4 | |
| pwning noobs Join Date: Jun 2009 Location: The Great White North
Posts: 125
| Quote:
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 | |
| | #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 | |
![]() |
| Tags |
| message queue, pointers, strutures |
| Thread Tools | |
| Display Modes | |
|
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 |