04/04课后作业

1.要求实现AB进程对话

  1. A进程先发送一句话给B进程,B进程接收后打印
  2. B进程再回复一句话给A进程,A进程接收后打印
  3. 重复1.2步骤,当收到quit后,要结束AB进程
  4. 提示:两根管道

(1)

#include "head.h"
/*
1.要求实现AB进程对话

	A进程先发送一句话给B进程,B进程接收后打印
	B进程再回复一句话给A进程,A进程接收后打印
	重复1.2步骤,当收到quit后,要结束AB进程
*/

int main(int argc, const char *argv[])
{
	umask(0);
	// 创建有名管道
	if (mkfifo("./myfifo", 0664) < 0)
	{
		printf("errno=%d\n", errno);
		if (17 != errno)
		{
			perror("mkfifo"); // 合法错误
			return -1;
		}
	}
	printf("myfifo create success\n");

	char buf[128] = "";
	ssize_t res = 0;
	int flag = 0;

	while (1)
	{
		if (0 == flag)
		{
			// 以只写的方式打开有名管道,阻塞
			int fd = open("./myfifo", O_WRONLY);
			if (fd < 0)
			{
				perror("open");
				return -1;
			}
			// printf("open fifo success wronly  fd=%d\n", fd);

			printf("A请输入>>> ");
			fgets(buf, sizeof(buf), stdin);
			buf[strlen(buf) - 1] = 0; // 将'\n'修改成\0

			if (write(fd, buf, sizeof(buf)) < 0)
			{
				perror("write");
				return -1;
			}
			if (strcmp(buf, "quit") == 0)
				break;
			printf("写入成功\n");

			close(fd);
			flag = 1;
		}

		if (1 == flag)
		{
			// 以只读的方式打开有名管道,阻塞
			int fd = open("./myfifo", O_RDONLY);
			if (fd < 0)
			{
				perror("open");
				return -1;
			}
			// printf("open fifo success rdonly  fd=%d\n", fd);

			bzero(buf, sizeof(buf));
			res = read(fd, buf, sizeof(buf));
			if (res < 0)
			{
				perror("read");
				return -1;
			}
			else if (0 == res)
			{
				printf("写端退出\n");
				break;
			}
			printf("res=%ld  buf = %s\n", res, buf);
			if (strcmp(buf, "quit") == 0)
				break;

			close(fd);
			flag = 0;
		}
	}

	return 0;
}

 (2)

#include "head.h"
/*
1.要求实现AB进程对话

	A进程先发送一句话给B进程,B进程接收后打印
	B进程再回复一句话给A进程,A进程接收后打印
	重复1.2步骤,当收到quit后,要结束AB进程
*/

int main(int argc, const char *argv[])
{
	umask(0);
	// 创建有名管道
	if (mkfifo("./myfifo", 0664) < 0)
	{
		printf("errno=%d\n", errno);
		if (17 != errno)
		{
			perror("mkfifo"); // 合法错误
			return -1;
		}
	}
	printf("myfifo create success\n");

	char buf[128] = "";
	ssize_t res = 0;
	int flag = 0;

	while (1)
	{
		if (0 == flag)
		{
			// 以只读的方式打开有名管道,阻塞
			int fd = open("./myfifo", O_RDONLY);
			if (fd < 0)
			{
				perror("open");
				return -1;
			}
			//printf("open fifo success rdonly  fd=%d\n", fd);

			bzero(buf, sizeof(buf));
			res = read(fd, buf, sizeof(buf));
			if (res < 0)
			{
				perror("read");
				return -1;
			}
			else if (0 == res)
			{
				printf("写端退出\n");
				break;
			}
			printf("res=%ld  buf = %s\n", res, buf);
			if (strcmp(buf, "quit") == 0)
				break;

			close(fd);
			flag = 1;
		}

		if (1 == flag)
		{
			// 以只写的方式打开有名管道,阻塞
			int fd = open("./myfifo", O_WRONLY);
			if (fd < 0)
			{
				perror("open");
				return -1;
			}
			//printf("open fifo success wronly  fd=%d\n", fd);

			printf("B请输入>>> ");
			fgets(buf, sizeof(buf), stdin);
			buf[strlen(buf) - 1] = 0; // 将'\n'修改成\0

			if (write(fd, buf, sizeof(buf)) < 0)
			{
				perror("write");
				return -1;
			}
			if (strcmp(buf, "quit") == 0)
				break;
			printf("写入成功\n");

			close(fd);
			flag = 0;
		}
	}

	return 0;
}

2.捕获2 3 20号信号

#include "head.h"
/*
捕获2 3 20号信号
*/

typedef void (*sighandler_t)(int);

void handler(int sig)
{
	printf("触发sig=%d信号\n", sig);
}

