Process management under the liunx of "inductive finishing" (iii), multi-process waiting and termination

Source: Internet
Author: User
Tags terminates

Normal exit of the process
------------------


1. Return from the main function.


int main (...) {
...
return x;
}


Equivalent to:


int main (...) {
...
Exit (x);
}


2. Call the Exit function of the standard C language.


#include <stdlib.h>


void exit (int status);


1) Call process exits,
Its parent process calls the Wait/waitpid function to return the low 8 bits of status.


2) before the process exits,
Call all functions that were previously registered through the Atexit/on_exit function,
Flush and close all standard I/O streams that are still open
Delete all files created through the Tmpfile function.


#include <stdlib.h>


int atexit (void (*function) (void));


Functions-function pointers,
A function that needs to be called before the process exits.
The function has neither a return value nor a parameter.


Successful return 0, failure returns nonzero.


int on_exit (void (*function) (int, void*), void* Arg);


Functions-function pointers,
A function that needs to be called before the process exits.
The function does not have a return value but has two parameters:
The first parameter is from the status parameter of the Exit function,
The second argument is from the arg parameter of the On_exit function.


Arg-arbitrary Pointer,
will be passed as the second argument to the function that functions point to.


Successful return 0, failure returns nonzero.


3) Use Exit_success/exit_failure Macro constants
(possibly 0/1) as a parameter, call the exit () function to indicate success/failure,
Improve platform compatibility.


4) The function does not return.


5) The implementation of this function calls the _exit/_exit function.


3. Call the _exit/_exit function.


#include <unistd.h>


void _exit (int status);


1) Call process exits,
Its parent process calls the Wait/waitpid function to return the low 8 bits of status.


2) before the process exits,
First close all file descriptors that are still open,
The adoption of all its sub-processes to the INIT process (the PID 1 process)
Delivers a sigchild signal to the parent process.


3) The function does not return.


4) The function has a fully equivalent standard C version:


#include <stdlib.h>


void _exit (int status);


4. The last thread of the process executed the return statement.


5. The last thread of the process calls the Pthread_exit function.






Abnormal termination of the process
------------------


1. Call the Abort function to generate a SIGABRT signal.


2. The process receives some signals.


3. The last thread responds to a "cancel" request.


Wait/waitpid
----------------


Waits for the child process to terminate and get its terminating state.


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


pid_t Wait (int* status);


pid_t waitpid (pid_t pid, int* status, int options);


Successfully returns the PID of the terminating subprocess, which fails back to 1.


1. When a process is normal or abnormally terminated,
The kernel sends a SIGCHLD signal to its parent process.
The parent process can ignore the signal,
or provide a signal processing function for the signal, which is ignored by default.


2. The parent process calls the wait function:


1) block If all child processes are running.


2) If a child process has been terminated,
Returns the PID and termination state of the child process (via the status parameter).


3) If there is no need to wait for the child process, the return fails, errno is echild.


3. Before any of the child processes are terminated, the wait function can only block the calling process.
And the Waitpid function can have more options.


4. If there is a child process before the wait function is called,
has been terminated and is in zombie state, the wait function returns immediately,
And gets the terminating state of the child process.


5. The terminating state of the child process is returned to the caller by the output parameter status,
If you do not care about the terminating state, you can leave this parameter blank.


6. The terminating state of a child process can be
View the parameter macros defined in Sys/wait.h:


Wifexited (): Whether the child process terminates normally,
Yes, by Wexitstatus () macro,
Gets the child process call exit/_exit/_exit function,
The lower 8 bits of the passed parameter.
Therefore, the parameters passed to the Exit/_exit/_exit function should not be more than 255.


Wifsignaled (): Whether the child process terminates abnormally,
Yes, the Wtermsig () macro gets the signal to terminate the child process.


Wifstopped (): Whether the child process is paused,
Yes, the signal to pause the subprocess is obtained through the WSTOPSIG () macro.


Wifcontinued (): Whether the child process continues to run after a pause



7. If there are multiple child processes at the same time, and you need to wait for a specific subprocess,
You can use the Waitpid function with its PID parameters:


-1-Waits for either child process, equivalent to the wait function;
>0-waits for a specific sub-process (identified by the PID parameter);
0-Waits for any child process with the calling process to be the same process group;
<-1-Any child process that waits for a specific process group (identified by the absolute value of the PID parameter).



8. The options parameter of the Waitpid function is preferable to 0 (omitted) or a bit of the following value or:


Wnohang-Non-blocking mode,
Returns 0 if no child process state is available.


wuntraced-If job control is supported and the child process is in a paused state,
The status is returned.


wcontinued-If job control is supported and the child process is paused and continues,
The status is returned.



VIII. exec
--------


1. The EXEC function will completely replace the calling process with the new process.
and starts executing from the main function.


2. The EXEC function does not create a child process, and the new process takes the PID of the calling process.


3. The new process created by the EXEC function,
Completely replaces the code snippet, data segment, and stack segment of the calling process.


4. If the EXEC function succeeds, it will not be returned, otherwise 1.


5. The EXEC function consists of six forms:


#include <unistd.h>


int Execl (
Const char* Path,
Const char* ARG,
...
);


int Execv (
Const char* Path,
char* Const argv[]
);


int Execle (
Const char* Path,
Const char* ARG,
...,
char* Const envp[]
);


int Execve (
Const char* Path,
char* Const argv[],
char* Const envp[]
);


int EXECLP (
Const char* File,
Const char* ARG,
...
);


int EXECVP (
Const char* File,
char* Const argv[]
);


L: The command parameters of the new program are passed in as a separate string pointer
(const char* ARG, ...), the parameter table ends with a null pointer.


V: the command parameters of the new program are passed in as a string pointer array
(char* const argv[]), the array ends with a null pointer.


E: The environment variables of the new program are passed in as a string pointer array
(char* const envp[]), the array ends with a null pointer,
No e is copied from the Environ variable of the calling process.


P: If the first parameter does not contain "/", it is treated as a file name,
Search for the file according to the PATH environment variable.






System
----------


#include <stdlib.h>


int system (const char* command);


1. Standard C functions. Execute command,
Successfully returns the terminating state of the command corresponding to the process, and the failure returns-1.


2. If command is null, returning nonzero indicates that the shell is available,
Returning 0 indicates that the shell is not available.


3. The implementation of the function,
Functions such as vfork, exec, and waitpid are called.
Its return value:


1) returns 1 if there is an error calling the Vfork or Waitpid function.


2) If there is an error calling the EXEC function, exit (127) is executed in the child process.


3) If all succeeds, returns the terminating status of the command corresponding process
(obtained by the status output parameter of the Waitpid).


4. The advantage of using the system function without vfork+exec is that

The system function does the necessary processing for various errors and signals.





Process management under the liunx of "inductive finishing" (iii), multi-process waiting and termination

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.