這個系列整理 Linux 行程間通訊的方式:管道、訊息佇列、訊號、訊號量、共享記憶體與 socket。本篇談訊號量。
系列前文:管道、訊息佇列。
學過作業系統的人對訊號量不陌生,但要特別注意:訊號與訊號量是兩回事。
臨界區是加鎖區間內的那段程式碼;臨界資源則是同一時間只能被一個行程使用的資源。
把訊號量理解成一個計數器即可。它有初值(>0),行程要取得資源時執行 P 操作讓它減一;減到 0 代表資源用盡,其他行程必須等待;使用完畢後執行 V 操作加一,釋放資源。設定初值為 1 時,就是最常見的互斥鎖。
int semget(key_t key, int nsems, int semflg); #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h>
nsems 指定要建立幾個訊號量——semget 實際建立的是一個訊號量集,可理解為訊號量陣列。semflg 常用 IPC_CREAT 與 IPC_EXCL。
初始化是另一個函式,這也帶來一個實務上的缺點:
int semctl(int semid, int semnum, int cmd, ...);
semnum 指定要初始化集合中的第幾個(從 0 起算),cmd 傳 SETVAL,第四個參數則是下列联合体中的 val:
union semun {
int val; /* Value for SETVAL */
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* Array for GETALL, SETALL */
struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */
};
由於建立與初始化是兩個獨立動作,若行程 A 才剛建立尚未初始化,行程 B 就已經執行 P 操作,減一便會卡住——這是實際開發時很容易踩到的問題,實務上應由建立者完成初始化後再通知其他行程。
int semop(int semid, struct sembuf *sops, unsigned nsops); #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h>
nsops 表示這次要同時操作幾個訊號量,具體目標則寫在 sembuf 結構中:
struct sembuf {
unsigned short sem_num; /* 訊號量在集合中的索引 */
short sem_op; /* 1 為 V 操作,-1 為 P 操作 */
short sem_flg; /* 操作旗標 */
};
sem_flg 有兩個常見值:
同樣用 semctl,把 cmd 換成 IPC_RMID 即可,此時不需第四個參數。
程式分為 comm.h、comm.c 與 server.c。父行程印 A、子行程印 B,未加鎖時會交錯;加上訊號量後則能各自連續輸出。
comm.h
#include <stdio.h> #include <sys/ipc.h> #include <string.h> #include <unistd.h> #include <sys/sem.h> #include <stdlib.h> #include <errno.h> #define _PATH_NAME_ "/tmp" #define _PROJ_ID_ 0x6666 static int comm_create_sem(int flags, int num); int create_sem(int num); int get_sem(); void P_sem(int sem_id, int index); void V_sem(int sem_id, int index);
comm.c
#include "comm.h"
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
};
static int comm_create_sem(int flags, int num)
{
key_t _key = ftok(_PATH_NAME_, _PROJ_ID_);
if(_key < 0) {
printf("%d:%s", errno, strerror(errno));
}
int sem_id;
if((sem_id = semget(_key, num, flags)) < 0) {
printf("semget errno,%d:%s", errno, strerror(errno));
}
return sem_id;
}
int create_sem(int num)
{
int flags = IPC_CREAT | IPC_EXCL;
int sem_id = comm_create_sem(flags, num);
union semun v;
v.val = 1;
if(semctl(sem_id, 0, SETVAL, v) < 0) // init
{
printf("init error,%d:%s", errno, strerror(errno));
}
return sem_id;
}
int get_sem()
{
int flags = 0;
return comm_create_sem(flags, 0);
}
void P_sem(int sem_id, int index)
{
struct sembuf s;
s.sem_num = index;
s.sem_op = -1;
s.sem_flg = 0;
if(semop(sem_id, &s, 1) < 0) {
printf("op error,%d:%s", errno, strerror(errno));
}
}
void V_sem(int sem_id, int index)
{
struct sembuf s;
s.sem_num = index;
s.sem_op = 1;
s.sem_flg = 0;
if(semop(sem_id, &s, 1) < 0) {
printf("op error,%d:%s", errno, strerror(errno));
}
}
void destory_sem(int sem_id)
{
semctl(sem_id, 0, IPC_RMID);
}
server.c
#include "comm.h"
int main()
{
int pid;
pid = fork();
if(pid > 0) {
// father
int sem_id = create_sem(1);
while(1) {
P_sem(sem_id, 0);
printf("A"); fflush(stdout);
sleep(1);
printf("A"); fflush(stdout);
V_sem(sem_id, 0);
}
} else {
// child
while(1) {
int sem_id = get_sem();
P_sem(sem_id, 0);
printf("B"); fflush(stdout);
sleep(1);
printf("B"); fflush(stdout);
V_sem(sem_id, 0);
}
}
return 0;
}