650) this.width=650; "Src=" http://mmbiz.qpic.cn/mmbiz_png/ZjOsibraW8CLWLVibeTRzXWw Uy4je8rmiconxyukfmuia43wtxta55grfbfia17sgcmnmfrcl2swpzwm9pbc2ndyhnw/640?wx_fmt=png&tp=webp&wxfrom=5 &wx_lazy=1 "style=" Margin:0px;padding:0px;height:auto;vertical-align:top;border-style:none;width:auto; "alt= "640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy="/>
650) this.width=650; "width=" 650 "src=" http://mmbiz.qpic.cn/mmbiz/ p6vlqvia1uicwutdysibyelgvclgzbgurlkfibp3kryetvj1b8gib4msykk8dobxwricgrftfuqslz3dfzfc5alzfyg/0?wx_fmt=gif& Tp=webp&wxfrom=5&wx_lazy=1 "style=" Margin:0px;padding:0px;height:auto;vertical-align:top;border-style: None;width:auto; "alt=" 0?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1 "/>
Process
The process represents a running instance of a program, which is the smallest unit of allocation of resources, which is particularly official.
Process is a very important thing, we run a system running n processes at the same time, these processes are silently working, we write code, compiled, run, will also generate a process. This process consists of program code, data, variables (which occupy the system memory), open files (file descriptors), and the environment. In general, for Linux systems, program code and system function libraries are shared between processes, so at any point in memory there is only one copy of the code.
Because the process is so important, this article makes a basic summary of the process in Linux.
Create a process
#include <stdio.h> #include <unistd.h> #include <stdlib.h>int main () { pid_t pid; char *msg; int n; printf ("fork program starting\n"); pid = fork (); // Create a process // once created, both the parent and child processes start from here switch (PID) { case -1: perror ("Fork failed."); exit (EXIT_FAILURE); case 0: // when Fork returns a value of 0 o'clock, it indicates that this is a child process msg = "this is the Child "; n = 2; break; default: // when Fork is not 0 and-1, Indicates that this is the parent process msg = "This is the parent "; n = 3 ; break; } for (; n > 0; --n) { printf ("%s\n", msg); sleep (1 ); } exit (exit_success);}
We use fork
the function to create a child process, and fork
the declaration of the function is as follows:
#include <unistd.h>pid_t fork (void);
fork
The function returns a value of type pid_t, and the pid_t type is an int type. When the call fork
creation process succeeds, both the child process and the parent process fork
run from the code behind the function. So how do you differentiate between a child process or a parent process since it is all running from the same place?
The return values used in Linux are fork
distinguished by the following methods:
-
-
when Fork returns the value of pid_t is 0 o'clock, it is a child process;
-
when Fork returns a value of pid_t not 0 and-1, it is the parent process.
In the above code, I'm judging the fork
child or parent process based on the return value. It's a weird feeling.
Structure of the process
Again, the process is important and we need to understand that the structure of the process has to be said. So what fork
is the structure of the process inside the memory after we have created a process? Let's take a look.
Process memory Image
As shown in the actual coding, we do not take into account what is said in the diagram, but what is shown is the key to understanding the parent process and the child process, many questions about the process, we also based on the starting to think. In the following, I will specifically come up with a few questions to analyze why the process memory image is so important.
Process scheduling
On a single processor, only one process can run at a time and the other processes are waiting to run. However, what we actually feel is that there are multiple processes at the same time running at "simultaneous", which is why?
The operating system will give each process a certain run time, called "Time slice". The process runs in this "time slice" because the "time slice" is so short that it gives the illusion that multiple programs are running simultaneously. How does the operating system dispatch the process? There are many rules for process scheduling, such as scheduling algorithm based on priority, FIFO scheduling algorithm and so on.
The Linux kernel Process Scheduler is a process scheduler based on the priority of the process. High-priority processes run more frequently.
Process status
There are 5 states of processes on Linux:
-
run (running or waiting in the run queue
-
interrupt (Hibernate, Blocked, waiting for a condition to form or receive a signal)
-
non-interruptible (Received signal does not wake up and not run, process must wait until interrupt occurs)
-
Zombie (The process has terminated, but the process descriptor exists until the parent process calls WAIT4 () after the system call is released)
-
Stop (process receives sigstop, SIGSTP, Sigtin, Sigtou signal stops running)
ps -aux
What do the letters that identify the process mean when we use commands to view the status of the process? , the character of the specific process state is identified as shown in the following table:
Status Flag |
Status Description |
d |
|
r |
run |
s |
interrupted |
t |
stop |
z |
zombie |
> But, sometimes, we'll see some other signs, such as:
status flag |
|
w |
Zombie |
< |
High priority process |
n |
low-priority process |
l |
memory lock page |
See the above is interrupted ah, not interrupted ah, stop ah, zombie Ah, directly dizzy dead, so how to understand these concepts?
Running state
In Linux, processes that wait only for CPU time are called ready processes, they are placed in a running queue, and the status flag bit for a ready process is task_running. Once a running process time slice is exhausted, the Linux kernel Scheduler will deprive the process of control of the CPU and choose a suitable process from the running queue to run.
Sleep state
There are two kinds of process sleep states in Linux:
One is an interruptible sleep state, its status flag bit task_interruptible;
-
sends a sigcont signal to the process that can be restored from task_stopped state to task_running state.
The zombie state process is in the Task_dead state during the exit process. In this exit process, all the resources that the process occupies will be recycled, in addition to the TASK_STRUCT structure (and a few resources). So the process only left task_struct such an empty shell, so called zombies. The reason for the retention of task_struct is that the exit code of the process, as well as some statistical information, are stored in task_struct. And its parent process is likely to be concerned about this information. In the shell, for example, the $ variable saves the exit code for the last exiting foreground process, and this exit code is often used as a condition for the IF statement. Of course, the kernel can also store this information elsewhere, freeing the task_struct structure to save some space. However, the use of the TASK_STRUCT structure is more convenient because the kernel has established a relationship between the PID and the Task_struct lookup, as well as the parent-child relationship between processes. Releasing the task_struct, you need to create some new data structures so that the parent process can find the exit information for its child processes. The parent process can wait for the exit of one or some of the child processes through a system call to the wait series, such as WAIT4, Waitid, and get its exit information. Then the system call of the wait series will also release the Corpse (task_struct) of the child process. As the child process exits, the kernel sends a signal to its parent process to notify the parent process to "corpse". This signal is SIGCHLD by default, but can be set when a child process is created through the clone system call. The following code can be used to create a exit_zombie state of the process: if (fork ()) while (1) sleep (100), using PS- Aux will see the following zombie process information. The child process of this zombie state persists as long as the parent process does not exit. So if the parent process exits, who is going to "corpse" the child process? When the process exits, it will host all its child processes to another process (making it a child of another process). Who's hosting it for? It may be the next process that exits the process group where the process is located, if one exists, or the number 1th process. So every process, every moment, has a parent process present. Unless it is process number 1th.
This article is from the "Home of Linux" blog, please be sure to keep this source http://pyhton.blog.51cto.com/8763915/1854157
Basic knowledge of Linux process summary