C_Linux programming_process getenv(), execlp(), WIFEXITED

Keywords: Programming

/*

char *getenv(const char *name)

Searches for the environment string pointed to by name and returns the associated value to the string

*/

/*

getppid() is used to get the parent process identifier of the current process.

*/

/*

int execlp(const char * file,const char * arg,....);

execlp() finds the file name that matches the parameter file from the directory indicated by the PATH environment variable, executes the file when it is found, and treats the second subsequent parameter as argv[0], argv[1]...Last parameter must be empty Pointer (NULL) End.

*/

/*

1. The macro WIFEXITED(status) is used to indicate whether the child process is exiting normally and, if so, returns a non-zero value.

(Note that although the name is the same, the parameter status here is not the same as the wait's only parameter--

Do not confuse the pointer status to an integer, but the integer that the pointer is pointing to.)

2, WEXITSTATUS(status) When WIFEXITED returns a non-zero value, we can use this macro to extract the return value of a child process.

If a subprocess calls exit(5) to exit, WEXITSTATUS(status) returns 5; if a subprocess calls exit(7),

WEXITSTATUS returns 7.Note that if the process does not exit normally, that is, WIFEXITED returns 0,

This value is meaningless.

3,WIFSIGNALED(status) is true if the status returned by the abnormally terminated child process

4. WTERMSIG(status) Gets the signal that the child process is aborted by the signal

 

Of course, there are more than two macros that handle process exit states, but most of them are rarely used in normal programming.

*/

Here's a program that recycles subprocesses and determines if it's okay.

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

int main(int argc, char const *argv[])
{
    pid_t pid = fork();
    if(pid == 0){
        printf("I am child,will die\n");
        sleep(2);
        return 120;
    }
    else if(pid > 0){
        int status;

        printf("I am parent,wait for child die!\n");
        /*
        pid_t wait(int *status);
        The parameter status is used to save some of the states when the collected process exits.
        Once a process calls wait, it immediately blocks itself, and waits automatically analyze whether a child process of the current process has exited.
        If it finds such a subprocess that has become a zombie, wait will collect information about it.
        And destroy it completely and return; if no such subprocess is found, wait will be blocked here until one appears
        */
        pid_t wpid = wait(&status);
        /*
        1,WIFEXITED(status) This macro is used to indicate whether the child process exited normally and, if so, returns a non-zero value.
        (Note that although the name is the same, the parameter status here is not the same as the wait's only parameter--
        Do not confuse the pointer status to an integer, but the integer that the pointer is pointing to.)
        2,WEXITSTATUS(status) When WIFEXITED returns a non-zero value, we can use this macro to extract the return value of a child process.
        If a subprocess calls exit(5) to exit, WEXITSTATUS(status) returns 5; if a subprocess calls exit(7),
        WEXITSTATUS(status)It returns 7.Note that if the process does not exit normally, that is, WIFEXITED returns 0,
        This value is meaningless.
        3,WIFSIGNALED(status)True if the state returned for the abnormally terminated child process
        4,WTERMSIG(status) Gets the signal that the child process was aborted by the signal

        Of course, there are more than two macros that handle process exit states, but most of them are rarely used in normal programming.
        */
        printf("wait ok,wpid=%d,pid=%d\n",wpid,pid);
        if(WIFEXITED(status)){
            printf("child exit with %d\n",WEXITSTATUS(status));
        }
        
        else if (WIFSIGNALED(status)){
            printf("child killed by %d\n",WTERMSIG(status));
        }
        {
            /* code */
        }
        
        while(1){
            sleep(1);
        }
    }

    /* code */
    return 0;
}

 

 

 

 

 

Thirty-eight original articles were published, 15 were praised, and 10,000 visits were received.
Private letter follow

Posted by the mysox1 on Thu, 06 Feb 2020 18:59:55 -0800