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;
    }
    
  • 相关阅读:
    【持续更新】leetcode算法-数组篇
    【转】敏捷开发之Scrum扫盲篇
    设计Twitter的api
    给一个表达式字符串加括号,计算它的所有的可能的值
    判断一个整数是否是平方数
    Spring Cloud 入门教程(七): 熔断机制 -- 断路器
    断路器(Curcuit Breaker)模式
    Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务
    Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡
    Spring Cloud 入门教程(四): 分布式环境下自动发现配置服务
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384218.html
Copyright © 2011-2022 走看看