2024.2.22

news2024/10/5 15:35:11

将互斥机制的代码实现

#include<myhead.h>
int num=7;
pthread_mutex_t mutex;//创建互斥锁变量
void *task1(void *arg)
{
	printf("task1:\n");
	//获取锁资源
	pthread_mutex_lock(&mutex);
	num=77777;
	sleep(2);
printf("task1:num=%d\n",num);
//释放锁资源
pthread_mutex_unlock(&mutex);
}
void *task2(void *arg)
{
	printf("task2:\n");
	//获取锁资源
	pthread_mutex_lock(&mutex);
	num=num+1;
	sleep(1);
printf("task2:num=%d\n",num);
//释放锁资源
pthread_mutex_unlock(&mutex);
}
int main(int argc, const char *argv[])
{
	//初始化互斥锁
	pthread_mutex_init(&mutex,NULL);
	//创建线程
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,task1,NULL)!=0)
	{
		printf("tid1 creat error");
		return 0;
	}
	if(pthread_create(&tid2,NULL,task2,NULL)!=0)
	{
		printf("tid2 creat error");
		return 0;
	}
	//回收线程资源
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	//销毁锁资源
	pthread_mutex_destroy(&mutex);

return 0;
}

将无名信号量的代码实现重新敲一遍

#include<myhead.h>
sem_t sem1;//创建无名信号量1

void *task1(void *arg)
{
	int num=5;
	while(num--)
	{
		printf("我生产了\n");
	sem_post(&sem1);//释放资源
	}
	pthread_exit(NULL);//退出线程
}
void *task2(void *arg)
{
	int num=5;
	while(num--)
	{
		//申请资源
		sem_wait(&sem1);
			printf("我消费了\n");
		
	}
	pthread_exit(NULL);//退出线程
}
int main(int argc, const char *argv[])
{
	//初始化无名信号量
	sem_init(&sem1,0,0);
	pthread_t pid1,pid2;
	if(pthread_create(&pid1,NULL,task1,NULL)!=0)
	{
		printf("pid1 create error\n");
		return 0;
	}
	if(pthread_create(&pid2,NULL,task2,NULL)!=0)
	{
		printf("pid2 create error\n");
		return 0;
	}
	pthread_join(pid1,NULL);
	pthread_join(pid2,NULL);
	sem_destroy(&sem1);
return 0;
}

将条件变量的代码实现重新敲一遍

#include<myhead.h>
pthread_cond_t cond;
pthread_mutex_t mutex;
void *task1(void *arg)
{
	int num=5;
	while(num--)
	{
		sleep(1);
		printf("%#lx:生产了一个\n",pthread_self());
		pthread_cond_signal(&cond);
	}
	pthread_exit(NULL);
}
void *task2(void *arg)
{
	pthread_mutex_lock(&mutex);
	pthread_cond_wait(&cond,&mutex);
		printf("%#lx:消费了一个\n",pthread_self());
		pthread_mutex_unlock(&mutex);
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	//初始化条件变量
	pthread_cond_init(&cond,NULL);
	//初始化互斥锁
	pthread_mutex_init(&mutex,NULL);
	//创建线程
	pthread_t tid1,tid2,tid3,tid4,tid5,tid6;
	if(pthread_create(&tid1,NULL,task1,NULL)!=0)
	{
	printf("tid1 create error\n");
	return 0;
	}
if(pthread_create(&tid2,NULL,task2,NULL)!=0)
	{
	printf("tid1 create error\n");
	return 0;
	}
if(pthread_create(&tid3,NULL,task2,NULL)!=0)
	{
	printf("tid1 create error\n");
	return 0;
	}
if(pthread_create(&tid4,NULL,task2,NULL)!=0)
	{
	printf("tid1 create error\n");
	return 0;
	}
if(pthread_create(&tid5,NULL,task2,NULL)!=0)
	{
	printf("tid1 create error\n");
	return 0;
	}
if(pthread_create(&tid6,NULL,task2,NULL)!=0)
	{
	printf("tid1 create error\n");
	return 0;
	}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_join(tid4,NULL);
pthread_join(tid5,NULL);
pthread_join(tid6,NULL);
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);

return 0;
}

将无名管道的代码实现重新敲一遍

#include<myhead.h>
sem_t sem1;//创建无名信号量1

void *task1(void *arg)
{
	int num=5;
	while(num--)
	{
		printf("我生产了\n");
	sem_post(&sem1);//释放资源
	}
	pthread_exit(NULL);//退出线程
}
void *task2(void *arg)
{
	int num=5;
	while(num--)
	{
		//申请资源
		sem_wait(&sem1);
			printf("我消费了\n");
		
	}
	pthread_exit(NULL);//退出线程
}
int main(int argc, const char *argv[])
{
	//初始化无名信号量
	sem_init(&sem1,0,0);
	pthread_t pid1,pid2;
	if(pthread_create(&pid1,NULL,task1,NULL)!=0)
	{
		printf("pid1 create error\n");
		return 0;
	}
	if(pthread_create(&pid2,NULL,task2,NULL)!=0)
	{
		printf("pid2 create error\n");
		return 0;
	}
	pthread_join(pid1,NULL);
	pthread_join(pid2,NULL);
	sem_destroy(&sem1);
return 0;
}

