我正在学习 IPC 编程。作为其中的一部分,我尝试了以下两个代码来了解消息队列....
消息队列创建者或消息发送者
struct my_msgbuf {
long mtype;
char mtext[200];
};
int main(void)
{
struct my_msgbuf buf;
int msqid;
key_t key;
if ((key = ftok("kirk.c", 'B')) == -1) {
perror("ftok");
exit(1);
}
if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) {
perror("msgget");
exit(1);
}
printf("Enter lines of text, ^D to quit:\n");
buf.mtype = 1; /* we don't really care in this case */
while(fgets(buf.mtext, sizeof buf.mtext, stdin) != NULL) {
int len = strlen(buf.mtext);
/* ditch newline at end, if it exists */
if (buf.mtext[len-1] == '\n') buf.mtext[len-1] = '\0';
if (msgsnd(msqid, &buf, len+1, 0) == -1) /* +1 for '\0' */
perror("msgsnd");
}
if (msgctl(msqid, IPC_RMID, NULL) == -1) {
perror("msgctl");
exit(1);
}
return 0;
}
消息接收者
struct my_msgbuf {
long mtype;
char mtext[200];
};
int main(void)
{
struct my_msgbuf buf;
int msqid;
key_t key;
if ((key = ftok("kirk.c", 'B')) == -1) { /* same key as kirk.c */
perror("ftok");
exit(1);
}
if ((msqid = msgget(key, 0644)) == -1) { /* connect to the queue */
perror("msgget");
exit(1);
}
printf("spock: ready to receive messages, captain.\n");
for(;;) { /* Spock never quits! */
if (msgrcv(msqid, &buf, sizeof(buf.mtext), 0, 0) == -1) {
perror("msgrcv");
exit(1);
}
printf("spock: \"%s\"\n", buf.mtext);
}
return 0;
}
以上代码可以在beej's guide for message queue找到.
当我尝试执行“spock”时,msgget() 抛出错误:没有这样的文件或目录。 ftok() 有什么问题吗?我将文件的权限更改为传递给 msgget() 函数的权限。但是同样的错误。提前致谢。 提前致谢。
最佳答案
ftok
要求文件存在,因为它使用 inode
信息来构造 key 。如果您在单独的目录中构建它们,则使用相对路径指向 kirk.c
应该可以正常工作,例如spock/spock.c
包含 spock 代码,kirk/kirk.c
包含 kirk 代码,在 spock/spock.c
你应该引用到 ../kirk/kirk.c
关于c - msgget() 和 ftok() 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14120080/