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;
    }
    
  • 相关阅读:
    java高并发
    阿里网盘挂载到本地主机
    frp突破内网ip限制教程
    docker学习总结(流水线、redis集群、网络待)
    git\svn提交记录规范
    【寻径06】如何突破学习瓶颈学习笔记
    leetcode 每日一题解题859. 亲密字符串
    字符串格式的json字符串转换为json格式的字符串
    如何使用github发布自己的静态项目
    软考第一课总结考前介绍(1)
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384218.html
Copyright © 2011-2022 走看看