2024/4/8 IOday7

发布于:2024-04-08 ⋅ 阅读:(143) ⋅ 点赞:(0)

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <dirent.h>
#include <semaphore.h>
#include <signal.h>
#include <math.h>
void sighandler(int signum){

	if(signum==SIGPIPE){
		printf("终端关闭\n");
		exit(0);
	}
}
void* task_A(void* arg){
    signal(SIGPIPE,sighandler);
	char myfifo1[32]="./myfifo1";//创建有名管道1,用来写数据
	int res1=access(myfifo1,F_OK);//检查管道文件是否存在
	if(res1==-1){
		mkfifo(myfifo1,0666);
	}
	int fd=open("./myfifo1",O_WRONLY|O_TRUNC);//写打开文件myfifo1
	char buf[128]={0};//发送端数组
	int len = 0;
	while(1){
        //发送端
		memset(buf,0,len);
		printf("请输入发送给终端2的内容:\n");
		scanf("%128s",buf);
		while(getchar()!=10);
		len=strlen(buf);
		write(fd,buf,len);//将buf数组中内容写入管道
	}
	close(fd);
}
void* task_B(void* arg){
	char myfifo2[32]="./myfifo2";//创建有名管道1,用来写数据
	int res2=access(myfifo2,F_OK);//检查管道文件是否存在
	if(res2==-1){
		mkfifo(myfifo2,0666);
	}
	int fd2=open("./myfifo2",O_RDONLY);

	char buf2[128]={0};//接收端数组
	int len2=0;

	while(1){
		//接收端
		memset(buf2,0,len2);
		len2=read(fd2,buf2,128);
		if(len2==0){
           break;
		}
		printf("接收终端2的数据wei:%s\n",buf2);
	 }
}
int main(int argc, const char *argv[])
{
	pthread_t id_a;
	pthread_t id_b;
	if(pthread_create(&id_a,NULL,task_A,NULL)!=0){
		perror("pthread_create");
		return -1;
	}
	if(pthread_create(&id_b,NULL,task_B,NULL)!=0){
		perror("pthread_create");
		return -1;
	}
	pthread_detach(id_a);
	pthread_detach(id_b);
	while(1){
	}

	return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <dirent.h>
#include <semaphore.h>
#include <signal.h>
#include <math.h>
void* task_A(void* arg){
    char myfifo1[32]="./myfifo1";//创建有名管道,用来写数据
	int res=access(myfifo1,F_OK);//检查管道文件是否存在
	if(res==-1){
		mkfifo(myfifo1,0666);
	}
	int fd=open("./myfifo1",O_RDONLY);
	char buf[128]={0};
   	int len=0;
	while(1){
		//接收端
		memset(buf,0,len);
		len=read(fd,buf,128);
		if(len==0){
			break;
		}
		printf("接收终端1的数据为:%s\n",buf);

	}
	close(fd);
}
void* task_B(void* arg){
    char myfifo2[32]="./myfifo1";//创建有名管道,用来写数据
	int res2=access(myfifo2,F_OK);//检查管道文件是否存在
	if(res2==-1){
		mkfifo(myfifo2,0666);
	}
    int fd2=open("./myfifo2",O_WRONLY|O_TRUNC);
	char buf2[128]={0};
	int len2=0;
	while(1){
		memset(buf2,0,len2);
		printf("请输入发送给终端1的内容:\n");
		scanf("%128s",buf2);
		while(getchar()!=10);
		len2=strlen(buf2);
		write(fd2,buf2,len2);
	}
}
int main(int argc, const char *argv[])
{
	pthread_t id_a;
	pthread_t id_b;
	if(pthread_create(&id_a,NULL,task_A,NULL)!=0){
		perror("pthread_create");
		return -1;
	}
	if(pthread_create(&id_b,NULL,task_B,NULL)!=0){
		perror("pthread_create");
		return -1;
	}
	pthread_detach(id_a);
	pthread_detach(id_b);
	while(1){
	}
    return 0;
}

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <dirent.h>
#include <semaphore.h>
#include <signal.h>
#include <math.h>
int main(int argc, const char *argv[])
{
	int pipefd[2]={0};
	pipe(pipefd);
	//pipefd[0]=3
	//pipefd[1]=4
	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;
}

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <dirent.h>
#include <semaphore.h>
#include <signal.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.0){
			s=data[0]*data[1];
			printf("长方形的面积=%g\n",s);
		}else{
			double a=data[0];
			double b=data[1];
			double c=data[2];
			double d=(a+b+c)/2;
			s=sqrt(d*(d-a)*(d-b)*(d-c));
			printf("三角形的面积=%g\n",s);
	}
	}
	return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到