图片拷贝父进程前半段子进程后半段

copy.c

#include
#include
#include
#include
#include
#include
#include
int main(int argc, const char *argv[])
{
	int map = open("map/cpy.jpg",O_RDONLY);
	int size = lseek(map,0,SEEK_END);
	if(-1==close(map))
	{
		perror("map");
	}
	int mid = size/2;
	pid_t son_pid = fork();
	if(son_pid>0)
	{	
		int map_p = open("map/cpy.jpg",O_RDONLY);
		int cmap = open("map/ccpy.jpg",O_RDWR | O_CREAT | O_APPEND,0777);
		char c;
		int i=0;
		while (i<mid)
		{
			read(map_p,&c,sizeof(c));
			write(cmap,&c,sizeof(c));
			i=lseek(map_p,0,SEEK_CUR);
		}
		close(map_p);
		close(cmap);
		wait(NULL);
	}else if(0==son_pid)
	{
		int i=0;
		char c;
		while(1)
		{
			int mapi = open("map/ccpy.jpg",O_RDWR);
			i=lseek(mapi,0,SEEK_END);
			close(mapi);
			if(i==mid)
			{
				break;
			}
		}
		int map_p = open("map/cpy.jpg",O_RDONLY);
		lseek(map_p,mid,SEEK_SET);
		int cmap = open("map/ccpy.jpg",O_RDWR | O_APPEND);
		lseek(cmap,mid,SEEK_SET);
		while (read(map_p,&c,sizeof(c))>0)
		{
			write(cmap,&c,sizeof(c));
		}
		close(map_p);
		close(cmap);
		exit(0);
	}else
	{
		perror("fork");
	}
	return 0;
}

图片拷贝父进程前半段子进程后半段_第1张图片
僵尸进程
图片拷贝父进程前半段子进程后半段_第2张图片
孤儿进程
图片拷贝父进程前半段子进程后半段_第3张图片
如使用非阻塞模式的waitpid
子进程结束后父进程还在运行,则子进程变成僵尸进程
图片拷贝父进程前半段子进程后半段_第4张图片

你可能感兴趣的:(笔记,c语言,科技)