开发者

the child process exited normally even if I sent a SIGABRT to it

开发者 https://www.devze.com 2023-01-09 02:45 出处:网络
I was trying some process operations like fork()ing and exec()ing I tried this Example which in it the parent wait()s his child to return and test if his child ended normally (by exit()ing or return t

I was trying some process operations like fork()ing and exec()ing I tried this Example which in it the parent wait()s his child to return and test if his child ended normally (by exit()ing or return to his caller) or abnormally (receiving Signal like 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;
}

I expect to see the sentence"the child process exited abnormally" but I saw "the child process exited normally, with exit code 0"!!! even if the child ended by calling abort() which send a SIGABRT any help? thanks in advance


When you call any functions in the exec() family the currently executing program is replaced with the one specified in the call to exec(). That means that there is never a call to abort(). So the ls program runs to completeion then exits normally.

0

精彩评论

暂无评论...
验证码 换一张
取 消