IO进程线程Day7

 1.实现两个终端的相互聊天

1 #include <myhead.h>                                                                                                                                                        
  2 #define ERR_MSG(msg) do{fprintf(stderr,"__%d__",__LINE__);\
  3                         perror(msg);\
  4                         }while(0)
  5 int main(int argc, const char *argv[])
  6 {
  7     //myfifo1  user1写,user2读
  8     //myfifo2  user1读,user2写
  9     if(-1==access("myfifo1",F_OK)){
 10         if(mkfifo("./myfifo1",0664)<0){
 11             ERR_MSG("mkfifo");
 12             return -1;
 13         }
 14     }
 15 
 16     if(-1==access("myfifo2",F_OK)){
 17         if(mkfifo("./myfifo2",0664)<0){
 18             ERR_MSG("mkfifo");
 19             return -1;
 20         }
 21     }
 22     int rfd=open("./myfifo2",O_RDONLY);
 23     if(rfd<0){
 24         ERR_MSG("open");
 25         return -1;
 26     }
 27     int wfd=open("./myfifo1",O_WRONLY);
 28     if(wfd<0){
 29         ERR_MSG("open");
 30         return -1;
 31     }
 32 
 33     char buf[128]="";
 34     int res;
 35     pid_t pid=fork();
 36     if(pid>0){
 37         while(1){
 38             //user1写
 39             bzero(buf,sizeof(buf));
 40             printf("请输入>>>");
 41             fgets(buf,sizeof(buf),stdin);
 42             buf[strlen(buf)-1]='\0';
 43             write(wfd,buf,sizeof(buf));
 44             if(0==strcmp(buf,"quit")){
 45                 printf("user1 offline\n");
 46                 break;
 47             }
 48         }
 49         exit(0);
 50     }else if(0==pid){
 51         //user1读
 52         while(1){
 53             bzero(buf,sizeof(buf));
 54             res=read(rfd,buf,sizeof(buf));
 55             if(res<0){
 56                 ERR_MSG("read");
 57                 break;
 58             }
 59             if(0==strcmp(buf,"quit")){
 60                 printf("user2 offline\n");
 61                 break;
 62             }
 63             printf("user2:%s\n",buf);
 64         }
 65         exit(0);
 66     }else{
 67         ERR_MSG("fork");
 68         return -1;
 69     }
 70     close(rfd);
 71     close(wfd);
 72     return 0;
 73 }

2.计算三角形面积和长方形面积

#include <myhead.h>
#include <math.h>
//1.c负责从键盘输入三角行的三边长,或者长方形的长和宽
//2.c负责根据1.c输入的数据计算三角形或者长方形的面积
int main(int argc, const char *argv[])
{
	int pipefd[2];
	double data[3];
	double s;
	pipe(pipefd);
	pid_t pid=fork();
	if(pid>0){
		close(pipefd[1]);
		while(1){
			read(3,data,24);
			printf("%g %g %g\n",data[0],data[1],data[2]);
			if(data[2]==0.0){
				//说明是长方形
				s=data[0]*data[1];
				printf("长方形面积%g\n",s);
		//		sleep(1);
			}
			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);
		//		sleep(1);
			}
		}
	}else if(0==pid){
		close(pipefd[0]);
		char wfd[4]={0};
		sprintf(wfd,"%d",pipefd[1]);
		if(execl("./b","b",wfd,NULL)!=0){
			perror("execl");
			return -1;
		}
	}else{
		perror("fork");
		return -1;
	}
	return 0;
}
//替换的进程
#include <myhead.h>
int main(int argc, const char *argv[])
{
	int wfd=atoi(argv[1]);
	while(1){
		double data[3];
		//从键盘输入三角行的三边长,或者长方形的长和宽
		printf("三角形三边长或长方形长和宽:");
		scanf("%lf %lf %lf",data,data+1,data+2);
		write(wfd,data,24);
		sleep(1);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值