5.1 Process Flags
Each process has a unique representation of the PID, which is unique but reusable.
The process with ID 0 is usually a scheduler process, which is also a system process because it does not execute any programs on disk.
The process with ID 1 is usually the init process, called by the kernel at the end of the bootlicking process.
ID 2 is the page daemon, which is responsible for supporting paging in the virtual storage system.
Pid_t getpid() Obtains the PID of the calling process pid_t getppId () Obtains the pid of the parent process of the calling process uid_t getuid() Obtains the USER ID of the calling process gid_t getgid() Obtains the group user ID of the calling processCopy the code
5.2 Creating a Process
You can use some functions to create a process
The child and parent continue to fork. The child gets a copy of the parent’s data space and stack, but not shared. The parent and child share only the body
The child process also copies the unclear portions of the parent’s standard IO buffer
Pay attention to the implementation part due to the file, file descriptors, pointing to the file table item is run by the kernel, and so is a parent and child process will point to the same file table items, unless modified file descriptors, file contains the file offset table items (i.e., any process modification after read the next process is the content of the modified)!
Network service processes typically individually close file descriptors that they don’t need to avoid unnecessary impact.
Many current implementations do not execute a complete copy of the parent’s stack, using copy-on-write techniques.
These areas are shared by the parent and child processes, and the kernel changes their access to read-only. If either of the parent or child processes tries to modify these areas, the kernel makes only a copy of that area of memory, usually a page in virtual memory.
In fact, the kernel also assigns a page table to the child process, but these page tables point to the parent process and are marked as read-only. When the child process tries to write, a write protection error occurs, and the error is handled by copying the error content into the page table.
pid_tFork () Returns two different values for the child process0The parent process returns the PID of the created child processCopy the code
Vfork is different from fork, which is typically created to call exec, so
Vfork:
Runs in the parent’s space, does not perform write time copy like fork! Turn the page table read-only and make it writable.
Once the child process modifies the data (except for the return value from Vfork), making a function call, or returning without exec or exit can cause problems
Ensure that the child process runs first, and the parent process is scheduled to resume running once exec or exit is called in the child process.
Note the difference between the exit() function and the _exit() function. The exit() function flushes the buffer, so the parent process’s buffer is likely to be cleaned.
pid_t vfork()
Copy the code
5.3 Zombie and Orphan Processes
Orphan Process:
For processes whose parent has terminated, their parent will be changed to init
When a process terminates, the kernel checks all the active processes one by one to see if it is a child of the process it is terminating, and if so, changes its parent process ID to 1
Note that an orphan process does not become a zombie process, because when it terminates, the init process must call a wait function to obtain its terminated state.
Zombie progression:
When the terminating parent calls wait or WaitPID, it can get some information about the child process, including the process ID, termination status, and total CPU time. Based on this information, the kernel releases the storage area that terminates the process and closes all its open files.
A process that has terminated but has not been cleaned up by its parent is called a zombie process.
// If I don't collect the body, my son will become a zombie
// Orphans need not be afraid, foster father is the eldest
// Have a grandson to be a dragon son, hahaha
// Simple example, there is no need to wait for grandchild process, and grandchild process does not become zombie process
if(fork())
{
// Process 1 processes process 2
wait(NULL);
}
else
{
// process 2fork processes 3 and terminates
if(fork())
{
exit(a); }else
{
// Successfully obtained the adoptive parent process init}}Copy the code
5.4 Handling process Termination
When a process terminates normally or abnormally, the kernel sends a SIGCHLD signal to its parent, which is an asynchronous event that can occur at any time the parent process is running.
// Returns the id of the terminated child process
pid_t wait(int*statloc) If the child process has terminated and is a zombie process, return immediately and obtain the status of the child process. Otherwise, wait until any child process terminates.NULLDoes not care about the end state integer pointer fills the end state into the memory to which the address pointspid_t waitpid(pid_t pid,int *statloc,int options)
pid:
- 1Wait for any child process >0Wait for the child process whose PROCESS ID is equal to PID to terminate0Any child process whose waiting group ID equals the calling process group ID <- 1Any child process whose wait group ID equals the absolute value of pid statloc0The result of the blocking wait or the following press or WCONTINUE WNOHANG is returned without blocking0WUNTRACED if no child processes are zombie processesCopy the code
Note that in addition to the parent process waiting for the child process to finish, the child process can also wait for the parent to finish
// The child waits for the parent to finish
while(getppid()! =1)
{
/ / non-blocking
}
while(getppid()! =1); // Block mode
Copy the code
5.5 the exec function
When process calls an exec, completely replace the process execution program became the new program, and the new program, since its main function is executed, because call exec does not create a new process, before and after the process id did not change, the exec just using a new process on the disk to replace the current process of text and data segments, heap and stack.
Exec is used for initial execution of the new program, and exit and wait functions handle terminations and wait terminations.
Exec //exec(l,v)(p) //l means to pass an argument to a function parameter //v means to define an argument to a function parameter //p means to customize the environment variable, If there is no p, the environment variable int system(const char *cmdstring) // calls fork, which returns the end status of the processCopy the code
5.6 Changing user IDS and Changing user group ids
Note Super permission is required
int setuid(uid_t uid) // Set the user ID
int setgid(gid_t gid) // Set the user group ID
Copy the code