Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在父进程中捕捉术语动作信号?_C++_C_Linux_Process_Signals - Fatal编程技术网

C++ 如何在父进程中捕捉术语动作信号?

C++ 如何在父进程中捕捉术语动作信号?,c++,c,linux,process,signals,C++,C,Linux,Process,Signals,我是LinuxC编程新手,我的任务是写一个关于进程的程序 我需要处理两个进程,父进程和子进程 我的目标是让父进程分叉一个processchild进程,然后子进程执行一个可能终止失败的程序。父进程等待子进程终止,并获取从子信号启动的信号,如中止或分段错误 然而,我遇到了一些问题 我发现核心动作信号很容易被检测到,但术语动作却无法被检测到 无法检测到术语动作信号,如SIGALRM14或SIGINT2。 这似乎被归类为成功 这是我的密码: #include <cstdio> #includ

我是LinuxC编程新手,我的任务是写一个关于进程的程序

我需要处理两个进程,父进程和子进程

我的目标是让父进程分叉一个processchild进程,然后子进程执行一个可能终止失败的程序。父进程等待子进程终止,并获取从子信号启动的信号,如中止或分段错误

然而,我遇到了一些问题

我发现核心动作信号很容易被检测到,但术语动作却无法被检测到

无法检测到术语动作信号,如SIGALRM14或SIGINT2。 这似乎被归类为成功

这是我的密码:

#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <cstring>
#include <errno.h>
using namespace std;


bool check = true; 

void mySignal( int sig ){
    int status; 
    pid_t childPid = wait( &status ) ; 


    if( WIFEXITED( status ) ){
        printf("The child is terminated success!!\n"); 
    }
    else{
        if( WIFSIGNALED( status ) ){
            int termsig = WTERMSIG( status ) ;
            printf("termsig = %d %d\n",status, termsig ) ;  
        }
    }
    check = false ; 
}


int main( int argc , char *argv[] ){

    signal( SIGCHLD, mySignal ) ; 
    pid_t pid = fork() ; 


    if( pid < 0 ){
        printf("fork error\n");
        exit( -1 ) ;
    }
    else if( pid == 0 ){
        execl( argv[1], NULL );
        exit( 0 ) ;
    }

    while( check ) ; 
    return 0 ; 
}
有人知道如何解决这个问题吗

void mySignal( int sig ){
  int status; 
  pid_t childPid = wait( &status ) ; 


  if( WIFEXITED( status ) ){
     printf("The child is terminated success!!\n"); 
  }

  if( WIFSIGNALED( status ) ){
     int termsig = WTERMSIG( status ) ;
     printf("termsig = %d %d\n",status, termsig ) ;  
  }
  check = false ; 
}

信号并不总是结束程序,使条件变得毫无意义。

您应该这样做:

if(WEXITED(status)){
  printf("Child %d exited with exit code %d.\n", (int)pid, WEXITSTATUS(status));
  // Note that a non-zero exit status normally indicates some kind of error.
}
else if(WIFSIGNALED(status)){
  printf(
    "Child %d terminated with signal %d, with%s a core dump.\n",
    (int)pid, WTERMSIG(status), WCOREDUMP(status)? "": "out"
  );
}
else if(WSTOPPED(status)){
  printf("Child %d was stopped by signal %d.\n", (int)pid, WSTOPSIG(status));
}
else{
  fprintf(stderr, "Unexpected signal condition.\n");
}

如上所述,非零退出状态通常表示错误。因此,您应该在代码中这样做:execl之后的exit0只有在调用execl失败时才会执行,因此您更愿意说exit1或exitEX_UNAVAILABLE from.

但最后,我发现我的报警测试程序有问题,因此我的父进程无法接收SIGALRM信号XDOh!!谢谢你的提醒!!我会考虑这个问题:如果有人修正了你的问题,ChihMin不要忘记验证答案。