IO进程线程->线程->day5

目录

练习

一、倒置线程

1.1 函数

1.2 执行结果

 二、线程代码复制图片

2.1 函数

2.2 执行结果


练习

1. 定义一个全局变量,char str[] = "123456",要求定义两个线程:线程A, 线程B
        1). 要求A线程循环打印全局字符串str;
        2). 要求B线程循环倒置全局字符串str:将str中的内容倒置为"654321",再倒置为"123456"....
           注意:是倒置不是倒着打印
        3). 要求A线程打印出的str字符串内容为;123456或者654321。不允许出现乱序,例如:623451 653451,,,、

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


一、倒置线程

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

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

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

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

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

本次使用的是互斥锁


1.1 函数

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

char str[]="123456";
pthread_mutex_t mutex;

void* callback1(void* arg)
{
	pthread_detach(pthread_self());
	int len=strlen(str);
	char temp;
	while(1)
	{
		pthread_mutex_lock(&mutex);
		for(int i=0;i<len/2;i++)
		{
			temp=str[i];
			str[i]=str[len-i-1];
			str[len-i-1]=temp;
		}
		pthread_mutex_unlock(&mutex); 
	}
	pthread_exit(NULL);
}

void* callback2(void* arg)
{
	pthread_detach(pthread_self());

	while(1)
	{
		pthread_mutex_lock(&mutex);
		printf("str=%s\n",str);
		pthread_mutex_unlock(&mutex); 
	}
}
int main(int argc, const char *argv[])
{
	printf("程序开始\n");
	if(pthread_mutex_init(&mutex,NULL) != 0)
	{
		perror("pthread_mutex_init");
		return -1;
	}
	pthread_t tid1,tid2;
	printf("互斥锁创建成功\n");
	//创建线程
	if(pthread_create(&tid1,NULL,callback1,NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}
	if(pthread_create(&tid2,NULL,callback2,NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	pthread_mutex_destroy(&mutex);

	return 0;
}

1.2 执行结果


 二、线程代码复制图片

难点在于创建的线程A与B,不知道哪一个先执行,会导致,只会复制一半。

我就先创建A,等A执行完后,再创建B,再执行。

代码中应该把fd_r和fd_w定义成全局变量,这样就只需要在主函数中执行open(打开文件)一次就好了,但我修改了很久才执行出结果,不想再改了,请需要者酌情修改。


2.1 函数

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


pthread_mutex_t mutex; //存储互斥锁的变量
char buf; 		//读取文件时用于存储的变量

void* cpy1(void* arg)
{
	pthread_detach(pthread_self()); //自己分离自己,等线程退出后,自动回收线程资源
	pthread_mutex_lock(&mutex); 	//上锁

	int fd_r=open("./1.png",O_RDONLY);
	int fd_w=open("./cpy.png",O_RDWR | O_CREAT | O_APPEND,0777);
	if(fd_r<0 || fd_w<0)
	{
		perror("open");
		return NULL;
	}
	off_t len=lseek(fd_r,0,SEEK_END);
	printf("len=%ld\n",len);
	lseek(fd_r,0,SEEK_SET);
	lseek(fd_w,0,SEEK_SET);
	for(int i=0;i<len/2;i++)
	{
		if(read(fd_r,&buf,1) <= 0)
		{
			perror("read");
			return NULL;
		}
		write(fd_w,&buf,1);
	}
	close(fd_r);
	close(fd_w);

	pthread_mutex_unlock(&mutex); 	//解锁
	pthread_exit(NULL);  			//结束线程
}
void* cpy2(void* arg)
{
	pthread_detach(pthread_self());
	pthread_mutex_lock(&mutex);

	int fd_r=open("./1.png",O_RDONLY);
	int fd_w=open("./cpy.png",O_RDWR | O_CREAT | O_APPEND,0777);
	if(fd_r<0 || fd_w<0)
	{
		perror("open");
		return NULL;
	}
	off_t len=lseek(fd_r,0,SEEK_END);
	printf("len=%ld\n",len);
	lseek(fd_r,len/2,SEEK_SET);
	lseek(fd_w,len/2,SEEK_SET);
	for(int i=len/2;i<len;i++)
	{
		if(read(fd_r,&buf,1) <= 0)
		{
			perror("read");
			return NULL;
		}
		write(fd_w,&buf,1);
	}
	close(fd_r);
	close(fd_w);

	pthread_mutex_unlock(&mutex);
	pthread_exit(NULL);
}

2.2 执行结果

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值