8.4-IO进程线程-作业

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 <string.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
	printf("A进程\n");
	if(mkfifo("./myfifo_1",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./myfifo_2",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	
	int fd_1 = open("./myfifo_1",O_RDONLY);
	if(fd_1 < 0)
	{
		perror("open");
		return -1;
	}
	int fd_2 = open("./myfifo_2",O_WRONLY);
	if(fd_2 < 0)
	{
		perror("open");
		return -1;
	}
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		printf("A发送>>>");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = '\0';

		if(strcmp(buf,"quit") == 0)
		{
			break;
		}
		if(write(fd_2,buf,sizeof(buf)) < 0)
		{
			perror("open");
			return -1;
		}


		bzero(buf,sizeof(buf));
		printf("等待B进程的发送\n");
		res = read(fd_1,buf,sizeof(buf));
		if(res < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res)
		{
			break;
		}
		printf("接受到B的信息:%s\n",buf);
	}
	close(fd_1);
	close(fd_2);
	printf("A进程退出\n");
	return 0;
}

B线程

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

int main(int argc, const char *argv[])
{
	printf("B进程\n");
	//创建二个有名管道
	if(mkfifo("./myfifo_1",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./myfifo_2",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	//打开有名管道	
	int fd_1 = open("./myfifo_1",O_WRONLY);
	if(fd_1 < 0)
	{
		perror("open");
		return -1;
	}
	int fd_2 = open("./myfifo_2",O_RDONLY);
	if(fd_2 < 0)
	{
		perror("open");
		return -1;
	}
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		printf("等待A进程的发送\n");
		res = read(fd_2,buf,sizeof(buf));//读取A线程的数据
		if(res < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res)
		{
			break;
		}
		printf("接受到A的信息:%s\n",buf);

		printf("B发送>>>");
		fgets(buf,sizeof(buf),stdin);//发送给A线程
		buf[strlen(buf)-1] = '\0';	
		if(strcmp(buf,"quit") == 0)
		{
			break;//输入quit退出
		}

		if(write(fd_1,buf,sizeof(buf)) < 0)
		{
			perror("open");
			return -1;
		}
	}
	close(fd_1);//关闭文件
	close(fd_2);
	printf("B进程退出\n");
	return 0;
}

 

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

 A进程

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

void* callBack_w(void* arg)
{
	int fd_1 = *(int*)arg;
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		printf("A发送>>>");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = '\0';

		if(write(fd_1,buf,sizeof(buf)) < 0)
		{
			perror("open");
			return NULL;
		}

		if(strcmp(buf,"quit") == 0)
		{
			printf("退出程序\n");
			exit(0);
		}
	}
	close(fd_1);
}
void* callBack_r(void* arg)
{
	int fd_2 = *(int*)arg;
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		res = read(fd_2,buf,sizeof(buf));
		if(res < 0)
		{
			perror("read");
			return NULL;
		}
		else if(0 == res)
		{
			printf("退出程序\n");
			exit(0);
		}
		printf("接受到B的信息:%s\n",buf);
	}
	close(fd_2);

}
int main(int argc, const char *argv[])
{
	printf("A进程\n");
	//创建二个有名管道
	if(mkfifo("./myfifo_1",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./myfifo_2",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	//打开有名管道	
	int fd_1 = open("./myfifo_1",O_WRONLY);
	if(fd_1 < 0)
	{
		perror("open");
		return -1;
	}
	int fd_2 = open("./myfifo_2",O_RDONLY);
	if(fd_2 < 0)
	{
		perror("open");
		return -1;
	}
	//创建线程
	pthread_t tid_1,tid_2;
	if(pthread_create(&tid_1,NULL,callBack_w,(void*)&fd_1) != 0)
	{
		perror("pthread_create");
		return -1;
	}
	if(pthread_create(&tid_1,NULL,callBack_r,(void*)&fd_2) != 0)
	{
		perror("pthread_create");
		return -1;
	}
	pthread_join(tid_1,NULL);
	pthread_join(tid_2,NULL);

	return 0;
}

B进程 

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
void* callBack_w(void* arg)
{
	int fd_2 = *(int*)arg;
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		printf("B发送>>>");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = '\0';

		if(write(fd_2,buf,sizeof(buf)) < 0)
		{
			perror("open");
			return NULL;
		}		
		
		if(strcmp(buf,"quit") == 0)
		{
			printf("退出程序\n");
			exit(0);
		}
	}
	close(fd_2);
}
void* callBack_r(void* arg)
{
	int fd_1 = *(int*)arg;
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		res = read(fd_1,buf,sizeof(buf));
		if(res < 0)
		{
			perror("read");
			return NULL;
		}
		else if(0 == res)
		{
			printf("退出程序\n");
			exit(0);
		}
		printf("接受到A的信息:%s\n",buf);
	}
	close(fd_1);
}

int main(int argc, const char *argv[])
{
	printf("B进程\n");
	//创建二个有名管道
	if(mkfifo("./myfifo_1",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./myfifo_2",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	//打开有名管道	
	int fd_1 = open("./myfifo_1",O_RDONLY);
	if(fd_1 < 0)
	{
		perror("open");
		return -1;
	}
	int fd_2 = open("./myfifo_2",O_WRONLY);
	if(fd_2 < 0)
	{
		perror("open");
		return -1;
	}
	//创建线程
	pthread_t tid_1,tid_2;
	if(pthread_create(&tid_1,NULL,callBack_r,(void*)&fd_1) != 0)
	{
		perror("pthread_create");
		return -1;
	}
	if(pthread_create(&tid_1,NULL,callBack_w,(void*)&fd_2) != 0)
	{
		perror("pthread_create");
		return -1;
	}

	pthread_join(tid_1,NULL);
	pthread_join(tid_2,NULL);

	return 0;
}

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

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

typedef void(*sighandler_t)(int);

void handler_2(int sig)
{
	printf("输入了ctrl + c\n");
}
void handler_3(int sig)
{
	printf("输入了ctrl + \\ \n");
}
void handler_20(int sig)
{
	printf("输入了ctrl + z\n");
}

int main(int argc, const char *argv[])
{
	//捕获2号信号SIGINT	
	sighandler_t s = signal(2,handler_2);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	
	s = signal(3,handler_3);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	
	s = signal(20,handler_20);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	while(1)
		sleep(1);

	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值