当前位置:网站首页>IO process thread -> thread -> day5
IO process thread -> thread -> day5
2022-08-03 04:42:00 【Whale calls me to take care of the sea QAQ】
目录
二、Thread code to copy pictures
练习
1. 定义一个全局变量,char str[] = "123456",Two threads are required to be defined:线程A, 线程B
1). 要求AThe thread loop prints the global stringstr;
2). 要求BThe thread loop inverts the global stringstr:将strThe contents of are inverted as "654321",Inverted to"123456"....
注意:It is printed upside down, not upside down
3). 要求A线程打印出的str字符串内容为;123456或者654321.Out of order is not allowed,例如:623451 653451,,,、
2. Requires two threads to copy an image,AThe thread copies the first half,BThe thread copies the second half,不允许使用sleep函数,不允许使用flag.
一、Invert the thread
定义一个全局变量,char str[] = "123456",Two threads are required to be defined:线程A, 线程B
1. 要求AThe thread loop prints the global stringstr;
2. 要求BThe thread loop inverts the global stringstr:将strThe contents of are inverted as "654321",Inverted to"123456"....
注意:It is printed upside down, not upside down
3. 要求A线程打印出的str字符串内容为:123456或者654321
This time, a mutex is used
1.1 函数
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
char str[]="123456";
pthread_mutex_t mutex;
void* callback1(void* arg)
{
pthread_detach(pthread_self());
int len=strlen(str);
char temp;
while(1)
{
pthread_mutex_lock(&mutex);
for(int i=0;i<len/2;i++)
{
temp=str[i];
str[i]=str[len-i-1];
str[len-i-1]=temp;
}
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* callback2(void* arg)
{
pthread_detach(pthread_self());
while(1)
{
pthread_mutex_lock(&mutex);
printf("str=%s\n",str);
pthread_mutex_unlock(&mutex);
}
}
int main(int argc, const char *argv[])
{
printf("程序开始\n");
if(pthread_mutex_init(&mutex,NULL) != 0)
{
perror("pthread_mutex_init");
return -1;
}
pthread_t tid1,tid2;
printf("The mutex was created successfully\n");
//创建线程
if(pthread_create(&tid1,NULL,callback1,NULL) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid2,NULL,callback2,NULL) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
1.2 执行结果
二、Thread code to copy pictures
The difficulty lies in the created threadA与B,Not sure which one executes first,会导致,Only half of it will be copied.
I will create it firstA,等A执行完后,再创建B,再执行.
should be put in the codefd_r和fd_w定义成全局变量,This only needs to be executed in the main functionopen(打开文件)一次就好了,But I modified it for a long time to execute the result,不想再改了,Please modify as necessary.
2.1 函数
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
pthread_mutex_t mutex; //The variable that stores the mutex
char buf; //The variable used for storage when reading the file
void* cpy1(void* arg)
{
pthread_detach(pthread_self()); //自己分离自己,等线程退出后,Automatically reclaim thread resources
pthread_mutex_lock(&mutex); //上锁
int fd_r=open("./1.png",O_RDONLY);
int fd_w=open("./cpy.png",O_RDWR | O_CREAT | O_APPEND,0777);
if(fd_r<0 || fd_w<0)
{
perror("open");
return NULL;
}
off_t len=lseek(fd_r,0,SEEK_END);
printf("len=%ld\n",len);
lseek(fd_r,0,SEEK_SET);
lseek(fd_w,0,SEEK_SET);
for(int i=0;i<len/2;i++)
{
if(read(fd_r,&buf,1) <= 0)
{
perror("read");
return NULL;
}
write(fd_w,&buf,1);
}
close(fd_r);
close(fd_w);
pthread_mutex_unlock(&mutex); //解锁
pthread_exit(NULL); //结束线程
}
void* cpy2(void* arg)
{
pthread_detach(pthread_self());
pthread_mutex_lock(&mutex);
int fd_r=open("./1.png",O_RDONLY);
int fd_w=open("./cpy.png",O_RDWR | O_CREAT | O_APPEND,0777);
if(fd_r<0 || fd_w<0)
{
perror("open");
return NULL;
}
off_t len=lseek(fd_r,0,SEEK_END);
printf("len=%ld\n",len);
lseek(fd_r,len/2,SEEK_SET);
lseek(fd_w,len/2,SEEK_SET);
for(int i=len/2;i<len;i++)
{
if(read(fd_r,&buf,1) <= 0)
{
perror("read");
return NULL;
}
write(fd_w,&buf,1);
}
close(fd_r);
close(fd_w);
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
2.2 执行结果
边栏推荐
- Interface testing framework combat (3) | JSON request and response assertion
- Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8
- Test drive: project management module - curd development project
- 建立树形结构
- OpenFOAM提取等职面并计算面积
- 接口测试框架实战(二)| 接口请求断言
- BIOTIN ALKYNE CAS:773888-45-2价格,供应商
- Assembly answers
- Secondary development of WinForm controls
- Two ways to simulate multi-user login in Jmeter
猜你喜欢
【Harmony OS】【ARK UI】ETS 上下文基本操作
Can Oracle EMCC be installed independently?Or does it have to be installed on the database server?
【Harmony OS】【ARK UI】Date 基本操作
【Harmony OS】【ArkUI】ets开发 基础页面布局与数据连接
BIOTIN ALKYNE CAS: 773888-45-2 Price, Supplier
Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
2022/08/02 学习笔记 (day22) 多线程
在线密码生成工具推荐
Test drive: project management module - curd development project
【Harmony OS】【FAQ】鸿蒙问题合集1
随机推荐
接口测试框架实战(四)| 搞定 Schema 断言
rosbag工具plotjuggler无法打开rosbag的问题
OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
2022河南萌新联赛第(四)场:郑州轻工业大学 E - 睡大觉
10.预测房价:回归问题
接口测试框架实战(二)| 接口请求断言
JS底层手写
js中的闭包
11.机器学习基础:机器学习的四个分支
OpenFOAM extracts equivalency and calculates area
【 Harmony OS 】 【 ano UI 】 lightweight data storage
typescript49-交叉类型
RequestContextHolder
接口测试 Mock 实战(二) | 结合 jq 完成批量化的手工 Mock
Secondary development of WinForm controls
MySQL 入门:Case 语句很好用
UV 裂解的生物素-PEG2-叠氮|CAS:1192802-98-4生物素接头
mysql bool盲注
GIS数据漫谈(五)— 地理坐标系统
I ported GuiLite to STM32F4 board