The parent process of Linux to the child process to send a Kill signal example and the status of the child process to judge

Source: Internet
Author: User

Let's look at the example of a parent process sending a kill signal to a child process:

#include <stdio.h> #include <unistd.h> #include <signal.h> #include <sys/types.h> #include < Sys/wait.h>int Main (int argc, const char *argv[]) {    pid_t pid;    int status;    PID = fork ();    if (0 = = pid)    {        printf ("Hi, I ' m child process!\n");        Sleep (ten);    }    else if (PID > 0)    {        printf ("Send signal to Child process (%d) \ n", pid);        Sleep (1);        Kill (PID, SIGABRT);        Wait (&status);        if (wifsignaled (status))        {            printf ("Child process received Singal%d\n", Wtermsig (status));        }    }    else    {        printf ("Fork wrong!\n");        return 1;    }    return 0;}


Macro that determines the status of the child process exit:

the end state of the child process is returned and stored in status, with several macros at the bottom to determine the end condition
wifexited (status) is a non-0 value if the child process ends normally.
wexitstatus (status) Gets the end code returned by the child process exit (), typically using wifexited to determine whether the macro ends properly before it can be used.
wifsignaled (status) This macro value is true if the child process ends because of a signal
wtermsig (status) Gets the signal code that the child process aborts because of the signal, generally uses wifsignaled to judge before using this macro.
wifstopped (status) This macro value is true if the child process is in a paused execution condition. This is generally the case only if you are using wuntraced.
wstopsig (status) Gets the signal code that caused the child process to pause.

The parent process of Linux to the child process to send a Kill signal example and the status of the child process to judge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.