Process switch in linux

Understanding the Linux Kernel, 3rd Edition by

Get full access to Understanding the Linux Kernel, 3rd Edition and 60K+ other titles, with a free 10-day trial of O’Reilly.

There are also live events, courses curated by job role, and more.

Process Switch

To control the execution of processes, the kernel must be able to suspend the execution of the process running on the CPU and resume the execution of some other process previously suspended. This activity goes variously by the names process switch , task switch , or context switch . The next sections describe the elements of process switching in Linux.

Hardware Context

While each process can have its own address space, all processes have to share the CPU registers. So before resuming the execution of a process, the kernel must ensure that each such register is loaded with the value it had when the process was suspended.

The set of data that must be loaded into the registers before the process resumes its execution on the CPU is called the hardware context . The hardware context is a subset of the process execution context, which includes all information needed for the process execution. In Linux, a part of the hardware context of a process is stored in the process descriptor, while the remaining part is saved in the Kernel Mode stack.

In the description that follows, we will assume the prev local variable refers to the process descriptor of the process being switched out and next refers to the one being switched in to replace it. We can thus define a process switch as the activity consisting of saving the hardware context of prev and replacing it with the hardware context of next . Because process switches occur quite often, it is important to minimize the .

Читайте также:  Преимущества mac os перед linux

Get Understanding the Linux Kernel, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.

Источник

Process switch in linux

Hardware context
The set of data that must be loaded into the registers before the process resumes its execution on the CPU is called the hardware context. It is the s ubset of the process execution context, which includes all information needed for the process execution. Part of the hardware context is stored in the process descriptor while the remaining part is saved in the Kernel Mode stack.

Process Switch
Process switch is the activity of saving the hardware context of the process being switched out and replacing it with the hardware context of the process being switched in. Process switching occurs only in Kernel mode. The contents of all registers used by a process in user mode have already been saved on the kernel mode stack before performing process switching. This includes the contents of the SS and ESP pointer pair that specifies the user mode stack pointer address.

Performing process switch

Process switch consists of two steps

  1. Switching the Page Global Directory to install a new address space
  2. Switching the Kernel Mode stack and the hardware context. This provides all the information needed by the kernel to execute the new process, including the CPU registers

In the schedule() function, prev points to A’s descriptor and next points to B’s descriptor. As soon as the switch_to macro deactivates A, the execution flow of A freezes.

Читайте также:  Linux mint удалить разделы

Later, when the kernel wants to reactivate A, it must switch off another process, running at that time, C by executing another switch_to macro with prev pointing to C and next pointing to A.

When A resumes execution flow, it finds its old Kernel Mode stack, so the prev local variable points to A’s descriptor and next points to B’s descriptor.

The last parameter is an output parameter that specifies a memory location in which the macro writes the descriptor address of process C.

Before process switching, prev local variable allocated on the Kernel Mode stack of A is saved in the EAX register.

After process switching, when A has resumed its execution, the macro writes the content of EAX register in the memory location of A identified by the third output parameter last.

Since CPU register doesn’t change across the process switch, this memory location receives the address of C’s descriptor.

In the current implementation of schedule(), the last parameter identifies the Prev local variable of A, so prev is overwritten with the address of C.

Источник

Understanding the Linux Kernel, Second Edition by

Get full access to Understanding the Linux Kernel, Second Edition and 60K+ other titles, with a free 10-day trial of O’Reilly.

There are also live events, courses curated by job role, and more.

Process Switch

To control the execution of processes, the kernel must be able to suspend the execution of the process running on the CPU and resume the execution of some other process previously suspended. This activity goes variously by the names process switch , task switch , or context switch . The next sections describe the elements of process switching in Linux.

Читайте также:  Linux удалить все процессы пользователя

Hardware Context

While each process can have its own address space, all processes have to share the CPU registers. So before resuming the execution of a process, the kernel must ensure that each such register is loaded with the value it had when the process was suspended.

The set of data that must be loaded into the registers before the process resumes its execution on the CPU is called the hardware context . The hardware context is a subset of the process execution context, which includes all information needed for the process execution. In Linux, a part of the hardware context of a process is stored in the process descriptor, while the remaining part is saved in the Kernel Mode stack.

In the description that follows, we will assume the prev local variable refers to the process descriptor of the process being switched out and next refers to the one being switched in to replace it. We can thus define a process switch as the activity consisting of saving the hardware context of prev and replacing it with the hardware context of next . Since process switches occur quite often, it is important to minimize the time spent .

Get Understanding the Linux Kernel, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.

Источник

Оцените статью
Adblock
detector