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(1000500);
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;
}