首页 > 运维 > linux运维 > 正文

【Linux】巧妙运用<信号量>解决<水果放取问题>(思维导图&代码演示&思路解析)

雪夜
发布: 2025-06-20 12:28:25
原创
571人浏览过

linux环境下,巧妙运用信号量解决水果放取问题,这是一个有趣且实用的多线程同步问题。以下是该问题的思维导图、代码演示和思路解析。

【Linux】巧妙运用<信号量>解决<水果放取问题>(思维导图&代码演示&思路解析)

一. 信号量相关实验回顾

  1. 回顾:使用信号量实现线程互斥

    • 解析:利用信号量实现两个线程互斥输出1-10数字。
    • 传送门
  2. 回顾:巧妙运用信号量控制两个线程之间谁先执行

  3. 回顾:巧妙运用信号量实现线程间通信

二. 巧妙运用信号量解决水果放取问题(思维导图&代码演示&思路解析)

问题描述:一个盘子只能放一个水果,爸爸往里面放苹果,妈妈往里面放橘子,儿子专门等吃橘子,女儿专门等吃苹果。只要盘子空,爸爸或妈妈就可以往里面放水果;仅当盘子里有自己需要的水果时,儿子或女儿才可以取出吃。

设置三个信号量:

  • plate:初始值为1,优先执行,面向父母端。
  • appleReady:初始值为0,对应女儿等待苹果。
  • orangeReady:初始值为0,对应儿子等待橘子。

定义:

  • fruitOnPlate:表示盘子中是否有水果。
  • fruitType:表示盘子中水果的类型。
  • https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15bdefine APPLE 1 和 https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15bdefine ORANGE 2:表示水果类型。

信号量操作:

  • 信号量等待(P操作):sem_wait()
  • 信号量唤醒(V操作):sem_post()

【Linux】巧妙运用<信号量>解决<水果放取问题>(思维导图&代码演示&思路解析)

代码语言:C

运行次数:0

https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15binclude <stdio.h>
https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15binclude <stdlib.h>
https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15binclude <pthread.h>
https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15binclude <semaphore.h>
https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15binclude <unistd.h>

https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15bdefine APPLE 1
https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15bdefine ORANGE 2

// 表示放入水果
int fruitOnPlate = 0;
int fruitType = 0;

// 设置信号量
sem_t plate, appleReady, orangeReady;

void *father(void *arg) {
    while (1) {
        sem_wait(&plate);
        fruitOnPlate = 1;
        fruitType = APPLE;  // 放入苹果
        printf("Father put an apple.\n");
        sem_post(&appleReady);
        sleep(rand() % 10); // 睡眠随机时间
    }
}

void *mother(void *arg) {
    while (1) {
        sem_wait(&plate);
        fruitOnPlate = 1;
        fruitType = ORANGE;  // 放入橘子
        printf("Mother put an orange.\n");
        sem_post(&orangeReady);
        sleep(rand() % 10); // 睡眠随机时间
    }
}

void *son(void *arg) {
    while (1) {
        sem_wait(&orangeReady); // 等待橘子
        if (fruitType == ORANGE) {
            printf("Son ate an orange.\n");
            sem_post(&plate);
        }
    }
}

void *daughter(void *arg) {
    while (1) {
        sem_wait(&appleReady); // 等待苹果
        if (fruitType == APPLE) {
            printf("Daughter ate an apple.\n");
            sem_post(&plate);
        }
    }
}

int main() {
    pthread_t f_thread, m_thread, son_thread, dau_thread;

    sem_init(&plate, 0, 1);
    sem_init(&appleReady, 0, 0);
    sem_init(&orangeReady, 0, 0);

    pthread_create(&f_thread, NULL, father, NULL);
    pthread_create(&m_thread, NULL, mother, NULL);
    pthread_create(&son_thread, NULL, son, NULL);
    pthread_create(&dau_thread, NULL, daughter, NULL);

    pthread_join(f_thread, NULL);
    pthread_join(m_thread, NULL);
    pthread_join(son_thread, NULL);
    pthread_join(dau_thread, NULL);

    sem_destroy(&plate);
    sem_destroy(&appleReady);
    sem_destroy(&orangeReady);

    return 0;
}
登录后复制

通过以上代码,我们可以看到信号量的巧妙运用,确保了水果放取的正确性和同步性。

以上就是【Linux】巧妙运用<信号量>解决<水果放取问题>(思维导图&代码演示&思路解析)的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号