1> 使用两个进程完成两个文件的拷贝,父进程拷贝前一半内容,子进程拷贝后一半内容,并且父进程要阻塞回收子进程资源
#include "myhead.h"
int main(int argc, const char *argv[])
{
pid_t pid;
pid=fork();//创建子进程
int fp,pp;
if((fp=open(argv[1],O_RDONLY))==-1)//只读打开文件1
{
perror("open error");
return -1;
}
if((pp=open(argv[2],O_WRONLY|O_TRUNC|O_CREAT,0664))==-1)//只写并且创建打开文件2
{
perror("open error");
return -1;
}
int size=lseek(fp,0,SEEK_END);//计算文件1大小
int halfsize=size/2;//求文件大小的一半
if(pid>0)//如果是父进程
{
lseek(fp,0,SEEK_SET);//文件1光标偏移到开头
lseek(pp,0,SEEK_SET);//文件2光标偏移到开头
char c;
for(int i=0;i<halfsize;i++)//对文件的前半进行操作
{
read(fp,&c,1);//读取文件1数据
write(pp,&c,1);//将数据写入文件2
}
printf("前半部分拷贝完毕\n");
wait(NULL);//阻塞等待回收子进程的资源
}
else if(pid==0)
{
char c;
lseek(fp,halfsize,SEEK_SET);//文件1光标偏移到中间位置
lseek(pp,halfsize,SEEK_SET);//文件2光标偏移到中间位置
for(int i=halfsize;i<size;i++)//对文件后半进行操作
{
read(fp,&c,1);//读取文件1数据
write(pp,&c,1);//将文件写入文件2
}
printf("后半部分拷贝完毕\n");
return -1;
}
else{
perror("fork error");
}
//关闭文件
close(fp);
close(pp);
printf("文件全部打印完成!\n");
return 0;
}
输出:
linux@linux:~/IO/04$ gcc zykb.c
linux@linux:~/IO/04$ ./a.out 02test.txt 03test.txt
前半部分拷贝完毕
后半部分拷贝完毕
文件全部打印完成!
linux@linux:~/IO/04$ cat 03test.txt
hyl
hhh
hhhohho
odaiohh
ddwaaho
ihcdaa
coaowwhhihao caio
ihao caio
linux@linux:~/IO/04$ cat 02test.txt
hyl
hhh
hhhohho
odaiohh
ddwaaho
ihcdaa
coaowwhhihao caio
ihao caio
2> 使用三个进程完成两个文件的拷贝,父进程拷贝前三分之一,子进程1拷贝中间三分之一,子进程2拷贝后三分之一,父进程要阻塞回收所有子进程资源
#include<myhead.h>
int main(int argc, const char *argv[])
{
pid_t pid1;
pid1 = fork();//创建子进程1
int fp,pp;
if((fp=open(argv[1],O_RDONLY))==-1)//只读打开文件1
{
perror("open error");
return -1;
}
if((pp=open(argv[2],O_WRONLY|O_TRUNC|O_CREAT,0664))==-1)//只写并且创建打开文件2
{
perror("open error");
return -1;
}
int size=lseek(fp,0,SEEK_END);//计算文件1大小
int size13=size/3;//求文件大小的1/3半
int size23=size13*2;
if(pid1 < 0)
{
perror("fork error");
return -1;
}else if(pid1 == 0)
{
sleep(2);
char c;
lseek(fp,size13,SEEK_SET);//文件1光标偏移到1/3位置
lseek(pp,size13,SEEK_SET);//文件2光标偏移到1/3位置
for(int i=0;i<size13;i++)//对文件后半进行操作
{
read(fp,&c,1);//读取文件1数据
write(pp,&c,1);//将文件写入文件2
}
printf("中间1/3半部分拷贝完毕\n");
return -1;
}else
{
pid_t pid2 = fork(); //在父进程中再创建一个子进程
if(pid2 < 0)
{
perror("fork error");
return -1;
}else if(pid2 == 0) //父进程中的子进程2
{
sleep(3);
char c;
lseek(fp,size23,SEEK_SET);//文件1光标偏移到中间位置
lseek(pp,size23,SEEK_SET);//文件2光标偏移到中间位置
for(int i=size23;i<size;i++)//对文件后半进行操作
{
read(fp,&c,1);//读取文件1数据
write(pp,&c,1);//将文件写入文件2
}
printf("后1/3分拷贝完毕\n");
return -1;
}else
{
lseek(fp,0,SEEK_SET);//文件1光标偏移到开头
lseek(pp,0,SEEK_SET);//文件2光标偏移到开头
char c;
for(int i=0;i<size13;i++)//对文件的前半进行操作
{
read(fp,&c,1);//读取文件1数据
write(pp,&c,1);//将数据写入文件2
}
printf("前1/3拷贝完毕\n");
wait(NULL);
wait(NULL);
}
}
close(fp);
close(pp);
printf("全部拷贝完成\n");
return 0;
}
输出:
linux@linux:~/IO/04$ gcc zykb2.c
linux@linux:~/IO/04$ rm 03test.txt
linux@linux:~/IO/04$ ./a.out 02test.txt 03test.txt
前1/3拷贝完毕
中间1/3半部分拷贝完毕
后1/3分拷贝完毕
全部拷贝完成
linux@linux:~/IO/04$ cat 03test.txt
hyl
hhh
hhhohho
odaiohh
ddwaaho
ihcdaa
coaowwhhihao caio
ihao caio
linux@linux:~/IO/04$ cat 02test.txt
hyl
hhh
hhhohho
odaiohh
ddwaaho
ihcdaa
coaowwhhihao caio
ihao caio
思维导图