![](https://i-blog.csdnimg.cn/blog_migrate/e50c5ff275bbdc9424521508521794c5.png)
1. 2个终端之间的互相聊天
2.三角形或长方形的面积
work1
#include<myhead.h>
int main(int argc, const char *argv[])
{
int pipefd[2] = {0};
pipe(pipefd);
int res = fork();
if(res > 0)
{
while(1)
{
double data[3] = {0};
printf("请输入三角形三边长或矩形的长和宽:");
scanf("%lf %lf %lf",data,data+1,data+2);
while(getchar()!=10);
write(pipefd[1],data,24);
sleep(1);
}
}
else if(res == 0)
{
char rfd[4] = {0};
sprintf(rfd,"%d",pipefd[0]);
execl("./2","2",rfd,NULL);
perror("execl");
}
return 0;
}
work2
#include<myhead.h>
int main(int argc, const char *argv[])
{
double data[3] = {0};
double s = 0;
int rfd = atoi(argv[1]);
while(1)
{
read(rfd,data,24);
if(data[2] == 0.0)
{
s = data[0] * data[1];
printf("长方形的面积为:%g\n",s);
}
else
{
double a = data[0];
double b = data[1];
double c = data[2];
double p = (a+b+c)/2;
s = sqrt(p * (p-a) * (p-b) * (p-c));
printf("三角形的面积为:%g\n",s);
}
}
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/53a2f6f13a03e871262b0d47136441f0.png)