• 多线程异步管理——信号


    #include <stdio.h>
    #include <pthread.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    void *sigone_program(void *arg);
    void *sigtwo_program(void *arg);
    void report(int);
    
    int main(int argc,char *argv[])
    {
        int i;
        void *status;
        pthread_t thread_one,thread_two;
        if(pthread_create(&thread_one,NULL,sigone_program,NULL)!=0)
        {
            fprintf(stderr,"pthread_create failure
    ");
            exit(EXIT_FAILURE);
        }
        if(pthread_create(&thread_two,NULL,sigtwo_program,NULL)!=0)
        {
            fprintf(stderr,"pthread_create failure
    ");
            exit(EXIT_FAILURE);
        }
        sleep(1);
        printf("this is parent, send SIGUSR1, SIGUSR2 to thread %u
    ",thread_one);
        if(pthread_kill(thread_one,SIGUSR1)!=0)
        {
            perror("pthread_kill");
            exit(EXIT_FAILURE);
        }
        if(pthread_kill(thread_one,SIGUSR2)!=0)
        {
            perror("pthread_kill");
            exit(EXIT_FAILURE);
        }
        printf("this is parent, send SIGUSR1, SIGUSR2 to thread %u
    ",thread_two);
        if(pthread_kill(thread_two,SIGUSR1)!=0)
        {
            perror("pthread_kill");
            exit(EXIT_FAILURE);
        }
        if(pthread_kill(thread_two,SIGUSR2)!=0)
        {
            perror("pthread_kill");
            exit(EXIT_FAILURE);
        }
        sleep(1);
        if(pthread_kill(thread_one,SIGKILL)!=0)
        {
            perror("pthread_kill");
            exit(EXIT_FAILURE);
        }
        if(pthread_kill(thread_two,SIGKILL)!=0)
        {
            perror("pthread_kill");
            exit(EXIT_FAILURE);
        }
        pthread_join(thread_two,NULL);
        pthread_join(thread_one,NULL);
        return 0;
    }
    void *sigone_program(void *arg)
    {
        int i;
        __sigset_t set;
        signal(SIGUSR1,report);
        sigfillset(&set);
        sigdelset(&set,SIGUSR2);
        pthread_sigmask(SIG_SETMASK,&set,NULL);
        for(i=0;i<5;i++)
        {
            printf("this is set mask %u thread
    ",pthread_self());
            pause();
        }
    }
    
    void report(int sig)
    {
        printf("
    in signal, the sig=%d	, the thread id=%u
    ",sig,pthread_self());
    }
    
    void *sigtwo_program(void *arg)
    {
        int i;
        signal(SIGUSR2,report);
        for(i=0;i<5;i++)
        {
            printf("this is no set mask %u thread
    ",pthread_self());
            pause();
        }
    }

  • 相关阅读:
    【转载】总结一下Android中主题(Theme)的正确玩法
    Android获唯一标识
    AS问题解决系列3—iCCP: Not recognizing known sRGB profile
    AS问题解决系列1—Unable to execute DX错误
    Android Studio Error2
    Android Error
    NAT简单介绍
    redis缓存工具Jedis进行跨jvm加锁(分布式应用)--不幸暂弃用--能够做第三方锁使用
    工作总结1.怎样高效跟客户确定需求?
    Sqoop处理Clob与Blob字段
  • 原文地址:https://www.cnblogs.com/lakeone/p/3792238.html
Copyright © 2020-2023  润新知