IO 5.9号

创建一对父子进程: 父进程负责向文件中写入 长方形的长和宽 子进程负责读取文件中的长宽信息后,计算长方形的面积

 #include <myhead.h>
 int main(int argc, const char *argv[])
 {
     int retval=fork();
     if(retval>0){
         float length,width;
         int wfd=open("./rectangle",O_WRONLY|O_CREAT|O_TRUNC,0664);
         char buf[128]={0};
         printf("please enter length/width:");
         scanf("%f %f",&length,&width);
         while(getchar()!='\n');
     //  int size=strlen(buf);
         write(wfd,&length,sizeof(float));
         write(wfd,&width,sizeof(float));
         close(wfd);
         sleep(4);
 
     }else if(retval==0){
         sleep(8);
         float length,width;
         int rfd=open("./rectangle",O_RDONLY);
         read(rfd,&length,sizeof(float));
         read(rfd,&width,sizeof(float));
         float area=length*width;
         printf("the area of rectangle is:%g \n",area);
         close(rfd);
                                                                                          
     }else if(retval==-1){
         perror("fork");
 
     }
     return 0;
 }

相关推荐

  1. <span style='color:red;'>IO</span> 5.9<span style='color:red;'>号</span>

    IO 5.9

    2024-05-10 00:26:08      15 阅读
  2. IP && 端口

    2024-05-10 00:26:08       7 阅读
  3. 59. 螺旋矩阵 II

    2024-05-10 00:26:08       43 阅读
  4. IOS面试题编程机制 51-55

    2024-05-10 00:26:08       18 阅读
  5. 商城数据库(51 52 53 54 55 56 57 58 59 60)

    2024-05-10 00:26:08       13 阅读
  6. LeetCode59 螺旋矩阵 II

    2024-05-10 00:26:08       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-10 00:26:08       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-10 00:26:08       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-10 00:26:08       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-10 00:26:08       20 阅读

热门阅读

  1. 2024-5-6(Vue)

    2024-05-10 00:26:08       13 阅读
  2. 深度学习学习日记5.8

    2024-05-10 00:26:08       11 阅读
  3. 华为开启telnet两种方式

    2024-05-10 00:26:08       16 阅读
  4. 基于picklerpc的pytorch单算子测试[单算子远程测试]

    2024-05-10 00:26:08       14 阅读
  5. Scala基础学习-循环

    2024-05-10 00:26:08       9 阅读