Day6(IO进程线程)

作业

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

#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>

void *CallBack(void *arg)
{
	while(1)
	{
		time_t t1 = time(NULL);
		struct tm *info = NULL;

		info = localtime(&t1);

		printf("%4d-%0d-%0d %0d-%0d-%d\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_create has failed\n");
		return -1;
	}

	char ans[20] = "";
	printf("是否退出时间打印?退出则打印quit\n");
	scanf("%s", ans);
	if(strcmp("quit", ans) == 0)
	{
		exit(0);
	}

	

	return 0;
}

输出结果:

ubuntu@ubuntu:Day6$ gcc 01_1homework.c -pthread
ubuntu@ubuntu:Day6$ ./a.out 
是否退出时间打印?退出则打印quit
quit-2-28 19-38-11

2.要求定义一个全局变量char buf=“1234567”,创建两个线程,不考虑退出条件。

a.A线程循环打印buf字符串,
b.B线程循环倒置buf字符串,即buf种本来存储1234567,倒置后buf仲存储7654321.不打印!!
c.倒置不允许使用辅助数组。
d.要求A线程打印出来的结果只能为1234567或者7654321
e.不允许使用sleep函数

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

char buf[] = "1234567";

//线程执行体
//尝试倒置buf
void* CallBack(void *arg)
{
	int len = sizeof(buf);
	printf("len = %d\n", len);
	int temp = 0;
	while(1)
	{
		for(int i=0; i<len/2; i++)
		{
			temp = buf[i];
			buf[i] = buf[len-i-2];
			buf[len-i-2] = temp;
		}
		//	printf("buf = %s\n", buf);
		pthread_exit(NULL);
	}
/*	while(1)
	{
		printf("支线程打印buf = %s\n", buf);
		sleep(1);
	}
	*/
}

int main(int argc, const char *argv[])
{
	pthread_t tid;
	//创建一个新线程
	if(pthread_create(&tid, NULL, CallBack, NULL) != 0)
	{
		fprintf(stderr, "pthread_create have failed __%d__\n", __LINE__);
		return -1;
	}

	int res = pthread_join(tid, NULL);
	printf("res = %d\n", res);

	//分离线程
//	pthread_detach(tid);

	int i = 0;
	//主线程循环打印buf
	while(i < 3)
	{
	//	pthread_join(tid, NULL);
		printf("主线程打印buf = %s\n", buf);
		i++;
	}

	return 0;
}

输出结果:

ubuntu@ubuntu:Day6$ ./a.out 
len = 8
res = 0
主线程打印buf = 7654321
主线程打印buf = 7654321
主线程打印buf = 7654321

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

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#if 0
/*
void Open()
{
	int fd_R = open("./1.png", O_RDONLY);
	int fd_W = open("./2.png", O_WRONLY | O_CREAT | O_TRUNC, 0664);
	struct stat buf;
	if(stat("./1.png", &buf) < 0)
	{
		perror("stat");
		return;
	}
	off_t size = buf.st_size;
	printf("size = %ld\n", size);
}
*/
#endif

void *CallBack(void *arg)
{
	int fd_R = open("./1.png", O_RDONLY);
	int fd_W = open("./2.png", O_WRONLY | O_CREAT | O_APPEND, 0664);
	struct stat buf;
	if(stat("./1.png", &buf) < 0)
	{
		perror("stat");
	}
	off_t size = buf.st_size;
	printf("size = %ld\n", size);

	lseek(fd_R, 0, SEEK_SET);
	lseek(fd_W, 0, SEEK_SET);

	int flag = size % 2;
	char copy = 0;

	for(int i=0; i<(size/2+flag); i++)
	{
		read(fd_R, &copy, 1);
		write(fd_W, &copy, 1);
	}

	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 has failed\n");
		return -1;
	}

	//设置阻塞
	pthread_join(tid, NULL);

	int fd_R = open("./1.png", O_RDONLY);
	int fd_W = open("./2.png", O_WRONLY | O_CREAT | O_APPEND, 0664);
	struct stat buf;
	if(stat("./1.png", &buf) < 0)
	{
		perror("stat");
	}
	off_t size = buf.st_size;
	printf("size = %ld\n", size);

	lseek(fd_R, size/2, SEEK_SET);
	lseek(fd_W, size/2, SEEK_SET);

	char copy = 0;

	for(int i=size/2; i<size; i++)
	{
		read(fd_R, &copy, 1);
		write(fd_W, &copy, 1);
	}

	return 0;
}

输出结果:

ubuntu@ubuntu:Day6$ diff 1.png 2.png 
ubuntu@ubuntu:Day6$ 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值