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

DAY6 线程

作业1:

        多线程实现文件拷贝,线程1拷贝一半,线程2拷贝另一半,主线程回收子线程资源。

代码:

#include <myhead.h>
sem_t sem1;
void *copy1()//子线程1函数 拷贝前一半内容
{int fd1=open("./1.txt",O_RDONLY);int fd2=open("./2.txt",O_CREAT|O_RDWR|O_APPEND,0664);if(fd1==-1){perror("open");}if(fd2==-1){perror("open");}char s[100];int sum1=0,res1=0;int len=lseek(fd1,0,SEEK_END);//统计文件字节数lseek(fd1,0,SEEK_SET);//统计完光标要返回while(1){res1=read(fd1,s,sizeof(s));sum1+=res1;//每读一次,把读取长度加起来if(res1==0||sum1>len/2){int k=res1-(sum1-len/2);//该次读取字节数-超过len/2的字节数write(fd2,s,k);break;}write(fd2,s,sizeof(s));//把读取到的字节写入2.txt}printf("线程1拷贝完成\n");close(fd1);close(fd2);sem_post(&sem1);pthread_exit(NULL);
}
void *copy2()//子线程2函数  拷贝后一半内容
{sem_wait(&sem1);int fd1=open("./1.txt",O_RDONLY);int fd2=open("./2.txt",O_CREAT|O_RDWR|O_APPEND,0664);if(fd1==-1){perror("open");}if(fd2==-1){perror("open");}int fd3;int res2=0;int len=lseek(fd1,0,SEEK_END);//统计文件字节数lseek(fd1,0,SEEK_SET);//统计完光标要返回dup2(fd1,fd3);//fd3重定向fd1lseek(fd3,len/2,SEEK_SET);//fd3光标移动中间char s[100];while(1){res2=read(fd3,s,sizeof(s));if(res2==0){break;}write(fd2,s,res2);}printf("线程2拷贝完成\n");close(fd1);close(fd2);close(fd3);pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{pthread_t tid1,tid2;sem_init(&sem1,0,0);if(pthread_create(&tid1,NULL,copy1,NULL)){perror("pthread_create");return -1;}if(pthread_create(&tid2,NULL,copy2,NULL)){perror("pthread_create");return -1;}pthread_join(tid1,NULL);pthread_join(tid2,NULL);printf("回收完成\n");sem_destroy(&sem1);return 0;
}

运行测试结果:

作业2:线程同步之条件变量

        生产者先让消费者组成一个队列,生产者生产了一台劳斯莱斯,唤醒第一个消费者来消费,然后再生产第二台劳斯莱斯,唤醒第二个消费者来消费,这样可以精确化的控制生产者线程和消费者线程的协同

代码:

