Threads Flashcards
(34 cards)
What is a thread/process represented by? And what is the structure of this representation?
a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system.
It is represented by a special data structure, called the thread control block (TCB). It contains relevant information about the thread:
- Thread characteristics: thread ID, program name
- State information: instruction counter, stack pointer, register contents
- Management data: Priority, rights, statistics
What are the data structures used to store and manage the TCBs?
Single scalars, static long array, variably long linked list, tree, inverted table, ..
How to efficiently manage multiple TCBs?
Form subsets of TCBs with regards to important attribute values (such as grouping threads of the same state together)
What is the difference between static and dynamic OSs handling threads?
In static OSs, all threads are known in advance and are statically defined. TCBs are declared as program variables and are used for a specific application and are all generated by a configuration program once.
In dynamic OSs, the threads are created and deleted by kernel operations (create_thread (id, initial values) which create a TCB and initialize the thread and delete_thread(id, final values) which return the final values and delete the TCB).
What is a thread’s address space?
A logical address space of a thread is the space of its valid addresses, which it can access. Relative addressing and address translation (using the MMU) allow to have an arbitrary number of logical address spaces that can be mapped to physical address space, leading to mutual protection of address spaces.
What is a Unix process?
It is an address space that contains at least one thread. So we use the term thread directly.
What is thread switching?
It means that the processor stops executing the current thread (its instruction sequence) and continues with the execution of another thread.
What is switching by jumping?
It is where we insert a jump instruction directly into the thread that jumps into another thread. This is however very inflexible and applicable only in very special cases.
Why is thread switching costly?
Because we have to memorize the continuation address in the interrupted thread, selection of the next thread. And the processor must hold on to essential parts of the interrupted thread description that must not get lost (during a register reload).
Memorizing continuation address
Store the address of the next instruction of the interrupted thread to be executed in the interrupted thread in a dedicated variable called ni (next instruction) of the TCB
What are the criteria for selecting the next thread?
Number of threads, order of arrival, priority of each thread (urgency).
Describe simple next thread selection with priority.
Threads are ordered with regards to their execution order or priority. Newly arriving threads are inserted into the priority sequence according to the chosen order. The threads can be organized in a priority queue with two dimensions where there are groups of equal priority.
What is a processor register in relation to threads?
Threads use arithmetic registers of the processor to store intermediate results.
What happens to the register when switching thread by jump?
Its content will be lost (overwritten) so switch by jump can only be used when the contents of the register will no longer needed and the new thread does not expect valid register contents.
What is a thread context?
It is the complete thread specific information that is stored in the processor registers (content of arithmetic registers, index registers, processor state, contents of address registers, segment tables, access control information, etc..)
What happens to the thread context when switching a thread?
It must be saved as part of the switching and restored when the thread is resumed. Constant data that is available in the TCB does not need to be saved.
What is thread switching by saving the thread context referred to? What are its characteristics and how to make it efficient?
It is context switch and it is the most time consuming part of the thread switch. To speed it up, processor can provide several sets of registers such that a thread switch does not require storing the thread context back to memory or provide a special instructions that allowing storing the complete thread context in one instruction.
Describe the thread switching sequence?
SWITCH -> save context of t_run -> select t_next -> save next instruction of t_run in t_run.ni -> jump to t_next.ni -> t_run = t_next -> load context of t_run
What is thread control in the context of the “Procedure” switch?
It means that each thread gets and gives up control within the switch procedure code at exactly the same point.
Describe the implementation of the “Procedure” switch and highlight what thread has control?
CT: current thread
NT: next thread
procedure switch(NT:thread)
control: CT
save context of CT
CT.sp = SP, SP = NT.sp (change stack pointer)
control: NT
load context of NT
return to NT (next instruction of NT)
What is automatic switching? And what does it require?
Realistically, it is not possible nor reasonable to explicitly insert switching points into the threads. Therefore, automatic thread switching is more desirable. To that end, we require a clock (timer), a hardware device that offers: specifying a deadline (timer set) and an interrupt on timeout. This allows programs to remain unchanged as the thread switch is triggered from outside the program and can happen at any arbitrary point in time.
What is conditioned switching?
There could arise a situation where a continuation of the thread processing is not possible (thread waiting for input data), instead of wasting time, processor can switch to another thread. This is called conditioned switching.
Describe thread switching due to end of time slice where there is no detailed clock interrupt handling and no other thread switching events.
03_threads slides: 53,60, 66, 75
What is a kernel stack?
The kernel stack is part of the kernel space. Hence, it is not directly accessible from a user process. Whenever a user process uses a syscall, the CPU mode switches to kernel mode. During the syscall, the kernel stack of the running process is used.