One, process identification
Each process has a unique process ID that is represented by a nonnegative integer. Although the ID is unique, the process ID is reusable. When a process terminates, its process ID becomes a candidate for reuse.
There are some specialized processes in the system, but the specifics vary with the implementation. A process with id 0 is usually a dispatch process, often referred to as a swap process. The process is part of the kernel, and it does not execute any programs on disk, so it is also a system process. Process ID 1 is typically the INIT process, which has a kernel call at the end of the bootstrap process. The init process never terminates, he is a user process for a normal user (unlike the swap process, he is not a system process in the kernel), but it runs with superuser privileges.
Each UNIX system implementation has its own set of kernel processes that provide operating system services, for example, in some UNIX virtual storage, Process ID 2 is the page daemon (Daemnon), which is responsible for paging operations that support virtual storage systems.
Attached: process-related functions
1 #include <sys/types.h>2 # Include <unistd.h>3 pid_t getpid (void ); Return: Process of calling process I D 4 pid_t getppid (void ); Return: The parent process of the calling process I D 5 uid_t getuid (void ); Return: The actual user of the calling process I D 6 uid_t geteuid (void ); Return: A valid user of the calling process I D 7 gid_t getgid (void ); Return: The actual group of the calling process I D 8 gid_t getegid (void ); Return: The active group of the calling process I D
second, fork function
#include <unistd.h>pid_t fork (void);
Explanation: A new process created with the fork function becomes a child process, the fork function is called once, but returns two times, two times the difference is the child process return value is 0, the parent process's return value is the process ID of the new child process. The child process and the parent process continue to execute the instruction after the fork call, which is a copy of the parent process. For example, a child process gets a copy of the parent process data space, heap, and stack. Note that this is a copy owned by the child process, and the parent and child processes do not share the portion of the storage space. This section performs write-time replication.
1#include <sys/types.h>2#include"Ourhdr.h"3 intglob=6;4 Charbuf[]="a write to stdout\n";5 intMainvoid)6 {7 8 int var;9 pid_t pid;Ten var= the; One if(Write (Stdout_fileno,buf,sizeof(BUF)-1)!=sizeof(BUF)-1) Aprintf"Write Error"); -printf"before fork\n"); - if((Pid=fork ()) <0) theprintf"Fork error\n"); - Else if(pid==0) - { -glob++; + var++; - } + Else ASleep2); atprintf"pid=%d,glob=%d,var=%d\n", Getpid (), Glob,var); -Exit0); -}
You can see that the changes made to the variable by the child process do not affect the value of the variable in the parent process.
In general, it is uncertain whether the parent or child processes are executed after the fork, depending on the scheduling algorithm used by the kernel, and requiring some form of interprocess communication if the parent process and child processes are required to synchronize with one another.
File sharing
There are two common scenarios in which file descriptors are processed after fork:
(1) The parent process waits for the child process to complete. (2) The parent and child processes each execute a different program segment.
In addition to opening files, many other properties of the parent process also have child process inheritance, including:
Actual user i d, actual group I d, active user I d, effective group I d.
• Add Group I D.
• Process Group I D.
• Conversation period I D.
• Control terminals.
• Set-user-I d flag and set-group-i-D flag.
• Current working directory.
• root directory.
• File mode to create a masking word.
• Signal shielding and alignment.
• Close flag on execution of any open file descriptor.
Environment
• Shared storage segments that are connected.
• Resource limitations.
The differences between parent and child processes are:
The return value of the fork.
• Process I D.
• Different parent process I D.
• Sub-process T M s _ u t i m e, T M s _ s t i m E, T M S _ c u t i m E and T M s _ u s t i m e set to 0.
• The parent process sets the lock that the child process does not inherit.
• Pending alarms for child processes are cleared.
• The pending signal set of the child process is set to the empty.
Two reasons for fork failure:
(a) There are already too many processes in the system (which usually means something has gone wrong)
(b) The total number of processes for the actual user ID exceeds the system limit.
Usage of fork:
(1) A parent process wants to assign itself, which is the parent and child processes executing different code snippets at the same time.
(2) A process has to execute a different program.
function Vfork
(1) The Vfork function is used to create a new process, and the purpose of the process is to exec a new program. The difference between vfork and fork: (1) v f o r K creates a sub-process like F o r K, but it does not completely copy the address space of the parent process into the child process because the child process immediately calls E x E C (or e x i T) and therefore does not save the address space. However, before the child process calls E x e C or E x i T, it runs in the space of the parent process. This mode of operation improves efficiency in some of the page-based virtual memory implementations of the U N I X (as mentioned in the previous section, followed by E x e c After f o r K and similar to the copy-on-write technique).
(2) v f o r K guarantees that the child process runs first, and the parent process may be scheduled to run after it calls E x e C or E x i t. (If the child process relies on further actions of the parent process before invoking these functions, it causes a deadlock.) )
function exit
The process has 5 normal terminations and 3 types of abnormal termination
5 Normal termination modes are:
Execute the return statement inside the main function, call the Exit function, call the _exit function or the _exit function, and the last thread of the process executes the return statement in its startup routine, and the last thread of the process calls the Pthread_exit function.
3 kinds of abnormal termination methods are as follows:
Call Abort, when the process receives some signals, the last thread responds to the cancel request.
In either case, the parent process of the terminating process can use the wait or Waitpid function to get its terminating state.
For all processes that the parent process has terminated, their parent process changes to the INIT process, which we call the INIT process adopted.
Zombie Process: A process that has been terminated, but whose parent process has not yet dealt with it, becomes a zombie process.
function wait and waitpid
#include <sys/wait.h>pid_t wait (int *statloc);p id_t waitpid (pid_t pid,int *statloc,int options);
What happens to calls to wait and waitpid processes
• Blocking (if all of its child processes are still running).
• The termination state of the tape process returns immediately (if a child process has been terminated and is waiting for the parent process to access its terminating state).
• An error is returned immediately (if it does not have any child processes).
The differences between the two functions are:
• Before a child process terminates, wait causes its callers to block, and Waitpid has a selection that allows the caller to
Plug
Waitpid does not wait for the first terminating child process-it has several choices that can control the process it waits for.
Macros that check the terminating state returned by W a i t and w a i t P i d
The function of the PID parameter in the Waitpid function is explained as follows:
• pid = =-1 waits for any child process. So in this function the W a i T P i d is equivalent to w a i t.
• pid > 0 Wait for its process i d and P I d to be equal to the child process.
• pid = = 0 waits for its group I d to be equal to any of the child processes that call the process's group I d.
• PID <-1 wait for any sub-process whose group I d equals the absolute value of p i d .
The function of the options parameter in the Waitpid function is explained as follows:
Options are either 0 or the result of a constant bitwise OR operation in a table.
The Waitpid function provides three features that the wait function does not provide:
(1) Waitpid waits for a specific process (while wait returns the state of either terminating child process).
(2) Waitpid provides a non-blocking version of wait. Sometimes you want to get the state of a child process, but you don't want to block.
(3) Waitpid support job control (with wuntraced selection).
1#include <sys/wait.h>2#include <unistd.h>3#include <stdio.h>4#include <stdlib.h>5 intMainvoid)6 {7 pid_t pid;8 if((Pid=fork ()) <0)9 {TenPerror ("Fork"); One}Else if(pid==0) A { - if((Pid=fork ()) <0) -Perror ("Fork"); the Else if(pid>0) -Exit0); -Sleep2); -printf"second child,parent pid is:%d\n",(Long) Getppid ()); +Exit0); - } + if(Waitpid (Pid,null,0)!=pid) APerror ("Waitpid"); atExit0); - } -~
The above program realizes the function is: a process fork a child process, but does not wait for the child process to terminate, also does not want the child process to be in the zombie state to know the parent process terminates, by calls the fork two times to implement.
Sleep is called in the second child process to ensure that the first child process is terminated when the parent process ID is printed. After the fork, the parent and child processes can continue to execute--we cannot predict which one will be executed first. If you do not sleep the second child process, after the fork, it may execute first than its parent process, and the parent process ID It prints will be the parent process that created it, not the init process.
Process Control of Linux environment Programming (ON)