利用线程与互斥锁拷贝图片

需求:

利用线程与互斥锁拷贝图片,线程A拷贝前半部分,线程B拷贝后半部分

代码实现过程:

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

pthread_mutex_t mutex;
char str[32]="";
int res = 0;

struct info{
	int fd1;
	int fd2;
	off_t size;
};

void *funcA(void *arg){

	int fd1 = ((struct info*)arg)->fd1;
	int fd2 = ((struct info*)arg)->fd2;
	off_t size = ((struct info*)arg)->size;

	ssize_t half_size = size/2;
	ssize_t read_size = 0;
	ssize_t offset = 0;

	while(1){
		//locking
		pthread_mutex_lock(&mutex);

		lseek(fd1,offset,SEEK_SET);
		lseek(fd2,offset,SEEK_SET);

		read_size = half_size/sizeof(str)==0?half_size:sizeof(str);	
		res = read(fd1,str,read_size);
		if(res == 0){
			pthread_mutex_unlock(&mutex);
			break;
		}
		if(write(fd2,str,res)<0){
			perror("write");
			return NULL;
		}
		if((half_size -= read_size) < 0){
			printf("0\n");
			break;
		}
	
		offset = lseek(fd1,0,SEEK_CUR);
		
		pthread_mutex_unlock(&mutex);
		//unlocking

	}


	pthread_exit(NULL);

	return NULL;

}

void *funcB(void *arg){

	int fd1 = ((struct info*)arg)->fd1;
	int fd2 = ((struct info*)arg)->fd2;
	off_t size = ((struct info*)arg)->size;
	ssize_t offset = size/2;

	while(1){
		//locking
		pthread_mutex_lock(&mutex);

		lseek(fd1,offset,SEEK_SET);
		lseek(fd2,offset,SEEK_SET);

		res = read(fd1,str,sizeof(str));
		if(res == 0){
			pthread_mutex_unlock(&mutex);
			break;
		}
		if(write(fd2,str,res) < 0){
			perror("write");
			return NULL;
		}

		offset = lseek(fd1,0,SEEK_CUR);
		
		pthread_mutex_unlock(&mutex);
		//unlocking

	}	

	pthread_exit(NULL);

	return NULL;

}

int main(int argc, const char *argv[])
{

	int fd1 = open("./1.png",O_RDONLY);
	int fd2= open("./copy.png",O_RDWR|O_CREAT,0664);
	if(fd1 < 0 || fd2 < 0){
		perror("open");
		return -1;
	}

	struct info msg;
	msg.fd1 = fd1;
	msg.fd2 = fd2;
	off_t size = lseek(fd1,0,SEEK_END);
	msg.size = size;

	pthread_mutex_init(&mutex,NULL);
	
	pthread_t tidA;
	if(pthread_create(&tidA,NULL,funcA,&msg) != 0){
		fprintf(stderr,"pthread_create A\n");
		return -1;
	}
	
	pthread_t tidB;
	if(pthread_create(&tidB,NULL,funcB,&msg) != 0){
		fprintf(stderr,"pthread_create B\n");
		return -1;
	}

	if(pthread_join(tidB,NULL) != 0 || pthread_join(tidA,NULL) != 0){
		fprintf(stderr,"pthread_join\n");
		return -1;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
	}

	if(pthread_mutex_destroy(&mutex) != 0){
		fprintf(stderr,"pthread_mutex_destroy\n");
		return -1;
	}
	
	if(close(fd1) < 0 || close(fd2) < 0){
		perror("close");
		return -1;
	}

	return 0;
}

代码实现结果:

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值