使用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 <unistd.h>
#include <string.h>

char str[]="123456";
pthread_mutex_t mutex;//初始化互斥锁变量
void* A(void* arg)
{
	//分离线程,线程结束后自动回收资源
	pthread_detach(pthread_self());
	//打印字符串
	for(; ;)
	{
		//上锁
		pthread_mutex_lock(&mutex);
		printf("A......>%s\n", str);
		//解锁
		pthread_mutex_unlock(&mutex);
	}
}
void* B(void* arg)
{
	//分离线程,线程结束后自动回收资源
	pthread_detach(pthread_self());
	int i;
	char c;
	for(; ;)
	{
		//上锁
		pthread_mutex_lock(&mutex);
		//首尾互换倒置字符串
		for(i=0; i<strlen(str)/2; i++)
		{
			c = str[i];
			str[i] = str[strlen(str)-1-i];
			str[strlen(str)-1-i] = c;
		}
		//解锁
		pthread_mutex_unlock(&mutex);
	}
}
int main(int argc, const char *argv[])
{
	//创建两个线程
	pthread_t tid1, tid2;
	if(0 != pthread_create(&tid1, NULL, A, NULL))
	{
		perror("pthread_create");
		return -1;
	}
	if(0 != pthread_create(&tid2, NULL, B, NULL))
	{
		perror("pthread_create");
		return -1;
	}
	printf("创建线程成功\n");
	if(0 != pthread_mutex_init(&mutex, NULL)) //创建互斥锁
	{
		printf("创建互斥锁失败\n");
		return -1;
	}
	while(1)
	{
		sleep(1);
	}
	pthread_mutex_destroy(&mutex);//销毁互斥锁
	return 0;
}

测试代码

A......>123456
A......>123456
A......>123456
A......>123456
A......>123456
A......>123456
A......>123456
A......>123456
A......>123456
A......>654321
A......>654321
A......>654321
A......>654321
A......>654321
A......>654321
A......>654321
A......>654321
A......>654321
A......>654321

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

#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
pthread_mutex_t m;//定义互斥锁变量
int size = 0;//文件大小
void* A(void* arg)
{
	//上锁
	if(pthread_mutex_lock(&m) != 0)
	{
		perror("pthread_mutex_lock");//上锁失败
		pthread_exit(NULL);//结束线程
	}
	//打开文件
	int fp = open("./1.jpg", O_RDONLY);
	if(fp < 0)
	{
		perror("open");
		pthread_exit(NULL);
	}
	int fq = open("./2.jpg", O_WRONLY|O_CREAT, 0777);
	if(fq < 0)
	{
		perror("open");
		pthread_exit(NULL);
	}
	//计算文件大小
	int i, size = lseek(fp, 0, SEEK_END);
	//文件偏移到开头
	lseek(fp, 0, SEEK_SET);
	lseek(fq, 0, SEEK_SET);
	char c = 0;
	//写入前半部分
	for(i=0; i<size/2; i++)
	{
		read(fp,&c,1);
		write(fq,&c,1);
	}
	//关闭文件
	close(fp);
	close(fq);
	//解锁
	pthread_mutex_unlock(&m);
}
void* B(void* arg)
{
	//上锁
	if(0 != pthread_mutex_lock(&m))
	{
		perror("pthread_mutex_lock");//上锁失败
		pthread_exit(NULL);//结束线程
	}
	//打开文件
	int fp = open("./1.jpg", O_RDONLY);
	if(fp < 0)
	{
		perror("open");
		pthread_exit(NULL);
	}
	int fq = open("./2.jpg", O_WRONLY|O_CREAT, 0777);
	if(fq < 0)
	{
		perror("open");
		pthread_exit(NULL);
	}
	//计算文件大小
	int i, size = lseek(fp, 0, SEEK_END);
	//文件偏移到中间
	lseek(fp, size/2, SEEK_SET);
	lseek(fq, size/2, SEEK_SET);
	char c = 0;
	//写入后半部分
	for(i=size/2; i<size; i++)
	{
		read(fp,&c,1);
		write(fq,&c,1);
	}
	//关闭文件
	close(fp);
	close(fq);
	//解锁
	pthread_mutex_unlock(&m);
}
int main(int argc, const char *argv[])
{
	//创建两个线程
	pthread_t tid1, tid2;
	if(0 != pthread_create(&tid1, NULL, A, NULL))
	{
		perror("pthread_create");
		return -1;
	}
	if(0 != pthread_create(&tid2, NULL, B, NULL))
	{
		perror("pthread_create");
		return -1;
	}
	if(0 != pthread_mutex_init(&m, NULL))
	{
		perror("pthread_mutex_init");
		return -1;
	}
	pthread_join(tid1, NULL);//阻塞,回收线程1资源
	pthread_join(tid2, NULL);//阻塞,回收线程2资源
	pthread_mutex_destroy(&m);//销毁互斥锁
	printf("拷贝成功\n");
	return 0;
}

 结果:

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

傾语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值