#include <myhead.h>
int main (int argc, const char *argv[])
{
int fd1=open("./1.txt",O_RDONLY);
if(fd1==-1)
{
perror("fd1");
return -1;
}
int fd2=open("./2,txt",O_WRONLY|O_CREAT|O_TRUNC,0644);
if(fd2==-1)
{
perror("fd2");
return -1;
}
int fd3=open("./1.txt",O_RDONLY);
if(fd3==-1)
{
perror("fd1");
return -1;
}
int fd4=open("");
if(fd4==-1)
{
perror("fd1");
return -1;
}
pid_t ID;
ID=frok();
if(ID>0)
{
printf("这是父进程\n");
int len;
while(1)
{
char s{10}="a";
len=read(fd3,s,sizeof(s));
if(len==-1)
{
printf("读取错误\n");
break;
}
if(len==0)
{
printf("已读取3.txt\n");
break;
}
len=write(fd4,s,sizeof(s));
if(len==-1)
{
printf("写入错误\n");
break;
}
}
if(len!=-1)
{
printf("写入4.txt成功\n");
}
close(fd4);
fd4=open("./4.txt",O_RDONLY);
printf("4.txt的内容如下\n");
printf("11111111111\n");
while(1)
{
char r[10]="a";
len=read(fd4,r,sizeof(r));
if(len==-1)
{
printf("读取错误\n");
break;
}
if(len==0)
{
printf("读取成功\n");
break;
}
printf("%s",r);
}
printf("111111111111111\n");
}
else if(ID==0)
{
printf("子进程\n");
int len;
while(1)
{
char s[10]="a";
len=read(fd1,s,sizeof(s));
if(len==-1)
{
printf("读取错误\n");
break;
}
else if(len==0)
{
printf("1.txt读取成功\n");
break;
}
len=write(fd2,s,sizeof(s));
if(len==-1)
{
printf("写入失败\n");
break;
}
else if(len==0)
{
printf("写入2.txt成功\n");
break;
}
close(fd2);
fd2=open("./2.txt",O_RDONLY);
printf("2.txt内容如下\n");
printf("1111111111\n");
while(1)
{
char r[10]="a";
len=read(fd2,r,sizeof(r));
if(len==-1)
{
printf("读取错误\n");
break;
}
else if(len==0)
{
printf("读取成功\n");
break;
}
printf("%s",r);
}
printf("1111111111\n");
}
}
else{
perror("ID");
return -1;
}
close(fd1);
close(fd2);
close(fd3);
close(fd4);
return 0;
}