C UNIX:如何分配新的信号处理程序

时间:2015-02-12 23:45:17

标签: c unix signals

我有一个信号处理程序,我将ctrl + z / SIGTSTP设置为只能被程序检测到。但是当我想将ctrl + z / SIGTSTP的信号处理程序更改为子进程中的默认行为时,ctrl + z不会改变。是否有适当的改变信号处理程序?

#include <stdio.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#include  <sys/types.h>
#include  <string.h>
#include <unistd.h>


void handler(int sig_num)
{
    printf("detected %d\n",sig_num);
}

int main()
{
    int x; 
    signal(SIGTSTP,handler);

    pid_t pid = fork();

    if(pid == 0)
    {  
        signal(SIGTSTP,SIG_DFL);
        printf("in child process \n");
        while(1);
    }
    else if(pid > 0)
    {
        printf("running parent\n");
        printf("waiting for my child to run\n");
        wait(&x);
        exit(0);
    }

    return 0;

}

0 个答案:

没有答案