Day8--IO进程线程8

一、

进程A

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
char buf[128]="";
void *callback1(void *arg)
{
	int fd1=open("./myfifo1",O_WRONLY);
	if(fd1<0)
	{
		perror("open1");
		return NULL;
	}
	while(1)
	{
		bzero(buf,sizeof(buf));
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]='\0';
		if(write(fd1,buf,sizeof(buf))<0){
			perror("write");
			return NULL;
		}
		if(strcmp(buf,"quit")==0){
			exit(0);
		}
	}
}
void *callback2(void *arg)
{
	int fd2=open("./myfifo2",O_RDONLY);
	if(fd2<0)
	{
		perror("open");
		return NULL;
	}
	while(1)
	{
		bzero(buf,sizeof(buf));
		if(read(fd2,buf,sizeof(buf))<0){
			perror("read");
			return NULL;
		}
		if(strcmp(buf,"quit")==0){
			exit(0);
		}
		printf("%s\n",buf);
	}
}
int main(int argc, const char *argv[])
{
	printf("进程A\n");
	umask(0);
	if(mkfifo("./myfifo1",0664)<0)
	{
		if(17!=errno)
		{
			perror("mkfifo1");
			return -1;
		}
	}
	if(mkfifo("./myfifo2",0664)<0)
	{
		if(17!=errno)
		{
			perror("mkfifo2");
			return -1;
		}
	}
	pthread_t pid1,pid2;
	if(pthread_create(&pid1,NULL,callback1,NULL)<0)
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}
	pthread_detach(pid1);
	if(pthread_create(&pid2,NULL,callback2,NULL)<0)
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}
	pthread_join(pid2,NULL);
	return 0;
}

进程B

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
char buf[128]="";
void *callback1(void *arg)
{
	int fd1=open("./myfifo1",O_RDONLY);
	if(fd1<0)
	{
		perror("open1");
		return NULL;
	}
	while(1)
	{
		bzero(buf,sizeof(buf));
		if(read(fd1,buf,sizeof(buf))<0)
		{
			perror("read");
			return NULL;
		}
		if(strcmp(buf,"quit")==0){
			exit(0);
		}
		printf("%s\n",buf);
	}
}
void *callback2(void *arg)
{
	int fd2=open("./myfifo2",O_WRONLY);
	if(fd2<0)
	{
		perror("open");
		return NULL;
	}
	while(1)
	{
		bzero(buf,sizeof(buf));
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]='\0';
		if(write(fd2,buf,sizeof(buf))<0)
		{
			perror("write");
			return NULL;
		}
		if(strcmp(buf,"quit")==0){
			exit(0);
		}
	}
}
int main(int argc, const char *argv[])
{
	printf("进程B\n");
	umask(0);
	if(mkfifo("./myfifo1",0664)<0)
	{
		if(17!=errno)
		{
			perror("mkfifo1");
			return -1;
		}
	}
	if(mkfifo("./myfifo2",0664)<0)
	{
		if(17!=errno)
		{
			perror("mkfifo2");
			return -1;
		}
	}
	pthread_t pid1,pid2;
	if(pthread_create(&pid1,NULL,callback1,NULL)<0)
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}
	pthread_detach(pid1);
	if(pthread_create(&pid2,NULL,callback2,NULL)<0)
	{
		fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
		return -1;
	}
	pthread_join(pid2,NULL);
	return 0;
}

二、捕获2,3,20信号

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
typedef void (*sighandler_t)(int);
//处理函数
void handler(int sig){
	printf("触发%d信号\n",sig);
}
int main(int argc, const char *argv[])
{
	//捕获信号
	//int n;
	//printf("请输入需要捕获的信号\n");
	//fscanf(stdin,"%d",&n);
	__sighandler_t s1=signal(2,handler);
	__sighandler_t s2=signal(3,handler);
	__sighandler_t s3=signal(20,handler);
	if(SIG_ERR==s1){
		perror("signal");
		return -1;
	}
	if(SIG_ERR==s2){
		perror("signal");
		return -1;
	}
	if(SIG_ERR==s3){
		perror("signal");
		return -1;
	}
	while(1){
		printf("signal...\n");
		sleep(1);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值