第一题
#include <myhead.h>
int main(int argc, const char *argv[])
{
pid_t pid1;
pid1 = fork();
int fd1 = open("./1.txt",O_RDWR);
if(fd1==-1)
{
perror("open");
return -1;
}
int fd2 = open("./1.txt",O_RDWR);
if(fd2==-1)
{
perror("open");
return -1;
}
int fd3 = open("./2.txt",O_RDWR);
if(fd3==-1)
{
perror("open");
return -1;
}
if(pid1==0)
{
char buf[100];
int len = read(fd1,buf,sizeof(buf));
write(fd3,buf,len/2);
sleep(3);
printf("子进程1要退出\n");
exit(EXIT_SUCCESS);
}
else if(pid1>0)
{
pid_t pid2;
pid2 = fork();
if(pid2==0)
{
char buf[100];
int len = read(fd2,buf,sizeof(buf));
lseek(fd2,len/2,SEEK_SET);
int len1 = read(fd2,buf,sizeof(buf));
lseek(fd3,len/2,SEEK_SET);
write(fd3,buf,len1);
sleep(3);
printf("子进程2要退出\n");
exit(EXIT_SUCCESS);
}
else if(pid2>0)
{
wait(NULL);
wait(NULL);
sleep(3);
printf("父进程要退出\n");
exit(EXIT_SUCCESS);
}
}
else
{
perror("fork");
return -1;
}
return 0;
}