• 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;
    }
    
  • 相关阅读:
    解决html2canvas插件object-fit样式不生效问题
    七牛云视频获取视频封面和缩略图
    实现一个简单拖拽
    elementui遮罩层下方内容禁止滑动
    echars适配代码
    修改vue项目页面的title
    用div实现textarea
    判断手机端和pc端
    angular的跨域
    angular的文件上传
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384218.html
Copyright © 2020-2023  润新知