3个经典线程同步问题

news2024/7/3 15:50:49

生产者消费者问题

问题描述

系统中有一组生产者进程和一组消费者进程,生产者进程每次生产一个产品放入缓冲区,消费者进程每次从缓冲区中取出一个产品并使用。生产者、消费者共享一个初始为空、大小为n的缓冲区

伪码描述

semaphore mutex = 1;//互斥信号量,实现对缓冲区的互斥访问
semaphore empty = n;//同步信号量,表示空闲缓冲区的数量
semaphore full = 0; //同步信号量,表示产品的数量,也即非空缓冲区的数量

producer () {
	while (1) {
		生产一个产品;
		P(empty);         //消耗一个空闲缓冲区
		P(mutex);         //实现互斥是在同一进程中进行一对PV操作
		把产品放入缓冲区;
		V(mutex);
		V(full);          //增加一个产品
	}
}
consumer () {
	while(1){
		P(full);          //消耗一个产品(非空闲缓冲区
		P(mutex);
		从缓冲区取出一个产品;
		V(mutex);
		V(empty);        //增加一个空闲缓冲区
		使用产品;
	}
}

C语言代码实现

//producer-consumer.c
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>

#define NUM 5

volatile int arr[NUM] = {-1, - 1, -1, -1, -1};  //the shared buffer
volatile int wp;    //write position
volatile int rp;    //read position

sem_t empty;
sem_t full;
pthread_mutex_t lock;

void* producer(void* arg) {
    while(1) {
        int t = rand() % 100;
        sem_wait(&empty);
        pthread_mutex_lock(&lock);
        arr[wp] = t;
        wp = (wp + 1) % NUM;
        printf("\033[32mproducer %ld: t = %d\033[0m\n", (long)arg, t);
        pthread_mutex_unlock(&lock);
        sem_post(&full);
        sleep(1);
    }
    return 0;
}

void* consumer(void* arg) {
    while(1) {
        int t = 0;
        sem_wait(&full);
        pthread_mutex_lock(&lock);
        t = arr[rp];
        arr[rp] = -1;
        rp = (rp + 1) % NUM;
        printf("\033[31mconsumer %ld: t = %d\033[0m\n", (long)arg, t);
        pthread_mutex_unlock(&lock);
        sem_post(&empty);
        sleep(1);
    }
    return 0;
}

int main() {
    sem_init(&empty, 0, NUM);
    sem_init(&full, 0, 0);
    pthread_mutex_init(&lock, NULL);
    srand(time(NULL));
    wp = rp = 0;

    pthread_t tid[8];
    long i = 0;
    for(; i < 4; ++i) {     //4 producers
        pthread_create(tid + i, NULL, producer, (void*)i);
    }
    for(; i < 8; ++i) {     //4 consumers
        pthread_create(tid + i, NULL, consumer, (void*)i);
    }
    for(i = 0; i < 8; ++i) {
        pthread_join(tid[i], NULL);
    }
    sem_destroy(&empty);
    sem_destroy(&full);
    pthread_mutex_destroy(&lock);
    return 0;
}

读者写者问题

问题描述

有读者和写者两组并发进程,共享一个文件,当两个或两个以上的读进程同时访问共享数据时不会产生副作用,但若某个写进程和其他进程(读进程或写进程)同时访问共享数据时则可能导致数据不一致的错误。因此要求:

  1. 允许多个读者可以同时对文件执行读操作
  2. 只允许一个写者往文件中写信息
  3. 任一写者在完成写操作之前不允许其他读者或写者工作
  4. 写者执行写操作前,应让已有的读者和写者全部退出

借助POSIX线程库提供的读写锁,这个问题很容易用代码实现(当然采用信号量也是可以实现的)

C语言代码实现

//reader-writer.c
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int n = 0;
pthread_rwlock_t rwlock;

void* reader(void* arg) {
    while (1) {
        int t = 0;
        pthread_rwlock_rdlock(&rwlock);
        t = n;
        printf("\033[32mreader %ld: n = %d\033[0m\n", (long)arg, n);
        pthread_rwlock_unlock(&rwlock);
        sleep(1);
    }
    return 0;
}

