进程间通信-PIPE-FIFO

Question1

1)要求AB进程做通信

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

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

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

进程1

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

int main(int argc, const char *argv[])
{
	umask(0);

	//创建有名管道
	if(mkfifo("./myfifo", 0777) < 0)
	{
		printf("errno = %d\n", errno);
		if(errno != 17) 	//如果errno==17代表管道文件已经存在,是合法错误不处理
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./myfifo1", 0777) < 0)
	{
		printf("errno = %d\n", errno);
		if(errno != 17) 	//如果errno==17代表管道文件已经存在,是合法错误不处理
		{
			perror("mkfifo1");
			return -1;
		}
	}

	printf("mkfifo success\n");

	//以只读的方式打开有名管道文件


	int fd = open("./myfifo", O_RDWR);

	//以只写的方式打开有名管道文件
	int ed = open("./myfifo1", O_RDWR);
	if(ed < 0)
	{
		perror("open");
		return -1;
	}

	if(fd < 0)
	{
		perror("open");
		return -1;
	}
	printf("open readonly success\n");
	printf("open writeonly success\n");

	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		bzero(buf, sizeof(buf));
		res = read(fd, buf, sizeof(buf));
		if(strcmp(buf,"quit\n")==0)
		{
			printf("进程退出");
			break;
		}
		if(res < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res) 	//没有写端
		{
			printf("对方进程退出\n");
			break;
		}
		printf("read success :%ld : %s\n", res, buf);
		printf("进程1输入:");
		bzero(buf,sizeof(buf));

		fgets(buf,sizeof(buf),stdin);
		if(write(ed,&buf,sizeof(buf))<0)
		{
   		 	perror("write" );
			return -1;
		}
		printf("write success\n"); 

		if(strcasecmp(buf,"quit\n")==0) 
		{
			printf("进程1退出");
			break;
		}



	}


	//关闭文件描述符
	close(fd);
	close(ed);
	exit(0);
	return 0;
}

进程2

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include<stdlib.h>
int main(int argc, const char *argv[])
{
	umask(0);

	//创建有名管道
	if(mkfifo("./myfifo", 0777) < 0)
	{
		printf("errno = %d\n", errno);
		if(errno != 17) 	//如果errno==17代表管道文件已经存在,是合法错误不处理
		{
			perror("mkfifo");
			return -1;
		}
	}
     if(mkfifo("./myfifo1", 0777) < 0)
     {
         printf("errno = %d\n", errno);
         if(errno != 17)     //如果errno==17代表管道文件已经存>
         {
             perror("mkfifo1");
             return -1;                                        
         }
     }

	printf("mkfifo success\n");
	//以只读的方式打开有名管道文件 
	
	int ed = open("./myfifo1", O_RDWR);


	//以只写的方式打开有名管道文件
	int fd = open("./myfifo", O_RDWR);

	if(fd < 0)
	{
		perror("open");
		return -1;
	}
	if(ed < 0)
	{
		perror("open");
		return -1;      
	}

	printf("open writeonly success\n");
 	printf("open readonly success\n");
	
	ssize_t res = 0;
	char buf[128] = "";
	while(1)
	{
		printf("进程2请输入>>>");
		bzero(buf, sizeof(buf));
		fgets(buf, sizeof(buf), stdin);
		//buf[strlen(buf)-1] = 0;

		if(write(fd, buf, sizeof(buf)) < 0)
		{
			perror("write");
			return -1;
		}
		if(strcmp(buf,"quit\n")==0)
		{
			printf("进程退出");
			break;
		}

		printf("write success\n");
		bzero(buf, sizeof(buf));
		res = read(ed, buf, sizeof(buf));
		if(strcmp(buf,"quit\n")==0)
		{
			printf("进程退出");
			break;
		}
		if(res < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res)   //没有写端
		{
			printf("对方进程退出\n");
			break;
		}
		printf("read success :%ld : %s\n", res, buf);

	}


	//关闭文件描述符
	close(fd);
    close(ed);
    exit(0);

	return 0;
}

Question2

捕获2)3)20)号信号

代码实现

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
typedef void (*sighandler_t)(int);
void handler1(int sig);                     //2号信号新的处理函数
void handler2(int sig);                     //3号信号新的处理函数
void handler3(int sig);                     //20号信号新的处理函数
int main(int argc, const char *argv[])
{
    printf("h1:%p h2:%p h3:%p\n",handler1,handler2,handler3);
    sighandler_t s1 = signal(2,handler1);   //捕捉2号信号SIGINT
    if(s1 == SIG_ERR)
    {
        perror("signal");
        return -1;
    }
    sighandler_t s2 = signal(3,handler2);   //捕捉3号信号SIGQUIT
 
    if(s2 == SIG_ERR)
    {
        perror("signal");
        return -1;
    }
    sighandler_t s3 = signal(20,handler3);   //捕捉20号信号SIGTSTP
 
    if(s3 == SIG_ERR)
    {
        perror("signal");
        return -1;                                                                 
    }
 
    while(1)
    {
        printf("this is man\n");
        sleep(1);
    }
 
    return 0;
}
void handler1(int sig)                      //2号信号新的处理函数
{
    printf("这是handler1 是%d号信号\n",sig);
 
}
 
void handler2(int sig)                     //3号信号新的处理函数
{
    printf("这是handler2 是%d号信号\n",sig);
 
}
 
void handler3(int sig)                     //20号信号新的处理函数
{
    printf("这是handler3 是%d号信号\n",sig);
 
}
                                                                                   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值