20191317王鹏宇第六章学习笔记

第五章:定时器及时钟服务

知识点归纳总结

本章讲述了信号和信号处理;介绍了信号和中断的统一处理,有助于从正确的角度看待信号;

将信号视为进程中断,将进程从正常执行转移到信号处理;解释了信号的来源,包括来自硬件、异常和其他进程的信号;

举例说明了信号在Unix/Linux中的常见用法;详细解释了Unix/Linux中的信号处理,包括信号类型、信号向量位、信号掩码位、进程PROC结构体中的信号处理程序以及信号处理步骤;

用示例展示了如何安装信号捕捉器来处理程序异常,如用户模式下的段错误;

讨论了将信号用作进程间通信(IPC)机制的适用性。读者可借助该编程项目,使用信号和管道来实现用于进程交换信息的进程间通信机制。


其中让我最有收获的几个部分如下:

  • 信号和中断
  • Unix/Linux 信号示例
  • Unix/Linux中的信号处理
  • 信号处理步骤
  • 信号与异常
  • 编程项目:实现一个消息IPC


Unix/Linux中的信号处理

Unix/Linux支持31种不同的信号,每种信号在signal.h文件中都有定义,每种信号都有一个符号名,如 SIGHUP ( 1 )、SIGEMT ( 2 )、SIGKILL ( 9 )、S1GSEGV (11)等。

#define	SIGHUP	1
#define	SIGINT	2
#define	SIGQUIT	3
#define	SIGILL	4
#define	SIGTRAP	5
#define	SIGABRT	6
#define	SIGIOT	6
#define	SIGBUS	7
#define	SIGFPE	8
#define	SIGKILL	9
#define	SIGUSR1	10
#define	SIGSEGV	11
#define	SIGUSR2	12
#define	SIGPIPE	13
#define	SIGALRM	14
#define	SIGTERM	15
#define	SIGSTKFLT 16
#define	SIGCHLD	17

#define	SIGCONT	18
#define	SIGSTOP	19
#define	SIGTSTP	20
#dpfine	STGTTTN	21
#define	SIGTTOU	22
#define	SIGURG	23
#define	SIGXCPU	24
#define	SIGXFSZ	25
#define	SIGVTALRM 26
#define	SIGPROF	27
#define	SIGWINCH 28
#define	SIGPOLL	29
#define	SIGPWR	30
#define	SIGSYS	31


信号处理步骤

(1)当某进程处于内核模式时,会检査信号并处理未完成的信号,如果某信号有用户安装的捕捉函数,该进程会先清除信号,获取捕捉函数地址,对于大多数陷阱信号,则将已安装的捕捉函数重置为DEFault,,然后,它会在用户模式下返回,以执行捕捉函数,以这种方式篡改返回路径当捕捉函数结束时.它会返间到最初的中断点,即它最后进入内核模式的地方因此,该进程会先迂回执行捕捉函数,然后再恢复正常执行。

<font face="宋体" color=black size=4(2)重置用户安装的信号捕捉函数:用户安装的陷阱相关信号捕捉函数用于处理用户代码中的陷阱错误。由于捕捉函数也在用户模式下执行,因此可能会再次出现同样的错误如果是这样,该进程最终会陷入无限循环,一直在用户模式和内核模式之间跳跃:为了防止这种情况,Unix内核通常会在允许进程执行捕捉函数之前先将处理函数重置为DEFault这意味着用户安装的捕捉函数只对首次出现的信号有效若要捕捉再次出现的同一信号,则必须重新安装捕捉函数°但是,用户安装的信号捕捉函数的处理方法并不都一样,在不同Unix 版本中会有所不同。例如,在BSD Unix中,信号处理函数不会被重置。但是该信号在执行信号捕捉函数时会被阻塞,感兴趣的读者可参考关于Linux信号和sigaction函数的手册页,以了解更多详细侑息

