First assignment: Analysis of process model based on Linux0.11 operating system

Source: Internet
Author: User
Tags terminates

1. Preface

Based on the source code of Linux0.11 operating system, this paper analyzes its process model.

Linux0.11:https://zhidao.baidu.com/share/20396e17045cc4ce24058aa43a81bf7b.html

2. Definition of the process

A program is an executable file, and a process is an executing instance of a program.

The difference between a process and a program:

Several processes can execute a program concurrently

A process can execute several programs sequentially

A process consists of executable instruction code, data, and stack areas. The Code and data sections in the process correspond to a code snippet, a data segment, in an execution file, respectively. Each process can only execute its own code and access its own data and stack area. The communication between the processes needs to be done through system calls.

2.1 Task Data Structures

The kernel program manages processes through the process tables, each of which occupies one item in the process table. In Linux, the process table entry is a task_struct task structure pointer.

The task data structure is defined in the header file include/linux/sched.h . The Process Control block or process descriptor PD(Processor descriptor ). ).

It holds all the information that is used to control and manage processes.

2.2 Process Identifiers

The kernel program identifies each process by using the process ID,PID.

struct task_struct {...    pid_t pid; ---------- Process ID    pid_t tgid; --------- thread ID ...}
3. Process running state 3.1 time-sharing technology

Using time-sharing technology, multiple programs can be run on the Linux operating system. The basic principle of time-sharing technology is to divide the CPU 's runtime into a time slice of a specified length, allowing each process to run within a single time slice.

When the time slice of the process runs out, the system switches to another program using the scheduler. So in fact, for a machine with a single CPU , only one program can run at a time.

But because the time slices for each process are short, it seems as if all the processes are running at the same moment.

3.2 Process Status

A process can be in a different set of States and become a process state within its lifetime. The status of the process is saved in the state field of the process task structure.

