zoukankan      html  css  js  c++  java
  • Linux_信号操作 demo

    main1.c

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        while(1) {
            printf("work...
    ");
            sleep(3);
        }
    
        return 0;
    }
    

    main2.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    
    
    void myhandle(int sig) 
    {
        printf("Catch a signal : %d
    ", sig);
    }
    
    int main(void) 
    {
    
        signal(SIGINT, myhandle);
        while (1) {
    
        }
    
        return 0;
    }
    

    main3.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    
    void myhandle(int sig) 
    {
        static int cnt = 0;
        printf("Catch a signal : %d
    ", sig);
    
        signal(SIGINT, SIG_DFL); //µ»Õ¨”⁄signal(sig, SIG_DFL);
    }
    
    int main(void) 
    {
        signal(SIGINT, myhandle);
    
        while (1) {
    
        }
    
        return 0;
    }
    

    main4.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    
    void myhandle(int sig) 
    {
        printf("Catch a signal : %d
    ", sig);
    }
    
    int main(void) 
    {
        struct sigaction act;
    
        act.sa_handler = myhandle;
        sigemptyset(&act.sa_mask);
           act.sa_flags = 0;
    
        sigaction(SIGINT, &act, 0);
    
        while (1) {
    
        }
    
        return 0;
    }
    

    main5.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    
    void myhandle(int sig) 
    {
        printf("Catch a signal : %d
    ", sig);
    }
    
    int main(void) 
    {
        struct sigaction act;
    
        act.sa_handler = myhandle;
        sigemptyset(&act.sa_mask);
        //act.sa_flags = 0;
        act.sa_flags = SA_RESETHAND;
    
        sigaction(SIGINT, &act, 0);
    
        while (1) {
    
        }
    
        return 0;
    }
    

    main6.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    
    int workflag = 0;
    
    void work_up_handle(int sig) 
    {
        workflag = 1;
    }
    
    void work_down_handle(int sig) 
    {
        workflag = 0;
    }
    
    
    
    int main(void) 
    {
        pid_t pd;
        char c;
    
    
        pd = fork();
        if (pd == -1) {
            printf("fork error!
    ");
            exit(1);
        } else if (pd == 0) {
            char *msg;
            struct sigaction act; 
            act.sa_flags = 0;
            act.sa_handler = work_up_handle;
            sigemptyset(&act.sa_mask);      
            sigaction(SIGUSR1, &act, 0);
    
            act.sa_handler = work_down_handle;
            sigaction(SIGUSR2, &act, 0);
    
            while (1) {
                if (!workflag) {
                    msg = "child process work!";
                } else {
                    msg = "CHILD PROCESS WORK!";
                }
                printf("%s
    ", msg);
                sleep(1);
            }
    
        } else {
            while(1) {
                c = getchar();
                if (c == 'A') {
                    kill(pd, SIGUSR1);
                } else if (c == 'a') {
                    kill(pd, SIGUSR2);
                }
            }
        }
    
    
        return 0;
    }
    

    main7.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    
    int wakeflag = 0;
    
    void wake_handle(int sig) 
    {
        wakeflag = 1;
    }
    
    int main(void) 
    {
        pid_t pd;
        char c;
    
    
        pd = fork();
        if (pd == -1) {
            printf("fork error!
    ");
            exit(1);
        } else if (pd == 0) {
            sleep(5);
            kill(getppid(), SIGALRM);
        } else {
            struct sigaction act; 
            act.sa_handler = wake_handle;
            act.sa_flags = 0;
            sigemptyset(&act.sa_mask);
    
            sigaction(SIGALRM,  &act, 0);
    
            pause(); //∞—∏√Ω¯≥Ãπ“∆£¨÷±µΩ ’µΩ»Œ“‚“ª∏ˆ–≈∫≈
    
            if (wakeflag) {
                printf("Alarm clock work!!!
    ");
            }
        }
    
        return 0;
    }
    

    main8.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <time.h>
    
    int wakeflag = 0;
    
    void wake_handle(int sig) 
    {
        wakeflag = 1;
    }
    
    int main(void) 
    {
        int ret;
    
        struct sigaction act;
        act.sa_flags = 0;
        act.sa_handler = wake_handle;
        sigemptyset(&act.sa_mask);
        sigaction(SIGALRM, &act, 0);
    
        printf("time =%ld
    ", time((time_t*)0));
    
        ret = alarm(5);
        if (ret == -1) {
            printf("alarm error!
    ");
            exit(1);
        }
    
        //π“∆µ±«∞Ω¯≥ã¨÷±µΩ ’µΩ»Œ“‚“ª∏ˆ–≈∫≈
        pause();
    
        if (wakeflag) {
            printf("wake up, time =%ld
    ", time((time_t*)0));
        }
    
        return 0;
    }
    

    main9.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    
    int wakeflag = 0;
    
    void wake_handle(int sig) 
    {
        printf("in handle
    ");   // ‘⁄∏√–≈∫≈¥¶¿Ì∫Ø ˝÷– π”√printf£¨“‘±„µ˜ ‘£¨±æ≤ª“À π”√£¨“ÚŒ™printf «≤ªø…÷ÿ»Îµƒ
        sleep(10);
        printf("in handle2
    ");
        wakeflag = 1;
    }
    
    int main(void) 
    {
        pid_t pd;
        char c;
    
        pd = fork();
        if (pd == -1) {
            printf("fork error!
    ");
            exit(1);
        } else if (pd == 0) {
            sleep(3);
            kill(getppid(), SIGALRM);
        } else {
            struct sigaction act; 
            act.sa_handler = wake_handle;
            act.sa_flags = 0;
            sigemptyset(&act.sa_mask);
    
            sigaddset(&act.sa_mask, SIGALRM); //SIGINT);
    
            sigaction(SIGALRM,  &act, 0);
    
            pause(); //∞—∏√Ω¯≥Ãπ“∆£¨÷±µΩ ’µΩ»Œ“‚“ª∏ˆ–≈∫≈
    
    
            if (wakeflag) {
                printf("Alarm clock work!!!
    ");
            } else {
                printf("Alarm clock not work!
    ");
            }
        }
    
        return 0;
    }
    
  • 相关阅读:
    ISO/IEC 9899:2011 条款6.10.3——宏替换
    ISO/IEC 9899:2011 条款6.10.2——源文件包含
    关于Objective-C新增的__kindof关键字
    ISO/IEC 9899:2011 条款6.10.1——条件包含
    ISO/IEC 9899:2011 条款6.10——预处理指示符
    ISO/IEC 9899:2011 条款6.9.2——外部对象定义
    Objective-C中使用不定参数个数的方法调用
    php添加数据到xml文件的例子
    nginx rewrite重写与防盗链配置
    nginx url自动加斜杠的问题
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384218.html
Copyright © 2011-2022 走看看