day0818

 

1.

#include <myhead.h>
int file_len(const char*strfile, const char*dstfile);
void file_copy(const char* strfile, const char* dstfile, int start, int size);
int main(int argc, const char *argv[])
{
	//判断是否传入两个文件
	if(argc!=3)
	{
		printf("input file error\n");
		printf("usage:./a.out strfile dstfile\n");
	}
	int len;  //记录源文件的大小
	len = file_len(argv[1], argv[2]);

	//创建进程
	pid_t pid1 = fork();
	if(pid1 < 0)
	{
		perror("fork error");
		return -1;
	}else if(pid1 == 0)
	{
		//子进程
		sleep(3);
		file_copy(argv[1] ,argv[2] ,len/2 ,len-len/2);
		printf("2.拷贝成功\n");
		exit(EXIT_SUCCESS);
	}else
	{
			//父进程
			sleep(7);
			file_copy(argv[1], argv[2], 0, len/2);
			printf("1.拷贝成功\n");

			wait(NULL);
			printf("拷贝成功\n");
			while(1);
	}
	return 0;
}
/*************求源文件的大小**************/
int file_len(const char*strfile, const char*dstfile)
{
	//定义文件描述符
	int strfd, dstfd;
	//以只读的形式打开源文件
	if((strfd = open(strfile, O_CREAT))==-1)
	{
		perror("open strfile error");
		return -1;
	}
	//以创建的形式打开目标文件
	if((dstfd = open(dstfile, O_WRONLY|O_CREAT|O_TRUNC,0664))==-1)
	{
		perror("open dstfile error");
		return -1;
	}
	int len=lseek(strfd, 0, SEEK_END);  //求出文件的大小

	//关闭文件
	close(strfd);
	close(dstfd);
	return len;
}
/********************拷贝文件函数*********************/
void file_copy(const char* strfile, const char* dstfile, int start, int size)
{
	//定义两个文件描述符
	int strfd, dstfd;
	//以只读的形式打开源文件
	if((strfd = open(strfile, O_RDONLY))==-1)
	{
		perror("open strfile error");
		return ;
	}
	//以只写的形式打开目标文件
	if((dstfd = open(dstfile, O_WRONLY))==-1)
	{
		perror("open dstfile error");
		return ;
	}
	//将光标偏移到start处
	lseek(strfd, start, SEEK_SET);
	lseek(dstfd, start, SEEK_SET);

	char buf[128]="";
	int count = 0;
	while(1)
	{
		int ret = read(strfd, buf, sizeof(buf));
		count += ret;
		if(ret==0||count>=size)
		{
			write(dstfd, buf, ret-(count-size));
			break;
		}
		write(dstfd, buf, ret);
	}
	close(strfd);
	close(dstfd);
}

 2.

#include <myhead.h>
int file_len(const char*strfile, const char*dstfile);
void file_copy(const char* strfile, const char* dstfile, int start, int size);
int main(int argc, const char *argv[])
{
	//判断是否传入两个文件
	if(argc!=3)
	{
		printf("input file error\n");
		printf("usage:./a.out strfile dstfile\n");
	}
	int len;  //记录源文件的大小
	len = file_len(argv[1], argv[2]);

	//创建进程
	pid_t pid1 = fork();
	if(pid1 < 0)
	{
		perror("fork error");
		return -1;
	}else if(pid1 == 0)
	{
		//第一个子进程
		sleep(3);
		file_copy(argv[1] ,argv[2] ,len/3 ,len/3);
			printf("2.拷贝成功\n");
		exit(EXIT_SUCCESS);
	}else
	{
		//第二个进程
		pid_t pid2 = fork();  //父进程中在创建一个子进程
		if(pid2 < 0)
		{
			perror("fork erroe");
			return -1;
		}else if(pid2 == 0)    //父进程中的子进程2
		{
			sleep(7);
			file_copy(argv[1], argv[2], len/3*2, len/3);
			printf("3.拷贝成功\n");
			exit(EXIT_SUCCESS);
		}else
		{
			//父进程
			sleep(13);
			file_copy(argv[1], argv[2], 0, len/3);
			printf("1.拷贝成功\n");

			wait(NULL);
			wait(NULL);
			printf("拷贝成功\n");
			while(1);
		}
	}
	return 0;
}
/*************求源文件的大小**************/
int file_len(const char*strfile, const char*dstfile)
{
	//定义文件描述符
	int strfd, dstfd;
	//以只读的形式打开源文件
	if((strfd = open(strfile, O_CREAT))==-1)
	{
		perror("open strfile error");
		return -1;
	}
	//以创建的形式打开目标文件
	if((dstfd = open(dstfile, O_WRONLY|O_CREAT|O_TRUNC,0664))==-1)
	{
		perror("open dstfile error");
		return -1;
	}
	int len=lseek(strfd, 0, SEEK_END);  //求出文件的大小

	//关闭文件
	close(strfd);
	close(dstfd);
	return len;
}
/********************拷贝文件函数*********************/
void file_copy(const char* strfile, const char* dstfile, int start, int size)
{
	//定义两个文件描述符
	int strfd, dstfd;
	//以只读的形式打开源文件
	if((strfd = open(strfile, O_RDONLY))==-1)
	{
		perror("open strfile error");
		return ;
	}
	//以只写的形式打开目标文件
	if((dstfd = open(dstfile, O_WRONLY))==-1)
	{
		perror("open dstfile error");
		return ;
	}
	//将光标偏移到start处
	lseek(strfd, start, SEEK_SET);
	lseek(dstfd, start, SEEK_SET);

	char buf[128]="";
	int count = 0;
	while(1)
	{
		int ret = read(strfd, buf, sizeof(buf));
		count += ret;
		if(ret==0||count>=size)
		{
			write(dstfd, buf, ret-(count-size));
			break;
		}
		write(dstfd, buf, ret);
	}
	close(strfd);
	close(dstfd);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值