void* writer(void* arg) {
    while (1) {
        int t = rand() % 100000;
        pthread_rwlock_wrlock(&rwlock);
        n = t;
        printf("\033[31mwriter %ld: n = %d\033[0m\n", (long)arg, n);
        pthread_rwlock_unlock(&rwlock);
        sleep(1);
    }
    return 0;
}

int main() {
    pthread_t tid[8];
    pthread_rwlock_init(&rwlock, NULL);
    srand(time(NULL));
    long i = 0;
    for (; i < 6; ++i) {  // 6 readers
        pthread_create(tid + i, NULL, reader, (void*)i);
    }
    for (; i < 8; ++i) {  // 2 writers
        pthread_create(tid + i, NULL, writer, (void*)i);
    }
    for (i = 0; i < 8; ++i) {
        pthread_join(tid[i], NULL);
    }
    pthread_rwlock_destroy(&rwlock);
    return 0;
}

哲学家进餐问题

问题描述

一张圆桌上坐着 5 名哲学家,桌子上每两个哲学家之间摆了一根筷子,桌子的中间是一碗米饭,如图所示:

在这里插入图片描述

哲学家们倾注毕生精力用于思考和进餐,哲学家在思考时,并不影响他人。只有当哲学家饥饿的时候,才试图拿起左、右两根筷子(一根一根拿起)。如果筷子已在他人手上,则需等待。饥饿的哲学家只有同时拿到了两根筷子才可以开始进餐,当进餐完毕后,放下筷子继续思考

伪码实现

首先看一种会导致死锁的native实现:

sem chopstick[5]={1,1,1,1,1}//5根筷子
Pi()
{
   while(1)
   {
        P(chopstick[i]);//取左边筷子
        P(chopstick[(i+1)%5]);//取右边筷子
        eat;//吃饭
        V(chopstick[i]);//放左边筷子
        V(chopstick[(i+1)%5]);//放右边筷子
        think;//思考
   }
}

一种导致死锁的情况是:当5位哲学家都执行了P(chopstick[i]);,那么他们就会因为都拿不起右边的筷子而死锁

解决死锁的方式有3种:

  1. 最多允许四个哲学家同时进餐
  2. 仅当一个哲学家左右两边的筷子都可用时才允许他抓起筷子,破坏请求条件
  3. 对哲学家顺序编号,要求奇数号哲学家先抓左边的筷子,然后再抓他右边的筷子,而偶数号哲学家刚好相反,破坏循环等待

对应的伪码实现分别为:

  1. 最多允许四个哲学家同时进餐:
sem cho[5]={1,1,1,1,1}//5根筷子
sem count = 4;//设置一个count最多有四个哲学家可以进来

void pi(int i)
{
    while(1)
    {
        P(count);//请求进入房间进餐,获取资源,当count为0的时候,不能允许哲学家再进来了,阻塞
        P(cho[i]);//请求左手边的筷子
        P(cho[(i+1)%5]);//请求右手边的筷子
        eat();
        V(cho[i]); //释放左手边的筷子
        V(cho[(i+1)%5]);//释放右手边的筷子   
        V(count);//退出房间释放资源
    }
}
  1. 仅当一个哲学家左右两边的筷子都可用时才允许他抓起筷子,也就是两只筷子必须同时拿起:
sem cho[5]={1,1,1,1,1}//5根筷子
sem mutex=1;//允许拿筷子
void pi(int i)
{
   while(1)
   {
        P(mutex);//允许拿筷子,同一时刻只允许一个哲学家获取
        P(cho[i]);//请求左手边的筷子
        P(cho[(i+1)%5]);//请求右手边的筷子
        V(mutex);//拿完筷子,释放资源
        eat();
        V(cho[i]); //释放左手边的筷子
        V(cho[(i+1)%5]);//释放右手边的筷子
   }
}
  1. 对哲学家顺序编号,要求奇数号哲学家先抓左边的筷子,然后再抓他右边的筷子,而偶数号哲学家刚好相反:
sem cho[5]={1,1,1,1,1}//5根筷子
void pi(int i)
{
   while(1)
   {
        if(i%2==0)//偶数哲学家,先右后左
        {
             P(cho[(i+1)%5]);//请求右手边的筷子
             P(cho[i]);//请求左手边的筷子
             eat();
             V(cho[(i+1)%5]);//释放右手边的筷子    
             V(cho[i]); //释放左手边的筷子   
        }
        else//奇数哲学家,先左后右
        {
             P(cho[i]);//请求左手边的筷子
             P(cho[(i+1)%5]);//请求右手边的筷子
             eat();
             V(cho[i]); //释放左手边的筷子
             V(cho[(i+1)%5]);//释放右手边的筷子  
        }
   }
}

