IO day6:线程

文章展示了三个多线程程序实例。第一个程序创建一个线程显示时间,输入quit结束进程。第二个程序中,两个线程同步修改字符串buf,一个打印,一个反转,不使用sleep函数。第三个程序用两个线程分别拷贝图片的前半部分和后半部分,同样避免使用sleep。
摘要由CSDN通过智能技术生成

练习
1.标准lO函数时候讲解的时钟代码,要求输入quit字符串后,结束进程

 代码段:

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
void* callback(void *arg)
{
	while(1)
	{
		time_t t1;
		struct tm *info = NULL;

		time(&t1);
		info = localtime(&t1);
		printf("%4d-%02d-%02d %02d-%02d-%02d\r",\
				info->tm_year+1900,info->tm_mon+1,info->tm_mday,\
				info->tm_hour,info->tm_min,info->tm_sec);
		sleep(1);
		fflush(stdout);
	}	
}
int main(int argc, const char *argv[])
{

	pthread_t tid;
	if(pthread_create(&tid,NULL,callback,NULL) != 0)
	{
		fprintf(stderr,"pthread_creat failed __%d__\n",__LINE__);
		return -1;
	}
	char str[20]="";
	scanf("%s",str);
	while(1)
	{
		if(0 == strcmp(str,"quit"))
		{
			break;
		}
	}

	return 0;
}

输出结果:

输入quit直接结束
2.要求定义一个全局变量char buf="1234567",创建两个线程,不考虑退出条件。
a.A线程循环打印buf字符串,
b.B线程循环倒置buf字符串,即buf种本来存储1234567,倒置后buf仲存储7654321.不打印!!c.倒置不允许使用辅助数组。
d.要求A线程打印出来的结果只能为1234567或者7654321e.不允许使用sleep函数

代码段

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
char buf[]="1234567";
void* callback1(void *arg)
{
	char t = 0;
	int n = strlen(buf)-1;
	for(int i = 0;i<n/2;i++)
	{
		t = buf[i];
		buf[i] = buf[n-i];
		buf[n-i] = t;
	}	
	pthread_exit(NULL);
}

void* callback(void *arg)
{

	while(1)
	{
		pthread_t tid1;
		if(pthread_create(&tid1,NULL,callback1,NULL) != 0)
		{
			fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
			return NULL;
		}
		pthread_join(tid1,NULL);

		printf("buf = %s\n",buf);

	}
	pthread_exit(NULL);

}
int main(int argc, const char *argv[])
{
	pthread_t tid;
	if(pthread_create(&tid,NULL,callback,NULL) != 0)
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}

	pthread_join(tid,NULL);
	return 0;
}

输出结果


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

 代码段

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
void* callback1(void* arg)
{
	int fd = open("./1.png",O_RDONLY);
	struct stat buf;
	if(stat("./1.png",&buf)<0)
	{
		perror("stat");
		return NULL;
	}
	long int a=buf.st_size;
	int fb = open("./2.png",O_WRONLY|O_CREAT,0777);
	if(fd < 0)
	{
		perror("open");
		return NULL;
	}
	if(fb < 0)
	{
		perror("open");
		return NULL;
	}
	lseek(fd,a/2,SEEK_SET);
	lseek(fb,a/2,SEEK_SET);
	char c = 0;
	if(a%2==1)
	{
		for(int i=0;i<a/2+1;i++)
		{
			read(fd,&c,1);
			write(fb,&c,1);
		}
	}
	else
	{
		for(int i=0;i<a/2;i++)
		{
			read(fd,&c,1);
			write(fb,&c,1);
		}
	}
	pthread_exit(NULL);

}

void* callback(void* arg)
{
	int fd = open("./1.png",O_RDONLY);
	struct stat buf;
	if(stat("./1.png",&buf)<0)
	{
		perror("stat");
		return NULL;
	}
	long int a=buf.st_size;
	int fb = open("./2.png",O_WRONLY|O_CREAT|O_TRUNC,0777);
	if(fd < 0)
	{
		perror("open");
		return NULL;
	}
	if(fb < 0)
	{
		perror("open");
		return NULL;
	}
	lseek(fd,0,SEEK_SET);
	lseek(fb,0,SEEK_SET);
	char c = 0;
	for(int i=0;i<a/2;i++)
	{
		read(fd,&c,1);
		write(fb,&c,1);
	}
	pthread_t tid1;
	if(pthread_create(&tid1,NULL,callback1,NULL) != 0)
	{
		fprintf(stderr,"pthread_create failed __%d__",__LINE__);
		return NULL;
	}
	pthread_join(tid1,NULL);
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	pthread_t tid;
	if(pthread_create(&tid,NULL,callback,NULL) != 0)
	{
		fprintf(stderr,"pthread_create failed __%d__",__LINE__);
		return -1;
	}

	pthread_join(tid,NULL);
	return 0;

}

 输出结果:

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值