天天看點

Linux IPC實踐(6) --System V消息隊列(3)

消息隊列實作回射客戶/伺服器

Linux IPC實踐(6) --System V消息隊列(3)

server程序接收時, 指定msgtyp為0, 從隊首不斷接收消息

server程序發送時, 将mtype指定為接收到的client程序的pid

client程序發送的時候, mtype指定為自己程序的pid

client程序接收時, 需要将msgtyp指定為自己程序的pid, 隻接收消息類型為自己pid的消息;

附-ftok用法

描述資訊:

   The ftok() function uses the identity(象征) of the file named by the given pathname (which must refer 

to an existing, accessible file[必須是一個已經存在,并且可通路的檔案]) and the least significant(有效的) 8 bits[有效的最低8位] of proj_id (which must  be  nonzero)  to  generate  a  key_t  type  System V IPC key, suitable 

for use with msgget(2), semget(2), or shmget(2).   The resulting value is the same for all pathnames that name the same file, when the  same value  of  proj_id  

is used(如果檔案名與proj_id的有效位全都相同的話, 則生成的key一定也是相同的).  The value returned should be different when 

the (simultaneously existing) files or the project IDs differ.

RETURN VALUE   On success, the generated key_t value is returned.  On failure -1 is returned, 

with errno indicating the error as for the stat(2) system call.

繼續閱讀