902作业

cs 复制代码
#include<myhead.h>
int main(int argc, const char *argv[])
{
	pid_t pid = -1;
	int fd1 = -1;
	int fd2 = -1;
	int num=0;
	ssize_t s;
	if (argc!=3)
	{
		printf("输入格式错误\n");
		return -1;
	}
	pid = fork();
	if ((fd1 = open(argv[1],O_RDONLY))==-1)
	{
		printf("源文件打开失败\n");
		return -1;
	}
	printf("源文件打开成功\n");

	off_t len = lseek(fd1,0,SEEK_END);

	lseek(fd1,0,SEEK_SET);

	close(fd1);
	if (pid>0)
	{
		printf("父进程:%d,拷贝前一半的内容\n",getpid());
		if ((fd1 = open(argv[1],O_RDONLY))==-1)
		{
			printf("源文件打开失败\n");
			return -1;
		}
		printf("源文件打开成功\n");

		if ((fd2 = open(argv[2],O_RDWR|O_CREAT|O_TRUNC))==-1)
		{
			printf("目标文件打开失败\n");
			return -1;

		}
		printf("目标文件打开成功\n");

		char str[128]="";
		while (num<(len/2)&&(s=read(fd1,str,sizeof(str)>(len/2-num)?(len/2-num):sizeof(str)))!=0)
		{
			write(fd2,str,s);
			num=s+num;
			bzero(str,sizeof(str));
			
		}
		
		close(fd1);
		close(fd2);

		wait(NULL);
		printf("已经成功回收\n");
	}
	else if (pid==0)
	{
		num=0;
		printf("子进程:%d,拷贝后一半的内容\n",getpid());
		if ((fd1 = open(argv[1],O_RDONLY))==-1)
		{
			printf("源文件打开失败\n");
			return -1;
		}
		printf("源文件打开成功\n");

		if ((fd2 = open(argv[2],O_WRONLY|O_CREAT|O_APPEND))==-1)
		{
			printf("目标文件打开失败\n");
			return -1;

		}
		printf("目标文件打开成功\n");

		lseek(fd1,len/2,SEEK_SET);
		lseek(fd2,len/2,SEEK_SET);

		char str[128]="";

		while (num<(len-(len/2))&&(s=read(fd1,str,sizeof(str)>(len/2-num)?(len/2-num):sizeof(str)))!=0)
		{
			write(fd2,str,s);
			num=s+num;
			bzero(str,sizeof(str));
		}
		
		printf("子进程已拷贝\n");
		close(fd1);
		close(fd2);
		exit(EXIT_SUCCESS);
	}

	return 0;
}
cs 复制代码
#include<myhead.h>
int main(int argc, const char *argv[])
{
	pid_t pid1 = -1;
	int fd1 = -1;
	int fd2 = -1;
	int num=0;
	ssize_t s;
	if (argc!=3)
	{
		printf("输入格式错误\n");
		return -1;
	}
	pid1 = fork();
	if ((fd1 = open(argv[1],O_RDONLY))==-1)
	{
		printf("源文件打开失败\n");
		return -1;
	}
	printf("源文件打开成功\n");

	off_t len = lseek(fd1,0,SEEK_END);

	lseek(fd1,0,SEEK_SET);

	close(fd1);
	if (pid1>0)
	{
		pid_t pid2 = fork();
		if (pid2>0)
		{
			printf("等待两个子进程的完成\n");	
			waitpid(pid1,NULL,0);
			waitpid(pid2,NULL,0);
			printf("已经成功回收\n");

		}
		else if(pid2==0)
		{
			printf("pid2子进程:%d,拷贝前一半的内容\n",getpid());
			if ((fd1 = open(argv[1],O_RDONLY))==-1)
			{
				printf("源文件打开失败\n");
				return -1;
			}
			printf("源文件打开成功\n");

			if ((fd2 = open(argv[2],O_RDWR|O_CREAT|O_TRUNC))==-1)
			{
				printf("目标文件打开失败\n");
				return -1;

			}
			printf("目标文件打开成功\n");

			char str[128]="";
			while (num<(len/2)&&(s=read(fd1,str,sizeof(str)>(len/2-num)?(len/2-num):sizeof(str)))!=0)
			{
				write(fd2,str,s);
				num=s+num;
				bzero(str,sizeof(str));

			}

			close(fd1);
			close(fd2);
			exit(EXIT_SUCCESS);
		}
	}
	else if (pid1==0)
	{
		num=0;
		printf("子进程:%d,拷贝后一半的内容\n",getpid());
		if ((fd1 = open(argv[1],O_RDONLY))==-1)
		{
			printf("源文件打开失败\n");
			return -1;
		}
		printf("源文件打开成功\n");

		if ((fd2 = open(argv[2],O_WRONLY|O_CREAT|O_APPEND))==-1)
		{
			printf("目标文件打开失败\n");
			return -1;

		}
		printf("目标文件打开成功\n");

		lseek(fd1,len/2,SEEK_SET);
		lseek(fd2,len/2,SEEK_SET);

		char str[128]="";

		while (num<(len-(len/2))&&(s=read(fd1,str,sizeof(str)>(len/2-num)?(len/2-num):sizeof(str)))!=0)
		{
			write(fd2,str,s);
			num=s+num;
			bzero(str,sizeof(str));
		}

		printf("子进程已拷贝\n");
		close(fd1);
		close(fd2);
		exit(EXIT_SUCCESS);
	}

	return 0;
}
相关推荐
代码充电宝3 分钟前
LeetCode 算法题【中等】189. 轮转数组
java·算法·leetcode·职场和发展·数组
微笑尅乐1 小时前
从递归到迭代吃透树的层次——力扣104.二叉树的最大深度
算法·leetcode·职场和发展
im_AMBER1 小时前
Leetcode 28
算法·leetcode
杰 .1 小时前
C++ Hash
数据结构·c++·哈希算法
让我们一起加油好吗1 小时前
【基础算法】多源 BFS
c++·算法·bfs·宽度优先·多源bfs
B站计算机毕业设计之家1 小时前
深度学习实战:python动物识别分类检测系统 计算机视觉 Django框架 CNN算法 深度学习 卷积神经网络 TensorFlow 毕业设计(建议收藏)✅
python·深度学习·算法·计算机视觉·分类·毕业设计·动物识别
韧竹、1 小时前
数据结构之单链表
数据结构·链表
And_Ii1 小时前
LeetCode 3350. 检测相邻递增子数组 II
数据结构·算法·leetcode
想唱rap1 小时前
C++ string类的使用
开发语言·c++·笔记·算法·新浪微博
JAVA学习通1 小时前
Replication(下):事务,一致性与共识
大数据·分布式·算法