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;
}
相关推荐
KarrySmile15 小时前
Day11--HOT100--25. K 个一组翻转链表,138. 随机链表的复制,148. 排序链表
数据结构·链表·递归·哈希表·分治·hot100·灵茶山艾府
Goboy15 小时前
亲手算一遍神经网络的反向传播,才算入门深度学习!
人工智能·算法·ai编程
四七伵15 小时前
接口设计的“潜规则”:为何字段 key 偏爱英文?
数据结构·代码规范·设计
小屁孩大帅-杨一凡16 小时前
大模型gpt-4o 参数介绍
人工智能·深度学习·算法·机器学习
是三好16 小时前
单例模式(Singleton Pattern)
java·开发语言·算法·单例模式
大阳12316 小时前
51单片机(单片机基础,LED,数码管)
开发语言·单片机·嵌入式硬件·算法·51单片机·学习经验
HR Zhou16 小时前
DAG与云计算任务调度优化
算法·云计算·任务调度·调度
91刘仁德18 小时前
c++ 类和对象(上)
开发语言·c++·经验分享·笔记·算法
何妨重温wdys1 天前
贪心算法解决钱币找零问题(二)
算法·贪心算法