struct task_struct{/** *    long State;            /* */

Operational status (task_running)

When the process is being executed by the CPU, or is ready to be executed by the scheduler at any time, the process is said to be in a running state (running). The process can run in the kernel state or in the user state. When a system resource is already available, the process is awakened and ready to run, which is called the ready state. These states (the middle column in the diagram) are represented in the kernel in the same way, and are all in the task_running state.

Interruptible Sleep status (task_interruptible)

When the process is in an interruptible wait state, the system does not dispatch the execution. When the system generates an outage or frees a resource that the process is waiting for, or the process receives a signal, it can wake the process to a ready state (running state).

Non-disruptive Sleep state (task_uninterruptible)

is similar to the interruptible sleep state. However, a process in that State can only be converted to a ready-to-run state if it is explicitly awakened using the WAKE_UP () function.

Paused state (task_stopped)

When a process receives a signal sigstop,sigtstp,sigttin , or Sigttou , it enters a paused state. You can send a sigcont signal to it to make the process transition to a running state.

Zombie State (Task_zombie)

When a process has stopped running, but its parent process has not yet queried its state, the process is said to be in a zombie state.

When the time slice of the process runs out, the system switches to another program using the scheduler. If a process needs to wait for a resource on the system while the kernel is executing, the process calls sleep_on () or sleep_on_interruptible () to abandon the CPU and go to sleep , the scheduler executes other processes.

extern void sleep_on (struct task_struct * *p); // can be interrupted to wait for sleep. (KERNEL/SCHED.C, 167)
3.3 Process Initialization

For the Linux0.11 kernel, the system can have up to a maximum of one process at the same time, in addition to the first process is "manual" establishment, the rest is the process using the system call fork to create a new process, the process is created called Child process, which the creator is called the parent process.

After the boot program loads the kernel into memory in the boot/directory and puts the system into protected mode, it begins to execute the system initialization program INIT/MAIN.C. The program does some work to make the parts of the system operational.

The program then moves itself "manually" to process 0 and uses the fork () call to create process 1 for the first time.

The "Move to Task 0 execution" process is done by macro Move_to_user_mode () include/asm/system.h .

////Switch to user mode to run. //The function uses the iret instruction to switch from kernel mode to user mode (initial task 0). #defineMove_to_user_mode () \_asm {_asm mov eax,esp/*Save the stack pointer to ESP in the EAX register. */_asm Push 00000017h/*the stack segment selector (SS) is first put into the stack. */_asm push eax/*The saved stack pointer value (ESP) is then put into the stack. */_asm PUSHFD/*put the Flag register (eflags) contents into the stack. */_asm Push 0000000fh/*Enter the kernel snippet selector (CS) into the stack. */_asm push offset L1/*Enter the offset address (EIP) of the following designator L1 into the stack. */_asm iretd/*execution of the interrupt return instruction will jump to the following label 1. */_asm L1:mov eax,17h/*at this point, perform task 0,*/_ASM mov ds,ax/*initializes the segment register to point to the data segment of this local table. */_asm mov es,ax _asm mov fs,ax _asm mov gs,ax}

4. Process scheduling

The Linux process is preemptive . The preempted process is still in the task_running state, but is temporarily not run by the CPU. The preemption of a process occurs when the process is in the user state execution stage and cannot be preempted while the kernel is executing .

In order to enable the process to use the system resources effectively, and to make the process have a relatively fast response time, it is necessary to use a certain scheduling strategy for the switching schedule of the process. The scheduling strategy based on priority queueing is adopted in the Linux0.11 operating system.

The Schedule () function first scans the task array .

voidSchedule (void){    intI, next, C; structTask_struct **p;//pointer to the task structure pointer. /*detects alarm (process alarm timing value), wakes up any interruptible tasks that have been signaled*///starts the detection of alarm from the last task in the task array.      for(p = &LAST_TASK; p > &FIRST_TASK;--p)if(*p) {//if the alarm time of the task has expired (alarm<jiffies), place the SIGALRM signal in the signal bitmap and clear alarm. //Jiffies is the number of ticks (10ms/ticks) from the start of the system. defined on line 139th of Sched.h.             if((*p)->alarm && (*p)->alarm <jiffies) {                (*P)->signal |= (1<< (SIGALRM-1)); (*P)->alarm =0; }//If there are other signals in the signal bitmap other than the blocked signal, and the task is in a interruptible state, the task is in the ready state. //where ' ~ (_blockable & (*p)->blocked) ' is used to ignore blocked signals, but Sigkill and Sigstop cannot be blocked.             if((*p)->signal & ~ (_blockable & (*p)->blocked)) &&                    (*p)->state = =task_interruptible) (*p)->state = task_running;//to the Ready (executable) state.         }  /*This is the main part of the scheduler.*/     while(1) {C= -1; Next=0; I=Nr_tasks; P= &Task[nr_tasks];//This code also loops through the last task in the task array and skips the array slots that do not contain the task. Compare each ready//The counter of the status task (the descending tick count of the task run time) value, which is large and not running long, next//the task number to which to point.          while(--i) {if(!*--p)Continue; if((*p)->state = = task_running && (*p)->counter >c) C= (*p)->counter, next =i; }      //If you compare a result with a counter value greater than 0, exit the loop starting with line 124 and perform a task switch (141 rows).         if(c) Break; //Otherwise, update the counter value of each task according to the priority value of each task, and then return to the 125 row to re-compare. //The counter value is calculated as Counter = COUNTER/2 + priority. [Right counter=0??]         for(p = &LAST_TASK; p > &FIRST_TASK;--p)if(*p) (*p)->counter = ((*p)->counter >>1) + (*P)Priority ;        } switch_to (next); //Switch to task number for next task and run it. }

Select the process to run by comparing the value of the run-time count counter for each Ready state (task_running) task to determine which process runs the least time.

If all the time slices in the task_running state process are exhausted at this point, the system will recalculate the time slice value counter that each task needs to run, based on the priority valueof the process, for all (including the sleep) processes in the system.

The formula that is calculated is

The schedule () function then re-scans all the tasks in the task array that are in the task_running state, repeating the process until a process is selected.

Finally call switch_to () to perform the actual process switching operation. If no other processes are running at this time, process 0 is selected to run.

5. Terminating the process

When a process finishes running or terminates in half-way, the kernel needs to release the system resources that the process consumes.

When the user program calls the exit () system call, the kernel function do_exit ()is executed.

////Program Exit handler. Called in the interrupt handler of the system call. intDo_exit (LongCode//code is the error code. {  inti;//frees the Memory page (Free_page_tables () on the mm/memory.c,105 line) occupied by the current process code snippet and data segment. Free_page_tables (Get_base (current->ldt[1]), Get_limit (0x0f)); Free_page_tables (Get_base ( current->ldt[2]), Get_limit (0x17));//If the current process has child processes, the father of the child process is set to 1 (its parent process is changed to process 1). If the child process has//in the Zombie (ZOMBIE) state, the child process signaled SIGCHLD is sent to process 1.    for(i =0; i < nr_tasks; i++)    if(Task[i] && task[i]->father = = current->pid) {Task[i]->father =1; if(Task[i]->state = =Task_zombie)/*Assumption Task[1] is always init*/      (void) Send_sig (SIGCHLD, task[1],1); }//closes all files that are open by the current process.    for(i =0; i < Nr_open; i++)    if(current->Filp[i]) sys_close (i);//Synchronize the current process working directory pwd, the root directory, and the I node running the program, and empty each. Iput (current->pwd); Current->pwd =NULL; Iput ( current-root); Current->root =NULL; Iput ( current-executable); Current->executable =NULL;//If the current process is the lead (leader) process and has a controlled terminal, the terminal is freed.   if(Current->leader && Current->tty >=0) Tty_table[current-&GT;TTY].PGRP =0;//if the coprocessor was last used by the current process, the Last_task_used_math is empty.   if(Last_task_used_math = =Current ) Last_task_used_math=NULL;//If the current process is a leader process, all related processes are terminated.   if(current->leader) kill_session ();//sets the current process to a zombie state and set the exit code. Current->state =Task_zombie; Current->exit_code =Code;//notifies the parent process, which sends a signal to the parent process SIGCHLD--the child process stops or terminates. Tell_father (current->father);            Schedule (); //re-schedule the run of the process.   return(-1);/*just to suppress warnings*/}

If the process has child processes, let the init Process Act as its parent process for the child process.

The process state is then set to the zombie state Task_zombie. and sends a SIGCHLD signal to its original parent process to notify it that a child process has terminated.

When the process is terminated, its task data structure is still preserved . Because its parent process also needs to use the information in it.

During child process execution, the parent process usually waits for a child process to terminate by using the wait () or waitpid () function.

When the waiting child process is terminated and is in a zombie state, the parent process accumulates the time it takes to run the process into its own process, releasing the child process task data structure.

6. View of the operating system process model

As the founders of the Linux system said in a newsgroup submission, to understand the real operating mechanism of a software system, be sure to read its source code.

However, due to the current Linux kernel the entire source code size is very large (for example, 2.2.20 version has 2.68 million lines of code!!) Therefore, based on the source code of Linux0.11 operating system, this paper analyzes its process model.

Although the selected version is low, all aspects have a lot of room for improvement, but the kernel has been able to compile and run, which has included the essence of the Linux working principle, and the basic functions of the Linux kernel is relatively close, the source code is very short and capable, so there will be very high learning efficiency, can do more with less, Quick Start.

7. References

53150536

Http://ishare.iask.sina.com.cn/f/21489966.html

First assignment: Analysis of process model based on Linux0.11 operating system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.