系統(tǒng)建立IPC通訊(如消息隊(duì)列、共享內(nèi)存時(shí))必須指定一個(gè)ID值。通常情況下,該id值通過ftok函數(shù)得到。
ftok原型如下:
key_t ftok( char * fname, int id )
fname就時(shí)你指定的文件名(該文件必須是存在而且可以訪問的),id是子序號(hào),雖然為int,但是只有8個(gè)比特被使用(0-255)。
當(dāng)成功執(zhí)行的時(shí)候,一個(gè)key_t值將會(huì)被返回,否則 -1 被返回。
在一般的UNIX實(shí)現(xiàn)中,是將文件的索引節(jié)點(diǎn)號(hào)取出,前面加上子序號(hào)得到key_t的返回值。如指定文件的索引節(jié)點(diǎn)號(hào)為65538,換算成16進(jìn)制為 0x010002,而你指定的ID值為38,換算成16進(jìn)制為0x26,則最后的key_t返回值為0x26010002。
查詢文件索引節(jié)點(diǎn)號(hào)的方法是: ls -i
以下為測試程序:
#include
#include
#include
#define IPCKEY 0x11
int main( void )
{
int i=0;
for ( i = 1; i < 256; ++ i )
printf( "key = %xn", ftok( "/tmp", i ) );
return 0;
}
在成功獲取到key之后,就可以使用該key作為某種方法的進(jìn)程間通信的key值,例如shmget共享內(nèi)存的方式。
shmget的函數(shù)原型為
int shmget( key_t, size_t, flag);
在創(chuàng)建成功后,就返回共享內(nèi)存的描述符。在shmget中使用到的key_t就是通過ftok的方式生成的
實(shí)例:
#include
#include
#include
#include
#include
#define SIZE 1024
extern int errno;
int main()
{
int shmid;
char *shmptr;
//創(chuàng)建共享內(nèi)存
if((shmid = shmget(IPC_PRIVATE, SIZE, 0600)) < 0)
{
printf("shmget error:%sn", strerror(errno));
return -1;
}
//將共享內(nèi)存連接到 可用地址上
if((shmptr = (char*)shmat(shmid, 0, 0)) == (void*)-1)
{
printf("shmat error:%sn", strerror(errno));
return -1;
}
memcpy(shmptr, "hello world", sizeof("hello world"));
printf("share memory from %lx to %lx, content:%sn",(unsigned long)shmptr, (unsigned long)(shmptr + SIZE), shmptr);
//拆卸共享內(nèi)存
if((shmctl(shmid, IPC_RMID, 0) < 0))
{
printf("shmctl error:%sn", strerror(errno));
return -1;
}
}
多進(jìn)程之間共享內(nèi)存情況:
#include
#include
#include
#include
#include
#include
#include
#include
#define SIZE 1024
extern int errno;
int main()
{
int shmid;
char *shmptr;
key_t key;
pid_t pid;
if((pid = fork()) < 0)
{
printf("fork error:%sn", strerror(errno));
return -1;
}
else if(pid == 0)
{
sleep(2);
if((key = ftok("/dev/null", 1)) < 0)
{
printf("ftok error:%sn", strerror(errno));
return -1;
}
if((shmid = shmget(key, SIZE, 0600)) < 0)
{
printf("shmget error:%sn", strerror(errno));
exit(-1);
}
if((shmptr = (char*)shmat(shmid, 0, 0)) == (void*)-1)
{
printf("shmat error:%sn", strerror(errno));
exit(-1);
}
//memcpy(shmptr, "hello world", sizeof("hello world"));
printf("child:pid is %d,share memory from %lx to %lx, content:%sn",getpid(), (unsigned long)shmptr, (unsigned long)(shmptr + SIZE
), shmptr);
printf("child process sleep 2 secondsn");
sleep(2);
if((shmctl(shmid, IPC_RMID, 0) < 0))
{
printf("shmctl error:%sn", strerror(errno));
exit(-1);
}
exit(0);
}
//parent
else
{
if((key = ftok("/dev/null", 1)) < 0)
{
printf("ftok error:%sn", strerror(errno));
return -1;
}
if((shmid = shmget(key, SIZE, 0600|IPC_CREAT|IPC_EXCL)) < 0)
{
printf("shmget error:%sn", strerror(errno));
exit(-1);
}
if((shmptr = (char*)shmat(shmid, 0, 0)) == (void*)-1)
{
printf("shmat error:%sn", strerror(errno));
exit(-1);
}
memcpy(shmptr, "hello world", sizeof("hello world"));
printf("parent:pid is %d,share memory from %lx to %lx, content:%sn",getpid(),(unsigned long)shmptr, (unsigned long)(shmptr + SIZE
), shmptr);
printf("parent process sleep 2 secondsn");
sleep(2);
if((shmctl(shmid, IPC_RMID, 0) < 0))
{
printf("shmctl error:%sn", strerror(errno));
exit(-1);
}
}
waitpid(pid,NULL,0);
exit(0);
}
輸出為: