当前位置: 首页 > news >正文

IO进程线程;多线程;线程互斥同步;互斥锁;无名信号量;条件变量;0905

思维导图

多线程打印ABC

运用无名面量 实现进程同步

#include<myhead.h>
//定义 无名信号量
sem_t sem1;
sem_t sem2;
sem_t sem3;
//线程1
void* task1(void *arg)
{while(1){sem_wait(&sem1);printf("A");fflush(stdout);sleep(1);sem_post(&sem2);}
}
//线程2
void* task2(void *arg)
{while(1){sem_wait(&sem2);printf("B");fflush(stdout);sleep(1);sem_post(&sem3);}
}
//线程3
void* task3(void *arg)
{while(1){sem_wait(&sem3);printf("C");fflush(stdout);sleep(1);sem_post(&sem1);}
}
int main()
{//初始化无名信号量sem_init(&sem1,0,1);sem_init(&sem2,0,0);sem_init(&sem3,0,0);pthread_t tid1,tid2,tid3;pthread_create(&tid1,NULL,task1,NULL);pthread_create(&tid2,NULL,task2,NULL);pthread_create(&tid3,NULL,task3,NULL);pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);//销毁 无名信号sem_destroy(&sem1);sem_destroy(&sem1);sem_destroy(&sem3);return 0;
}

一个生产者和多个消费者

互斥锁

#include<myhead.h>//创建互斥锁
pthread_mutex_t mutex;
//创建 条件变量 等待队列
pthread_cond_t cond;
//线程体 生产者
void *task(void *arg)
{//for(int i=0;i<5;i++)//{sleep(1);//printf("富士康公司组装了一台iPhone手机\n");printf("富士康公司组装了三台iPhone手机\n");//将等待队列中的 线程唤醒//唤醒等待队列中的第一个线程//pthread_cond_signal(&cond);//唤醒等待队列中的所有线程pthread_cond_broadcast(&cond);//}
}
//线程体1 消费者
void *task1(void *arg)
{//获取 互斥锁pthread_mutex_lock(&mutex);//进入等待队列 条件变量pthread_cond_wait(&cond,&mutex);printf("我将购买一台iPhone手机\n");//释放互斥锁pthread_mutex_unlock(&mutex);
}int main()
{//初始化互斥锁pthread_mutex_init(&mutex,NULL);//初始化 条件变量pthread_cond_init(&cond,NULL);//创建多线程pthread_t tid;//生产者线程pthread_t tid1;//和他的 三个消费线程pthread_t tid2;pthread_t tid3;//多线程pthread_create(&tid,NULL,task,NULL);pthread_create(&tid1,NULL,task1,NULL);pthread_create(&tid2,NULL,task1,NULL);pthread_create(&tid3,NULL,task1,NULL);//阻塞回收线程资源pthread_join(tid,NULL);pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);//销毁互斥锁pthread_mutex_destroy(&mutex);//销毁 信号变量pthread_cond_destroy(&cond);return 0;
}

多线程文件拷贝

尝试用 互斥锁 实现

线程a拷贝前一半

线程b拷贝后一半

认为是,线程传参,出现了错误,我能力有限写不出来

#include<myhead.h>
//创建互斥锁
pthread_mutex_t mutex;//源文件 目标文件 结构体
struct copy
{char w[128];char r[128];
};
//线程结构体1 拷贝前一半文件
void *task1(void *arg)
{//申请 互斥锁pthread_mutex_lock(&mutex);//打开文件//打开源文件int rfd=open((struct copy *)arg->r,O_RDONLY);//求文件的大小int S=lseek(rfd,0,SEEK_END);//再把光标移动到开头lseek(rfd,0,SEEK_SET);//打开 目标文件int wfd=open((struct copy *)arg->w,O_WRONLY|O_CREAT|O_TRUNC,0664);//不存在就创建 存在就清空//拷贝前一半文件for(int i=0;i<S/2;i++){char buff;read(rfd,&buff,1);write(wfd,&buff,1);}//关闭文件close(rfd);	close(wfd);	//释放 互斥锁pthread_mutex_unlock(&mutex);
}
//线程结构体2 拷贝后一半文件
void *task2(void *arg)
{//申请 互斥锁pthread_mutex_lock(&mutex);//打开文件//打开源文件int rfd=open((struct copy *)arg->r,O_RDONLY);//求文件的大小int S=lseek(rfd,0,SEEK_END);//再把光标移动到一半的位置lseek(rfd,S/2,SEEK_SET);//打开 目标文件int wfd=open((struct copy *)arg->w,O_WRONLY|O_CREAT|O_TRUNC,0664);//不存在就创建 存在就清空//拷后一半文件char buff;while(read(rfd,&buff,1)){write(wfd,&buff,1);}//关闭文件close(rfd);close(wfd);//释放 互斥锁pthread_mutex_unlock(&mutex);
}int main(int argc,const char *argv[])
{//初始化互斥锁pthread_mutex_init(&mutex,NULL);//创建多线程pthread_t tid1;pthread_t tid2;//多线程 函数传参数 结构体struct copy word;struct copy *arg=&word;strcpy(arg->r,argv[1]);strcpy(arg->w,*argv[2]);pthread_create(&tid1,NULL,task1,arg);pthread_create(&tid2,NULL,task2,arg);//回收线程资源pthread_join(tid1,NULL);pthread_join(tid2,NULL);//销毁互斥锁pthread_mutex_destroy(&mutex);return 0;
}


