【发布时间】:2018-10-10 07:16:33
【问题描述】:
我正在对消息队列进行一些编码。
ftoK() 有什么用?
什么是创建密钥?
钥匙有什么用?
在我的代码中,我使用它作为键“(key_t)1234”,代码运行良好。
这个键“(key_t)1234”是什么意思?
如何创建自己的密钥?
发件人:
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");
}
}
}
接收者:
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);
}
}
【问题讨论】:
-
在我注意到你并不是一个新手之前,我已经为你做了很多格式化。请尽早学习如何为你自己做这些。
-
请参考 ftok() 的规范(大概您参考例如man7.org/linux/man-pages/man3/ftok.3.html),以更清楚地解释您所询问的内容的详细信息。乍一看,该规范似乎可以解释您的问题。因此,您需要更具体地说明您不清楚的地方。如果您要求提供有关此功能的基本使用以及您需要它的情况的教程,请记住,对教程的请求在 StackOverflow 上是题外话。
标签: c data-structures message-queue embedded-linux