#include <myhead.h>
int main (int argc, const char *argv[])
{
pid_t pid;
pid=fork();
if(pid>0)
{
pid_t pid2;
pid2=fork();
if(pid==0)
{
printf("子进程2");
int fd1,fd3;
fd1=open("1.txt",O_RDONLY);
if(fd1==-1)
{
perror("open 1.txt");
return -1;
}
fd3=open("3.txt",O_WRONLY|O_TRUNC|O_CREAT,0664);
if(fd3==-1)
{
perror("open 3.txt");
return -1;
}
char debuff[100];
int len=lseek(fd1,1,SEEK_SET);
int len1=len/2;
lseek(fd1,-1,SEEK_END);
while(1)
{
read(fd1,debuff,len1);
write(fd3,debuff,len1)
if(len1=0)
{
break;
}
}
printf("子进程2拷贝成功\n");
}
}
else if(pid==0)
{
printf("子进程1");
int fd1,fd2;
fd1=open("1.txt",O_RDONLY);
if(fd==-1)
{
perror("open 1.txt");
return -1;
}
fd2=open("2.txt",O_WRONLY|O_TRUNC|O_CREAT,0664);
if(fd2==-1)
{
perror("open 2.txt");
return -1;
}
char buff[100];
int len=lseek(fd1,1,SEEK_SET);
int len1=len/2;
while(1)
{
read(fd1,buff,len1);
write(fd2,buff,len1);
if(len1==0)
{
break;
}
}
printf("子进程1拷贝成功\n");
else
{
perror("fork");
return -1;
}
return 0;
}