C语言代码实现

  1. 最多允许4个哲学家同时进餐:
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

sem_t chopsticks[5];
sem_t max_dining_num;

void* philosopher(void* arg) {
    long i = (long)arg;
    while (1) {
        sem_wait(&max_dining_num);
        sem_wait(&chopsticks[i]);
        sem_wait(&chopsticks[(i + 1) % 5]);
        printf("philosopher %ld: eating\n", i);
        printf("philosopher %ld: finished\n", i);
        sem_post(&chopsticks[(i + 1) % 5]);
        sem_post(&chopsticks[i]);
        sem_post(&max_dining_num);
    }
    return 0;
}

int main() {
    long i = 0;
    for (; i < 5; ++i) {
        sem_init(chopsticks + i, 0, 1);
    }
    sem_init(&max_dining_num, 0, 4);

    pthread_t tid[5];
    for (i = 0; i < 5; ++i) {
        pthread_create(tid + i, NULL, philosopher, (void*)i);
    }

    for (i = 0; i < 5; ++i) {
        pthread_join(tid[i], NULL);
    }

    for(i = 0; i < 5; ++i) {
        sem_destroy(&chopsticks[i]);
    }
    sem_destroy(&max_dining_num);
    return 0;
}
  1. 强制哲学家必须同时拿起左右两只筷子:
//dining-philosophers-2.c
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

sem_t chopsticks[5];
pthread_mutex_t pickup_chopsticks;  //enable pickup chopsticks

void* philosopher(void* arg) {
    long i = (long)arg;
    while (1) {
        pthread_mutex_lock(&pickup_chopsticks);
        sem_wait(&chopsticks[i]);
        sem_wait(&chopsticks[(i + 1) % 5]);
        pthread_mutex_unlock(&pickup_chopsticks);
        printf("philosopher %ld: eating\n", i);
        printf("philosopher %ld: finished\n", i);
        sem_post(&chopsticks[(i + 1) % 5]);
        sem_post(&chopsticks[i]);
    }
    return 0;
}

int main() {
    long i = 0;
    for (; i < 5; ++i) {
        sem_init(chopsticks + i, 0, 1);
    }
    pthread_mutex_init(&pickup_chopsticks, NULL);

    pthread_t tid[5];
    for (i = 0; i < 5; ++i) {
        pthread_create(tid + i, NULL, philosopher, (void*)i);
    }

    for (i = 0; i < 5; ++i) {
        pthread_join(tid[i], NULL);
    }

    for(i = 0; i < 5; ++i) {
        sem_destroy(&chopsticks[i]);
    }
    pthread_mutex_destroy(&pickup_chopsticks);
    return 0;
}
  1. 奇数号哲学家先拿左手边的筷子,偶数号哲学家先拿右手边的筷子:
//dining-philosophers-3.c
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

sem_t chopsticks[5];

void* philosopher(void* arg) {
    long i = (long)arg;
    while (1) {
        if (i % 2 == 0) {
            sem_wait(&chopsticks[i]);
            sem_wait(&chopsticks[(i + 1) % 5]);
            printf("philosopher %ld: eating\n", i);
            printf("philosopher %ld: finished\n", i);
            sem_post(&chopsticks[(i + 1) % 5]);
            sem_post(&chopsticks[i]);
        } else {
            sem_wait(&chopsticks[(i + 1) % 5]);
            sem_wait(&chopsticks[i]);
            printf("philosopher %ld: eating\n", i);
            printf("philosopher %ld: finished\n", i);
            sem_post(&chopsticks[i]);
            sem_post(&chopsticks[(i + 1) % 5]);
        }
    }
    return 0;
}

