概念:
二值信號量:信號量的值爲 0 或爲 1。與互斥鎖類似。
計數信號量:其值在 0 和某個限制值之間的信號量。信號量的值就是可用資源數。
等待操作:信號量的值變爲大於 0,然後將它減 1。
掛出操作:將信號量的值加 1,從而喚醒正在等待該信號量值變爲大於 0 的任何線程。
計數信號量集:一個或多個信號量(構成一個集合),其中每個都是計數信號量。每個集合的信號量存在一個限制,一般在 25 個的數量級上。
信號量集的數據結構(定義在<sys/sem.h>):
struct semid_ds
{
struct ipc_perm sem_perm; //operation permission struct
struct sem
*sem_base; //ptr to array of semaphores in set
ushort sem_nsems; //of semaphores in set
time_t sem_otime; //time of last semop()
time_t sem_ctime; //time of creation or
last IPC_SET
};
sem_perm 結構含有這個信號量的訪問權限:
struct ipc_perm
{
uid_t uid; // owner's user id
gid_t gid; //owner's group id;
uid_t cuid; //creator's user id
gid_t cgid; //creator's group id
mode_t mode; //read-write permissions
ulong_t seq; //slot usage sequence number
key_t key; //IPC key
};
sem結構是內核用於維護某個 給定信號量的一組值的內部數據結構,描述每個成員的數據結構:
struct sem
{
ushort_t semval; //semaphore value,nonnegative
short sempid; //PID of last successful semop(),SETVAL,SETALL
ushort_t semncnt; //awaiting semval > current value
unshort_t semzcnt; //awaiting semval = 0
};
注意: sem_base含有指向某個sem 結構數組的指針:當前信號量集中在每個信號量對應其中一個數組元素。
信號量設計相關函數:
1. semget :創建一個信號量集或訪問一個已經存在的信號量集。定義如下:
#include<sys/sem.h>
int semget(key_t key,int nsems,int oflag); //成功返回非負標識符,出錯則返回 -1
返回值是一個稱爲信號量標識符(semaphore identifier)的整數,semop
和 semctl 函數將使用它。
key:不相關的進程可以通過他訪問同一個信號量。
nsems:信號量的數目,一般取 1
oflag:信號量標誌
2.semop :改變信號量的值。定義如下:
#include<sys/sem.h>
int semop(int semid,struct sembuf *opsptr,size_t nops);
semid:semget 返回的信號量標識符
semops:指向一個結構數組的指針:
struct sembuf
{
short sem_num; //信號量的編號
short sem_op; //一次操作中需要改變的數值
例如 -1,或+1
short sem_flg;
//通常設爲 SEM_UNDO
};
3. semctl:用來直接控制信號量的信息。定義如下:
#include<sys/sem.h>
int semctl(int semid,int sem_num,int command,...);
semid:semget返回的信號量符號
sem_num:信號量的編號
command:將要採取的動作
程序(轉載自linux 程序設計 人民郵電出版社 作者Neil Matthew ,Richard Stones)
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/sem.h>
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
unsigned short int *array; /* array for GETALL, SETALL */
struct seminfo *__buf; /* buffer for IPC_INFO */
};
static int set_semvalue(void);
static void del_semvalue(void);
static int semaphore_p(void);
static int semaphore_v(void);
static int sem_id;
int main(int argc, char *argv[])
{
int i;
int pause_time;
char op_char = 'O';
srand((unsigned int)getpid());
sem_id = semget((key_t)1234, 1, 0666 | IPC_CREAT);
if (argc > 1) {
if (!set_semvalue()) {
fprintf(stderr, "Failed to initialize semaphore\n");
exit(EXIT_FAILURE);
}
op_char = 'X';
sleep(2);
}
/* Then we have a loop which enters and leaves the critical section ten times.
There, we first make a call to semaphore_p which sets the semaphore to wait, as
this program is about to enter the critical section. */
for(i = 0; i < 10; i++) {
if (!semaphore_p()) exit(EXIT_FAILURE);
printf("%c", op_char);fflush(stdout);
pause_time = rand() % 3;
sleep(pause_time);
printf("%c", op_char);fflush(stdout);
/* After the critical section, we call semaphore_v, setting the semaphore available,
before going through the for loop again after a random wait. After the loop, the call
to del_semvalue is made to clean up the code. */
if (!semaphore_v()) exit(EXIT_FAILURE);
pause_time = rand() % 2;
sleep(pause_time);
}
printf("\n%d - finished\n", getpid());
if (argc > 1) {
sleep(10);
del_semvalue();
}
exit(EXIT_SUCCESS);
}
/* The function set_semvalue initializes the semaphore using the SETVAL command in a
semctl call. We need to do this before we can use the semaphore. */
static int set_semvalue(void)
{
union semun sem_union;
sem_union.val = 1;
if (semctl(sem_id, 0, SETVAL, sem_union) == -1) return(0);
return(1);
}
/* The del_semvalue function has almost the same form, except the call to semctl uses
the command IPC_RMID to remove the semaphore's ID. */
static void del_semvalue(void)
{
union semun sem_union;
if (semctl(sem_id, 0, IPC_RMID, sem_union) == -1)
fprintf(stderr, "Failed to delete semaphore\n");
}
/* semaphore_p changes the semaphore by -1 (waiting). */
static int semaphore_p(void)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = -1; /* P() */
sem_b.sem_flg = SEM_UNDO;
if (semop(sem_id, &sem_b, 1) == -1) {
fprintf(stderr, "semaphore_p failed\n");
return(0);
}
return(1);
}
/* semaphore_v is similar except for setting the sem_op part of the sembuf structure to 1,
so that the semaphore becomes available. */
static int semaphore_v(void)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = 1; /* V() */
sem_b.sem_flg = SEM_UNDO;
if (semop(sem_id, &sem_b, 1) == -1) {
fprintf(stderr, "semaphore_v failed\n");
return(0);
}
return(1);
}
運行結果: