io-day4

  1 #include <head.h>
  2 int main(int argc, const char *argv[])
  3 {
  4     int pid1=-1;
  5     pid1=fork();//复制进程
  6     int fd=-1;//定义fd,fd1文件指针用于子进程2
  7     int fd1=-1;
  8     int fd2=-1;//定义fd2,fd3文件指针用于进程1
  9     int fd3=-1;
 10     if (pid1>0)
 11     {
 12         //父进程
 13         int pid2=-1;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
 14         pid2=fork();
 15         if (pid2>0)
 16         {
 17             //父进程
 18             wait(NULL);//以阻塞的形式回收子进程的资源,因为有2个所以要wait(NULL)2次
 19             wait(NULL);
 20         }
 21         else if(pid2==0)
 22         {
 23             //子进程2
 24             if ((fd=open("./a.txt",O_RDONLY))==-1)//以只读的形式打开文件
 25             {
 26                 perror("open error");
 27                 return -1;
 28             }
 29 
 30             if ((fd1=open("./b.txt",O_WRONLY|O_CREAT,0664))==-1)
 31             {
 32                 perror("open error");
 33                 return -1;
 34             }
 35             //定义一个count用来累加复制的字节
 36             //定义一个buf搬运工,用来复制
 37             //SUM_h是总字节大小
 38             //mid是中间值
 39             int count=0;
 40             char buf[10]="";
 41             int SUM_h=lseek(fd,0,SEEK_END);
 42             int mid=SUM_h/2;
 43             printf("mid = %d\n",mid);
 44             lseek(fd,0,SEEK_SET);
 45             //循环读fd,写fd1
 46             while(1)
 47             {
 48                 int res=read(fd,buf,sizeof(buf));//每次读buf即(10)个,返回值是读取的字节个数
 49                 count=count+res;
 50                 if (count>mid)//比较读取的字节大小和二分之一文件大小,如果读取的字节大小大于二分之一文件,那么读取过sizeof(buf)倍数后,还有剩余字节未读,要怎么读进去
 51                 {
 52                     write(fd1,buf,mid-(count-res)); //此处的res就是sizeof(buf)个
 53                     break;
 54                 }
 55                 else
 56                 {
 57                     write(fd1,buf,res);//否则就是正常读取
 58                 }
 59             }
 60             close(fd);//关闭文件
 61             close(fd1);
 62             exit(EXIT_SUCCESS);//正常退出进程
 63 
 64         }
 65         else
 66         {
 67             perror("fork error");//复制进程失败
 68             return -1;
 69         }
 70 
 71     }
 72     else if(pid1==0)
 73     {
 74     //子进程1
 75             if ((fd2=open("./a.txt",O_RDONLY))==-1)
 76             {
 77                 perror("open error");
 78                 return -1;
 79             }
 80             if ((fd3=open("./b.txt",O_WRONLY|O_CREAT,0664))==-1)
 81             {
 82                 perror("open error");
 83                 return -1;
 84             }
 85             char buf[10]="";
 86             int SUM_h=lseek(fd,0,SEEK_END);
 87             int mid=SUM_h/2;
 88             lseek(fd2,mid,SEEK_SET);//将写光标移到中间位置
 89             lseek(fd3,mid,SEEK_SET);//将读光标也移到中间位置
 90             int res=-1;//定义一个res,表示read的返回值
 91             while((res=read(fd2,buf,sizeof(buf)))>0)//>0是因为read的正常返回是读的数据大小,是一个大于0的数,异常是-1,如果没读到数据是0
 92             {
 93                 write(fd3,buf,res);
 94             }
 95             close(fd2);
 96             close(fd3);
 97             exit(EXIT_SUCCESS);
 98 
 99     }
100     else
101     {
102         perror("fork error");
103         return -1;
104     }
105     return 0;
106 }
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
~                    

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值