文章转载自:

http://nhSvdESm.nqmkr.cn
http://LdutTSvs.nqmkr.cn
http://Ux97d4i7.nqmkr.cn
http://wKy3QO9U.nqmkr.cn
http://SPFgevOR.nqmkr.cn
http://6Ct0751G.nqmkr.cn
http://tHXrxJkA.nqmkr.cn
http://zu44CvJX.nqmkr.cn
http://vcO6sCPx.nqmkr.cn
http://YfEWwFs8.nqmkr.cn
http://WssjwU7w.nqmkr.cn
http://46bDQybp.nqmkr.cn
http://TRDOog9D.nqmkr.cn
http://kR4dTWVi.nqmkr.cn
http://tw9kpKXX.nqmkr.cn
http://KtIcg3X6.nqmkr.cn
http://Oy60afiZ.nqmkr.cn
http://AfVmGkgz.nqmkr.cn
http://qMvIzL2o.nqmkr.cn
http://3ckLIEMn.nqmkr.cn
http://zEOokdpy.nqmkr.cn
http://7DQuLfUm.nqmkr.cn
http://faJSMb9a.nqmkr.cn
http://neaxHd5T.nqmkr.cn
http://3p2lXKuf.nqmkr.cn
http://hGEOE9oC.nqmkr.cn
http://qbh768Wd.nqmkr.cn
http://me5g20D4.nqmkr.cn
http://CItgUCOi.nqmkr.cn
http://dp7RlQ60.nqmkr.cn
http://www.dtcms.com/a/368626.html

相关文章:

  • 虚拟机详细图文教程系列15、Linux虚拟机Centos8系统部署禅道开源项目
  • uniapp开发小程序,列表 点击后加载更多数据
  • 云市场周报 (2025.09.05):解读腾讯云AI安全、阿里数据湖与KubeVela
  • 一键生成PPT的AI工具排名:2025年能读懂你思路的AI演示工具
  • 【数据结构、java学习】数组(Array)
  • 越南电网3D地图
  • 大数据毕业设计选题推荐-基于大数据的分化型甲状腺癌复发数据可视化分析系统-Spark-Hadoop-Bigdata
  • Kubernetes 全景指南:从核心概念到云原生未来
  • 视频监控展示插件-js,支持多种视频格式
  • 2025年国家高新技术企业认定:申报材料和流程详解
  • 【面试场景题】spring应用启动时出现内存溢出怎么排查
  • 【NVIDIA AIQ】自定义函数实践
  • 【RelayMQ】基于 Java 实现轻量级消息队列(六)
  • 解锁 Claude Code 终极工作流:从基础到进阶的全流程指南
  • 深入浅出 全面剖析消息队列(Kafka,RabbitMQ,RocketMQ 等)
  • 工业HMI:人机交互的核心与智能制造的桥梁
  • 解决rt_pin_get返回错误码的问题
  • 基于单片机汽车防撞系统设计
  • Java 提取 PDF 文件内容:告别手动复制粘贴,拥抱自动化解析!
  • 【AI总结】Python BERT 向量化入门指南
  • 《sklearn机器学习——回归指标2》
  • 投资储能项目能赚多少钱?小程序帮你测算
  • 基于开源AI智能名片链动2+1模式S2B2C商城小程序的公益课引流策略研究
  • 医疗问诊陪诊小程序:以人性化设计构建健康服务新生态
  • modbus_tcp和modbus_rtu对比移植AT-socket,modbus_tcp杂记
  • 云手机的空间会占用本地内存吗
  • HTML 各种事件的使用说明书
  • docker 部署RustDesk服务
  • 【Python基础】 20 Rust 与 Python 循环语句完整对比笔记
  • 为什么后端接口不能直接返回数据库实体?聊聊 Product 到 ProductDetailVo 的转换逻辑