Process (DAY09)

Source: Internet
Author: User
Tags terminates

process Management One, process-based process and program differentiation each process has its own PID, and all processes running on the PCB operating system form a tree. How do I view this tree? Pstree (1) The root process is the init PID is1inter-process affinity the relationship between the two parent-child relationships uses top (1) command to observe the status of the process using PS (1) command to view the process. Ii. process Creation Parent process How does the parent process create a child process? The parent process calls fork (2) to create a new child process. #include<unistd.h>pid_t Fork (void); function: Create a sub-process parameter:void: Return value: in parent process-1error errno is set successfully in the parent process the PID of the subprocess is returned in the child process0A copy-on-write technique is returned to illustrate the use of fork (2), create a new sub-process code see FORK.C III, Process exit1, pay attention to return and exit (3return is only returned from the function. Ends the life cycle of a function. Exit (3End the life cycle of the process exit (3) #include<stdlib.h>voidExitintstatus); function: Causes the process to terminate normally parameters: status: Specifies the exit status code for the process. Return value: Returns this value status&0377 give the parent process an example of exit (3See EXIT.C Currently, a program executed under Bash, the parent process of this process is bash. 2, you can use Atexit (3) or On_exit (3registers a function with the process and calls the write-registered function when the process exits. Is the process over when it is returned from the main function? The process did not end. Atexit (3) #include<stdlib.h>intAtexitvoid(*function) (void) ; function: Registers a function with the process, the parameter is called when the process exits: function: Specifies the return value of the last words function :0Success Not 0 failed Note:1, the same function is registered once, it is called once, and the registration is called multiple times. 2, the order in which the functions are registered and the order of the calls3, registered last words function quilt process inheritancevoid(*function) (voidFor example, use atexit to register a note function code with the process, see Atexit.con_exit (3) #include<stdlib.h>intOn_exit (void(*function) (int,void*),void*arg); function: Registers a function that is called when the process terminates: function: Specifies the name of the last Word function arg: Specifies the second parameter of the function, the first argument of function is exit (3) exit status code. return value:0Success not 0 failedvoid(*function) (int,void*) Examples of using on_exit (3) Register the last words function code see On_exit.c_exit (2The parent process creates the child process and then the parent process exits immediately, and the child process is adoptive to the INIT process without terminating the child process. These child processes are called orphan processes. Show the orphan process phenomenon code see ALONE.C Iv., Process resource reclamation when the process terminates, sends a SIGCHLD signal to the parent process, after the parent process receives the signal, calls wait (2) family of functions, to reclaim the resources of the child process. The child process is in zombie state when the parent process has not yet reclaimed the resources of the child process. The child process at this time is called a zombie process. Example of a zombie process code see ZOMBLE.CWAIT (2) #include<sys/types.h>#include<sys/wait.h>pid_t Wait (int*Status ); function: Wait for the process state to change parameters: status: If it is not empty, the information for the child process is stored. This integer can be detected by using a macro. wifexited (status) Returns True if the child process terminates normally. Wexitstatus (status) returns the exit status code for the child process. Only when the top macro returns to True, is used wifsignaled (status) If the child process is signaled, then the return Truewtermsig (status) is only used if the macro above is true. Returns the number of the signal that caused the child process to terminate. return value:-1The error returns the PID of the terminating child process. For example, use Wait (2) to reclaim the resources of the child process. Code See WAIT.C Supplement: How to send a signal to a process to terminate the process? Kill-signal number PID signal number2 3 9pid_t waitpid (pid_t pid,int*status,intoptions); function: Wait for the child process state to change parameters: PID: Specifies the PID of the subprocess to wait. <-1: Waits for any child process, the group ID of the child process equals the absolute value of the PID. -1: Waits for any child process0: Waits for any child process, which writes the group ID of the child process equal to the current process group ID. >0The:p ID specifies the child process PID to wait on. Status: If it is not empty, the information for the child process is stored. Options: You can change whether or not to wait for the child process to terminate. Wnohang: Returns immediately if no child process exits. 0If no child process exits, the block waits for the child process to exit. return value: PID error for successful child process return-1if Wnohang is specified. 0all of the child processes that are waiting are not terminated. Wait (&s) ===waitpid (-1, &s,0) There are 1 or more processes in a process group. In general, the subprocess and parent processes belong to the same process group. Summary: First, the basis of the process Pstree PS top two, the creation of the process fork (2) Iii. exit of the process exit _exit atexit on_exit Orphan process Iv. recovering resources for child processes wait Waitpid zombie process

Process (DAY09)

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.