5月9日作业

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

1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <pthread.h>
9 #include <semaphore.h>
0 #include <wait.h>
1 #include <signal.h>
2 #include <sys/socket.h>
3 #include <arpa/inet.h>
4 #include <sys/socket.h>
5 #include <sys/ipc.h>
6 #include <sys/sem.h>
7 #include <semaphore.h>
8 #include <sys/msg.h>
9 #include <sys/shm.h>
0 #include <sys/un.h>
1 
2 int main(int argc, const char *argv[])
3 {
4     int retval=fork();
5     if(retval>0)
6     {
7         int fb=open("./TYS.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);
8         int buf[2]={0};
9         for(int i=0;i<2;i++)
0         {
1             scanf("%d",&buf[i]);
2         write(fb,buf+i,4);
3         }
4         close(fb);
5         wait(0);
6     }
7     else if(retval==0)                                            
8     {
9         sleep(3);
0         int fb1=open("./TYS.txt",O_RDONLY);
1         int buf1[2]={0};
2         for(int j=0;j<2;j++)
3         {
4 
5             read(fb1,buf1+j,4);
6 
7         }
8         close(fb1);
9     printf("长方形的面积=%d\n",buf1[0]*buf1[1]);
0     }
1     return 0;
2 }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值