#include <myhead.h>
int main(int argc, const char *argv[])
{
char arr[100];
ssize_t read_b,write_b;
pid_t pid;
pid=fork();
if(pid==0)
{
int Q;
Q=open("./1.txt",O_RDONLY);
if(Q==-1)
{
perror("open");
return -1;
}
read_b=read(Q,arr,100);
if(read_b==-1)
{
perror("read");
close(Q);
return -1;
}
close(Q);
Q=open("./2.txt",O_WRONLY | O_CREAT | O_TRUNC,0664);
if(Q==-1)
{
perror("open");
return -1;
}
write_b=write(Q,arr,read_b);
if(write_b==-1|| write_b!=read_b)
{
perror("write");
return -1;
}
close(Q);
}
else if(pid>0)
{
int T;
T=open("./3.txt",O_RDONLY);
if(T==-1)
{
perror("open");
return -1;
}
read_b=read(T,arr,100);
if(read_b==-1)
{
perror("read");
return -1;
}
close(T);
T=open("./4.txt",O_WRONLY | O_CREAT | O_TRUNC,0664);
if(T==-1)
{
perror("open");
return -1;
}
write_b= write(T,arr,read_b);
if(write_b==-1||write_b!=read_b)
{
perror("write");
return -1;
}
close(T);
}
else
{
perror("fork");
return -1;
}
return 0;
}
创建子父进程,子进程将1.txt内容拷贝到2.txt中,父进程将3.txt内容拷贝到4.txt中。
最新推荐文章于 2024-08-28 22:53:24 发布