#include <myhead.h>
int main(int argc, const char *argv[])
{
pid_t pid;
pid=fork();
if(pid==0)
{
int fd,fd3;
fd=open("1.txt",O_RDONLY);
if(fd==-1)
{
perror("open 1.txt");
exit(EXIT_FAILURE);
}
fd3=open("2.txt",O_WRONLY|O_TRUNC|O_CREAT,0664);
{
if(fd3==-1)
{
perror("open 2.txt");
exit(EXIT_FAILURE);
}
char buff[1024];
while(1)
{
int len=read(fd,buff,sizeof(buff));
write(fd3,buff,len/2);
{
if(len==0)
{
break;
}
}
printf("子进程1拷贝成功\n");
}
}
}
else if(pid>0)
{
pid_t pid2;
pid2=fork();
if(pid2==0)
{
int fd1,fd2;
fd1=open("1.txt",O_RDONLY);
if(fd1==-1)
{
perror("open 1.txt");
exit(EXIT_FAILURE);
}
fd2=open("2.txt",O_WRONLY|O_TRUNC|O_CREAT,0664);
if(fd2==-1)
{
perror("open 2.txt");
exit(EXIT_FAILURE);
}
char buff[1024];
while(1)
{
int len=read(fd1,buff,sizeof(buff));
write(fd2,buff,len);
if(len==0)
{
break;
}
}
printf("子进程2拷贝成功\n");
}
else if(pid2>0)
{
wait(NULL);
wait(NULL);
printf("父进程回收成功\n");
sleep(3);
exit(EXIT_SUCCESS);
}
}
else
{
perror("fork");
return -1;
}
while(1);
return 0;
}