《深入理解Linux内核》阅读笔记 --- Chapter 3 Processes

Process Switching

1、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.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.

2、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 pair that specifies the User Mode stack pointer address.

3、The 80x86 architecture includes a specific segment type called the Task State Segment(TSS), to store hardware contexts.When an 80x86 CPU switches from User Mode to Kernel Mode, it fetches the address of the Kernel Mode stack from the TSS.

4、Each process descriptor includes a field called thread of type thread_struct, in which the kernel saves the hardware context whenever the process is being switched out.This data structure includes fields for most of the CPU registers, except the general-purpose registers such as eax, ebx, etc, which are stored in the Kernel Mode stack.

Processes,Lightweight Processes, and Threads

Linux uses lightweight processes to offer better support for multithreaded applications.Basically, two lightweight processes may share some resources, like the address space, the open files, and so on.Whenever one of them modifies a shared resource, the other immediately sees the change.

A straightforward way to implement multithreaded applications is to associate a lightweight process with each thread.

In Linux a thread group is basically a set of lightweight processes that implement a multithreaded application and act as a whole with regards to some system calls such as getpid(), kill(), and _exit().

Unix programmers expect threads in the same group to have a common PID.To comply with this standard, Linux makes use of thread groups.The identifier shared by the threads is the PID of the thread group leader, that is, the PID of the first lightweight process in the group;it is stored in the tgid field of the process descriptors.The getpid() system call returns the value of tgid relative to the current process instead of the value of pid, so all the threads of a multithreaded application share the same identifier.Most processes belong to a thread group consisting of a single member; as thread group leaders, they have the tgid field equal to the pid field, thus the getpid() system call works as usual for this kind of process.

原文地址:https://www.cnblogs.com/YaoDD/p/6306389.html