作业:
1:找出来我们之前写的链表的加载和保存的代码,实现,当按 ctrl + c的时候,保存链表 2:编写2个.c文件,生成2个可执行文件
1.c 输入正方形的长和宽 或者 三角形的三边长
2.c 输出长方形或者三角形的面积
要求数据通信使用无名管道实现
3:使用有名管道,实现2个终端之间的互相聊天功能 要求:能够做舔狗(能够并发)
作业1:
运行代码:
运行截图:
作业2:
运行代码:
输入:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<pthread.h>
#include<semaphore.h>
int main(int argc, const char *argv[])
{
//创建管道->while->fork()->父进程:写->子进程算,进程替换
int pipefd[2]={0};
if(pipe(pipefd)==-1){
perror("pipe");
return -1;
}
while(1){
int ret = fork();
if(ret>0){
double buf[3]={0};//放长、宽、高。
printf("请输入>>>");
scanf("%lf %lf %lf",buf,buf+1,buf+2);
while(getchar()!='\n');
write(pipefd[1],buf,sizeof(buf));
sleep(1);
}else if(ret == 0){
char str_fd[16]="";
sprintf(str_fd,"%d",pipefd[0]);
execl("./hw2",str_fd,NULL);
printf("进程替换失败\n");
}
}
return 0;
}
运算:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<pthread.h>
#include<semaphore.h>
#include<math.h>
int main(int argc, const char *argv[])
{
int fd = atoi(argv[0]);
double arr[3]={0};
read(fd,arr,sizeof(arr));
if(0==arr[2]){
double s=arr[0] * arr[1];
printf("正方形s==%g\n",s);
}else{
double a=arr[0];
double b=arr[1];
double c=arr[2];
double p = (a+b+c)/2;
double s= sqrt(p*(p-a)*(p-b)*(p-c));
printf("三角形面积为:%g\n",s);
}
return 0;
}