zoukankan      html  css  js  c++  java
  • ch6信号学习笔记

    知识点归纳

    Unix/Linux支持31种不同的信号,每种信号在signal.h文件中都有定义。

    #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
    

    问题与解决思路

    实践内容

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    #include <setjmp.h>
    #include <string.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
    ",
               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
    ");
        *ip = 123; // dereference a NULL pointer
        printf("should not see this line
    ");
    }
    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
    ", getpid(), r);
    
        printf("proc %d looping
    ", getpid());
        while (1)
            ;
    }
    

      1 #include <stdio.h>
      2 #include <signal.h>
      3 #include <fcntl.h>
      4 #include <string.h>
      5 #include <unistd.h>
      6 
      7 #define LEN 64
      8 int ppipe[2]; // pipe descriptors
      9 int pid; // child pid
     10 char line[LEN];
     11 
     12 int parent()
     13 {
     14     printf("parent %d running
    ", getpid());
     15     close(ppipe[0]); // parent = pipe writer
     16     while(1){
     17         printf("parent %d: input a line : 
    ", getpid());
     18         fgets(line, LEN, stdin);
     19         line[strlen(line)-1] = 0; // kill 
     at end
     20         printf("parent %d write to pipe
    ", getpid());
     21         write(ppipe[1], line, LEN); // write to pipe
     22         printf("parent %d send signal 10 to %d
    ", getpid(), pid);
     23         kill(pid, SIGUSR1); // send signal to child process
     24     } }
     25 void chandler(int sig)
     26 {
     27     printf("
    child %d got an interrupt sig=%d
    ", getpid(), sig);
     28     read(ppipe[0], line, LEN); // read pipe
     29     printf("child %d get a message = %s
    ", getpid(), line);
     30 }
     31 int child()
     32 {
     33     char msg[LEN];
     34     int parent = getppid();
     35     printf("child %d running
    ", getpid());
     36     close(ppipe[1]); // child is pipe reader
     37     signal(SIGUSR1, chandler); // install signal catcher
     38     while(1);
     39 }
     40 int main()
     41 {
     42     pipe(ppipe); // create a pipe
     43     pid = fork(); // fork a child process
     44     if (pid) // parent
     45       parent();
     46     else
     47       child();
     48 }
    
    

  • 相关阅读:
    Scrum会议5
    小组项目alpha发布的评价
    第二阶段冲刺记录三
    第二阶段冲刺记录二
    第13周学习进度
    第二阶段冲刺记录1
    《人月神话》阅读笔记01
    第12周学习进度
    意见汇总
    双人结对,四则运算(三阶段)
  • 原文地址:https://www.cnblogs.com/cfqlovem-521/p/15552860.html
Copyright © 2011-2022 走看看