2024.4.8

文章描述了如何使用FIFO(命名管道)在两个终端间实现异步消息传递,同时提供了一个示例代码,用于计算用户输入的长方形和三角形的面积。
摘要由CSDN通过智能技术生成

实现2个终端之间的互相聊天 要求:千万不要做出来2个终端之间的消息发送是一读一写的,一定要能够做到,一个终端发送n条消息,另一个终端一条消息都不回复都是没有问题的

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/un.h>
int main(int argc, const char *argv[])
{
	//读数据
	char myfifo[32]="./myfifo";
	int res=access(myfifo,F_OK);
	if(res==-1)
	{
		mkfifo(myfifo,0666);
	}
	int rea=fork();
	if(rea>0)
	{
	int fd=open(myfifo,O_RDONLY);
	char buf[128]={0};
	int len=0;
	while(1)
	{
		memset(buf,0,len);
		len=read(fd,buf,128);
		printf("收到的数据位:%128s\n",buf);
	}
	close(fd);
	}
	else if(rea==0)
	{
		int fd2=open(myfifo,O_WRONLY|O_TRUNC);
			char buf[128]={0};
			int len=0;
			while(1)
			{
				memset(buf,0,len);
				printf("请输入要发送的数据:\n");
				scanf("%128s",buf);
				while(getchar()!=10);
				len=strlen(buf);
				write(fd2,buf,len);
			}
			close(fd2);
	}
	return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/un.h>
int main(int argc, const char *argv[])
{
	//写数据
	char myfifo[32]="./myfifo";
	int res=access(myfifo,F_OK);
	if(res==-1)
	{
		mkfifo(myfifo,0666);
	}
	int rea=fork();
	if(rea>0)
	{
	int fd=open(myfifo,O_WRONLY|O_TRUNC);
	char buf[128]={0};
	int len=0;
	while(1)
	{
		memset(buf,0,len);
		printf("请输入要发送的数据:\n");
		scanf("%128s",buf);
		while(getchar()!=10);
		len=strlen(buf);
		write(fd,buf,len);

	}
	close(fd);
	}
	else if(rea==0)
	{
		int fd2=open(myfifo,O_RDONLY);
		char buf[128]={0};
		int len=0;
		while(1)
		{
			memset(buf,0,len);
			len=read(fd2,buf,128);
			printf("接到的数据为:%128s\n",buf);
		}
		close(fd2);
	}
	return 0;
}

2:再把计算长方形 三角形面积的题,重新写一遍

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/un.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)
	{
		//z准备一个字符数组,用来
		char rfd[4]={0};
		sprintf(rfd,"%d",pipefd[0]);
		execl("./4","4",rfd,NULL);
		perror("execl");
	}
	return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/un.h>
#include <math.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)
		{
			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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值