#include <myhead.h>
#define MAX 10
pthread_cond_t cond;
pthread_mutex_t mtx;
sem_t sem;
int n=0,count=0;
void *producer()
{for(int i=0;i<MAX;i++){sem_wait(&sem);//用条件变量卡一下n++;printf("生产了一台特斯拉%d\n",n);pthread_cond_signal(&cond);//唤醒一个等待线程}pthread_exit(NULL);
}
void *consumer()
{pthread_mutex_lock(&mtx);//进入后锁住pthread_cond_wait(&cond,&mtx);//条件变量每接受一次进入后重新上锁count++;printf("消费了一台特斯拉%d\n",count);usleep(200000);pthread_mutex_unlock(&mtx);sem_post(&sem);//条件变量放行pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{pthread_t tid1,tid2[MAX];pthread_cond_init(&cond,NULL);//条件变量pthread_mutex_init(&mtx,NULL);//互斥锁sem_init(&sem,0,1);//条件变量if(pthread_create(&tid1,NULL,producer,NULL)!=0){perror("pthread_create");return -1;}for(int i=0;i<MAX;i++){if(pthread_create(&tid2[i],NULL,consumer,NULL)!=0){perror("pthread_create");return -1; }}pthread_join(tid1,NULL);for(int i=0;i<MAX;i++){pthread_join(tid2[i],NULL);}pthread_mutex_destroy(&mtx);pthread_cond_destroy(&cond);return 0;
}

运行测试结果:

作业3:

互斥锁,无名信号量,条件变量再练习一遍

1.互斥锁代码如何实现

代码:

#include <myhead.h>
pthread_mutex_t mtx;//定义互斥锁
void *fun(void *n)
{pthread_mutex_lock(&mtx);//上锁printf("%d\n",*(int *)n);pthread_mutex_unlock(&mtx);//解锁pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{pthread_t tid1,tid2;int num1=1,num2=2;pthread_mutex_init(&mtx,NULL);//初始化互斥锁if(pthread_create(&tid1,NULL,fun,&num1))//子线程1{perror("pthread_create");return -1;}if(pthread_create(&tid2,NULL,fun,&num2))//子线程2{perror("pthread_create");return -1;}pthread_join(tid1,NULL);//阻塞回收线程pthread_join(tid2,NULL);pthread_mutex_destroy(&mtx);//毁锁return 0;
}

运行测试结果:

2.无名信号量

代码:

#include <myhead.h>
#define MAX 5
sem_t sem;//定义无名信号量
int n=0;
void *producer()
{for(int i=0;i<MAX;i++){n++;printf("生产了特斯拉%d\n",n);}sem_post(&sem);//释放无名变量pthread_exit(NULL);
}
void *consumer()
{sem_wait(&sem);//申请无名变量printf("消费了一台特斯拉%d\n",n);n--;sem_post(&sem);pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{pthread_t tid1,tid[MAX];sem_init(&sem,0,0);//初始化无名信号量if(pthread_create(&tid1,NULL,producer,&n)!=0){perror("pthread_create");return -1;}for(int i=0;i<MAX;i++){if(pthread_create(&tid[i],NULL,consumer,&n)!=0){perror("pthread_create");return -1;}}pthread_join(tid1,NULL);for(int i=0;i<MAX;i++){pthread_join(tid[i],NULL);}sem_destroy(&sem);//销毁无名变量return 0;
}

运行测试结果:

3.条件变量

见作业2

Xmind知识点:

http://www.lryc.cn/news/483406.html

相关文章:

  • 基于STM32的智能门锁系统设计思路:蓝牙、RFID等技术
  • AndroidStudio-广播
  • 基于表格滚动截屏(表格全部展开,没有滚动条)
  • 洛谷P1255
  • vue3设置第三方组件 样式::v-deep
  • JAVA学习日记(十四)集合进阶
  • mysql全量与增量备份
  • “非法”操控lambda(python)
  • UDP协议和TCP协议之间有什么具体区别?
  • 论文5—《基于改进YOLOv5s的轻量化金银花识别方法》文献阅读分析报告
  • 快手直播间采集教程,快手引流,快手截流,截流工具,直播间截流,快手直播间采集,获客系统,获客软件
  • 探索MoviePy:Python视频编辑的瑞士军刀
  • mysql 实现分库分表之 --- 基于 MyCAT 的分片策略详解
  • Opencascade基础教程(14): 一个模型显示问题
  • ISP——你可以从这里起步(二)
  • Qt / Qt Quick程序打包的一些坑 (四)
  • 《传统视觉算法在视觉算法中的地位及应用场景
  • 老老实实干一辈子程序员是没出息的!这本证书你早该学!
  • 鸿蒙next版开发:相机开发-录像(ArkTS)
  • 闯关leetcode——3206. Alternating Groups I
  • 多个摄像机画面融合:找到同一个目标在多个画面中的伪三维坐标,找出这几个摄像头间的转换矩阵
  • Three.js性能优化和实践建议
  • C#入门 023 什么是类(Class)
  • 一篇Spring Boot 笔记
  • 一生一芯 预学习阶段 NEMU代码学习(2)
  • 《手写Spring渐进式源码实践》实践笔记(第二十章 实现简单ORM框架)
  • AI技术赋能电商行业:创新应用与未来展望
  • windows 11编译安装ffmpeg(包含ffplay)
  • 系统启动时将自动加载环境变量,并后台启动 MinIO、Nacos 和 Redis 服务
  • [ACTF2020 新生赛]Upload 1--详细解析