当前位置:网站首页>作业8.2 线程同步互斥机制——互斥锁
作业8.2 线程同步互斥机制——互斥锁
2022-08-03 00:05:00 【不知名大学生M】
文章目录
题目一:定义一个全局变量,char str[] = “123456”,要求定义两个线程:线程A, 线程B
1.要求A线程循环打印全局字符串str;
2.要求B线程循环倒置全局字符串str:将str中的内容倒置为"654321",再倒置为"123456"…
注意:是倒置不是倒着打印
4.要求A线程打印出的str字符串内容为:123456或者654321。
不允许出现乱序,例如:623451 653451,,,
实现代码
#include <stdio.h>
#include <pthread.h>
#include <string.h>
//临界资源(共享资源)
char str[] = "1234567";
pthread_mutex_t mutex; //互斥锁
void* callBack_print(void* arg)
{
while(1)
{
/***临界区*****/
pthread_mutex_lock(&mutex); //上锁
printf("%s\n", str);
pthread_mutex_unlock(&mutex); //解锁
/***临界区*****/
}
pthread_exit(NULL);
}
void* callBack_reserve(void* arg)
{
int i = 0;
while(1)
{
/***临界区*****/
pthread_mutex_lock(&mutex); //上锁
for(i=0; i<strlen(str)/2; i++)
{
char temp = str[i];
str[i] = str[strlen(str)-1-i];
str[strlen(str)-1-i] = temp;
}
pthread_mutex_unlock(&mutex); //解锁
/***临界区*****/
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建互斥锁,并初始化
if(pthread_mutex_init(&mutex, NULL) != 0)
{
perror("pthread_mutex_init");
return -1;
}
printf("mutex_init success\n");
pthread_t tid1, tid2;
//创建一个线程,循环打印
if(pthread_create(&tid1, NULL, callBack_print, NULL) != 0)
{
perror("pthread_create");
return -1;
}
//创建一个线程,循环倒置
if(pthread_create(&tid2, NULL, callBack_reserve, NULL) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
//销毁互斥锁
pthread_mutex_destroy(&mutex);
return 0;
}
运行结果

题目二:要求用两个线程拷贝一张图片,A线程拷贝前半部分,B线程拷贝后半部分
不允许使用sleep函数,不允许使用flag
实现代码
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define MSG_ERR(msg) do{
\ fprintf(stderr,"line: %d ", __LINE__); \ perror(msg);\ }while(0)
off_t size;
pthread_mutex_t mutex; //互斥锁
void* callBack_A(void* arg)
{
int* fd=(int*)arg;
char* buf1=(char*)malloc(size/2);
/***临界区*****/
pthread_mutex_lock(&mutex); //上锁
while(read(fd[0],buf1,size/2)>0)
{
write(fd[1],buf1,size/2);
}
pthread_mutex_unlock(&mutex); //解锁
/***临界区*****/
free(buf1);
buf1=NULL;
pthread_exit(NULL);
}
void* callBack_B(void* arg)
{
int* fd=(int*)arg;
char* buf2=(char*)malloc(size/2+1);
ssize_t c;
/***临界区*****/
pthread_mutex_lock(&mutex); //上锁
while((c=read(fd[0],buf2,size/2+1))>0)
{
write(fd[1],buf2,c);
}
pthread_mutex_unlock(&mutex); //解锁
/***临界区*****/
free(buf2);
buf2=NULL;
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
int fd1=open("/home/ubuntu/图片/sllh.jpg",O_RDONLY);
if(fd1<0)
{
MSG_ERR("open");
return -1;
}
int fd2=open("./sllh.jpg",O_WRONLY|O_CREAT|O_TRUNC,0777);
if(fd2<0)
{
MSG_ERR("open");
return -1;
}
int fd[2];
fd[0]=fd1;
fd[1]=fd2;
size=lseek(fd1,0,SEEK_END);
lseek(fd1,0,SEEK_SET);
//创建互斥锁,并初始化
if(pthread_mutex_init(&mutex, NULL) != 0)
{
perror("pthread_mutex_init");
return -1;
}
printf("mutex_init success\n");
pthread_t tid1, tid2;
//创建一个线程A,拷贝前半部分
if(pthread_create(&tid1, NULL, callBack_A,(void*)fd)!= 0)
{
perror("pthread_create");
return -1;
}
//创建一个线程B,拷贝后半部分
if(pthread_create(&tid2, NULL, callBack_B,(void*)fd)!= 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
//销毁互斥锁
pthread_mutex_destroy(&mutex);
return 0;
}
运行结果
将/home/ubuntu/图片/sllh.jpg拷贝到当前目录

边栏推荐
- js基础知识整理之 —— 判断语句和三元运算符
- UPC2022暑期个人训练赛第23场(Credit Card Payment)
- 年近30 ,4月无情被辞,想给划水的兄弟提个醒...
- 优秀论文以及思路分析01
- 2022 China Eye Expo, Shandong Eye Health Exhibition, Vision Correction Instrument Exhibition, Eye Care Products Exhibition
- vue3的keepAlive缓存组件
- 浅谈I2C知识
- NLP commonly used Backbone model cheat sheet (1)
- RollBack Rx Professional RMC 安装教程
- 一文读懂 Web 3.0 应用架构
猜你喜欢

MySQL的多表查询(1)

牛客网剑指offer刷题练习之链表中环的入口结点

文树勋率长沙市人大常委会主任会议成员莅临麒麟信安调研数字经济发展情况

RollBack Rx Professional RMC 安装教程

matlab常微分方程在传染病建模中的应用

2022 开放原子全球开源峰会 | 麒麟信安携手openEuler助力开源产业繁荣发展

Visual Studio中vim模拟器

HVV红队 | 渗透测试思路整理

npm运行项目dependencies were not found: core-js/modules/es6.array.fill

NLP commonly used Backbone model cheat sheet (1)
随机推荐
即席查询—— Kylin使用
flutter空安全问题,平时用到的数据一定要注意
并查集总结
Last Common Ancestor (LCA) Study Notes | P3379 【Template】Least Common Ancestor (LCA) Problem Solution
德邦科技通过注册:年营收5.8亿 国家集成电路基金为大股东
7.31
letcode 第20题-有效的括号
49. 字母异位词分组-排序法
文树勋率长沙市人大常委会主任会议成员莅临麒麟信安调研数字经济发展情况
Heartwarming AI Review (1)
Auto.js special positioning control method cannot perform blocking operations on the ui thread, please use setTimeout instead
ORA-55610: Invalid DDL statement on history-tracked table
2149. 按符号重排数组
pytest-常用运行参数
TensorFlow学习记录(一):基本介绍
别再到处乱放配置文件了!我司使用 7 年的这套解决方案,稳的一秕
【软考 系统架构设计师】软件架构设计① 软件架构的概念
十年架构五年生活-05第一次出差
为了面试阿里,熬夜肝完这份软件测试笔记后,Offer终于到手了
HVV红队 | 渗透测试思路整理