8.2 线程互斥锁练习

1.对全局变量倒置并输出

2.使用两个线程分别拷贝图片前后半部分

1.对全局变量倒置并输出

       本次功能实现中,将对变量的导致和输出分别封装到了两个线程中进行操作。因此需要通过互斥锁防止在功能未完成时cpu时间片耗尽强制切换到另一个功能中。

        代码如下:

#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
char str[]={'1','2','3','4','5','6'};
pthread_mutex_t mute;
void * callA(void * arg)   
{
	int i=0;
	int len=strlen(str);
	while(1)
	{
		pthread_mutex_lock(&mute);	//上锁
		printf(" %s \n ",str);
		pthread_mutex_unlock(&mute);//解锁
	}
	pthread_exit(NULL);
}
void * callB(void * arg)   
{
	int i=0;
	int len=strlen(str);
	int count = len/2;
	char t;
	while(1)
	{
		
		pthread_mutex_lock(&mute); //上锁
		for(i=0;i<count;i++)
		{
			t=str[i];
			str[i]=str[len-1-i];
			str[len-1-i]=t;
		}
		pthread_mutex_unlock(&mute);  //解锁
		
		
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	
	if (pthread_mutex_init(&mute,NULL)!=0)
	{
		perror("pthread_mutex_init");
		return -1;
	}//创建互斥锁


	pthread_t tid;
   	
	if (pthread_create(&tid,NULL,callA,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}//创建线程
	pthread_t tid2;
	if (pthread_create(&tid2,NULL,callB,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}
	pthread_join(tid,NULL);


	pthread_join(tid2,NULL);
	return 0;
}

测试结果:

由于未使用延时函数导致程序的输出速度很快未能截取到线程交界处。但可见已完成预计功能,且输出序列不存在倒置一般的结果。

 

 2.使用两个线程分别拷贝图片前后半部分

        由于需要使用两个线程分别拷贝图片的前后两半部分,所以也使用互斥锁保证临界区代码的完整性。

代码如下:

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

pthread_mutex_t mute;
int r,w,l;

int copy(int fd_r,int fd_w,off_t len) //拷贝函数(只进行一半)
{		
	off_t count=0;
	if(fd_r < 0)
		{
			return -1; 
		}
		if(fd_w < 0) 
		{ 
			return -1; 
		}
		char buf[10]; 
		ssize_t res = 0; 
		while(1) 
		{
			count+=res;
			if (count>=(len/2))
			{
				break;
			}
			bzero(buf, sizeof(buf));
		 	res = read(fd_r, buf, sizeof(buf));
		 	if(0 == res)
				break; 
			write(fd_w, buf, res); 
		}
	return 0;
}


void *callA(void*arg)
{
	pthread_mutex_lock(&mute);
	copy(r,w,l);
	pthread_mutex_unlock(&mute);
	pthread_exit(NULL);
}



void *callB(void*arg)
{
	pthread_mutex_lock(&mute);
	copy(r,w,l);
	pthread_mutex_unlock(&mute);
	pthread_exit(NULL);
}






int main(int argc, const char *argv[])
{
	
	pthread_mutex_t mute;
	int fd_r = open("./pic.png", O_RDONLY); 
	int fd_w = open("./eo.png", O_WRONLY|O_CREAT|O_TRUNC, 0664); 	
	off_t len = lseek(fd_r,0,SEEK_END);
	lseek(fd_r,0,SEEK_SET);
	r=fd_r;
	w=fd_w;
	l=len;
	if (pthread_mutex_init(&mute,NULL)!=0)
	{
		perror("pthread_mutex_init");
		return -1;
	}//创建互斥锁
	pthread_t tid;
	if (pthread_create(&tid,NULL,callA,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}//创建线程
	pthread_t tid2;
	if (pthread_create(&tid2,NULL,callB,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}
	pthread_join(tid,NULL);


	pthread_join(tid2,NULL);
	return 0;
}

测试结果:

        程序执行后成功将该文件目录下的pic.png拷贝成eo.png。若需要从其他路径进行拷贝仅需将open中的文件路径进行更改,或者改为argv通过外部传参输入路径完成所需功能。

           

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值