IO / day04 作业

1.使用两个子进程完成两个文件的拷贝,子进程1拷贝前一半内容,子进程2拷贝后一半内容,父进程用于回收两个子进程的资源

代码

#include <myhead.h>

int main(int argc, const char *argv[])
{
	if(argc<3)
	{
		printf("input error\n");
		printf("usage: ./a.out src dest\n");
		return -1;
	}

	char psrc[256]="./"; // src
	strcat(psrc, argv[1]);
	printf("psrc=%s\n", psrc);

	char pdest[256]="./"; //dest
	strcat(pdest, argv[2]);
	printf("pdest=%s\n", pdest);

	struct stat stbuf;
	int fsize=0;
	stat(psrc, &stbuf);
	fsize = stbuf.st_size; // get file size

	int rpid1=-1; 

	rpid1=fork(); // 

	if(rpid1 > 0)
	{
		int rpid2 = -1;

		rpid2 = fork();

		if(rpid2>0)
		{
			//printf("I am parent process\n");
			//printf("my 1st child process pid=%d\n", rpid1);
			//printf("my 2nd child process pid=%d\n", rpid2);

			//recycle child process resources
			wait(NULL);
			wait(NULL);

		}else if(rpid2==0)
		{ // child process copy 1st half of file
			int fdr1;
			int fdw1;

			int count1 = 0;
			char buf1[128]="";

			if( (fdr1=open(psrc, O_RDONLY)) == -1 )
			{
				perror("p1 src open error;");
				return -1;
			}

			if( (fdw1=open(pdest, O_WRONLY|O_CREAT , 0777 )) == -1 )
			{
				perror("p1 dest open error;");
				return -1;
			}

			lseek(fdw1, 0, SEEK_SET);

			while(1)
			{

				int res1 = read(fdr1, buf1, sizeof(buf1));	
				//printf("res1=%d, buf1=%s", res1, buf1 );
				if(count1 + res1>=fsize/2) // read to the last time
				{
					write(fdw1, buf1, fsize/2-count1);
					break;

				}
				else
				{
					write(fdw1, buf1, res1);

				}

				lseek(fdw1, res1, count1); // move cursor to next one
				count1+=res1;


			}

			close(fdr1); // close fd
			close(fdw1);


			printf("I am the 2nd child process\n");
			//exit process
			exit(EXIT_SUCCESS);


		}
		else
		{
			perror("fork error");
			return -1;

		}

	}else if(rpid1==0)
	{
		// child process copy 1st half of file



		int fdr2;
		int fdw2;

		int count2 = 0;
		char buf2[128]="";

		if( (fdr2=open(psrc, O_RDONLY)) == -1 )
		{
			perror("p2 src open error:");
			return -1;
		}

		if( (fdw2=open(pdest, O_WRONLY|O_CREAT ,0777 )) == -1 )
		{
			perror("p2 src open error;");
			return -1;
		}

		lseek(fdr2, (fsize/2+1), SEEK_SET); // move cursor to 2nd half
		lseek(fdw2, (fsize/2+1), SEEK_SET); // move cursor to 2nd half

		while(1)
		{

			int res2 = read(fdr2, buf2, sizeof(buf2));	
			printf("res2=%d, buf2=%s", res2, buf2 );

			if(count2 + res2>=fsize)
			{
				write(fdw2, buf2, fsize- (fsize/2)-count2);
				break;

			}
			else
			{
				write(fdw2, buf2, res2);

			}

			lseek(fdr2, res2+count2, SEEK_SET); // move cursor to next read postion
			lseek(fdw2, res2+ count2, SEEK_SET);

			count2+=res2;


		}

		close(fdr2); // close fd
		close(fdw2);

		printf("I am the 1st child process!\n");
		//exit
		exit(EXIT_SUCCESS);


	}else
	{
		perror("fork  error");
		return -1;

	}

	return 0;
}

运行结果

ubuntu@ubuntu:~/embedded/04_io/day04$ ./a.out test.txt test.bk.txt

-rwxrwxr-x 1 ubuntu ubuntu      32 12月  6 20:20 test.bk.txt
-rw-rw-r-- 1 ubuntu ubuntu      32 12月  6 19:37 test.txt

2.思维导图

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值