waitpid函数()不允许SIGINT被发送到子进程? [英] waitpid() not allowing SIGINT to be sent to child process?

查看:277
本文介绍了waitpid函数()不允许SIGINT被发送到子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>


void sig_handler(int signal); 

int pid, forkFlag = 0;

int main( int argc, char **argv, char **envp )
{
  sigset(SIGINT, sig_handler); //handle ctrl + c
  sigignore(SIGTSTP);
  sigignore(SIGSTOP);

 int ex, rv, status;
        forkFlag = 1; //fork is being called

        pid = fork();

        if(pid == -1){

            perror("fork");
            exit(2);
        }
        else if (pid == 0){    //if child process

            ex = access(argv[0], X_OK);   //check if file is executable

            if(ex){

                perror("access");
                exit(1);
            }
            else{
                rv = execve(argv[0], argv, envp);  //run program in child process

                if(rv == -1){

                    perror("execve");
                    exit(1);
                }
            }
            exit(0);  //end child process
        }
        else{
            rv = waitpid(pid, &status, 0); //wait for child

            if(rv == -1){

                perror("waitpid");  
            }

            if(WEXITSTATUS(status)){   //check status of child if it did ot return 0

                printf("The return status of the child was %d\n", WEXITSTATUS(status));
            }
        }
        forkFlag=0;
}

void sig_handler(int signal)
{
    if(signal == SIGINT && (pid && forkFlag)){

        kill(pid,signal); //send kill to child
    }
}

我想我的程序忽略CTRL + C,除非有一个子进程在运行,那么它发送的SIGINT子进程。然而,当我preSS CTRL +当子进程正在运行C,waitpid函数()返回-1,错误系统调用被中断。这使得子进程停止运行,但如果我用ps,子进程仍然存在,但现在打成解散。我知道从杀死正在被卡莱嚣功能sig_handler,而PID和forkFlag是他们正确的价值观的printf语句。是waitpid函数()使我的程序忽略杀?我该如何解决这个问题?我知道这code旁边不落空,但它是我的code的一小部分(涉及叉只有一部分)

I'm trying to make my program ignore ctrl + C, except when there is a child process running, then it sends the the SIGINT to the child process. However, when I press ctrl + c when the child process is running, waitpid() returns -1 with the error "Interrupted System Call." This makes the child process stop running, but if I use ps, the child process is still there, but now labeled as defunct. I know from printf statements that kill is being calle din the function sig_handler, and that pid and forkFlag are their correct values. Is waitpid() making my program ignore the kill? How do I fix this? I know this code does next to nothing, but it's a small portion of my code (the only part involving fork)

感谢您的帮助。

推荐答案

的问题是,子进程得到SIGINT相同覆盖的处理程序。你可能想叉后子进程的信号处理程序复位,或者你可能想在安装后,你已经分叉孩子家长的信号处理程序,因此它不会继承被覆盖的处理程序。

The problem is that the child processes get the same overridden handler for SIGINT. You probably want to reset the signal handler in the child process after the fork, or you might want to install the signal handler in the parent after you've already forked the child, so it doesn't inherit the overriden handler.

这篇关于waitpid函数()不允许SIGINT被发送到子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