#include <myhead.h>
int main(int argc, const char *argv[])
{
pid_t pid;
pid = fork();
if(pid>0)
{
printf("父进程");
int fd,fd1;
fd = open("./1.txt",O_RDONLY);
fd1 = open("./2.txt",O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd==-1)
{
perror("open");
return -1;
}
if(fd1==-1)
{
perror("open");
return -1;
}
int len = lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
char s[5];
read(fd,s,len);
write(fd1,s,len);
close(fd);
close(fd1);
fd1 = open("./2.txt",O_RDONLY);
if(fd1==-1)
{
perror("open");
return -1;
}
char buf[1024];
read(fd,buf,len);
printf("%s\n",buf);
close(fd1);
}
else if(pid==0)
{
printf("子进程");
int fd2,fd3;
fd2 = open("./3.txt",O_RDONLY);
fd3 = open("./4.txt",O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd2==-1)
{
perror("open");
return -1;
}
if(fd3==-1)
{
perror("open");
return -1;
}
int len = lseek(fd2,0,SEEK_END);
lseek(fd2,0,SEEK_SET);
char s[5];
read(fd2,s,len);
write(fd3,s,len);
close(fd2);
close(fd3);
fd2 = open("./3.txt",O_RDONLY);
if(fd2==-1)
{
perror("open");
return -1;
}
char buf[1024];
read(fd2,buf,len);
printf("%s\n",buf);
close(fd2);
}
else
{
printf("fork");
}
while(1);
return 0;
}
8.23作业
最新推荐文章于 2024-08-27 15:54:39 发布