8.2-IO进程线程-作业

1.要求用两个线程拷贝一张图片,A线程拷贝前半部分,B线程拷贝后半部分。不允许使用sleep函数,不允许使用flag

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

int fd_r; 
int fd_w;
pthread_mutex_t mutex; //互斥锁

void* callA(void* arg)
{
	pthread_mutex_lock(&mutex); //上锁
	int size = *(int*)arg;
	lseek(fd_r,0,SEEK_SET);//偏移到开头
	lseek(fd_w,0,SEEK_SET);
	char c;
	for(int i=0;i<size/2;i++)//复制前半部分
	{
		read(fd_r,&c,1);
		write(fd_w,&c,1);
	}
	printf("图片前半部分复制完成\n");
	pthread_mutex_unlock(&mutex); //解锁
	pthread_exit(NULL);
}
void* callB(void* arg)
{
	pthread_mutex_lock(&mutex); //上锁
	int size = *(int*)arg;
	lseek(fd_r,size/2,SEEK_SET);//偏移到中间
	lseek(fd_w,size/2,SEEK_SET);
	char c;
	for(int i=size/2;i<size;i++)//复制后半部分
	{
		read(fd_r,&c,1);
		write(fd_w,&c,1);
	}
	printf("图片后半部分复制完成\n");
	pthread_mutex_unlock(&mutex); //解锁
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	fd_r = open("./1.png", O_RDONLY);//打开图片
	if(fd_r < 0)
	{
		perror("fopen");
		return -1;
	}
	fd_w = open("./2.png",O_WRONLY|O_CREAT|O_TRUNC, 0664);
	if(fd_w < 0)
	{
		perror("fopen");
		return -1;
	}

	int size = lseek(fd_r,0,SEEK_END);//计算图片大小

	pthread_t tid1;//线程A
	pthread_t tid2;//线程B


	//创建互斥锁,并初始化
	if(pthread_mutex_init(&mutex, NULL) != 0)
	{
		perror("pthread_mutex_init");
		return -1;
	}

	//创建线程A
	if(pthread_create(&tid1,NULL,callA,&size)!=0)
	{
		perror("pthread_create");
		return -1;
	}
	
	//创建线程B
	if(pthread_create(&tid2,NULL,callB,&size)!=0)
	{
		perror("pthread_create");
		return -1;
	}

	//阻塞线程,回收资源
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);

	printf("图片复制完成\n");

	close(fd_r);
	close(fd_w);

	return 0;
}

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

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

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

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

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

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

#include <stdio.h>
#include <pthread.h>
#include <string.h>

pthread_mutex_t mutex;
char str[] = "123456";
void* callA(void*arg)
{
	pthread_mutex_lock(&mutex);
	while(1)
	{
		printf("%s\n",str);
	}
	pthread_mutex_unlock(&mutex);
}

void* callB(void*arg)
{
	pthread_mutex_lock(&mutex);
	char temp;
	while(1)
	{
		for(int 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);
	}
}


int main(int argc, const char *argv[])
{
	if(pthread_mutex_init(&mutex,NULL) != 0)
	{
		perror("pthread_mutex_init");
		return -1;
	}
	pthread_t tid1;
	pthread_t tid2;

	if(pthread_create(&tid1,NULL,callA,NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}
	if(pthread_create(&tid2,NULL,callB,NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	pthread_mutex_destroy(&mutex);

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值