当前位置: 技术问答>linux和unix
谁能解释一下key_t ftok(char*, char)是做什么用的啊?请看例子:
来源: 互联网 发布时间:2015-09-10
本文导语: #include #include #include #define MSG_FILE "server.c" int main() { struct msgtype msg; key_t key; int msgid; //这里的ftok到底是什么作用呢?为什么要用这个? //它好像返回与路径MSG_FILE相对应的...
#include
#include
#include
#define MSG_FILE "server.c"
int main()
{
struct msgtype msg;
key_t key;
int msgid;
//这里的ftok到底是什么作用呢?为什么要用这个?
//它好像返回与路径MSG_FILE相对应的一个键值,那又有什么用呢?
if((key=ftok(MSG_FILE,'a'))==-1)
{
fprintf(stderr,"Creat Key Error:%san",strerror(errno));
exit(1);
}
if((msgid=msgget(key,PERM|IPC_CREAT|IPC_EXCL))==-1)
{
fprintf(stderr,"Creat Message Error:%san",strerror(errno));
exit(1);
}
。。。。
}
#include
#include
#define MSG_FILE "server.c"
int main()
{
struct msgtype msg;
key_t key;
int msgid;
//这里的ftok到底是什么作用呢?为什么要用这个?
//它好像返回与路径MSG_FILE相对应的一个键值,那又有什么用呢?
if((key=ftok(MSG_FILE,'a'))==-1)
{
fprintf(stderr,"Creat Key Error:%san",strerror(errno));
exit(1);
}
if((msgid=msgget(key,PERM|IPC_CREAT|IPC_EXCL))==-1)
{
fprintf(stderr,"Creat Message Error:%san",strerror(errno));
exit(1);
}
。。。。
}
|
ftok - generate an IPC key
SYNOPSIS
#include
key_t ftok(const char *path, int id);
DESCRIPTION
The ftok() function returns a key based on path and id that is usable in subsequent calls to msgget(), semget() and shmget(). The path argument must be the pathname of an existing file that the process is able to stat().
The ftok() function will return the same key value for all paths that name the same file, when called with the same id value, and will return different key values when called with different id values or with paths that name different files existing on the same file system at the same time. It is unspecified whether ftok() returns the same key value when called again after the file named by path is removed and recreated with the same name.
Only the low order 8-bits of id are significant. The behaviour of ftok() is unspecified if these bits are 0.
SYNOPSIS
#include
key_t ftok(const char *path, int id);
DESCRIPTION
The ftok() function returns a key based on path and id that is usable in subsequent calls to msgget(), semget() and shmget(). The path argument must be the pathname of an existing file that the process is able to stat().
The ftok() function will return the same key value for all paths that name the same file, when called with the same id value, and will return different key values when called with different id values or with paths that name different files existing on the same file system at the same time. It is unspecified whether ftok() returns the same key value when called again after the file named by path is removed and recreated with the same name.
Only the low order 8-bits of id are significant. The behaviour of ftok() is unspecified if these bits are 0.
|
man ftok