信号

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;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 信号的基本概念 信号被认为是一种软件中断(区别于硬件中断),信号机制提供了一种在单进程/线程下处理异步事件的方法。...
    小叶大孟阅读 2,011评论 0 1
  • 一、信号及信号来源 信号本质 信号是在软件层次上对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一...
    丶Em1tu0F阅读 1,471评论 0 1
  • 转自:http://www.dbafree.net/?p=870 我们可以使用kill -l查看所有的信号量解释,...
    AndreaArlex阅读 6,222评论 0 4
  • 概述 Linux 在进程间通信时,有时候需要用到异步通讯方式,而信号机制是Linux系统本身提供的一种异步通讯. ...
    大风qixi阅读 1,925评论 0 4
  • 1.今天去给孩子们逛街买衣服,占用了不少时间。 在公交站牌等车时,一个二十出头的女孩拿着一个小的笔记本在看,上面写...
    小蚂蚁爬呀爬阅读 126评论 0 1