信号

作者: 静倚晴窗笑此生 | 来源:发表于2019-03-21 18:45 被阅读0次

man 7 signal

1.信号分为实时信号和标准信号,标准信号都有默认行为

2.标准信号的5种默认行为

   Term   Default action is to terminate the process.

   Ign    Default action is to ignore the signal.

   Core   Default action is to terminate the process and dump core (see core(5)).

   Stop   Default action is to stop the process.

   Cont   Default action is to continue the process if it is currently stopped.

3.标准信号要记住的

2 SIGINT ctrl+C Interrupt from keyboard

3 SIGQUIT ctrl+\ Quit from keyboard

9 SIGKILL Kill signal

11 SIGSEGV 段错误产生的信号 Invalid memory reference

14 SIGALRM Timer signal from alarm(2)

15 SIGTERM Termination signal

19 SIGSTOP ctrl+z Stop process

4.改变信号

SIGKILL和SIGSTOP信号不可改变

signal(2)

​ 为信号注册行为,返回默认行为

#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);

参数含义

signum为标准信号,第二个参数有三种情况

第一 SIG_IGN 忽略信号

第二 SIG_DFL 恢复默认信号

第三 传递一个函数,函数类型为 typedef void (*sighandler_t)(int);

返回值

返回sighandler_t类型,即默认信号

示例

/* 
 * signal函数的简单用法
 */
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

static void sig_handler(int s); 
int main(int argc, char *argv[])
{
    signal(SIGINT, SIG_DFL);
    //signal(SIGQUIT, sig_handler);

    while(1)
    {   
        write(1, "*", 1); 
        sleep(1);
    }   
    exit(0);
}

static void sig_handler(int s)
{
    write(1,"!", 1); 
}

5.信号响应过程

(1).标准信号在响应的过程中会丢失

(2).标准信号在响应过程中会打断阻塞的系统调用

(3).信号的响应过程是嵌套的

(4).不能使用setjmp( )\longjmp( )跨函数跳转

(5).想要忽略信号只需将 mask 置 0

(6).不可重入函数。(如果函数中有使用静态区,第二次调用会覆盖第一次通用的结果)

6.产生信号

kill(2)

​ 产生任意信号

#include <sys/types.h>
#include <signal.h>
   
int kill(pid_t pid, int sig);

示例

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

static void handler(int s)
{

}

int main(void)
{
    pid_t pid;

    signal(SIGINT, handler);

    pid = fork();
    if (pid < 0) {
        perror("fork()");
        exit(1);
    }
    if (pid == 0) {
        // 等待父进程通知在执行
        pause();
        printf("bye-bye!\n");
        exit(0);
    }

    sleep(2);
    kill(pid, SIGINT);

    wait(NULL);

    exit(0);
}

abort(3)

​ 产生SIGABRT信号

#include <stdlib.h>

void abort(void);

alarm(2)

产生 SIGALRM信号,返回上次调用alarm剩余的秒数

#include <unistd.h>
unsigned int alarm(unsigned int seconds);

示例

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

int main(void)
{
    printf("%d\n", alarm(5));
    printf("%d\n", alarm(10));

    while (1) {
        write(1, "*", 1);
        sleep(1);
    }

    exit(0);
}

7.等待信号

pause()

#include <unistd.h>

int pause(void);

8.流量控制

(1).漏桶

​ 没有数据到来的时候,只是等,其他什么都不做

以系统IO复制文件到标准输出举例

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

#define BUFSIZE 10

static int flag = 0;

static void alrm_handler(int s)
{
    alarm(1);
    flag = 1;
}

int main(int argc, char **argv)
{
    int rfd, wfd;
    char buf[BUFSIZE] = {};
    int cnt, ret, pos;

    if (argc < 2) {
        fprintf(stderr, "Usage....\n");
        return 1;
    }

    // SIGALRM 注册行为
    signal(SIGALRM, alrm_handler);
    alarm(1);

    while (1) {
        rfd = open(argv[1], O_RDONLY);
        if (rfd == -1) {
            if (errno == EINTR) {
                // 信号打断---》假错
                continue;
            }
            perror("open()"); // 输出到标准错误输出
            return 1;
        }
        break;
    }


    wfd = 1;

    while (1) {
        /*
         流量控制
            漏桶:没有数据到来的时候,只是等,其他什么都不做
         */

/*
        while (!flag)
            ; // 轮循
*/
        while (!flag)
            pause(); // 通知法
        flag = 0;

        cnt = read(rfd, buf, BUFSIZE); // 阻塞
        if (-1 == cnt) {
            if (errno == EINTR)
                continue;
            perror("read()");
            goto ERROR2;
        }
        if (0 == cnt) {
            // rfd结束标志
            break;
        }
        pos = 0;
        while (1) {
            ret = write(wfd, buf+pos, cnt);
            if (ret < 0) {
                if (errno == EINTR)
                    continue;
                perror("write()");
                goto ERROR2;
            }
            cnt -= ret;
            if (cnt > 0) {
                // 没写完
                pos += ret;
            } else {
                break;
            }
        }
    }
    close(rfd);
    close(wfd);

    return 0;
ERROR2:
    close(wfd);
ERROR1:
    close(rfd);
    return 1;
}

