创建一对父子进程: 父进程负责向文件中写入 长方形的长和宽 子进程负责读取文件中的长宽信息后,计算长方形的面积
#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;
}