20230306 作业

实现AB进程对话,要求AB进程能够随时收发。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>

#define PIPE_NAME_1 "03_pipe_1"
#define PIPE_NAME_2 "03_pipe_2"

typedef void (*sighandler_t)(int);

void handler(int sig){
	if(SIGUSR1 == sig){
		exit(0);
	}
}

void *fun_write(void *arg){
	int fd = *(int *)arg;
	char buf[32]="";
	while(1){
		bzero(buf,sizeof(buf));
		//printf("B 等待输入:");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = '\0';

		if(write(fd ,buf ,sizeof(buf)) < 0){
			perror("write");
			pthread_exit(NULL);
		}

		if(strcmp(buf,"quit") == 0){
			kill(getpid(),SIGUSR1);
			break;
		}

	}
	pthread_exit(NULL);
}
void *fun_read(void *arg){
	int fd = *(int *)arg;

	char buf[32]="";
	ssize_t res=0;
	while(1){

		bzero(buf,sizeof(buf));
		res = read(fd,buf,sizeof(buf));
		if(res < 0){
			perror("read");
			pthread_exit(NULL);
		}else if(0 == res){
			break;
		}

		if(strcmp(buf,"quit") == 0){
			kill(getpid(),SIGUSR1);
			break;
		}

		printf("B buf=%s res=%ld\n",buf,res);

	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	//注册新的处理函数
	sighandler_t res = signal(SIGUSR1,handler);
	if(SIG_ERR == res){
		perror("signal");
		return -1;
	}
	printf("用户自定义信号SIGUSR1已捕获\n");
	//定义2个管道文件
	if(mkfifo(PIPE_NAME_1,0775) < 0){
		//printf("errno=%d\n",errno);
		if(EEXIST != errno){
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo(PIPE_NAME_2,0775) < 0){
		//printf("errno=%d\n",errno);
		if(EEXIST != errno){
			perror("mkfifo");
			return -1;
		}
	}

	int fd_r = open(PIPE_NAME_1,O_RDONLY);
	if(fd_r < 0){
		perror("open r");
		return -1;
	}
	int fd_w = open(PIPE_NAME_2,O_WRONLY);
	if(fd_w < 0){
		perror("open w");
		return -1;
	}
	
	pthread_t thread_write,thread_read;
	if(pthread_create(&thread_write,NULL,fun_write,&fd_w) != 0){
		perror("pthread_create write");
		return -1;
	}
	if(pthread_create(&thread_read,NULL,fun_read,&fd_r) != 0){
		perror("pthread_create read");
		return -1;
	}
	
	pthread_join(thread_write,NULL);
	pthread_join(thread_read,NULL);

	close(fd_w);
	close(fd_r);

	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>

#define PIPE_NAME_1 "03_pipe_1"
#define PIPE_NAME_2 "03_pipe_2"

typedef void (*sighandler_t)(int);

void handler(int sig){
	if(SIGUSR1 == sig){
		exit(0);
	}
}

void *fun_write(void *arg){
	int fd = *(int *)arg;
	char buf[32]="";
	while(1){
		bzero(buf,sizeof(buf));
		//printf("A 等待输入:");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = '\0';

		if(write(fd ,buf ,sizeof(buf)) < 0){
			perror("write");
			pthread_exit(NULL);
		}

		if(strcmp(buf,"quit") == 0){
			kill(getpid(),SIGUSR1);
			break;
		}

	}
	pthread_exit(NULL);
}
void *fun_read(void *arg){
	int fd = *(int *)arg;

	char buf[32]="";
	ssize_t res=0;
	while(1){

		bzero(buf,sizeof(buf));
		res = read(fd,buf,sizeof(buf));
		if(res < 0){
			perror("read");
			pthread_exit(NULL);
		}else if(0 == res){
			break;
		}

		if(strcmp(buf,"quit") == 0){
			kill(getpid(),SIGUSR1);
			break;
		}

		printf("A buf=%s res=%ld\n",buf,res);

	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	//注册新的处理函数
	sighandler_t res = signal(SIGUSR1,handler);
	if(SIG_ERR == res){
		perror("signal");
		return -1;
	}
	printf("用户自定义信号SIGUSR1已捕获\n");
	//定义2个管道文件
	if(mkfifo(PIPE_NAME_1,0775) < 0){
		//printf("errno=%d\n",errno);
		if(EEXIST != errno){
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo(PIPE_NAME_2,0775) < 0){
		//printf("errno=%d\n",errno);
		if(EEXIST != errno){
			perror("mkfifo");
			return -1;
		}
	}

	int fd_w = open(PIPE_NAME_1,O_WRONLY);
	if(fd_w < 0){
		perror("open w");
		return -1;
	}
	int fd_r = open(PIPE_NAME_2,O_RDONLY);
	if(fd_r < 0){
		perror("open r");
		return -1;
	}

	
	pthread_t thread_write,thread_read;
	if(pthread_create(&thread_write,NULL,fun_write,&fd_w) != 0){
		perror("pthread_create write");
		return -1;
	}
	if(pthread_create(&thread_read,NULL,fun_read,&fd_r) != 0){
		perror("pthread_create read");
		return -1;
	}
	
	pthread_join(thread_write,NULL);
	pthread_join(thread_read,NULL);

	close(fd_w);
	close(fd_r);

	return 0;
}
ubuntu@ubuntu:20230306_ipc$ ./3
用户自定义信号SIGUSR1已捕获
B buf=aaa res=32
B buf=vvv res=32
aaa
aaaa
ubuntu@ubuntu:20230306_ipc$ 

ubuntu@ubuntu:20230306_ipc$ ./4
用户自定义信号SIGUSR1已捕获
aaa
vvv
A buf=aaa res=32
A buf=aaaa res=32
quit


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值