Day 32 信号注册函数, 模拟两人聊天,进程管道方式, ------还好吧

1.信号注册函数原型:

 void ( *signal(int signum, void (*handler)(int)) ) (int);


 typedef void (*sighandler_t)(int);
 ===》void (*xx)(int); == void fun(int);
 ===》xx是 void fun(int) 类型函数的函数指针
 ===》typedef void(*xx)(int)   sighandler_t; ///错误
	  typedef int   myint;

 ===>sighandler_t signal(int signum, sighandler_t handler);
 ===> signal(int sig, sighandler_t fun);
 ===> signal(int sig, xxx fun);
 ===>fun 有三个宏表示:SIG_DFL 表示默认处理
					   SIG_IGN 表示忽略处理
					   fun     表示自定义处理


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

void handle(int a)
{

	static int i = 0;
	printf("找你有事情\n");
	i++;
	if(5 == i)
	{
		signal(SIGINT, SIG_DFL);
	}

}

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

signal(SIGINT, handle);	
while(1)
{
	printf("在忙\n");
	sleep(1);
}
return 0;

}

2.模拟聊天室。使用进程间管道通信的方式。



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
/**此为A 方
程序向B程序发送不同的数据,并从B中接收信息
将接收的信息打印输出,当双方收到quit的时候
程序全部退出。*/

int main(int argc, const char argv[])
{
//创建一个子进程
pid_t pid = fork();
if(pid > 1)
{
//此处为A方发送
int fifo = mkfifo("./fifo1", 0666);
if(-1 == fifo)
{
if(errno != EEXIST)
{
perror(“mkfifo error”);
exit(1);
}
}
//打开第一个管道
int fd = open(“fifo1”, O_WRONLY);
if(-1 == fd)
{
perror(“open_w1”);
exit(1);
}
//等待A方发送
while(1)
{
char buf[1024] = {0};
printf(“send A:\n”);
scanf("%s", buf);
//printf(“write len is: %d\n”, strlen(buf));
if(0 == strcmp(buf, “over”))
{
write(fd, buf, strlen(buf));
//usleep(1000
500);
break;
}
write(fd, buf, strlen(buf));
//printf(“yi fa\n”);
}
close(fd);
}

//此处为A 方接收
if(0 == pid)
{
	//创建第二个有名管道
	int fifo2 = mkfifo("./fifo2", 0666);
	if(-1 == fifo2)
	{
		if(errno != EEXIST)
		{
			perror("mkfifo2 error");
			exit(1);
		}
	}
    //只读模式
	int fd2 = open("fifo2", O_RDONLY);
	if(-1 == fd2)
	{
		perror("open_R2");
		exit(1);
	}
	//等待接受,当收到over 结束会话
	while(1)
	{
		char buf[1024] = {0};
		read(fd2, buf, 1024);
		if(0 == strcmp(buf, "over"))
		{
			//销毁有名管道二
			remove("fifo2");
			break;
		}
		printf("recieve B: %s\n", buf);
	}
	close(fd2);
}
if(pid < 0)
{
	perror("fork error");
	exit(1);
}
return 0;

}


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
/** 此为B 方
程序向A程序发送不同的数据,并从A中接收数据
将接收的信息打印输出,当双方收到quit的时候
程序全部退出。*/

int main(int argc, const char *argv[])
{
//创建一个子进程
pid_t pid = fork();
if(pid > 1)
{
//此为B方接收
int fifo = mkfifo("./fifo1", 0666);
if(-1 == fifo)
{
if(errno != EEXIST)
{
perror(“mkfifo error”);
exit(1);
}
}
//打开第一个有名管道
int fd = open(“fifo1”, O_RDONLY);
if(-1 == fd)
{
perror(“open_R1”);
exit(1);
}
//B方等待接收
while(1)
{
char buf[1024] = {0};
read(fd, buf, 1024);
if(0 == strcmp(buf, “over”))
{
break;
}
printf(“recieve A: %s\n”,buf);

	}
	close(fd);
	remove("fifo1");
}
//创建第二个有名管道,此为B方的发送
if( 0 == pid)
{

	int fifo2 = mkfifo("./fifo2", 0666);
	if(-1 == fifo2)
	{
		if(errno != EEXIST)
		{
			perror("mkfifo2 error");
			exit(1);
		}
	}
	//打开第二个有名管道
	int fd2 = open("fifo2", O_WRONLY);
	if(-1 == fd2)
	{
		perror("open_w2");
		exit(1);
	}
	//等待B方发送
	while(1)
	{
		char buf[1024] = {0};
		printf("send B:\n");
		scanf("%s", buf);
       // printf("B write len is: %d\n", strlen(buf));
		if(0 == strcmp(buf, "over"))
		{
			write(fd2, buf, strlen(buf));
			break;
		}
		write(fd2, buf, strlen(buf));
		//printf("yi fa 2\n");
	}
	close(fd2);
}
if(pid < 0)
{
	perror("fork error");
	exit(1);
}
return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值