int main(int argc, const char *argv[])
{
	void (*pfunc)(int) = handler;
	// 捕获2)SIGINT,注册新的处理函数
	sighandler_t s = signal(2, pfunc);
	if (SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}

	while (1)
	{
		printf("signal...\n");
		sleep(1);
	}

	// int *parr = arr;

	return 0;
}

 

附加题:在第1题的基础上,能够实现随时收发

(a)

#include "head.h"
/*
1.要求实现AB进程对话

	A进程先发送一句话给B进程,B进程接收后打印
	B进程再回复一句话给A进程,A进程接收后打印
	重复1.2步骤,当收到quit后,要结束AB进程
	附加题:在第1题的基础上,能够实现随时收发
*/

int main(int argc, const char *argv[])
{
	umask(0);
	// 创建有名管道
	if (mkfifo("./myfifo_1", 0664) < 0)
	{
		printf("errno=%d\n", errno);
		if (17 != errno)
		{
			perror("mkfifo"); // 合法错误
			return -1;
		}
	}
	printf("myfifo_1 create success\n");

	if (mkfifo("./myfifo_2", 0664) < 0)
	{
		printf("errno=%d\n", errno);
		if (17 != errno)
		{
			perror("mkfifo"); // 合法错误
			return -1;
		}
	}
	printf("myfifo_2 create success\n");

	int fd_w = open("./myfifo_1", O_WRONLY);
	if (fd_w < 0)
	{
		perror("open");
		return -1;
	}
	int fd_r = open("./myfifo_2", O_RDONLY);
	if (fd_r < 0)
	{
		perror("open");
		return -1;
	}

	char buf[128] = "";
	ssize_t res = 0;

	pid_t cpid = fork();

	if (cpid > 0)
	{
		while (1)
		{
			printf("A请输入>>> ");
			fgets(buf, sizeof(buf), stdin);
			buf[strlen(buf) - 1] = 0; // 将'\n'修改成\0

			if (write(fd_w, buf, sizeof(buf)) < 0)
			{
				perror("write");
				return -1;
			}
			if (strcmp(buf, "quit") == 0)
				return -1;
			printf("写入成功\n");
		}
	}
	else if (0 == cpid)
	{
		while (1)
		{
			bzero(buf, sizeof(buf));
			res = read(fd_r, buf, sizeof(buf));
			if (res < 0)
			{
				perror("read");
				return -1;
			}
			else if (0 == res)
			{
				printf("写端退出\n");
				break;
			}
			printf("res=%ld  buf = %s\n", res, buf);
			if (strcmp(buf, "quit") == 0)
				return -1;
		}
	}
	else
	{
		perror("fork");
		return -1;
	}

	close(fd_w);
	close(fd_r);

	return 0;
}

(b)

#include "head.h"
/*
1.要求实现AB进程对话

	A进程先发送一句话给B进程,B进程接收后打印
	B进程再回复一句话给A进程,A进程接收后打印
	重复1.2步骤,当收到quit后,要结束AB进程
	附加题:在第1题的基础上,能够实现随时收发
*/

int main(int argc, const char *argv[])
{
	umask(0);
	// 创建有名管道
	if (mkfifo("./myfifo_1", 0664) < 0)
	{
		printf("errno=%d\n", errno);
		if (17 != errno)
		{
			perror("mkfifo"); // 合法错误
			return -1;
		}
	}
	printf("myfifo_1 create success\n");

	if (mkfifo("./myfifo_2", 0664) < 0)
	{
		printf("errno=%d\n", errno);
		if (17 != errno)
		{
			perror("mkfifo"); // 合法错误
			return -1;
		}
	}
	printf("myfifo_2 create success\n");

	int fd_r = open("./myfifo_1", O_RDONLY);
	if (fd_r < 0)
	{
		perror("open");
		return -1;
	}
	int fd_w = open("./myfifo_2", O_WRONLY);
	if (fd_w < 0)
	{
		perror("open");
		return -1;
	}

	char buf[128] = "";
	ssize_t res = 0;

	pid_t cpid = fork();

	if (cpid > 0)
	{

		while (1)
		{
			bzero(buf, sizeof(buf));
			res = read(fd_r, buf, sizeof(buf));
			if (res < 0)
			{
				perror("read");
				return -1;
			}
			else if (0 == res)
			{
				printf("写端退出\n");
				break;
			}
			printf("res=%ld  buf = %s\n", res, buf);
			if (strcmp(buf, "quit") == 0)
				return -1;
		}
	}
	else if (0 == cpid)
	{
		while (1)
		{
			printf("B请输入>>> ");
			fgets(buf, sizeof(buf), stdin);
			buf[strlen(buf) - 1] = 0; // 将'\n'修改成\0

			if (write(fd_w, buf, sizeof(buf)) < 0)
			{
				perror("write");
				return -1;
			}
			if (strcmp(buf, "quit") == 0)
				return -1;
			printf("写入成功\n");
		}
	}
	else
	{
		perror("fork");
		return -1;
	}

	close(fd_r);
	close(fd_w);

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值