Process model analysis based on Linux 0.12 for the first time job

Source: Internet
Author: User
Tags terminates

Job Content

Select an open source operating system, drill down into the source code to analyze its process model, including the following:

    • How the operating system organizes processes
    • How process state is transformed (gives process state transition diagram)
    • How the process is scheduled
    • Talk about your view of the operating system process model
1. How the operating system organizes processes 1.1 what is the process

A program is an executable file, and a process is an executing instance of a program. Using time-sharing technology on the Linux operating system, multiple processes can be run concurrently. Using time-sharing technology, multiple processes can be run concurrently 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 process using the scheduler.

(In the Linux kernel, processes are often referred to as tasks, and programs that run in user space are referred to as processes)

1.2 Task data Structures

The kernel program manages the process through the process table, and each process occupies one item in the process table, and in the Linux system, the process table entry is a TASK_STRUCT task structure pointer, defined in the header file include/linux/ Sched.h, which holds all the information that is used to control and manage processes, mainly includes state information, signal, process number, parent process number, elapsed time cumulative value, file used and local descriptor for the task, and task status segment information, which are currently running on the process.

structtask_struct {LongState//the running state of the task (-1 is not operational, 0 is operational (ready), >0 is stopped). LongCounter;//Task Run time count (decrement) (tick count), run time slice. LongPriority//Run priority number. When the task starts to run, the counter=priority, the larger the longer. Longsignal;//signal. is a bitmap, each bit represents a signal, and the signal value = bit offset value +1. structSigaction sigaction[ +] ;//a signal execution property structure that corresponds to the operation and flag information that the signal will perform. LongBlocked//process Signal Mask (corresponding to the signal bitmap). intExi T_code;//after the task stops executing the exit code, its parent process is taken. UnsignedLongStart._code;//The code snippet address. UnsignedLongEnd_code;//The length of the code (in bytes). UnsignedLongEnd_data;//code length + length of data (bytes). UnsignedLongBRK;//total length (in bytes). UnsignedLongStart_stack;//The stack segment address. Longpid//process identification Number (process number). LongFather//The parent process number. LongPGRP;//The process group number. LongSession//The session number. LongLeader;//conversation leader. Unsigned ShortUid//user identification number (user ID). Unsigned ShortEuid;//Valid User ID. Unsigned ShortSuid//the saved user ID. Unsigned ShortGid//Group identification number (group ID). Unsigned ShortEgid;//Valid group ID. Unsigned ShortSgid//the saved group ID. Longalarm;//Alarm Timer value (tick count). LongUtime;//User state elapsed time (tick count). LongStime;//System State Elapsed time (tick count). LongCutime;//The child process user state run time. LongCstime;//The system state run time of the child process. Longstart_t IME;//The process starts at run time. Unsigned ShortUsed_math;//flag: Whether the coprocessor is used. intTty//The process uses the child device number of the TTY terminal. -1 indicates no use. Unsigned ShortUmask//The file creation property screen bit. structM_inode * PWD;//Current working Directory I node structure pointer. structM _inode * Root;//root directory I node structure pointer. structM_inode * executable;//executes the file I node structure pointer. UnsignedLongClose_on_exec;//when executed, closes the file handle bitmap flag. structDesc_struct Ldt [3] ;//The Local Descriptor table. O-Space, 1-code snippet cs,2-data and stack segment Ds&ssostructTss_struct TSS;//the task State segment information structure of the process. }

2. How the process state transforms all States of the 2.1 process
    • Running state: When the process is being executed by the CPU, or is ready to be executed by the scheduler at any time. The process can run in the kernel state or in the user state
    • Interruptible sleep: When the process is in an interruptible sleep state, the process execution is not dispatched. When a system generates an outage or frees a resource that the process is waiting for, or a process receives a signal, it can wake the process to a ready state
    • Non-disruptive sleep state: In addition to not being awakened by receiving a signal, the interrupt is similar to an interruptible sleep state in which a process can be converted to a ready-to-run state only when it is explicitly awakened using the WAKE_UP () function
    • Paused state: When the process receives a signal SIGSTOP, SIGTSTP, Sigttin, Sigttou, it goes into a paused state, and the process accepts any signal during debugging to enter that state. The process can be converted to a running state by sending a sigcont signal to it

  


The process state, which is the process's lifetime, can be in a different set of States (the process state is saved in the Status field of the process task structure)

When a process runs out of time slices, the system uses the scheduler to force the switch to other processes to execute. If a process needs to wait for a resource on the system while the kernel is executing, the process will voluntarily abandon the CPU and let the scheduler go to other processes and the process goes to sleep.
The kernel does a process switch operation only when the process is moved from kernel run state to sleep. Processes running in the kernel state cannot be preempted by other processes, and one process cannot change the state of another process.

3. How the process is dispatched 3.1 scheduling process

The scheduler in the kernel is used to select the next process to run in the system. A scheduler can be thought of as a management code that allocates CPU uptime between all running processes. The Linux process is preemptive, and preemption occurs when the process is in the user execution state and cannot be preempted while the kernel is executing.

The task array is scanned by the Dispatch function schedule () function, which determines which process runs at least the current time by comparing the elapsed time of each ready state task to the value of the tick count counter. Which value is large indicates that the run time is not long, so select the process and use the Task switch macro function to switch to the process run

3.2 Process Switching

Whenever a new running process is selected, the schedule () function invokes the SWITCH_TO0 macro defined in the include/asm/system.h to perform the actual process switch operation. The macro replaces the current process state (context) of the CPU with the state of the new process. Before switching, switch to () first checks to see if the process being switched to is the current process, and if it does nothing, exit directly. Otherwise, the kernel global variable current is set as a pointer to a new task, and then a long jump to the task status segment of the new task is composed of the address of TSS, causing the CPU to perform a task switching operation.  The CPU then saves the state of all its registers to the TSS structure of the current process task data structure pointed to by the TSS segment selector in the current task register TR, and then restores the register information in the TSS structure to the CPU in the new task data structure pointed to by the new task State segment selector. The system is officially starting to run the new switching task.

3.3 Abort Process

When a process finishes running or terminates in half-way, the kernel needs to release the system resources that the process consumes. This includes files that are open when the process is running, memory requested, and so on.

When a user program calls the exit () system call, it executes the kernel function do exit (). This function first frees the memory pages occupied by the process code snippet and data segment, closes all files opened by the process, and synchronizes the current working directory, root directory, and the I node running the program using the process. If the process has child processes, leave the INIT process as the parent of all its child processes. If the process is a conversation process and has a control terminal, release the control terminal and send a hang signal sighup to all processes that belong to the session, which typically terminates all processes in that session. 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. Finally Do_ exit () calls the dispatch function to execute the other process. This shows that 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 the execution of a child process, the parent process usually waits for one of its child processes to terminate by using the wait () or waitpid () function. When a 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. Finally releases the memory page occupied by the aborted child process task data structure, and the null-pointer item occupied by the empty process in the task array.

4. Talk about your views

The first contact with the system outside the window system, the sense of information is very large, to learn a lot, a start without a clue, but catch the keyword ' Process ', and around the status of the process, you can find a step-by-step operation of the Linux operating system, because the system does not know much, there is no contact with other operating system source code, no contrast, I hope in the next study, will have more in-depth understanding of this course.

5. Finally

  Resources

  Linux Kernel Tour http://mp.sohu.com/profile?xpt=c29odW1wam00MXNoQHNvaHUuY29t&_f=index_pagemp_1  

Full anatomy of the Linux kernel-based on the 0.12 kernel (Zhao Jiong) pdf document Download: Http://vdisk.weibo.com/s/dn1igR_OGfuWY

Process model analysis based on Linux 0.12 for the first time job

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.