linux 程序设计 System V 信号量编程

本文深入探讨了Linux中的信号量概念,包括二元信号量、计数信号量、信号量集及其相关数据结构。文章详细介绍了信号量集的创建、修改和控制方法,并通过示例展示了如何在多线程环境中利用信号量实现资源同步和管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概念:

二值信号量:信号量的值为 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,...);

semidsemget返回的信号量符号

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);
}
运行结果:























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值