IO进程线程之线程同步互斥的简单应用(8.2)

 

目录

1. 定义一个全局变量,char str[] = "123456",要求定义两个线程:线程A, 线程B

不使用flag

使用flag:

2.要求用两个线程拷贝一张图片,A线程拷贝前半部分,B线程拷贝后半部分


1. 定义一个全局变量,char str[] = "123456",要求定义两个线程:线程A, 线程B

  1. 要求A线程循环打印全局字符串str;

  2. 要求B线程循环倒置全局字符串str:将str中的内容倒置为"654321",再倒置为"123456"....

    注意:是倒置不是倒着打印

  3. 要求A线程打印出的str字符串内容为:123456或者654321。

    不允许出现乱序,例如:623451 653451

不使用flag

#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;
}

使用flag:

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<string.h>
char str[]="123456";

void *callBack1(void* arg)
{
	//分离线程
	pthread_detach(pthread_self());
	int *p=(int *)arg;
	int i=0;
	while(1)
	{
		if(*p==1)
		{
			printf("%s",str);
			printf("\n");
			i++;
			*p=0;
		}
	}

	//return NULL
	pthread_exit(NULL);
}

void *callBack2(void* arg)
{
	//分离线程
	pthread_detach(pthread_self());

	char temp;
	int i=0;
	int *p=(int *)arg;
	printf("this is callBack2:");

	while(1)
	{
		for(int j=0;j<strlen(str)/2;j++)
		{
			temp=str[j];
			str[j]=str[5-j];
			str[5-j]=temp;
		}
		*p=1;
		printf("倒置成功\n");
		sleep(1);
	}
	//return NULL
	pthread_exit(NULL);
}


int main(int argc, const char *argv[])
{
	printf("准备运行程序\n"); 
	int flag=0;
	pthread_t tid1;
	if(pthread_create(&tid1,NULL,callBack1,&flag) != 0)
	{
		perror("pthread_create");
		return -1;
	}

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

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

	printf("分支退出\n");

	return 0;
}

2.要求用两个线程拷贝一张图片,A线程拷贝前半部分,B线程拷贝后半部分

不允许使用sleep函数,不允许使用flag

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>

pthread_mutex_t mutex; 	//互斥锁
int fp;
int pic;

off_t len;


void* callBack1(void* arg)
{
	pthread_detach(pthread_self());
	/***临界区*****/
	pthread_mutex_lock(&mutex); 	//上锁 
	char buf;
	lseek(pic,0,SEEK_SET);
    lseek(fp,0,SEEK_SET);
	while(1)
	{

		read(pic,&buf,1);
		write(fp,&buf,1);
		if(lseek(pic,0,SEEK_CUR)==len/2)
		{
			break;
		}
	}
	pthread_mutex_unlock(&mutex);
	/***临界区*****/
	pthread_exit(NULL);

}


void* callBack2(void* arg)
{
	pthread_detach(pthread_self());
	//	pthread_mutex_lock(&mutex); 	//上锁 
	len=lseek(pic,0,SEEK_END);

	char buf;
	pthread_mutex_lock(&mutex); 

	lseek(pic,len/2,SEEK_SET);
	lseek(fp,len/2,SEEK_SET);


	while(1)
	{
		read(pic,&buf,sizeof(buf));
		write(fp,&buf,1);
		if(lseek(pic,0,SEEK_CUR)==len)
		{
			break;
		}
	}
	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;
	}


   	fp=open("./111.png",O_RDWR|O_CREAT|O_TRUNC,0775);
	if(fp < 0)
	{
		perror("open1");
	}
	 pic=open("./11.png",O_RDWR);
	if(pic < 0)
	{
		perror("open2");
	}
	//创建锁	
	pthread_mutex_init(&mutex,NULL);
	
	//创建一个线程,打印前半部分
	pthread_t tid1;
	if(pthread_create(&tid1, NULL, callBack1, NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}

	//创建一个线程,打印后半部分
	pthread_t tid2;
	if(pthread_create(&tid2, NULL, callBack2, NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}


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

	close(fp);
	close(pic);


	//销毁互斥锁
	pthread_mutex_destroy(&mutex);	
	return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值