linux ftok()函數

來源:互聯網
上載者:User


linux ftok()函數 系統建立IPC通訊(如訊息佇列、共用記憶體時)必須指定一個ID值。通常情況下,該id值通過ftok函數得到。ftok原型如下:key_t ftok( char * fname, int id )fname就時你指定的檔案名稱(該檔案必須是存在而且可以訪問的),id是子序號,雖然為int,但是只有8個位元被使用(0-255)。當成功執行的時候,一個key_t值將會被返回,否則 -1 被返回。    在一般的UNIX實現中,是將檔案的索引節點號取出,前面加上子序號得到key_t的傳回值。如指定檔案的索引節點號為65538,換算成16進位為 0x010002,而你指定的ID值為38,換算成16進位為0x26,則最後的key_t傳回值為0x26010002。查詢檔案索引節點號的方法是: ls -i  www.2cto.com  以下為測試程式:#include <stdio.h>#include <sys/types.h>#include <sys/ipc.h> #define IPCKEY 0x11int main( void ){    int i=0;    for ( i = 1; i < 256; ++ i )        printf( "key = %x\n", ftok( "/tmp", i ) );     return 0;}在成功擷取到key之後,就可以使用該key作為某種方法的處理序間通訊的key值,例如shmget共用記憶體的方式。shmget的函數原型為int shmget( key_t, size_t, flag);在建立成功後,就返回共用記憶體的描述符。在shmget中使用到的key_t就是通過ftok的方式產生的執行個體:  www.2cto.com  #include <sys/shm.h>#include <stdio.h>#include <errno.h>#include <string.h>#include <stdlib.h>#define SIZE 1024extern int errno;int main(){int shmid;char *shmptr;//建立共用記憶體if((shmid = shmget(IPC_PRIVATE, SIZE, 0600)) < 0)   {    printf("shmget error:%s\n", strerror(errno));    return -1;   }//將共用記憶體串連到 可用地址上if((shmptr = (char*)shmat(shmid, 0, 0)) == (void*)-1){    printf("shmat error:%s\n", strerror(errno));    return -1;}memcpy(shmptr, "hello world", sizeof("hello world"));printf("share memory from %lx to %lx, content:%s\n",(unsigned long)shmptr, (unsigned long)(shmptr + SIZE), shmptr);//拆卸共用記憶體if((shmctl(shmid, IPC_RMID, 0) < 0)){    printf("shmctl error:%s\n", strerror(errno));    return -1;  www.2cto.com  }}多進程之間共用記憶體情況:#include <sys/shm.h>#include <stdio.h>#include <errno.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>#define SIZE 1024extern int errno;int main(){int shmid;char *shmptr;key_t key;pid_t pid;if((pid = fork()) < 0){    printf("fork error:%s\n", strerror(errno));    return -1;   }else if(pid == 0)   {     sleep(2);    if((key = ftok("/dev/null", 1)) < 0)    {      printf("ftok error:%s\n", strerror(errno));      return -1;    }if((shmid = shmget(key, SIZE, 0600)) < 0){    printf("shmget error:%s\n", strerror(errno));    exit(-1);  www.2cto.com     }if((shmptr = (char*)shmat(shmid, 0, 0)) == (void*)-1){    printf("shmat error:%s\n", strerror(errno));    exit(-1);}//memcpy(shmptr, "hello world", sizeof("hello world")); printf("child:pid is %d,share memory from %lx to %lx, content:%s\n",getpid(), (unsigned long)shmptr, (unsigned long)(shmptr + SIZE), shmptr);printf("child process sleep 2 seconds\n");sleep(2);if((shmctl(shmid, IPC_RMID, 0) < 0)){    printf("shmctl error:%s\n", strerror(errno));    exit(-1);}   exit(0);   }   //parent   else   {    if((key = ftok("/dev/null", 1)) < 0)    {      printf("ftok error:%s\n", strerror(errno));      return -1;    }if((shmid = shmget(key, SIZE, 0600|IPC_CREAT|IPC_EXCL)) < 0){    printf("shmget error:%s\n", strerror(errno));    exit(-1);   }if((shmptr = (char*)shmat(shmid, 0, 0)) == (void*)-1){    printf("shmat error:%s\n", strerror(errno));    exit(-1);  www.2cto.com  }memcpy(shmptr, "hello world", sizeof("hello world"));printf("parent:pid is %d,share memory from %lx to %lx, content:%s\n",getpid(),(unsigned long)shmptr, (unsigned long)(shmptr + SIZE), shmptr);printf("parent process sleep 2 seconds\n");sleep(2);if((shmctl(shmid, IPC_RMID, 0) < 0)){    printf("shmctl error:%s\n", strerror(errno));    exit(-1);}   }waitpid(pid,NULL,0);exit(0);}  作者 軌跡16

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.