int main() {
    long i = 0;
    for (; i < 5; ++i) {
        sem_init(chopsticks + i, 0, 1);
    }

    pthread_t tid[5];
    for (i = 0; i < 5; ++i) {
        pthread_create(tid + i, NULL, philosopher, (void*)i);
    }

    for (i = 0; i < 5; ++i) {
        pthread_join(tid[i], NULL);
    }

    for (i = 0; i < 5; ++i) {
        sem_destroy(&chopsticks[i]);
    }
    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/479884.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Zabbix服务端监控目标主机的Web服务(网站的访问延迟)

zabbix服务端和目标主机的部署见上一篇文章&#xff1a; http://t.csdn.cn/XD5Hc Zabbix服务端监控目标主机 服务端启动zabbix服务后&#xff0c;在浏览器上访问&#xff1a;http&#xff1a;//IP/zabbix 1.创建主机群主&#xff08;名字自定义&#xff09; 2.创建主机 主…

javaEE基于SSh学生选课系统

设计内容1. 搜集相关资料、作出功能需求分析&#xff1b; 2. 各个功能模块的基本功能大体如下&#xff1a; (1). 管理员模块 包括个人中心、专业管理、班级管理、课程管理、教师管理、选课管理。&#xff0e; (2).教师模块 包括个人中心、课程信息、出勤管理、成绩管理。 (3)…

大数据-玩转数据-netcat

Netcat&#xff08;简称nc&#xff09;是一款强大的命令行网络工具&#xff0c;用来在两台机器之间建立TCP/UDP连接&#xff0c;并通过标准的输入输出进行数据的读写。 一、Windows 下载安装 netcat(nc)命令 1、netcat(nc)下载地址&#xff1a; https://eternallybored.org/…

《程序员面试金典(第6版)》面试题 16.08. 整数的英语表示

题目描述 给定一个整数&#xff0c;打印该整数的英文描述。 示例 1: 输入: 123输出: “One Hundred Twenty Three” 示例 2: 输入: 12345输出: “Twelve Thousand Three Hundred Forty Five” 示例 3: 输入: 1234567输出: “One Million Two Hundred Thirty Four Thousand…

Kali 更换源(超详细,附国内优质镜像源地址)

1.进入管理员下的控制台。 2. 输入密码后点击“授权”。 3.在控制台内输入下面的内容。 vim /etc/apt/sources.list 4.敲击回车后会进入下面的页面。 5.来到这个页面后的第一部是按键盘上的“i”键&#xff0c;左下角出现“插入”后说明操作正确。 6.使用“#”将原本的源给注释…

武汉大学惯性导航课程合集【2021年秋】1.2 惯性器件的误差和标定

前提平台惯导NED与本地对齐&#xff0c;body系和navigation对齐。地表IMU感受到的是 朝天上的力【0&#xff0c;0&#xff0c;-9.8】和 赤道的【15deg/hr&#xff0c;0&#xff0c;0】或者北极 【0&#xff0c;0&#xff0c;-15deg/hr】或者【15cos纬度&#xff0c;0&#xff0…

「STM32入门」USART串口通信

通信 通信的目的&#xff1a;将一个设备的数据传送到另一个设备&#xff0c;扩展硬件系统 通信协议&#xff1a;制定通信的规则&#xff0c;通信双方按照协议规则进行数据收发 STM32常见的通信协议 本文将介绍USART 概念解释 TX、RX分别是Transmit和Receive的缩写&#xff0c…

一文把 JavaScript 中的 this 聊得明明白白

文章目录 1.this 是什么&#xff1f;2.this的指向2.1 全局上下文的 this 指向2.2 函数&#xff08;普通函数&#xff09;上下文中的 this 指向2.3 事件处理程序中的 this 指向2.4 以对象的方式调用时 this 的指向2.5 构造函数中的 this 指向2.6 在 类上下文中 this 的指向。2.7…

开源企业资源规划ERPNext的安装

往常节假日&#xff0c;都是呆在家里看别人堵&#xff0c;这回老苏也出门凑了个热闹&#xff0c;28号早上 7 点半出的门 2 点半往回走的 一天啥也没干&#xff0c;就开了 7 个小时的车去舅舅家蹭了顿饭。还别说&#xff0c;那个田园鸡味道是真不错。 车很久没开了&#xff0c;…

(详解)vue中实现 ‘换肤 / 主题切换’ 功能的三种方式

目录 一、背景 二、实现思路 方法1&#xff1a;定义全局的CSS变量 方法2&#xff1a;切换已定义好的css文件 方法3&#xff1a;切换顶级CSS类名 (需使用css处理器,如sass、less等) 一、背景 在我们开发中我们会遇到像是需要切换程序风格、主题切换啦这种应用场景。 二、实现…

JavaScript通过函数异常处理来输入圆的半径,输出圆的面积的代码

以下为实现通过函数异常处理来输入圆的半径&#xff0c;输出圆的面积的代码和运行截图 目录 前言 一、通过函数异常处理来输入圆的半径&#xff0c;输出圆的面积 1.1 运行流程及思想 1.2 代码段 1.3 JavaScript语句代码 1.4 运行截图 前言 1.若有选择&#xff0c;您可以…

【RPA开发】pymongo 使用教程

实际开发时抓取到的诸多数据如何保存是一个关键问题&#xff0c;MongoDB 相比传统关系型数据库&#xff08;比如mysql&#xff09;来说灵活度更高&#xff0c;爬虫时字段格式及数量很可能会随着需求或实际数据的变动而改变&#xff0c;因此 MongoDB 作为数据库来说最合适不过了…

2023年华中杯选题人数公布

2023年华中杯选题人数公布 经过一晚上代码的编写&#xff0c;论文的写作&#xff0c;C题完整版论文已经发布&#xff0c; 注&#xff1a;蓝色字体为说明备注解释字体&#xff0c;不能出现在大家的论文里。黑色字体为论文部分&#xff0c;大家可以根据红色字体的注记进行摘抄。…

【STM32】知识补充 分频技术深度解析: 原理、应用与实现方法

【STM32】知识补充 分频技术深度解析: 原理、应用与实现方法 概述分频概念分频原理技术器分频器触发器分频器模数计数器分频器 分频应用微控制器时钟分频通信系统中的频率合成数字电路设计中的计时与同步 分频实现方法硬件分频器软件分频器 案例总结 概述 分频 (Frequency Div…

c#笔记-创建一个项目

创建一个项目 创建控制台程序 在你安装完成Visual Studio后打开它&#xff0c;你会的到一个启动窗口 点击创建新项目&#xff0c;选择右上角c#的没有Framework的控制台应用。 项目名称&#xff0c;位置自己随意。 目标框架选择NET7.0。 项目创建完成后应该你的界面应该类似…

nvdec与vaapi与vdpau的关系

nvdec/vaapi/vdpau的关系 NVDEC&#xff1a;nvidia video decoder。 英伟达官网中有关video codec SDK的介绍&#xff0c;其中有头文件、开发API文档等。 Nvidia Video SDK中提供了NVDEC、NVENC&#xff0c;其中NVDEC以前也叫做NVCUVID&#xff08;不知道NVDEC的开发API中很…

【9.HTML入门知识-其他知识补充】

其他知识补充 1.使用Web字体和图标1.1 web fonts兼容性写法1.2 字体图标1.2.1 字体图标的使用 2.精灵图 CSS Sprite2.1 精灵图的使用 3.置鼠标指针&#xff08;光标&#xff09;样式cusor4.居中方案4.1 使用绝对定位元素居中 1.使用Web字体和图标 1.1 web fonts兼容性写法 1.2 …

软件开发团队的护网低成本应对方案

主题&#xff1a; 1、攻击方技术手段说明&#xff0c;结合攻击队手段重点关注的防御点介绍&#xff1b; 2、防守方&#xff08;软件开发团队&#xff09;的低成本应对思路&#xff1b;系统是如何被攻破的 攻防演练&#xff08;APT&#xff09;攻击路径 未知攻&#xff0c;焉知…

IGH EtherCAT主站应用层代码开发:控制驱动电机

1、安装IGH EtherCAT主站 Ubuntu18.04环境下安装igH EtherCAT Master 2、查询从站配置信息 连接从站通过网线连接主站与从站 启动主站打开终端,输入: sudo /etc/init.d/ethercat star 显示Starting EtherCAT master 1.5.2 done则说明成功。 查询从站列表终端输入: eth…

【C++】 list-map 链表与映射表的简单使用

目录 list 链表 定义链表&#xff0c;并在首、尾添加、删除元素 迭代器遍历链表 任意位置插入或删除 获取首尾节点中元素的值 使用增强的范围for循环进行遍历链表 其他常见的函数 map 映射表 定义map 添加 使用函数插入元素 迭代器遍历map 修改 删除 使用增强的范…