(2).令牌桶:

​ 在数据没有来的时候积攒权限

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

#define BUFSIZE 10
#define CPS     BUFSIZE
#define BURST   CPS*10 //为了方便,速率和积攒权限的个数匹配

static int token = 0;

static void alrm_handler(int s)
{
    alarm(1);
    token += CPS;
    if (token >= BURST)
        token = BURST;
}

int main(int argc, char **argv)
{
    int rfd, wfd;
    char buf[BUFSIZE] = {};
    int cnt, ret, pos;

    if (argc < 2) {
        fprintf(stderr, "Usage....\n");
        return 1;
    }

    // SIGALRM 注册行为
    signal(SIGALRM, alrm_handler);
    alarm(1);

    while (1) {
        rfd = open(argv[1], O_RDONLY);
        if (rfd == -1) {
            if (errno == EINTR) {
                // 信号打断---》假错
                continue;
            }
            perror("open()"); // 输出到标准错误输出
            return 1;
        }
        break;
    }
    wfd = 1;

    while (1) {
        /*
         流量控制
            令牌桶:在数据没有来的时候积攒权限
                令牌(权限) token
                速率     cps
                上限     burst
         */
        while (token <= 0)
            pause(); // 通知法
        token -= CPS;

        cnt = read(rfd, buf, CPS); // 阻塞
        if (-1 == cnt) {
            if (errno == EINTR)
                continue;
            perror("read()");
            goto ERROR2;
        }
        if (0 == cnt) {
            // rfd结束标志
            break;
        }
        pos = 0;
        while (1) {
            ret = write(wfd, buf+pos, cnt);
            if (ret < 0) {
                if (errno == EINTR)
                    continue;
                perror("write()");
                goto ERROR2;
            }
            cnt -= ret;
            if (cnt > 0) {
                // 没写完
                pos += ret;
            } else {
                break;
            }
        }
    }
    close(rfd);
    close(wfd);

    return 0;
ERROR2:
    close(wfd);
ERROR1:
    close(rfd);
    return 1;
}

相关文章

  • 信号种类

    信号种类 信号分为可靠信号与不可靠信号,可靠信号又称为实时信号,非可靠信号又称为非实时信号。 不可靠信号 信号代码...

  • RAC的冷信号和热信号

    冷信号和热信号 什么是冷信号与热信号? Signal vs Subject 冷信号 -> 热信号 冷信号与热信号的...

  • 心理学手记--压力的预警信号

    心理信号 情绪信号 精神信号 行为信号

  • 信号与系统

    信号与系统 whye 信号 信号的分类 确定信号与不确定信号 确定信号 可以用函数表示的信号 不确定信号 不能用确...

  • 第一章信号与系统

    信号的分类 1.确定信号与随机信号 确定信号:信号可以用一个确定的时间函数表示随机信号:不满足确定信号定义的信号 ...

  • RAC学习之二

    RACSignal(信号)介绍: 核心思想:创建信号、订阅信号、发送信号 1、创建信号及发送信号 类方法创建RAC...

  • 03_读书笔记_RAC中的RACSubject

    RACSubject :信号提供者,自己可以充当信号,又能够发送信号 基本使用 过程分析 创建信号 订阅信号 发送信号

  • APUE信号

    第10章 信号 信号是软件中断 信号提供了一种处理异步事件的方法 10.2 信号概念 信号都以SIG开头 信号名都...

  • 得到专栏学习 3-29

    关键字:信号和反信号库克 加里·哈默尔 1.信号和反信号信号 和 反信号 是经济学概念,所谓信号,就是你做一些事情...

  • iOS_RAC进阶学习

    一、信号合并 (1)信号合并_Merge (2)信号合并_CombineLatest (3)信号合并_concat...

网友评论

      本文标题:信号

      本文链接:https://www.haomeiwen.com/subject/rzncvqtx.html