Process and process scheduling

Source: Internet
Author: User

1.1 Process structure

Each process has its own properties, represented by a TASK_STRUCT data structure, which contains the details of the process, including the process identifier (PID), the memory area occupied by the process, the associated file descriptor, security information, process environment, signal processing, resource scheduling, and the state of synchronization processing.

The array task contains pointers to all task_struct structures in the system. When the process is created, Linux allocates a TASK_STRUCT structure from system memory and joins it in the task array. After the operating system is initialized, the INIT process is established, which establishes a TASK_STRUCT data structure init_task. The structure of the currently running process is indicated by the current pointer.

Process switching consists of three levels:

1) The saving of user data includes the body segment, data segment (DATA,BSS), stack segment (stack), shared memory segment.

2) The protection of the Register data includes the PC, PSW (processor status word), SP (stack pointer), PCBP (Process control block pointer), FP (the first address of the local variable pointing to a function in the stack), P (pointing to the argument position of the calling function in the stack), ISP (interrupt stack pointer), and other general-purpose registers.

3) System-level protection includes proc, U ' Virtual memory space management table, interrupt processing stack.

(The proc file system is a special file system in Linux that provides the user with a readable window into the inner core work process, access to the kernel's internal data structures at run time, and a mechanism to change the kernel settings.) )

A sleep process with a status of Task_interruptble or task_uninterruptible gets the resources it needs to be woken up and enters the task_running state via Schdule (). The sleep process of state task_uninterruptible cannot be awakened by a signal or timer interrupt, only if the resource it is requesting is valid.

The process executes do_exit () and then enters the status Task_zombile, releasing the requested resource.

1.2 Creation of a process 1.2.1 object cache allocation

In the boot kernel function at system startup, there is a process-managed initialization function, where the Fork_init function initializes the number of threads and allocates the object cache of the process structure. Process structure objects allocate space from here when each process is created.

1.2.1 System call Sys_fork

When the system calls Sys_fork to create a process, it calls the implementation function do_fork directly. The Do_fork function copies data about the parent process, such as files, semaphores, memory, and so on. After the process initialization is complete, the parent process calls the wake_up_process () function to wake it up, the state changes to task_running, the queue to the ready, and the PID of the child process. (The child process that created the completion is hooked up to the ready queue)

1.3 Kernel Threads

A process can have multiple threads. If the process is running on an SMP machine, multiple CPUs execute each thread to achieve maximum parallelism. The context switch overhead for a thread is much smaller than the process, and the thread shares resources other than the CPU in the process.

Threads have kernel threads, lightweight processes, and user threads, three of which kernel threads are scheduled in the kernel and can use multiple processors concurrently. The user thread is implemented in user space, which reduces the context switching overhead, which processes multiple transactions in a process in parallel.

1) Kernel thread

Kernel threads are created and revoked by the kernel and are used to execute a specified function. Kernel threads share the kernel's body segment kernel global data, but each has its own kernel stack. It can be dispatched separately and can be run on a single processor by using the standard kernel synchronization mechanism. A kernel thread is actually a process that shares the address space with the parent process.

2) Lightweight process

A lightweight process is a user thread supported by the kernel. It provides multithreading control in a single process. These lightweight processes are scheduled separately, can run on multiple processors, and each lightweight process is bound to a kernel thread. Lightweight processes are not scheduled independently, and share the address space and other resources in the process, but each lightweight process should have its own program counters, register collections, core stacks, and user stacks.

3) User Thread

User threads are implemented by line libraries. They are created, freed, and managed without the involvement of the kernel. Line libraries provides a method for synchronizing and scheduling. The context of the user thread is saved and restored without kernel intervention. Each user thread can have its own user stack, a piece of memory that holds the user-level register context, and into the state information such as signal masking.

The kernel dispatches only the processes under the user thread, and these processes then dispatch their threads through the line libraries function.

1.4 Task Force Column

The Work queue interface is used to dispatch kernel tasks. Each work queue uses a dedicated thread, and all the work tasks from the running queue run in this thread , and the threads run in the context of the process. This thread can therefore be dispatched at the appropriate time to run the work task.

1.5 Process scheduling

In Linux, each CPU maintains a ready queue of its own runqueue structure.

1.5.1 Runqueue Structure

The RUNQUEUE structure is the main CPU running queue data structure. The run queue is used to manage processes by priority, and each run queue represents a list of processes that are prioritized. The difference from a work queue is that a task force uses a thread to run various work tasks in the work queue, and the kernel thread is essentially a process where the work queue is completely unrelated to the run queue.

1.5.2 Process Scheduling initialization

The Linux process has schedule function execution. It runs only in the kernel state, and any process that returns from the system call is transferred to schedule (). Most interrupt services are also transferred to schedule () after the interrupt response is complete.

1) Initialization of the scheduler

function Sched_init Initialization Scheduler

2) Establishment of Process scheduler related environment

The function Sched_fork establishes a scheduler-related environment for process p, which is a new process used by the current process with function fork ().

The Do_fork function is called in the process Creation fork system call, the Copy_process function is called in the Do_fork function, and the copy_process function is called to the Sched_fork function, which indicates that the scheduler-related environment is established when the process is created. The Shed_fork function assigns a time slice to the process p, marking the time stamp.

Schedule Analysis of 1.5.3 function

The scheduling of the process has a direct start-up scheduling and passive scheduling two ways, in different ways to schedule the execution of the steps are not the same:

1) direct Start scheduling

A direct-start schedule occurs when the current process needs to enter a blocked state because of a resource. The scheduler executes the following steps:

A put the current process into the appropriate waiting queue;

B Set the state of the current process to Task_interruptibel or task_uninterruptibel;

C Call Schedule () to prepare the new process to master the CPU

D Check if the resources required for the current process are available, and if so, remove the current process from the waiting queue.

2) Passive Dispatch

By need_resched the current process to implement the passive scheduling, each time a user state process is called, the value of this variable is checked to determine whether to call the function schedule () to implement the dispatch

function Schedule function is to select a suitable process to execute on the CPU, its basic process is divided into five operation steps:

1) Clean up the current running process

2) Select the next operational process

3) Set the operating environment for the new process

4) Execution Process context switch

5) Post-finishing

1.6 Linux Kernel preemption

Kernel preemption is to allow the scheduler to run as much as possible, thus reducing the time lag from one event to the scheduler being executed. When the current process has a "secure" preemption condition and there is a rescheduling request waiting to be processed, the kernel invokes the scheduler for process scheduling.

Kernel preemption requires that all variables and data structures in the kernel that may be shared by more than one process are protected by mutually exclusive mechanisms, or in critical sections. In the preemptive kernel, the task can be "safely" toggled if the kernel is not in an interrupt handler and is not in spinlock protected code.

The principle of preemptive kernel implementation is that when Spinlock is released, or when an interrupt is returned, a preemptive dispatch occurs if the need_resched of the current execution process is marked.

  

 

Process and process scheduling

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.