8.2 IO线程相关函数(互斥锁)

一、定义一个全局变量,char str[] = “123456”,要求定义两个线程:线程A, 线程B

要求A线程循环打印全局字符串str;
要求B线程循环倒置全局字符串str:将str中的内容倒置为"654321",再倒置为"123456"…注意:是倒置不是倒着打印
要求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;
	char temp = 0;
	while(1)
	{
		/***临界区*****/
		pthread_mutex_lock(&mutex); 	//上锁

		for(i=0; i<strlen(str)/2; i++)
		{
			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 <pthread.h>
#include <string.h>
pthread_mutex_t mutex;

FILE* fp_r;
FILE* fp_w;
long half;

void* A(void* arg)
{
	pthread_mutex_lock(&mutex);
	rewind(fp_r);
	rewind(fp_w);
	char c;
	long count=0;
	while(fread(&c,1,1,fp_r)!=0)
	{
		fwrite(&c,1,1,fp_w);
		count++;
		if(count==half)
			break;
	}

	pthread_mutex_unlock(&mutex);

	pthread_exit(NULL);
}


void* B(void* arg)
{
	pthread_mutex_lock(&mutex);
	
	fseek(fp_r,half,SEEK_SET);
	fseek(fp_w,half,SEEK_SET);

	char c;
	while(fread(&c,1,1,fp_r)!=0)
	{
		fwrite(&c,1,1,fp_w);
	}


	pthread_mutex_unlock(&mutex);

	pthread_exit(NULL);

}

int main(int argc, const char *argv[])
{
	
	fp_r = fopen("./1.png","r");
	if(NULL == fp_r)
	{
		perror("open");
		return -1;
	}
	fp_w = fopen("./2.png","w+");
	if(NULL == fp_w)
	{
		perror("open");
		return -1;
	}
	fseek(fp_r,0,SEEK_END);
	half = ftell(fp_r)/2;

	if(pthread_mutex_init(&mutex,NULL)!=0)
	{
		perror("pthread_mutex_init");
		return -1;
	}
	//printf("mutex_init success\n");


	pthread_t tid1;
	if(pthread_create(&tid1,NULL,A,NULL) != 0)
	{
		perror("Apthread_create");
		return -1;
	}

	pthread_t tid2;
	if(pthread_create(&tid2,NULL,B,NULL) != 0)
	{
		perror("Bpthread_create");
		return -1;
	}

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);

	fclose(fp_r);
	fclose(fp_w);
	pthread_mutex_destroy(&mutex);
	return 0;
}


测试结果

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知名社畜L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值