0911hw

1.

void *fun()
{
	int fd1 = open("1.png", O_RDONLY);
	int fd2 = open("3.png", O_WRONLY | O_CREAT | O_TRUNC, 0666);
	const off_t size = lseek(fd1, 0, SEEK_END);
	lseek(fd1, 0, SEEK_SET);
	char str[10];
	ssize_t res;
	while (1) {
		if (lseek(fd1, 0, SEEK_CUR) > size / 2) {
			puts("拷贝结束!");
			break;
		}
		res = read(fd1, str, sizeof(str));
		write(fd2, str, res);
	}
	close(fd2);
	close(fd1);
}
int main(int argc, const char *argv[])
{
	pthread_t tid ;
	if(pthread_create(&tid,NULL,fun,NULL)!=0)
	{
		printf("create wrong\n");
	}
	sleep(1);
	int fd1 = open("./1.png", O_RDONLY);
	int fd2 = open("./3.png", O_WRONLY);

	char str[10];
	ssize_t res;

	const off_t size = lseek(fd1, 0, SEEK_END);
	//置位光标到文件的一半处
	lseek(fd1, size / 2, SEEK_SET);
	lseek(fd2, size / 2, SEEK_SET);
	while (1) {

		res = read(fd1, str, sizeof(str));
		if (res <= 0) {
			puts("拷贝结束!");
			break;
		}
		write(fd2, str, res);
	}
	close(fd2);
	close(fd1);

	return 0;
}

2.

char buf[]="1234567";
void *fun()
{
	while(1){
	int i=0,j=6;
	while(i<j)
	{
		char t=buf[i];
		buf[i]=buf[j];
		buf[j]=t;
		i++;
		j--;
	}
	}
}
int main(int argc, const char *argv[])
{
	pthread_t tid ;
	if(pthread_create(&tid,NULL,fun,NULL)!=0)
	{
		printf("create wrong\n");
	}
	while(1){
		if(strcmp(buf,"1234567")==0||strcmp(buf,"7654321")==0)
		printf("%s\n",buf);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值