0%

Process & Thread

img

After a programmer finishes writing a program, it is saved permanently on the hard drive. Just as a chef needs a cutting board and a countertop to prepare food, the CPU needs to load the program from the hard drive to memory to execute it.

What’s loaded into memory is a process, which is an executable instance of the program

When we run a program, what happens internally is that its executable file is loaded into memory. At this point, our program becomes a process.

The memory assigned to it has a special name: the address space of the process.

A process, however, is more than just its address space.

Process

When a process gains access to the CPU

Context Switching

但是切换进程并不像我们说的那么简单 并不是一个进程不用了 去排队 下一个进程用就可以 如果是这样的话 当切换进程后 后使用cpu的进程就会发现自己处于的cpu环境(各个寄存器的数据)是之前那个进程的

这样的话有两个大问题 : 1. 当前进程可以访问前一进程的敏感信息 也就是前一进程要是存储有密码 没有什么可以阻止当前进程读取前一进程的密码

So, the first concern here is security.

Now, let’s assume that all processes are honest and won’t use the information from a previous process.Does that solve all the problems?

  1. Even if the current process has no intention of using that information, it still needs to manipulate the registers to carry out its own tasks.

为了做到这一点it alters the CPU state of the previous process But 在将 CPU 分配给第二个进程之前,我们没有将该地址存储在任何地方,因此现在我们无法恢复第一个进程。

So, when the previous process regains CPU access later, the CPU state it had when it was interrupted would be lost.

当CPU必须分配给第二个进程的时候。操作系统并不是简单地覆盖地址寄存器来使CPU跳转到第二个进程的可执行代码,而是首先运行一个特殊的例程来捕获CPU的当前状态——就像拍摄快照一样。这样做的目的是复制内容

通过这样做,每次进程重新获得对 CPU 的控制时,它都会找到与中断时完全相同的寄存器。此过程解决了这两个问题。进程无法再访问前一个进程正在使用的信息,并且由于其自身的状态尚未被其他进程更改,因此它可以继续执行,并确信它正在使用正确的数据。

在switch前OS会看有无 即将要切换到的目标进程的PCB 如果没有说明是第一次执行这个进程 所以会把相关的寄存器设为0

这种捕获进程的 CPU 状态并恢复不同进程的状态以便其可以继续执行的操作称为上下文切换。

supplement

但是进程还有什么呢?一个进程可能还有一个打开文件的列表,以及分配给它的 IO 设备

Summary

image-20250210163005387

进程不只是单个实体,而是整个上下文,与其他进程隔离。

PCB

如果一个进程就是整个上下文,从寄存器等低级组件到文件列表等高级组件,我们如何将它们放入队列中?意思是,进程不像我们可以简单地用作数据结构中的元素的对象.

PCB:操作系统用来跟踪每个进程的特殊结构。

进程控制块不是进程本身,而是进程的表示。它充当启动或恢复流程所需的所有数据以及一些会计信息的存储库。这个表示就是实际放置在队列中的内容。

这是Linux里的PCB

多核CPU

在计算机体系结构中,pipeline(流水线)是指将指令处理分解为多个阶段的技术。

What’s in each process :Thread

一个程序计数器
It’s going to compute based on the program-flow
one of the program-flow or execution streams here is one thread
if there are multiple threads
You need multiple program counters

So why do we need a design thread

we look at it through a document editor Like a notepad

This notepad holds some text-data and some document which makes it a process

Imagine:now the user uses the keyboard to type a return at the end of the second line

There needs to be an interactive program that knows about keyboard press events and then the process that manages the layout recalculates the text and renders it

and every once in a while the writing-program needs to save the document to the hard disk

Three programs need to execute concurrently, but they must access and modify the same document. Since processes have their own independent memory spaces, they must operate within the same process to access shared resources. Therefore, three lighter-weight constructs—threads—are necessary for this scenario.

Threads allow these programs to run in parallel while sharing the same memory space for document access and modification.


Threads are the smallest unit of parallel execution.

However, if there is only a single-core CPU, it can execute only one thread at a time. In this case, threads must take turns to run, effectively sharing the CPU. This sharing is managed through time slicing, where each thread is allocated a specific time slice to execute.

This mechanism allows multiple threads to make progress by switching between them rapidly, creating the illusion of concurrent execution even on a single-core system.