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;
}
相关推荐
小O的算法实验室21 小时前
2026年ASOC,基于深度强化学习的无人机三维复杂环境分层自适应导航规划方法,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
‎ദ്ദിᵔ.˛.ᵔ₎21 小时前
LIST 的相关知识
数据结构·list
M--Y1 天前
Redis常用数据类型
数据结构·数据库·redis
郭涤生1 天前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法
༾冬瓜大侠༿1 天前
vector
c语言·开发语言·数据结构·c++·算法
Ricky111zzz1 天前
leetcode学python记录1
python·算法·leetcode·职场和发展
汀、人工智能1 天前
[特殊字符] 第58课:两个正序数组的中位数
数据结构·算法·数据库架构··数据流·两个正序数组的中位数
liu****1 天前
第16届省赛蓝桥杯大赛C/C++大学B组(京津冀)
开发语言·数据结构·c++·算法·蓝桥杯
汀、人工智能1 天前
[特殊字符] 第79课:分割等和子集
数据结构·算法·数据库架构·位运算·哈希表·分割等和子集
汀、人工智能1 天前
[特殊字符] 第74课:完全平方数
数据结构·算法·数据库架构·图论·bfs·完全平方数