/MY PROGRAM ARE COMPILED IN FEDORA15 ..WITH 4.6 gcc compiler...../
/* writer */ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/ipc.h> #include<sys/msg.h> #include<sys/types.h> struct anu { unsigned char *payload; long int type; }; struct anu a; //int type; //unsigned char *payload; int main() { int kid,ret; kid=msgget(412,666|IPC_CREAT); a.type=13; a.payload="ANU IS ALWAYS LEFT BUT RIGHT"; printf("a.payload=%s",a.payload); printf("kid=%d\n",kid); if(!msgsnd(kid,a.payload,28,0))//send the message printf("msg is snd succesfully\n"); if(!msgsnd(kid,&a.type,4,0))//send the message printf("msg is snd succesfully\n"); return 0; }
/* Reader */ /*if u had received the type on other side ..then queue has no type now...message queue has only message with no type....then u try to identify message with same type that u have received...that type which is not in message queue...could not identify the message while message receive & message could not be recieved .. //.yes..if u had written type=0....& no use of IPC_NOWAIT flag...then this message queue will act like a normal fifo........so it will receive the message using fifo like message queue..but leaves the type everytime... //also in fifo everytime ..when new process starts data from that fifo erased but in message queue data is not erased untill read from that message queue or erase message queue using command ipcrm -Q 412 //412 is +ve inter value is given to msgget() to identify fifo */ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/ipc.h> #include<sys/types.h> #include<sys/msg.h> struct set { long int type; char *pay; }; //unsigned char *pay; struct set s; int main() { int i,kid,ret; //printf("s.type=%d\n",s.type); s.pay=malloc(30);//if u r using pointer malloc it..other o/p will not be printed kid=msgget(412,666|IPC_CREAT); printf("kid=%d\n",kid); ret=msgrcv(kid,&s.type,4,0,IPC_NOWAIT);//u may get message directly into s.pay or get into &s with type (block no.) also { printf("ret=%d\n",ret); //goto OUT; } printf("s.type=%d\n",(s.type)); ret=msgrcv(kid,s.pay,28,13,IPC_NOWAIT);//u may get message directly into s.pay or get into &s with type (block no.) also // //printf("message nt rcv in 28\n"); //goto OUT; } //ret=msgrcv(kid,s.pay,28,13,0);//u may get message directly into s.pay or get into &s with type (block no.) also
//printf("ret=%d\n",ret); //printf("msg is rcv succesfully\n"); printf("s.payload=%s\n",s.pay);//print //for(i=0;i<100;i++) //what i have done is..........in message send i have send ...two message send at same type i.e. 13....//1st is string & 2nd is type.........so while message receiving in msgrcv()...it will try to recive 1st characters type message....not int type value....so in this program ...it will fail 1st recieve in which we r trying to recieve int value.....sucess 2nd receive ..in which we r trying to recieve characters type message...where to identify message ...it will see that there is int type value that is 13...send after characters type message......it will see that it has got message type.... then in the same 2nd recieve....what it is doing that....algorithm of msgrcv is set like that 1st message is type which will be left while recieving the message...& 2nd message will be actual message ..which msgrcv has to recieve......then it will receive 2nd message that is type...so instead recieving message of characters ...it is receiving 4 bytes of type value....
/when we again compile same program...then it will try to recive type now..but it cannot be recived because type is already recieved from same message queue & same block...default message recieve type is used that is zero(0)...means it has to receive all the messages coming in continue way..but still it will leave the 1st message in block that is epected to be type ...but here we have 1st message as characters message ..& 2nd is type ...so it cant receive the type ...bcz already recieved ...so could not recieve... //again if we will compile...for dafault recieve type 0...it will scan whole block it has to ignore the type again whatever it would be...here it is like characters message..& expect message related to that type which is type itself ..that received at 1st compile....so could not be recived again //no. of times compile is attempt....but message of characters is due to 1st message leaved everytime either there is expected message i.e. type behind it is received once...could not be recieved again.. //so characters message will always be in message queue......use ipcs command to see message queue return 0; OUT: return -1; }