0804 IO进程day7

1)要求AB进程做通信

        1. A进程发送一句话,B进程接收打印

        2. 然后B进程发送给A进程一句话,A进程接收打印

        3. 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;

A进程

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<unistd.h>
#include<strings.h>
#include<string.h>

int main(int argc, const char *argv[])
{
	if(mkfifo("./fifo1",0664)<0)
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./fifo2",0664)<0)
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	printf("创建管道成功\n");
	int fd_1 =open("./fifo1",O_RDONLY);
	if(fd_1<0)
	{
		perror("open");
		return -1;
	}
	int fd_2=open("./fifo2",O_WRONLY);
	if(fd_2<0)
	{
		perror("open");
		return -1;
	}

	printf("打开管道成功\n");
	char str[50]="";
	ssize_t res = 0;
	while(1)
	{
		bzero(str, sizeof(str));
		res = read(fd_1,str,50);
		if(res < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res)
		{
			printf("写端关闭\n");
			break;
		}
		printf("%s\n",str);
		bzero(str, sizeof(str));
		fgets(str, sizeof(str), stdin);
		str[strlen(str)-1] = '\0';
		if(write(fd_2,str,50)<0)
		{
			perror("write");
			return -1;
		}

	}
	close(fd_1);
	close(fd_2);
	return 0;
}

B进程

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

int main(int argc, const char *argv[])
{
	if(mkfifo("./fifo1",0664)<0)
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./fifo2",0664)<0)
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	printf("创建管道成功\n");
	int fd_1=open("./fifo1",O_WRONLY);
	if(fd_1<0)
	{
		perror("open");
		return -1;
	}
	int fd_2=open("./fifo2",O_RDONLY);
	if(fd_2<0)
	{
		perror("open");
		return -1;
	}
	printf("打开管道成功\n");
	char str[50]="";
	ssize_t res = 0;
	while(1)
	{
		bzero(str, sizeof(str));
		fgets(str, sizeof(str), stdin);
		str[strlen(str)-1] = '\0';
		if(write(fd_1,str,50)<0)
		{
			perror("write");
			return -1;
		}
		bzero(str, sizeof(str));
		res = read(fd_2,str,50);
		if(res < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res)
		{
			printf("写端关闭\n");
			break;
		}
		printf("%s\n",str);

	}
	close(fd_1);
	close(fd_2);
	return 0;
}



2)在第一题的基础上实现,AB进程能够随时收发数据(附加题)

        A进程

        

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

int fd_1;
int fd_2;
sem_t sem1;
sem_t sem2;

void *pi_pe1(void *arg)
{
	char str[50]="";
	ssize_t res = 0;
	while(1)
	{
		bzero(str, sizeof(str));
		
		res = read(fd_1,str,50);
		if(res < 0)
		{
			perror("read");
			return NULL;
		}
		else if(0 == res)
		{
			printf("写端关闭\n");
			break;
		}

		printf("%s\n",str);
	}
	pthread_exit(NULL);
}

void *pi_pe2(void *arg)
{
	char str[50]="";
	while(1)
	{
		bzero(str, sizeof(str));
		fgets(str, sizeof(str), stdin);
		str[strlen(str)-1] = '\0';
		//
		
		if(write(fd_2,str,50)<0)
		{
			perror("write");
			return NULL;
		}
		//
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	if(mkfifo("./fifo1",0664)<0)
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./fifo2",0664)<0)
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	printf("创建管道成功\n");
	fd_1 =open("./fifo1",O_RDONLY);
	if(fd_1<0)
	{
		perror("open");
		return -1;
	}
	fd_2=open("./fifo2",O_WRONLY);
	if(fd_2<0)
	{
		perror("open");
		return -1;
	}

	printf("打开管道成功\n");
	
	pthread_t td1;
	pthread_t td2;
	if(pthread_create(&td1,NULL,pi_pe1,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}
	if(pthread_create(&td2,NULL,pi_pe2,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}
	
	pthread_join(td1,NULL);
	pthread_join(td2,NULL);
	close(fd_1);
	close(fd_2);
	return 0;
}

        B进程

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

int fd_1;
int fd_2;

void *pi_pe1(void *arg)
{
	char str[50]="";
	while(1)
	{
		bzero(str, sizeof(str));
		fgets(str, sizeof(str), stdin);
		str[strlen(str)-1] = '\0';
		if(write(fd_1,str,50)<0)
		{
			perror("write");
			return NULL;
		}
	}
	pthread_exit(NULL);
}

void *pi_pe2(void *arg)
{
	char str[50]="";
	ssize_t res = 0;
	while(1)
	{
		bzero(str, sizeof(str));

		res = read(fd_2,str,50);
		if(res < 0)
		{
			perror("read");
			return NULL;
		}
		else if(0 == res)
		{
			printf("写端关闭\n");
			break;
		}
		printf("%s\n",str);
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	if(mkfifo("./fifo1",0664)<0)
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./fifo2",0664)<0)
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	printf("创建管道成功\n");
	fd_1=open("./fifo1",O_WRONLY);
	if(fd_1<0)
	{
		perror("open");
		return -1;
	}
	fd_2=open("./fifo2",O_RDONLY);
	if(fd_2<0)
	{
		perror("open");
		return -1;
	}
	printf("打开管道成功\n");
	pthread_t td1;
	pthread_t td2;
	if(pthread_create(&td1,NULL,pi_pe1,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}
	if(pthread_create(&td2,NULL,pi_pe2,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}

	pthread_join(td1,NULL);
	pthread_join(td2,NULL);
	close(fd_1);
	close(fd_2);
	return 0;
}

         



3)捕获2)3)20)号信号

        2)

#include<stdio.h>
#include<signal.h>
#include<unistd.h>

typedef void (*sighandler_t)(int);
void capture(int signum)
{
	printf("被我改成这样啦\n");
}
int main(int argc, const char *argv[])
{
	sighandler_t s=signal(2,capture);
	if(SIG_ERR==s)
	{
		perror("signal");
		return -1;
	}
	while(1)
	{
		printf("关不掉啦\n");
		sleep(1);
	}
	return 0;
}

3)

#include<stdio.h>
#include<signal.h>
#include<unistd.h>

typedef void (*sighandler_t)(int);
void capture(int signum)
{
	printf("被我改成这样啦\n");
}
int main(int argc, const char *argv[])
{
	sighandler_t s=signal(3,capture);
	if(SIG_ERR==s)
	{
		perror("signal");
		return -1;
	}
	while(1)
	{
		printf("关不掉啦\n");
		sleep(1);
	}
	return 0;
}

 

20)

#include<stdio.h>
#include<signal.h>
#include<unistd.h>

typedef void (*sighandler_t)(int);
void capture(int signum)
{
	printf("被我改成这样啦\n");
}
int main(int argc, const char *argv[])
{
	sighandler_t s=signal(20,capture);
	if(SIG_ERR==s)
	{
		perror("signal");
		return -1;
	}
	while(1)
	{
		printf("关不掉啦\n");
		sleep(1);
	}
	return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值