将有名管道的代码实现重新敲一遍

name_creat.c

#include<myhead.h>
int main(int argc, const char *argv[])
{
	//创建管道
	if(mkfifo("./myfifo",0664)==-1)
	{
		perror("mkfifo error");
		return -1;
	}
	getchar();//阻塞
	system("rm myfifo");
return 0;
}


 write_namepipe.c 

#include<myhead.h>
int main(int argc, const char *argv[])
{
	//打开管道
	int wfd=-1;
	//只写打开文件
	if((wfd=open("./myfifo",O_WRONLY))==-1)
	{
		perror("open error");
		return -1;
	}
	//定义容器
	char wbuf[128]="";
	while(1)
	{
		printf("please input:\n");
		fgets(wbuf,sizeof(wbuf),stdin);
		wbuf[strlen(wbuf)-1]=0;
		//将数据写入管道
		write(wfd,wbuf,strlen(wbuf));
		//判断是否退出
		if(strcmp(wbuf,"quit")==0)
		{
			break;
		}
 		close(wfd);
		return 0;
	}

return 0;
}

read_namepipe.c 

#include<myhead.h>
int main(int argc, const char *argv[])
{
	//打开管道文件
	int rfd=-1;
	//只读形式打开
	if((rfd=open("./myfifo",O_RDONLY))==-1)
	{
		perror("open error");
		return -1;
	}
	//定义容器
	char rbuf[100]="";
	while(1)
	{
		//清空
		bzero(rbuf,sizeof(rbuf));
		//读取管道中的数据
		read(rfd,rbuf,sizeof(rbuf));
		//输出结果
		printf("receive data is:%s\n",rbuf);
		//判断结果
		if(strcmp(rbuf,"quit")==0)
		{
			break;
		}
	}
	close(rfd);
return 0;
}


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

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

相关文章

分享WebGL物体三维建模

界面效果 代码结构 模型素材类似CT (Computed Tomography)&#xff0c;即电子计算机断层扫描&#xff0c;它是利用精确准直的X线束、γ射线、超声波等&#xff0c;与灵敏度极高的探测器一同围绕物体的某一部位作一个接一个的断面扫描。 坐标系统 渲染流程 渲染流程是个将之前准…

报表控件Stimulsoft 新版本2024.1中,功能区工具栏新功能

今天&#xff0c;我们将讨论Stimulsoft Reports、Dashboards 和 Forms 2024.1版本中的一项重要创新 - 在一行中使用功能区工具栏的能力。 Stimulsoft Ultimate &#xff08;原Stimulsoft Reports.Ultimate&#xff09;是用于创建报表和仪表板的通用工具集。该产品包括用于WinF…

game项目(梦开始的地方)

梦开始的地方 由于easyx只支持vis&#xff0c;所以这个项目的书写以后都是在vis上进行&#xff0c;希望自己能够把这个项目好好完成&#xff0c;相信自己&#xff0c;加油&#xff01; 我们需要一个头文件来包括作图工具 (这个头文件在easyx上面下载) #include<graphics.…

PostgreSQL与MySQL,谁更胜一筹

前言 PostgreSQL与MySQL都是优秀的开源数据库。在日常学习中&#xff0c;新手可能接触最多的是MySql,但是实际工作中&#xff0c;两者的应用场景其实都很广。我之前的做过上网流量销售业务&#xff0c;用的是MySQL,现在接触广告业务&#xff0c;用的是pg数据库&#xff0c;每天…

汉诺塔问题—java详解(附源码)

来源及应用 相传在古印度圣庙中&#xff0c;有一种被称为汉诺塔(Hanoi)的游戏。该游戏是在一块铜板装置上&#xff0c;有三根杆(编号A、B、C)&#xff0c;在A杆自下而上、由大到小按顺序放置64个金盘(如图1)。游戏的目标&#xff1a;把A杆上的金盘全部移到C杆上&#xff0c;并仍…

Spring学习上下文【ConfigurableApplicationContext】

话不多说&#xff0c;先上图&#xff1a; ConfigurableApplicationContext是Spring框架中的一个接口&#xff0c;它继承了ApplicationContext接口&#xff0c;并扩展了一些额外的方法&#xff0c;用于允许应用程序在运行时动态地修改和管理应用上下文。ConfigurableApplicati…

matlab代码--基于matlabLDPC-和积译码系统

LDPC编码 一个码长为n、信息位个数为k的线性分组码&#xff08;n,k&#xff09;可以由一个生成矩阵 来定义&#xff0c;信息序列 通过G被映射到码字XS.G。线性分组码也可以由一个校验矩阵 来描述。所以码字均满足 。校验矩阵的每一行表示一个校验约束 &#xff0c;其中所有的非…

【大数据】Flink 之部署篇

