1.14 互斥与同步

1.思维导图

2.有一个隧道,长1000m,有一辆高铁,每秒100米;有一辆快车,每秒50米;要求模拟这两列火车通过隧道的场景。

1>程序代码:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;#define LENGTH 1000  //隧道的长度typedef struct
{const char *name;int speed;int position;
}Train;void *simulate_train(void *arg) 
{Train *train = (Train *)arg;while (train->position < LENGTH) {train->position += train->speed;  // 火车前进if (train->position > LENGTH)train->position = LENGTH;  // 防止超出隧道长度printf("%s正在 %d 米处\n", train->name, train->position);sleep(1); }printf("%s已出隧道\n", train->name);return NULL;
}
int main(int argc, const char *argv[])
{pthread_t high_speed_thread, fast_train_thread;// 初始化两列火车Train high_speed_train = {"高铁", 100, 0};Train fast_train = {"快车", 50, 0};// 创建线程模拟火车运行pthread_create(&high_speed_thread, NULL, simulate_train, &high_speed_train);pthread_create(&fast_train_thread, NULL, simulate_train, &fast_train);// 等待两列火车完成运行pthread_join(high_speed_thread, NULL);pthread_join(fast_train_thread, NULL);printf("两列火车都已出隧道\n");return 0;
}
2>运行效果:

3.有一条隧道,长1000m,有一辆高铁,每秒100米;有一辆快车,每秒50米;有一辆慢车,每秒25米;模拟它们通过隧道的场景,要求:高铁最先过隧道,快车其次,慢车最后。

1>程序代码:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;#define LENGTH 1000  // 隧道长度typedef struct 
{const char *name;  int speed;         int position;      int order;        //火车顺序 
} Train;pthread_mutex_t mutex;
pthread_cond_t cond;
int current_order = 1; // 当前允许进入隧道的火车顺序void *simulate_train(void *arg) 
{Train *train = (Train *)arg;pthread_mutex_lock(&mutex);while (train->order != current_order) {pthread_cond_wait(&cond, &mutex);}pthread_mutex_unlock(&mutex);while (train->position < LENGTH) {train->position += train->speed;  // 火车前进if (train->position > LENGTH) {train->position = LENGTH;  // 防止超出隧道长度}printf("%s正在%d米处\n", train->name, train->position);sleep(1);  // 模拟每秒前进}printf("%s已出隧道\n", train->name);pthread_mutex_lock(&mutex);current_order++;pthread_cond_broadcast(&cond);pthread_mutex_unlock(&mutex);return NULL;
}int main(int argc, const char *argv[])
{pthread_t id1,id2,id3;// 初始化三列火车Train high_speed_train = {"高铁", 100, 0, 1};Train fast_train = {"快车", 50, 0, 2};Train slow_train = {"慢车", 25, 0, 3};// 创建线程模拟火车运行pthread_create(&id1, NULL, simulate_train, &high_speed_train);pthread_create(&id2, NULL, simulate_train, &fast_train);pthread_create(&id3, NULL, simulate_train, &slow_train);// 等待所有线程完成pthread_join(id1, NULL);pthread_join(id2, NULL);pthread_join(id3, NULL);printf("所有火车都已出隧道\n");return 0;
}
2>运行效果:

4.使用条件变量实现一个生产消费模型(pv模型)。生产者线程:每秒生成2个苹果;消费者线程:每3秒消费5~9个苹果。

1>程序代码:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;#define MAX_APPLES 100  //定义仓库最大容量
int apple_count = 0; //当前苹果的数量//使用条件变量
pthread_mutex_t mutex;
pthread_cond_t cond;//生产者线程
void* producer(void* arg)
{while(1){pthread_mutex_lock(&mutex);//生产苹果if(apple_count+2 <= MAX_APPLES){apple_count += 2;printf("生产者生产2个苹果,目前苹果总数为:%d\n",apple_count);}else{printf("仓库已满,生产者等待生产中...\n");}//唤醒消费者线程pthread_cond_signal(&cond);pthread_mutex_unlock(&mutex);sleep(1);}return NULL;
}//消费者线程
void* consumer(void* arg)
{while(1){pthread_mutex_lock(&mutex);int consumption = rand() % 5 + 5;  //随机消费5~9个苹果while(apple_count < consumption){printf("消费者想要消费 %d 个苹果,但是只剩 %d 个苹果,等待...\n",consumption,apple_count);pthread_cond_wait(&cond,&mutex);  //等待生产者生产}//消费苹果apple_count -= consumption;printf("消费者消费 %d 个苹果,剩余苹果: %d个\n",consumption,apple_count);pthread_mutex_unlock(&mutex);sleep(3);}return NULL;
}int main(int argc, const char *argv[])
{pthread_t id_producer,id_consumer;pthread_create(&id_producer,0,producer,0);pthread_create(&id_consumer,0,consumer,0);pthread_join(id_producer,NULL);pthread_join(id_consumer,NULL);return 0;
}
2>运行效果:

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

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

相关文章

solidity基础 -- 枚举

在智能合约开发领域&#xff0c;Solidity语言因其简洁高效而被广泛使用。其中&#xff0c;枚举&#xff08;enum&#xff09;作为一种特殊的数据类型&#xff0c;为合约的状态管理提供了极大的便利。本文将通过一个具体的Solidity合约示例&#xff0c;深入探讨枚举的定义、使用…

14.STM32F407ZGT6-SPI

参考&#xff1a; 1.正点原子 前言&#xff1a; SPI一般用在中高速的外围器件上&#xff0c;如FLASH, GPS模块等。很常用的一种通信方式&#xff0c;学习总结很有必要。 1.SPI的概念及时序。 2.通过SPI操作Flash芯片。 37.1 SPI 及 NOR Flash 介绍 37.1.1 SPI 介绍 我们将从…

基于SpringBoot的中华诗词赏析文化交流平台

作者&#xff1a;计算机学姐 开发技术&#xff1a;SpringBoot、SSM、Vue、MySQL、JSP、ElementUI、Python、小程序等&#xff0c;“文末源码”。 专栏推荐&#xff1a;前后端分离项目源码、SpringBoot项目源码、Vue项目源码、SSM项目源码、微信小程序源码 精品专栏&#xff1a;…

44.ComboBox的数据绑定 C#例子 WPF例子

固定最简步骤&#xff0c;包括 XAML&#xff1a; 题头里引入命名空间 标题下面引入类 combobox绑定资源属性和选择属性&#xff0c;block则绑定和combobox一样的选择属性 C#&#xff1a; 通知的类&#xff0c;及对应固定的任务 引入字段 引入属性 其中资源是只读的 选…

Flink类加载机制详解

1. 总览 在运行Flink应用时,它会加载各种类,另外我们用户代码也会引入依赖,由于他们依赖版本以及加载顺序等不同,就可能会导致冲突,所以很要必要了解Flink是如何加载类的。 根据加载的来源的不同,我们可以将类分为三种: Java Classpath:Java类路径下,这是Java通用的…

水下通信:特点、主要应用与典型系统

引言 海洋覆盖了地球表面的71%&#xff0c;是地球上最大的生态系统。随着人类对海洋资源的不断探索和开发&#xff0c;水下通信成为了连接水下设备与陆上控制中心、实现水下数据交换和远程监控的关键技术。水下通信不仅在水下科研、资源开发、环境监测、水下救援等领域发挥着重…

GPU算力平台|在GPU算力平台部署Qwen-2通义千问大模型的教程

文章目录 一、GPU平台介绍算力平台概述 二、人工智能应用开发需要GPU算力平台GPU算力原理账号注册流程Qwen-2通义千问大模型的部署登录/注册选择SettingsURL配置选择模型部署完成进行问答 一、GPU平台介绍 算力平台概述 GPU算力平台是一个专注于GPU加速计算的专业云服务平台&…

“深入浅出”系列之设计模式篇:(0)什么是设计模式

设计模式六大原则 1. 单一职责原则&#xff1a;一个类或者一个方法只负责一项职责&#xff0c;尽量做到类的只有一个行为原因引起变化。 核心思想&#xff1a;控制类的粒度大小&#xff0c;将对象解耦&#xff0c;提高其内聚性。 2. 开闭原则&#xff1a;对扩展开放&#xf…

微信小程序集成Vant Weapp移动端开发的框架

什么是Vant Weapp Vant 是一个轻量、可靠的移动端组件库&#xff0c;于 2017 年开源。 目前 Vant 官方提供了 Vue 2 版本、Vue 3 版本和微信小程序版本&#xff0c;并由社区团队维护 React 版本和支付宝小程序版本。 官网地睛&#xff1a;介绍 - Vant Weapp (vant-ui.gith…

【C++】:浅析 std::optional

std::optional 是 C17 引入的一个标准库特性&#xff0c;提供了一种简单的方式来表示一个可能存在或不存在的值。它可以用于替代指针或其他机制&#xff0c;以更安全和更清晰的方式处理可选值。 1. 基本概念 std::optional<T> 是一个模板类&#xff0c;其中 T 是存储的…

图形和动画本地化

图形和动画本地化是多媒体改编的一个关键方面&#xff0c;需要对技术技能和文化细微差别有深入的理解。当由母语人士和设计师进行时&#xff0c;这一过程达到了自动化系统通常无法复制的真实性和相关性水平。 本土专业人士对文化偏好、象征主义和视觉美学有着固有的理解&#…

浅谈云计算06 | 云管理系统架构

云管理系统架构 一、云管理系统架构&#xff08;一&#xff09;远程管理系统&#xff08;二&#xff09;资源管理系统&#xff08;三&#xff09;SLA 管理系统&#xff08;四&#xff09;计费管理系统 二、安全与可靠性保障&#xff08;一&#xff09;数据安全防线&#xff08;…

SpringBoot 基础学习

对于SpringBoot的了解&#xff0c;在初学者的角度看来&#xff0c;它是一种工具&#xff0c;用于简化一个Spring项目的初始搭建和开发过程。 1 入门案例 1.1 项目的创建 有四种方法创建&#xff0c;可以通过idea快捷创建&#xff0c;Spring的官网创建&#xff0c;阿里云创建&am…

latex 中页边距和字体大小以及行间距怎么修改

在 LaTeX 中修改页边距、字体大小和行间距可以通过调整文档类选项或使用特定的宏包来实现。 以下是详细的方法&#xff1a; 修改页边距 使用 geometry 宏包&#xff1a; 这是最常用的方法&#xff0c;geometry 宏包允许你非常灵活地设置页面尺寸和边距。你可以通过在导言区&am…

基于springboot+vue的洪涝灾害应急信息管理系统设计与实现

开发语言&#xff1a;Java框架&#xff1a;springbootJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包&#xff1a;…

QTreeWidget QTreeWidgetItem

QTreeWidgetItem 是 Qt 框架中用于在 QTreeWidget 中表示树形结构中每个节点的类。它是 QTreeWidget 的一部分&#xff0c;允许您创建和管理层次结构的数据展示。 QTreeWidgetItem 用于表示树形结构中的单个节点。 添加子节点&#xff1a; 可以通过 addChild() 方法向节点添加…

基于springboot果蔬供应链信息管理平台

基于Spring Boot的果蔬供应链信息管理平台是一种集成了先进信息技术和果蔬供应链管理理念的综合性系统。 一、背景与意义 随着人们生活水平的提高和对健康饮食的重视&#xff0c;果蔬市场需求不断增长。然而&#xff0c;果蔬供应链涉及多个环节&#xff0c;包括种植、采摘、加…

Python使用socket实现简易的http服务

在接触的一些项目中&#xff0c;有时为了方便可视化一些服务状态&#xff08;请求数很少&#xff09;&#xff0c;那么很容易想到使用http服务来实现。但开源的web后端框架&#xff0c;例如flask&#xff0c;fastapi&#xff0c;django等略显沉重&#xff0c;且使用这些框架会有…

25/1/13 嵌入式笔记 继续学习Esp32

PWM&#xff08;Pulse Width Modulation&#xff0c;脉宽调制&#xff09; 是一种通过快速切换高低电平来模拟中间电压值的技术。它广泛应用于控制 LED 亮度、电机速度、音频生成等场景。 analogWrite函数:用于在微控制器&#xff08;如 Arduino&#xff09;上生成模拟信号。 …

jupyter notebook练手项目:线性回归——学习时间与成绩的关系

线性回归——学习时间与学习成绩的关系 第1步&#xff1a;导入工具库 pandas——数据分析库&#xff0c;提供了数据结构&#xff08;如DataFrame和Series&#xff09;和数据操作方法&#xff0c;方便对数据集进行读取、清洗、转换等操作。 matplotlib——绘图库&#xff0c;p…