Reprint: https://blog.csdn.net/lvyibin890/article/details/82193900
PCB (progress control block), process control block. Let's not discuss what PCB is. In fact, the concept of process itself is a concept that is difficult to understand. When an executable program is executed by the system, it becomes a process. So what is there in this process, and what resources are allocated to this process by the system? In addition, how is the process stored in memory?
For a process, it is actually an executable program before it is executed. This program is placed on the disk. When it is to be executed, it is first loaded into the memory, then put into the register, and finally let the cpu execute the program. At this time, a static program becomes a process.
So how does the operating system manage these processes? The operating system connects processes through a two-way linked list. However, the process is actually an abstract concept. The system must describe the process through something, and then it can manage the process. So the PCB comes out. The operating system describes the process through the PCB, so the bidirectional linked list is actually connected to the PCB. What is this PCB? It is a structure used to describe the process. Under Linux, it is task_struct structure.
Process memory allocation
When each process runs, it will get 4G of virtual memory. Under 32-bit Linux, 3G is given to the user, 1G is given to the kernel, and task_struct is stored in the 1G kernel system space.
Each process has its own private user space (0-3G), which is invisible to other processes in the system.
The highest 1GB kernel space is shared by all processes and the kernel.
As for why the 1G kernel space is needed, it is because the process needs to call some system calls to run by the kernel. Part of the logic of the program may be run by the kernel, so part of the virtual address must be reserved for the kernel.
In addition, what we often call virtual address space is actually user space.
Therefore, through the above figure, we can find that the PCB of each process exists in the kernel space shared by all processes, which is easy to understand. We said that the operating system manages the process, that is, the PCB of all processes is managed in the kernel space through the linked list. If a process is to be created, In fact, such a 4G virtual memory is allocated, and its own PCB is added to the two-way linked list in the shared kernel space.
What is in the PCB?
Let's take task_strcut structure to speak, the following paragraph of source code comments, I copied from a great God, just to facilitate their understanding
struct task_struct { volatile long state; //Indicates whether the process can be executed or interrupted unsigned long flags; //Flag is the process number, which is given when fork() is called intsigpending; //Are there any pending signals on the process mm_segment_taddr_limit; //The process address space is used to distinguish between kernel processes and ordinary processes in different memory locations //0-0xBFFFFFFF foruser-thead //0-0xFFFFFFFF forkernel-thread //The scheduling flag indicates whether the process needs to be rescheduled. If it is not 0, scheduling will occur when returning from kernel state to user state volatilelong need_resched; int lock_depth; //Lock depth longnice; //Basic time slice of the process //There are three kinds of real-time process scheduling strategies: sched and SCHED_FIFO,SCHED_RR, time sharing process: SCHED_OTHER unsigned long policy; struct mm_struct *mm; //Process memory management information int processor; //If the process is not running on any CPU, CPUs_ The value of runnable is 0, otherwise it is 1. This value is updated when the running queue is locked unsigned long cpus_runnable, cpus_allowed; struct list_head run_list; //Pointer to the run queue unsigned longsleep_time; //Sleep time of the process //It is used to connect all processes in the system into a two-way circular linked list, and its root is init_task struct task_struct *next_task, *prev_task; struct mm_struct *active_mm; struct list_headlocal_pages; //Point to local page unsigned int allocation_order, nr_local_pages; struct linux_binfmt *binfmt; //The format of the executable file in which the process is running int exit_code, exit_signal; intpdeath_signal; //The termination of a parent process is a signal sent to a child process unsigned longpersonality; //Linux can run iBCS2 compliant programs generated by other UNIX operating systems intdid_exec:1; pid_tpid; //Process identifier, used to represent a process pid_tpgrp; //Process group ID, indicating the process group to which the process belongs pid_t tty_old_pgrp; //Group ID of the process control terminal pid_tsession; //Session ID of the process pid_t tgid; intleader; //Indicates whether the process is a session supervisor struct task_struct*p_opptr,*p_pptr,*p_cptr,*p_ysptr,*p_osptr; struct list_head thread_group; //Thread linked list struct task_struct*pidhash_next; //Used to chain processes into the HASH table struct task_struct**pidhash_pprev; wait_queue_head_t wait_chldexit; //For wait4() struct completion*vfork_done; //For vfork() unsigned long rt_priority; //Real time priority, which is used to calculate the weight value of real-time process scheduling //it_real_value,it_real_incr is used for REAL timer, and the unit is jiffies. The system is based on it_real_value //Set the first end time of the timer When the timer expires, send SIGALRM signal to the process, and //it_real_incr reset end time, it_prof_value,it_prof_incr is used for Profile timer, and the unit is jiffies. //When the process is running, no matter in what state, each tick makes it_ prof_ The value value is minus one. When it is reduced to 0, it is sent to the process //Signal SIGPROF, and according to it_prof_incr reset time //it_virt_value,it_ virt_ Value is used for Virtual timer, and the unit is jiffies. When the process is running, no matter what //In this state, each tick makes it_ virt_ When the value decreases to 0, it sends a signal SIGVTALRM to the process according to //it_virt_incr resets the initial value. unsigned long it_real_value, it_prof_value, it_virt_value; unsigned long it_real_incr, it_prof_incr, it_virt_value; struct timer_listreal_timer; //Pointer to real-time timer struct tmstimes; //Record the time consumed by the process unsigned longstart_time; //Time the process was created //Record the user state time and core state time consumed by the process on each CPU longper_cpu_utime[NR_CPUS],per_cpu_stime[NR_CPUS]; //Missing pages in memory and exchanging information: //min_flt, maj_flt accumulates the number of secondary missing pages (Copyon Write page and anonymous page) and the number of primary missing pages (from the mapping file or exchange page) of the process //Number of pages read by the device); nswap records the cumulative number of pages swapped out by the process, that is, the number of pages written to the switching device. //cmin_ flt, cmaj_ FLT and cnswap record the cumulative number of secondary missing pages, main missing pages and swapped out pages of all descendant processes of the ancestor. //When the parent process reclaims the terminated child process, the parent process will accumulate the information of the child process into these fields of its own structure unsignedlong min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap; int swappable:1; //Indicates whether the virtual address space of the process is allowed to be swapped out //Process authentication information //uid,gid is the user identifier and group identifier of the user running the process, usually the uid,gid of the process creator //euid and egid are valid uid and GID //fsuid and fsgid are file system uid and GID. These two ID numbers are usually equal to the valid uid and GID //Use them when you have access to the system. //suid and sgid are backup uid and GID uid_t uid,euid,suid,fsuid; gid_t gid,egid,sgid,fsgid; int ngroups; //Record how many user groups the process is in gid_t groups[NGROUPS]; //Record the group in which the process is located //The capabilities of the process are the set of significant bits, the set of inherited bits, and the set of allowed bits kernel_cap_tcap_effective, cap_inheritable, cap_permitted; int keep_capabilities:1; struct user_struct *user; struct rlimit rlim[RLIM_NLIMITS]; //Process related resource restriction information unsigned shortused_math; //Use FPU charcomm[16]; //The name of the executable that the process is running //file system information int link_count, total_link_count; //Null if the control terminal where the no TTY process is located. If the control terminal is not required, the pointer is null struct tty_struct*tty; unsigned int locks; //Interprocess communication information struct sem_undo*semundo; //All undo operations of the process on the semaphore struct sem_queue *semsleeping; //When a process is suspended due to a semaphore operation, it records the waiting operation in the queue //The CPU state of the process. When switching, save it to the task that stops the process_ In struct structthread_struct thread; //file system information struct fs_struct *fs; //Open file information struct files_struct *files; //Signal processing function spinlock_t sigmask_lock; struct signal_struct *sig; //Signal processing function sigset_t blocked; //The signal that the process is currently blocking. Each signal corresponds to one bit struct sigpendingpending; //Are there any pending signals on the process unsigned long sas_ss_sp; size_t sas_ss_size; int (*notifier)(void *priv); void *notifier_data; sigset_t *notifier_mask; u32 parent_exec_id; u32 self_exec_id; spinlock_t alloc_lock; void *journal_info; };
We can pick a few key points and write them down
Identification related: pid, ppid, etc
File related: the process needs to record the open file information, so it needs the file descriptor table
Memory related: memory pointer, pointing to the virtual address space (user space) information of the process
Priority related: the scheduling priority of a process relative to other processes
Context information related: the values in all registers of the CPU, the state of the process and the contents on the stack. When the kernel needs to switch to another process, it needs to save all the states of the current process, that is, save the process context of the current process, so that when the process is executed again, it can restore the state at the time of switching and continue to execute.
Status related: the current status of the process, indicating the status of the process
Signal correlation: the signal processing function of the process and the signal recording whether the current process has to be processed
I/O related: records the interaction between processes and various I/O devices
--------
Copyright notice: This article is the original article of CSDN blogger "TLpigff", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this notice for reprint.
Original link: https://blog.csdn.net/lvyibin890/article/details/82193900