.\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it) .\" .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .TH IPC 5 "November 1, 1993" "Linux 0.99.13" "Linux Programmer's Manual" .SH NAME ipc \- System V 進程間通信機制 .SH SYNOPSIS 總覽 .nf .B # include .B # include .B # include .B # include .B # include .SH DESCRIPTION 本手冊頁涉及 System V 進程間通信機制在 Linux 下的實現: 消息隊列, 信號燈集合, 以及共享內存段. 下面提到 .B 資源 時, 就是指上面這些通信機制中的一種. .SS 資源訪問權限 對每個資源, 系統用一個共有的 .BR "struct ipc_perm" 結構來存放權限信息, 以確定一個 ipc 操作是否可訪問該資源. 在 .I 中定義了 .B ipc_perm, 其成員如下: .sp .B ushort cuid; /* 創建者 uid */ .br .B ushort cgid; /* 創建者 gid */ .br .B ushort uid; /* 所有者 uid */ .br .B ushort gid; /* 所有者 gid */ .br .B ushort mode; /* 讀/寫權限 */ .PP 結構 .B ipc_perm 的成員 .B mode 的低九位定義了對該資源的訪問許 可, 以確定一個執行了 ipc 系統調用的進程能否訪問該資源. 其解 釋如下: .sp .nf 0400 用戶可讀. 0200 用戶可寫. .sp .5 0040 組成員可讀. 0020 組成員可寫. .sp .5 0004 其他用戶可讀. 0002 其他用戶可寫. .fi .PP 系統沒有使用執行位 0100, 0010 和 0001. 另外, 這裏的 "可寫" 等 效於信號燈集合裏的 "可更改". .PP 在 .I 系統頭文件裏還定義瞭如下符號常數: .TP 14 .B IPC_CREAT 如果 key 不存在就創建. .TP .B IPC_EXCL 如果 key 已經存在則失敗. .TP .B IPC_NOWAIT 如果請求必須等待, 產生錯誤. .TP .B IPC_PRIVATE 私有 key. .TP .B IPC_RMID 刪除資源. .TP .B IPC_SET 設置資源選項. .TP .B IPC_STAT 取得資源選項. .PP 請注意 .B IPC_PRIVATE 是一個 .B key_t 類型, 而別的符號常數都是標誌域,它們的可以或( OR )在一起形成 .B int 類型. .SS 消息隊列 消息隊列由正整數 .RI "(它的 " msqid ) 唯一標識, 其結構體 .BR "struct msquid_ds" 在 .IR 中定義, 包含如下成員: .sp .B struct ipc_perm msg_perm; .br .B ushort msg_qnum; /* 隊列中消息數目 */ .br .B ushort msg_qbytes; /* 一條隊列最大字節數 */ .br .B ushort msg_lspid; /* 上一次 msgsnd 調用的 pid */ .br .B ushort msg_lrpid; /* 上一次 msgrcv 調用的 pid */ .br .B time_t msg_stime; /* 上一次 msgsnd 的時間 */ .br .B time_t msg_rtime; /* 上一次 msgrcv 的時間 */ .br .B time_t msg_ctime; /* 上一次修改時間 */ .TP 11 .B msg_perm .B ipc_perm 結構, 指明瞭對該消息隊列的訪問權限. .TP .B msg_qnum 該隊列當前的消息總數. .TP .B msg_qbytes 該隊列所允許的消息正文最大字節總數. .TP .B msg_lspid 最後做 .B msgsnd 系統調用的進程的 ID. .TP .B msg_lrpid 最後做 .B msgrcv 系統調用的進程的 ID. .TP .B msg_stime 最近做 .B msgsnd 系統調用的時間. .TP .B msg_rtime 最近做 .B msgrcv 系統調用的時間. .TP .B msg_ctime 最後一次改變 .B msqid_ds 結構成員的時間. .SS 信號燈集合 信號燈集合由正整數 .RI "(它的 " semid ) 唯一標識, 並有一個與之關聯的結構體 .BR "struct semid_ds" 它在 .IR 中定義, 包含如下成員: .sp .B struct ipc_perm sem_perm; .br .B time_t sem_otime; /* 上一次操作的時間 */ .br .B time_t sem_ctime; /* 上一次修改的時間 */ .br .B ushort sem_nsems; /* 集合中信號燈數目 */ .TP 11 .B sem_perm .B ipc_perm 結構, 指明對該信號燈集合的訪問權限. .TP .B sem_otime 最近做 .B semop 系統調用的時間. .TP .B sem_ctime 最近做 .B semctl 系統調用的時間, 該調用修改了上面結構的一個成員 或者改變了屬於該集合的一個信號燈. .TP .B sem_nsems 該信號燈集合的信號燈數目. 集合中每個信號燈都可以用從 .B 0 到 .BR sem_nsems\-1 的一個非負整數來引用. .PP 一個信號燈就是一個 .B "struct sem" 結構, 包含如下成員: .sp .B ushort semval; /* 信號燈值 */ .br .B short sempid; /* 上一次操作的進程的 pid */ .br .B ushort semncnt; /* 等待增加 semval 值的進程數目 */ .br .B ushort semzcnt; /* 等待 semval = 0 的進程數目 */ .TP 11 .B semval 該信號燈值,是一個非負整數. .TP .B sempid 最後一個對該信號燈做操作的進程 ID. .TP .B semncnt 等待增加 .B semval 的進程數. .TP .B semznt 等待 .B semval 變成 0 的進程數. .SS 共享內存段 共享內存段由正整數 .RI "(它的 " shmid ) 唯一標識, 有一個關聯的結構類型 .BR "struct shmid_ds" 在 .IR 中定義, 包含如下成員: .sp .B struct ipc_perm shm_perm; .br .B int shm_segsz; /* 段尺寸 */ .br .B ushort shm_cpid; /* 創建者 pid */ .br .B ushort shm_lpid; /* 上一次操作的進程的 pid */ .br .B short shm_nattch; /* 目前附着的進程數目 */ .br .B time_t shm_atime; /* 上一次附着的時間 */ .br .B time_t shm_dtime; /* 上一次脫離的時間 */ .br .B time_t shm_ctime; /* 上一次修改的時間 */ .TP 11 .B shm_perm .B ipc_perm 結構, 指明對共享內存段的訪問權限. .TP .B shm_segsz 共享內存段的大小, 以字節爲單位. .TP .B shm_cpid 創建該共享內存段的進程的 ID. .TP .B shm_lpid 最後執行 .B shmat 或者 .B shmdt 系統調用的進程 ID. .TP .B shm_nattch 當前對該共享內存段的活躍連接數. .TP .B shm_atime 最後做 .B shmat 系統調用的時間. .TP .B shm_dtime 最後做 .B shmdt 系統調用的時間. .TP .B shm_ctime 最後做 .B shmctl 系統調用的時間, 如果該調用改變了 shmid_ds. .SH "又見" .BR ftok (3), .BR msgctl (2), .BR msgget (2), .BR msgrcv (2), .BR msgsnd (2), .BR semctl (2), .BR semget (2), .BR semop (2), .BR shmat (2), .BR shmctl (2), .BR shmget (2), .BR shmdt (2). .SH "[中文版維護人]" .B name .SH "[中文版最新更新]" .BR 2001/02/02 .SH "《中國 Linux 論壇 man 手冊頁翻譯計劃》:" .BI http://cmpp.linuxforum.net .SH "跋" .br 本頁面中文版由中文 man 手冊頁計劃提供。 .br 中文 man 手冊頁計劃:\fBhttps://github.com/man-pages-zh/manpages-zh\fR