#include<myhead.h>
int main(int argc, const char *argv[])
{
pid_t pid1;
pid1 = fork();
int fd1,fd2;
fd1 = open("./1.txt",O_RDONLY);
if(pid1 == 0)
{
fd2 = open("./2.txt",O_WRONLY|O_CREAT|O_TRUNC,0664);
char buff[1024];
int len = read(fd1,buff,sizeof(buff));
write(fd2,buff,len/2);
close(fd2);
exit(EXIT_SUCCESS);
}
else if(pid1>0)
{
pid_t pid2;
pid2 = fork();
if(pid2==0)
{
fd2 = open("./2.txt",O_WRONLY|O_CREAT|O_APPEND,0664);
char buff[1024];
int len = read(fd1,buff,sizeof(buff));
lseek(fd1,len/2,SEEK_SET);
char buff1[1024];
int len1 = read(fd1,buff1,len/2);
write(fd2,buff1,len/2);
printf("%s\n",buff1);
close(fd2);
exit(EXIT_SUCCESS);
}
else if(pid2>0)
{
wait(NULL);
wait(NULL);
exit(EXIT_SUCCESS);
}
}
else
{
perror("fork");
exit(EXIT_FAILURE);
}
close(fd1);
while(1);
return 0;
}
8.26作业
于 2024-08-26 21:45:47 首次发布