<font face="宋体" color=black size=4(3)信号和喚醒:在Unix/Linux内核中有两种SLEEP进程;深度休眠进程和浅度休眠进程。前一种进程不可中断,而后一种进程可由信号中断。如果某进程处于不可中断的SLEEP状态,到达的信号(必须来自硬件中断或其他进程)不会唤醒进程一如果它处于可中断的SLEEP状态,到达的信号将会唤醒它:例如,当某进程等待终端输入时,它会以低优先级休眠.这种休眠是可中断的。S1G1NT这类信号即可唤輩它


sigaction使用示例

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
//#include <siginfo.h>

void handler(int sig, siginfo_t *siginfo, void *context)
{
    printf("handler: sig=%d from PID=%d UID=%d\n",sig, siginfo->si_pid, siginfo->si_uid);
}
int main(int argc, char *argv[])
{
    struct sigaction act;
    memset(&act, 0, sizeof(act));
    act.sa_sigaction = &handler;
    act.sa_flags = SA_SIGINFO; 
    sigaction(SIGTERM, &act, NULL);
    printf("proc PID=%d looping\n", getpid()); 
    printf ("enter kill PID to send SIGTERM signal to it\n"); 
    while(1)
    {
        sleep (10);
    }
}

实践截图


段错误捕捉函数

#include <stdio.h>
#include <stdlib.h> 
#include <unistd.h> 
#include <signal.h> 
#include <setjmp.h>
#include <string.h>

//#include <siginfo.h>

jmp_buf env;
int count = 0;

void handler(int sig, siginfo_t *siginfo, void *context)
{
    printf ("handler sig=%d from PID=%d UID=%d count=%d\n", sig, siginfo->si_pid, siginfo->si_uid, ++count); 
    if (count >= 4) // let it occur up to 4 times
        longjmp(env, 1234);
}

int BAD()
{
    int *ip = 0;
    printf("in BAD(): try to dereference NULL pointer\n");
    *ip = 123;	// dereference a NULL pointer
    printf("should not see this line\n");
}

int main (int argc, char *argv[])
{
    int r;
    struct sigaction act; 
    memset (&act, 0, sizeof(act)); 
    act.sa_sigaction = &handler; 
    act.sa_flags = SA_SIGINFO;
    sigaction(SIGSEGV, &act, NULL); 
    if ((r = setjmp(env)) == 0) 
        BAD();
    else
        printf("proc %d survived SEGMENTATION FAULT: r=%d\n",getpid(), r);

    printf ("proc %d looping\n" ,getpid());
    while(1);
}


实践截图


编程项目实现一个IPC

#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h> 
#include <unistd.h> 
#include <signal.h> 
#include <setjmp.h>
#define LEN 64 
int ppipe[2]; 
int pid; 
char line[LEN];

int parent()
{
    printf("parent %d running\n", getpid());
    close(ppipe[0]);	// parent = pipe writer
    while(1)
    {
        printf("parent %d input a line : \n", getpid());
        fgets(line, LEN, stdin);
        line[strlen(line)-1] = 0; // kill \n at end
        printf("parent %d write to pipe\n", getpid());
        write(ppipe[1], line, LEN); // write to pipe
        printf("parent %d send signal 10 to %d\n", getpid(), pid);
        kill(pid, SIGUSR1);	// send signal to child process
    }
}

void chandler(int sig)
{
    printf("\nchild %d got an interrupt sig=%d\n", getpid(), sig); 
    read(ppipe[0], line, LEN);	// read pipe
    printf("child %d get a message = %s\n", getpid(), line);
}
int child()
{
    char msg[LEN];
    int parent = getppid();
    printf("child %d running\n", getpid());
    close(ppipe[1]);	// child is pipe reader
    signal(SIGUSR1, chandler);	// install signal catcher
    while(1);
}

int main ()
{
    pipe(ppipe);	// create a pipe
    pid = fork() ;	// fork a child process
    if (pid) // parent
        parent();
    else
        child ();
}

实践截图

posted @ 2021-11-14 15:59  Bzrael  阅读(55)  评论(0编辑  收藏  举报