2010-07-23 69 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程序運行完成,然後正常退出。