日期:2014-05-16  浏览次数:20611 次

获取消息队列ID
这是我在Linux(F5)下写的获取消息队列ID的程序,但就是获取不了,不知道是为什么?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <linux/types.h>
#include <linux/ipc.h>
#include <linux/msg.h>


int main(void)
{
key_t key;
int msgq_id;

key = ftok("./", "mymsgq");

printf("key: %d\n", key);

msgq_id = msgget(key, IPC_CREAT|0660);

if (msgq_id == -1)
{
printf("open message queue FAIL.\n");
}
else
{
printf("open message queue OK.\n");
}

exit(0);
}



------解决方案--------------------
msgq_id = msgget(key, IPC_PRIVATE);
------解决方案--------------------
ftok("./", "mymsgq"); 
====
ftok可以这么用吗?第二个参数应该是整数。
NAME
ftok - convert a pathname and a project identifier to a System V IPC
key

SYNOPSIS
# include <sys/types.h>
# include <sys/ipc.h>

key_t ftok(const char *pathname, int proj_id);