- How the operating system organizes processes
- What is a process
A process is a program that is in execution and all the resources it contains, including virtual processors, virtual spaces, registers, stacks, global data segments, and so on.
In Linux, each process is assigned a data structure when it is created, called the Process control block. The Process control block contains a lot of important information for system scheduling and the process itself to perform the use. The user space of the process primarily stores code and data.
- Creation of processes
The process is created by calling the:: Fork (),:: Vfork () and:: Clone () system call to create a new process, and all by calling Do_fork to implement the process.
the three differences are as follows :
- :: Fork (): All data structures of the parent process are copied to the child process.
- :: Vfork (): Copies only task_struct and kernel stacks, so only one thread of the parent process is generated.
- :: Clone ():: Clone () allows you to selectively inherit the resources of the parent process, either by choosing to share a virtual space with the parent process like: vfork (), so that the creation is a thread, and you can not share it with the parent process. The process that can be created and the parent process is no longer a parent-child relationship, but a sibling.
- Exit of the process
The process exits execution by calling exit (), which ends the process and frees all resources. The parent process can query whether the child process is terminated through WAIT4 (). The process exits after execution in a zombie state until its parent process calls wait () or waitpid (). When the parent process exits, the kernel specifies another process for the thread group or the INIT process as the new parent process for its child processes.
- How process state is transformed (gives process state transition diagram)
- Three basic statuses:
Running state: The process consumes CPU and runs on the CPU;
Ready state: The process already has operating conditions, but the CPU has not been allocated;
Blocking state: The process is temporarily unable to run because it waits for something to happen;
State transition Diagram:
Transition process between processes:
Run---"ready: This is caused by scheduling, mainly because the process takes up CPU time too long
Ready---Run: the time slice of the running process runs out and the dispatch goes to the ready queue to select the appropriate process to allocate the CPU
Run---Blocked: an I/O request or wait for something to happen
Block---Ready: The event that the process waits for occurs and enters the ready queue
Block-run: Even if the blocking process is allocated CPU and cannot be executed, the operating system download will not load the blocking queue to pick up, and its scheduled Selection object is the ready queue:
Ready-"blocked: Because the ready state is not executed at all
- How the process is scheduled
- Priority of the process
The process provides two priorities, one is the normal process priority, the second is the real-time priority, the former uses the Scheed_normal scheduling policy, and the latter is optional sched_fifo or SCHED_RR scheduling. At any time, real-time processes are prioritized higher than normal processes, and real-time processes are preempted only by higher-level real-time processes, while real-time processes are scheduled in either FIFO (one-shot) or RR (multiple rotation) rules.
2. Main Scheduler Schedule
Schedule is the function of the main scheduler, in many parts of the kernel, if you want to assign the CPU to another process that is different from the currently active process, call the main scheduler function schedule or its child function __schedule directly.
void __sched schedule (void) { struct task_struct *tsk = current ; Sched_submit_work (tsk); __schedule ();}
3. Switching of the process context
Context_switch is actually an allocator, and he will call the method of the specific architecture required
Call SWITCH_MM () to switch virtual memory from one process map to the new process
SWITCH_MM Replace the memory management context described by TASK_STRUCT->MM, the details of the work depend on the processor, mainly including loading the page table, brushing the address translation backup buffer (partial or all), providing new information to the Memory Management Unit (MMU)
Call switch_to () to switch from the processor state of the previous process to the processor state of the new process. This includes saving, resuming stack information, and register information
Static void__sched __schedule (void) { structTask_struct *prev, *Next; unsignedLong*Switch_count; structRQ *RQ; intCPU; CPU=smp_processor_id (); RQ=Cpu_rq (CPU); Rcu_note_context_switch (CPU); Prev= rq->curr;//Current process for current queueSchedule_debug (prev); ... if(Unlikely (!rq->nr_running))//when the current RQ no process runs, take a task execution from the other CPUsidle_balance (CPU, RQ); //Select the next scheduled processNext =Pick_next_task (RQ); ... //Context SwitchesContext_switch (RQ, Prev, next); ...}
Modern scheduling algorithm for 4.Linux: CFS (fully fair dispatch)
CFS is a completely fair scheduling algorithm that treats all processes uniformly. The schedule () function will preempt the currently running task, and when it starts to determine the next task to be dispatched, it calls the Pick_next_task function, which invokes the CFS scheduler through the Scheduler class, returning the associated sched_entity.
StaticInlinestructTask_struct *Pick_next_task (structRQ *RQ) { Const structSched_class *class; structTask_struct *p; /*the data structure used*/ if(Likely (rq->nr_running = = rq->cfs.nr_running)) {//if nr_running==cfs.nr_running, it indicates that there are no RT tasks in the current RQ queue.//The RT task is not in the CFS queue, and its priority setting is not the same as the CFS. P= Fair_sched_class.pick_next_task (RQ);//pick the process that will be switched in in the CFS queue if(likely (p))returnp; } for_each_class (class) {p=class-Pick_next_task (RQ); if(P)returnp; } }
- View of the Linux operating system
Linux system can support multiple users at the same time, each user to their own file devices have a special right to ensure that the user does not interfere with each other. and has better stability and efficiency than window. And the operation of Linux is more concise than the operation of Windows, in the command line and script above the operation, only a few simple lines of command to complete the mouse need to click many times the operation function.
11724277
Https://www.cnblogs.com/sky-heaven/p/8118110.html
Model analysis based on Linux process