2010-07-23 72 views
1

我正在尝试fork()ing和exec()等一些进程操作我试过这个例子,它在父级wait()中返回他的子级,测试,如果他的孩子正常结束(通过出口()ING或返回他的调用者)或异常(接收信号一样SIGABRT)子进程正常退出,即使我向它发送了SIGABRT

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

int spawn (char* program, char** arg_list) 
{ 
    pid_t child_pid; 
    child_pid = fork(); 
    if (child_pid != 0) 
    return child_pid; 

    else { 
    execvp (program, arg_list); 
    abort(); 
    } 
} 
int main() 
{ 
    int child_status; 
    char* arg_list[] = {"ls","/",NULL}; 

    spawn ("ls", arg_list); 

    wait (&child_status); 


    if (WIFEXITED (child_status)) 
     printf ("the child process exited normally, with exit code %d\n", 
     WEXITSTATUS (child_status)); 
    else 
     printf ("the child process exited abnormally\n"); 
return 0; 
} 

我希望看到的一句“子进程异常退出”,但我看到了“子进程正常退出,退出代码为0“!即使孩子通过调用abort()来结束发送SIGABRT 的任何帮助? 在此先感谢

回答

8

当您调用exec()系列中的任何功能时,当前正在执行的程序将被替换为exec()调用中指定的程序。这意味着从来没有打电话给abort()。所以ls程序运行完成,然后正常退出。