Flink 之部署篇 1.概述和参考架构2.可重复的资源清理3.部署模式3.1 Application 模式3.2 Per-Job 模式&#xff08;已废弃&#xff09;3.3 Session 模式 Flink 是一个多用途框架&#xff0c;支持多种不同的混合部署方案。下面&#xff0c;我们将简要介绍 Flink 集群的构建模块、…

免费搭建个人网盘

免费搭建一个属于个人的网盘。 服务端 详情请参考原网站的服务端下载和安装虚拟磁盘Fuse4Ui可以支持把网盘内容挂载成系统的分区&#xff1b; 挂载工具效果图&#xff1a;应用端应用端的下载 效果图

教你零基础制作产品画册,打开线上市场

​ 随着市场竞争的日益激烈&#xff0c;越来越多的企业开始注重产品的宣传和推广。而产品画册作为产品宣传的重要手段之一&#xff0c;也越来越受到企业的关注。今天&#xff0c;分享一个零基础制作产品画册的方法&#xff0c;帮助你打开线上市场的大门 1.选择合适的企业宣传…

代码随想录算法训练营第58天 | 392.判断子序列 115.不同的子序列

判断子序列 这道题可以双指针方法解决。 class Solution { public:bool isSubsequence(string s, string t) {int s_index 0;for(int t_index 0; t_index < t.size(); t_index) {if(s[s_index] t[t_index]) {s_index;}}return s_index s.size();} };用动态规划也是可解…

cilium-agent远程debug

文章目录 概述编译cilium-agent运行cilium-agent开启远程debug参考资料 概述 通过远程 debug&#xff0c;来分析 cilium-agent 是如何在容器创建的时候定义容器网络接口。 编译cilium-agent 首先是在本地编译 cilium-agent&#xff0c;下面是以 v1.14.4 的代码作为例子来阐述…

AJAX.

概念:AJAX&#xff1a;异步的 JavaScript 和 XML AJAX作用: 1.与服务器进行数据交换: 通过AJAX可以给服务器发送请求&#xff0c;并获取服务器响应的是数据 使用了AJAX和服务器进行通讯&#xff0c;就可以使用HTML和AJAX来替换JSP页面了 2.异步交互:可以在不重新加载整个页面的…

VSCode The preLaunchTask ‘C/C++: clang++ 生成活动文件‘ terminated with exit code -1

更改tasks.json文件里面的type为shell 选择g 选择g&#xff0c;然后点回到text.c&#xff0c;按下F5. 得到结果。 文中内容参考: 从零开始手把手教你配置属于你的VS Code_哔哩哔哩_bilibili https://blog.csdn.net/qq_63872647/article/details/128006861

Windows7安装指南

概要&#xff1a; 本篇演示Windows7的安装过程 一、说明 1、电脑 笔者的电脑品牌是acer(宏碁/宏基) 电脑开机按F2可进入BIOS 2、Windows7启动U盘 Windows7启动U盘作为Windows7的安装来源 该U盘的制作可参考笔者的文章 Windows制作Windows的U盘启动盘 Windows7没有USB…

《图解HTTP》笔记1:http的诞生

1&#xff0c;http的诞生&#xff1a; 1.1 为共享知识而生 我们现在使用web&#xff08;World Wide Web的简称&#xff0c;即万维网&#xff09;浏览器&#xff0c;目前可以输入一个网址&#xff08;http://www.baidu.com)&#xff0c;就会有一个网页显示出来。 最开始设想出…

百面嵌入式专栏(经验篇)如何在面试中介绍自己的项目经验

文章目录 1. 在面试前准备项目描述,别害怕,因为面试官什么都不知道2. 准备项目的各种细节,一旦被问倒了,就说明你没做过3.不露痕迹地说出面试官爱听的话4.一定要主动,面试官没有义务挖掘你的亮点5.一旦有低级错误,可能会直接出局6.引导篇:准备些加分点,在介绍时有意提到…

36、IO进程线程/进程和线程之间的通信练习

一、使用有名管道完成两个进程的相互通信(提示&#xff1a;可以使用多进程或多线程完成)。 代码1&#xff1a;创建两个有名管道文件 #include<myhead.h>int main(int argc, const char *argv[]) {if(mkfifo("./mingtohua",0664)-1)//创建小明向小华发信息的管…

【CSS】设置文字(文本)的渐变色

# 渐变色 文字 第一步 设置渐变颜色 background: linear-gradient(278.83deg, #5022bd 31.42%, #8636d1 75.55%); // 先设置渐变色背景&#xff1b; 第二步 设置颜色的使用范围 background-clip: text; // 背景被裁剪成文字的前景色。 -webkit-background-clip: text; 第三步…

C#使用一个泛型方法操作不同数据类型的数组

目录 一、泛型方法及其存在的意义 二 、实例 1.源码 2.生成效果 再发一个泛型方法的示例。 一、泛型方法及其存在的意义 实际应用中&#xff0c;查找或遍历数组中的值时&#xff0c;有时因为数组类型的不同&#xff0c;需要对不同的数组进行操作&#xff0c;那么,可以使用…