Thread, Address Space, Process and Dual Mode Operation

조수빈·2023년 7월 9일
0

OS

목록 보기
1/2

해당 노트는 UC Berkeley CS162 lecture 2를 듣고 작성한 강의 노트이다.

Thread

A thread is a single unique execution context that has a program counter, registers, execution flags, stack and memory state. Out of many threads, only one at a time gets executed on a processor when it is resident in the processor registers. Being resident means that the registers hold the root state of that thread including PC register pointing at the next instruction of the instructions in memory, intermediate values/pointers for ongoing computations and stack pointer holding the address of the top of stack. On the contrary, a thread is suspended, or, not executing when its state is not loaded into the processor registers. The last values from the execution are stored in memory during this period.

With a single processor, you can multiplex the hardware in time to achieve the illusion of multiple processors, running a part of each thread for a certain amount of time, alternating between threads. These threads are virtual cores. Switching from one thread to another is called context switching. During context switching, OS saves the current thread’s PC, SP, etc. information in its thread control block in memory and load that of the other thread.

While the illusion of multiple processors is achievable, the aforementioned way is not safe in that the threads share the same memory and thus can overwrite each other, even OS. A simple protection approach is base and bound where the start and end of the execution boundary of a thread are written in base register and bound register respectively. Whenever a program executes, hardware will check if the address program counter inputs is within that range. When a thread is loaded into memory, it is relocated according to the base register and the program counter will store addresses within the range. Instead, if you add an adder by which the program counter will store addresses as is and the adder will add the value of the base register when they are loaded to memory.

One significant problem this method has is when the currently running program needs more memory space, the current memory space’s contents have to be copied into somewhere bigger. After use, it can cause fragmentation errors. An alternative is translation where instructions operate on virtual addresses and the hardware translates them into physical addresses with a page table.

Address Space

An address space is a set of memory addresses accessible to program for read/write. It includes program counter, static data, stack, heap and so on.

Process

A process is an execution environment with restricted rights. In other words, it is a protected (by base and bound or translation mentioned in the thread section) address space with one or more threads. Application programs usually execute as a process. While it is easy for threads within the same process to communicate as they share memory, it is harder for processes to communicate with one another for protection. Bugs can only overwrite memory of process they are in and malicious process cannot read/write other process’ data.

Dual Mode Operation

Hardware provides at least two modes: kernel mode and user mode. Some operations are prohibited in user mode. Processes execute in user mode while kernel executes in kernel mode. A program can issue a system call to transition into kernel mode.

profile
신입 풀스택 웹개발자로 일하고 있습니다

0개의 댓글