信号练习

定时器干货

http://blog.csdn.net/lxmky/article/details/7669296

信号干货

http://www.cnblogs.com/mickole/p/3191804.html

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>

void sig_handler(int signo, siginfo_t* info, void* ctx)
{
    printf("info->si_int is %d\n",info->si_int);
    printf("info->si_value.sival_val %d\n",info->si_value.sival_int);
}

int main(int argc, char* argv[])
{
    struct sigaction act;

    act.sa_sigaction = sig_handler;

    sigemptyset(&act.sa_mask);
    act.sa_flags = SA_SIGINFO;

    printf("pid: %d\n", getpid());
    fflush(stdout);

    if(sigaction(SIGINT, &act, NULL) == -1)
    {
        return -1;
    }

    while(1)
    {
        puts("zzzZZZ");
        sleep(2);
    }

    return 0;
}

 

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>

void sig_handler(int signo, siginfo_t* info, void* ctx)
{
    printf("info->si_int is %d\n",info->si_int);
    printf("info->si_value.sival_int is %d\n",info->si_value.sival_int);
}

int main(int argc, char* argv[])
{
    printf("%d\n", argc);

    pid_t pid = 0;

    if(argc > 1)
        pid = atoi(argv[1]);

    union sigval myval;
    myval.sival_int = 1983;

    while(1)
    {
        if(sigqueue(pid, SIGINT, myval) == -1)
        {
            printf("shit\n");
        }
        else
        {
            printf("send to pid: %d\n", pid);
        }
        fflush(stdout);
        sleep(3);
    }

    return 0;
}

 

posted @ 2017-02-13 15:19  tomren  阅读(160)  评论(0编辑  收藏  举报