I'm doing some coding on message queues.
What is the use of ftoK()?
What is creation of keys?
What is the use of key?
In my code i use this as key "(key_t)1234", the code is running fine.
What the meaning of this key "(key_t)1234"?
How can i create my own key?
SENDER:
struct mesg_q
{
char msg_txt[100];
long msg_typ;
};
int main()
{
int msgid;
//key_t msg_key;
char buffer[100];
struct mesg_q msgq;
msgid=msgget((key_t)1234, 0666 | IPC_CREAT);
if(msgid== -1)
{
printf("msgget failed\n");
return -1;
}
while(1)
{
printf("Text Message\n");
fgets(msgq.msg_txt,100,stdin);
if(msgsnd(msgid,&msgq,100,0)==-1)
{
printf("Send failed\n");
return -1;
}
else
{
printf("Message send\n");
}
}
}
RECEIVER:
struct mesg_q
{
char msg_txt[100];
long msg_typ;
};
int main()
{
int msgid;
char buffer[100];
long int rec_buff=0;
key_t key;
struct mesg_q msgq;
msgid=msgget((key_t)1234, 0666 | IPC_CREAT);
if(msgid == -1)
{
printf("Msgget failed\n");
return -1;
}
while(1)
{
if(msgrcv(msgid,&msgq,100,rec_buff,0)==-1)
{
printf("Mesg recv failed\n");
return -1;
}
else
{
printf("Mesg Recvd\n");
}
printf("Recvd mesg=%s\n",msgq.msg_txt);
}
}