系统编程 --- Day4 进程篇

双进程的拷贝文件:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<time.h>
#include<unistd.h>
#include<wait.h>

int main(int argc, const char *argv[])
{
	umask(0);
	int f_r = open("/home/ubuntu/Pictures/a.png",O_RDONLY);
	if (f_r < 0)
	{
		printf("%d ",__LINE__);
		perror("open");
		return -1;
	}
	int f_w = open("./cpy2.png",O_WRONLY|O_CREAT|O_TRUNC,0664);
	if (f_w < 0)
	{
		printf("%d ",__LINE__);
		perror("open");
		return -1;
	}
	off_t size = lseek(f_r,0,SEEK_END);
	pid_t p = fork();
	if (p>0)
	{
		waitpid(-1,NULL,0);
		printf("我是父进程,我的PID:%d,我的父进程的PID:%d\n",getpid(),getppid());
		lseek(f_r,0,SEEK_SET);
		lseek(f_w,0,SEEK_SET);
		char a = 0;
		int i;
		for (i=0;i<size/2;i++)
		{
			if (read(f_r,&a,sizeof(a))<0)
			{
				perror("read");
				return -1;
			}
			if (write(f_w,&a,sizeof(a))<0)
			{
				perror("write");
				return -1;
			}
		}
		printf("父进程:前半部分拷贝成功!\n");
	}
	else if (p==0)
	{
		printf("我是子进程,我的PID:%d,我的父进程的PID:%d\n",getpid(),getppid());

		lseek(f_r,size/2,SEEK_SET);
		lseek(f_w,size/2,SEEK_SET);
		char a = 0;
		int i;
		for (i=size/2;i<size;i++)
		{
			if (read(f_r,&a,sizeof(a))<0)
			{
				perror("read");
				return -1;
			}
			if (write(f_w,&a,sizeof(a))<0)
			{
				perror("write");
				return -1;
			}
		}
		printf("子进程:后半部分拷贝成功!\n");	
		exit(1);
	}
	else
	{
		perror("fork");
		return -1;
	}
	return 0;
}

三进程的文件拷贝:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<time.h>
#include<unistd.h>
#include<sys/wait.h>

int main(int argc, const char *argv[])
{
	FILE *f_r;
	if((f_r = fopen("./cpy.png","r"))==NULL)
	{
		perror("fopen f_r");
		return -1;
	}
	fseek(f_r,0,SEEK_END);
	long len = ftell(f_r);
	fseek(f_r,0,SEEK_SET);
	FILE *f_w;
	if ((f_w=fopen("./a.png","w"))==NULL)
	{
		perror("fopen f_w");
		return  -1;
	}
	printf("len = %ld\n",len);


	pid_t pid = fork();
	if (pid > 0)
	{
		pid_t pid2 = fork();
		if (pid2 > 0)
		{
			wait(NULL);
			wait(NULL);
			int i;
			char buf;
			int n = 0;
			for (i=0;i<len/3;i++)
			{
				fseek(f_r,i,SEEK_SET);
				fseek(f_w,i,SEEK_SET);
				printf("%ld %ld ",ftell(f_r),ftell(f_w));
				if (!(i%10))
				{
					printf("\n");
				}
				if ((n = fread(&buf,1,1,f_r))>0)
				{
					fwrite(&buf,1,1,f_w);
					fflush(f_w);
				}
				//printf("%c",buf);
				buf = 0;
			}
			printf("len/3-0 = %ld\n",len/3-0);
			printf("父进程完成前三分之一\n");
		}
		else if (pid2 == 0)
		{
			int i;
			char buf;
			int n = 0;
			for (i=len/3;i<len-len/3;i++)
			{
				fseek(f_r,i,SEEK_SET);
				fseek(f_w,i,SEEK_SET);
				if ((n = fread(&buf,1,1,f_r))>0)
				{
					fwrite(&buf,1,1,f_w);
					fflush(f_w);
				}
				printf("%c",buf);
				buf = 0;
			}
			printf("len-len/3-len/3 = %ld\n",len-len/3-len/3);
			printf("子进程1完成中三分之一\n");

		}
		else
		{
			perror("fork_pid2");
			return -1;
		}
	}
	else if (pid == 0)
	{
		int i;
		char buf;
		int n = 0;
		for (i=len-len/3;i<len;i++)
		{
			fseek(f_r,i,SEEK_SET);
			fseek(f_w,i,SEEK_SET);
			if ((n = fread(&buf,1,1,f_r))>0)
			{
				fwrite(&buf,1,1,f_w);
				fflush(f_w);
			}
			printf("%c",buf);
			buf = 0;
		}
		printf("len/3 = %ld\n",len/3);
		printf("子进程2完成后三分之一\n");

	}
	else
	{
		perror("fork_pid");
		return -1;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值