1. Process concept
At the user level, a process is a dynamic running instance of a program,
At the operating system level, the process is the basic unit for the operating system to dispatch and allocate resources, each of which has its own independent address space and running state.
The operating system creates a series of data structures for the process to manage the process. The operating system creates a virtual address space and a process control block for each process.
2. Virtual address space
Under 32-bit operating system, a process can have up to 4G of memory space, in the early computer systems, the program is loaded directly into memory, that is,
The address space accessed by the program is the real memory space, and when multiple processes are executed at the same time, all the memory is loaded first, then the remaining memory
The pickup space is allocated to other processes.
There is a big problem with this allocation method, first of all, the address space is not isolated, so that a process is completely possible to modify the B process data, while the memory utilization is very low.
This introduces the concept of virtual address space, creating a virtual address space for each process, under a 32-bit system, the size of the virtual space is 4G, and then using
The page table mechanism maps virtual addresses to actual physical addresses
Virtual addresses enable the isolation of address spaces between multiple processes, and no one process interferes with the operation of other processes, while the mapping of the address space is determined by the operating system
To improve the space utilization.
Linux, the address space is divided into 5 segments, the address space from high to low for
1. Kernel zone: Kernel space reserved for operating system kernel, does not allow application to read and write kernel space
2. Stack area: Stack down, save local variables, function parameters, return address, etc.
3. Memory-Mapped segments:
4. Heap area: heap upward growth, save the dynamic open space
5.BSS segment: Global variables and local static variables that are uninitialized or initialized to 0
6. Data segment: initialized and initialized to non-0 global variables and local static variables
7. Code snippet: Executable code, string literal, read-only variable
8. Reserved area: At the bottom of the virtual space, no physical address, the reference to it is illegal operation, where the operating system is protected and the user is not authorized to access, this is why
C language to set the null pointer to 0, address 0 in the reserved area, generally does not point to valid data.
3.task_struct
The Linux operating system uses the TASK_STRUCT management process, in other words, at the kernel level, it does not see a variety of applications, various running processes, it sees a
Task_struct. A process corresponds to a task_struct, and the operating system uses a chained structure to manage task_struct. The data members in the task_struct identify the
All information about the process.
1. Process status
volatile Long State ; int
The state's value identifies the current status of the process, including running, blocking, zombie, and so on.
2. Process PID
pid_t pid;
Process PID Identification process number
3. Process Kernel stack
void *stack;
4. Mark
unsigned int flags;
5. Identify members of process kinship
structTask_struct *real_parent;/*Real Parent Process*/ structTask_struct *parent;/*recipient of SIGCHLD, WAIT4 () reports*/ structList_head children;/*List of my children*/ structList_head sibling;/*linkage in my parent ' s children list*/ structTask_struct *group_leader;/*Threadgroup leader*/
6.performance Event
#ifdef config_perf_events struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts]; struct mutex Perf_event_mutex; struct List_head perf_event_list; #endif
Analyze the performance issues of the process
7. Process scheduling
int Prio, Static_prio, Normal_prio; unsigned int rt_priority; const struct Sched_class *sched_class; struct sched_entity se; struct sched_rt_entity RT; unsigned int policy;
4. Process status
The basic state of the process is the following three types of
1. Ready state: The process has obtained all resources except the CPU, as long as the CPU is obtained, it can be executed immediately, the process is usually in the ready state is stored in
In the Ready queue
2. Execution status: Process gets CPU, program in execution state
3. Blocking status: The process discards the processor and is paused because some events are temporarily unable to continue execution.
In a Linux system, the specific state of the process is;
R: Run state: Ready state and execution state
S: Can interrupt the sleep state, the process is in a wait state, when the resources required by the process is freed, the process becomes a running state.
D: Non-disruptive sleep state: The process is in a wait state and can only be awakened with the wake_up () function
Z: Exit status, the process has terminated, but the parent process has not reclaimed the resources of the process.
Process Control block and virtual address space