信号量(一)

本文介绍了信号量的概念及其在操作系统中的作用。详细讲解了P操作和V操作,以及如何使用信号量解决汽车载人问题。通过示例展示了如何利用信号量控制进程同步,确保在没有乘客时汽车只能上人。

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


信号量

P操作 : sv > 0, 则减一. sv == 0, 挂起.
V操作 : sv == 0, 则加一. 有等待的就将其唤醒.


信号量函数

#include <semaphore.h>

// sem:信号量的地址。
// pshared:等于 0,信号量在线程间共享(常用);不等于0,信号量在进程间共享。
// value:信号量的初始值。
int sem_init(sem_t *sem, int pshared, unsigned int value)
// 调用成功时返回0,失败返回-1
// 取出信号量 sem 里的值, 把值存入 sval 中
int sem_getvalue(sem_t *sem, int *sval)
// 有 1 个或多个信号阻塞, 返回 0;
// P 操作, 即申请资源. 执行
int sem_wait(sem_t *sem)
// sem > 0 : 减一立即返回
// sem == 0 : 睡眠, 等待 sem > 0, 唤醒在返回.
// 相当于 V 操作, 把 sem 加一. 等待
// 唤醒正在等待该信号量的任意进程. 安全, 可重入的.
int sem_post(sem_t *sem)
#include <semapthore.h>

// 对信号的清理
int sem_destroy(sem_t *sem)
// 调用成功时返回0,失败返回-1

现在可以做一个信号量控制的汽车载人的问题, 司机开车, 有乘客要下车就停车, 中途可能会有人上车, 当没有乘客的时侯就只能上人. 用信号量来完成.

/*************************************************************************
    > File Name: sem_init_1_汽车问题_实现线程的竞争与同步.cpp
    > Author: Function_Dou
    > Mail: NOT
    > Created Time: 2018年03月22日 星期四 17时20分56秒
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#include <semaphore.h>

// 车上人数
static int num;
// 信号量
static sem_t sem;
// 线程调用的函数
void *Boarding(void *board);
void *Get_off(void *off);

// 线程创建的函数
void Pthread_create(pthread_t *t, void *(*fun)(void *), void *arg);
/// write函数
int Write(const char *s);

int main()
{
    srand(0);
    num = 0;
    pthread_t thread_father, thread_child;
    // 初始化信号量
    sem_init(&sem, 0, 0);

    // 创建两个线程
    Pthread_create(&thread_father, Boarding, NULL);
    Pthread_create(&thread_child, Get_off, NULL);

    // 等待两个线程
    pthread_join(thread_father, NULL);
    pthread_join(thread_child, NULL);

    exit(0);
}

// 创建线程
void Pthread_create(pthread_t *t, void *(*fun)(void *), void *arg)
{
    if ((errno = pthread_create(t, NULL, fun, arg)) != 0)
    {
        fprintf(stderr, "pthread_create error : %s\n", strerror(errno));
        exit(1);
    }
}

// 创建写操作
int Write(const char *s)
{
    int n;
    n = strlen(s);
    if (write(STDOUT_FILENO, s, n) != n)
    {
        fprintf(stderr, "write error\n");
        exit(1);
    }

    return n;
}

// 上车
void *Boarding(void *board)
{
    static int t;
    // 上车总次数的生成
    t = rand() % 5 + 3;
    while (t--)
    {
        // 人数为 0 : sv == 0, 只能上车, 阻塞下车
        if (num == 0)
            sem_post(&sem);
        num = rand() % 2 + 1;
        Write("Board\n");
        usleep(1000 * 10);
    }

    // 解锁
    pthread_exit((void *)0);
}

void *Get_off(void *off)
{
    static int tt;
    // 下车次数的随机次数
    tt = rand() % 4 + 4;
    int n;
    while (tt--)
    {
        // 人数为 0. 下车阻塞, 只能上车
        if (num == 0)
            sem_wait(&sem);
        n = rand() % 5;
        num = num - n ? num - n : 0;
        Write("get off\n");
        //sleep(1);
        usleep(1000 * 10);
    }

    pthread_exit((void *)0);
}

这里写图片描述

使用 usleep() 函数进行短暂的暂停, 输出效果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值