Processes and Threads Flashcards
Explain the difference between a program and a process.
A program is a sequence of instructions that accomplish a given task.
A process is an instance of a running program.
Consider an operating system, which supports the four process states “ready”, “running”, “blocked”, and “zombie”. Depict the possible state transitions and label each edge with an event causing the transition.
ready - (dispatch) -> running
running - (interrupt) -> ready
running - (I/O wait) -> blocked
running - (terminate, exception) -> zombie
blocked - (I/O complete) -> ready
Explain the purpose of the PCB and the TCB. For each structure, give two pieces of
information that is stored in it.
Process Control Block: The PCB is the kernel data structure to represent a process. Two typical fields are: list of open files, structure to describe the
address space.
Thread Control Block: The TCB is used by the kernel to represent a thread. Two typical fields are: pointers to stacks, thread execution state.
What is a User-Level Thread (ULT)? Which thread models presented in the lecture employ ULTs?
A user-level thread is a thread that is entirely implemented (i.e., control structures, scheduling, dispatching, etc.) in user level. Depending on the thread model, the kernel is not aware of user-level threads.
ULTs are used in the many-to-one and the M-to-N thread models.
A web server uses a fixed pool of processes to handle connections from multiple clients in parallel. Give an advantage and a disadvantage of this scheme compared to using the same number of threads.
+Using multiple processes, each worker has her own address space. As a result, a segmentation fault in one worker does not affect the others. When using multiple threads in the same address space, a segmentation fault in one thread crashes the entire server. In case of a security vulnerability, a separate process for each connection also prevents leaking data from different clients.
-communication and coordination between worker processes is more complex than doing the same between threads in the same address space
Besides the preemptive MLFQ-based scheduler, the Linux kernel also contains a non preemptive FCFS scheduler. Threads scheduled by the FCFS scheduler always have priority over threads under the MLFQ scheduler. Processes with administrator privileges can choose which scheduler their threads use. Processes without administrator privileges always use the MLFQ scheduler.
For which type of application is the FCFS scheduler beneficial?
> The non-preemptive FCFS scheduler is suited for applications that execute mostly in short bursts and that must be handled with high urgency. These are
typically real-time applications.
> Short Bursts: If the application does not execute in short bursts, there is a danger of these applications starving the system.
> Urgency: Since applications under the FCFS scheduler are always preferred, the application should be time-critical.
Why are only applications with administrator privileges allowed to use the FCFS scheduler?
It is easy for applications to abuse the non-preemptive FCFS scheduler: Since FCFS processes are never preempted, an application can easily hog the CPU by putting a CPU-bound thread into the FCFS scheduler. Therefore, untrustworthy users
should not be allowed to use the FCFS scheduler without approval from an administrator.
Consider an operating system, which assigns a 16-bit process ID (PID) to each process. On process creation, the system determines a unique PID by atomically incrementing a global counter and using the new counter value as PID. Explain why this system would cause problems in practice.
With a PID of 16 bits, a maximum of 65536 processes can be created before the counter overflows. Once this happens, the system will start to reuse previously used PIDs, possibly even those of existing processes.
How could the PID allocation be adjusted?
The system must find the next unused PID instead of simply incrementing the counter, for example by incrementing the counter repeatedly until reaching a PID that is not currently assigned to a process.
Which one of the thread models presented in the lecture would you choose for an I/O-intensive application?
The one-to-one model, because I/O-intensive applications can be expected to block frequently. Since all threads are known to the kernel in the one-to-one model, it is possible to block only those threads actually performing I/O operations.
In the many-to-one model, all user-level threads would be blocked as soon as one of them performs I/O, because the I/O operation would block the underlying kernel-level thread.
Explain the term long-term scheduling.
Long-term scheduling is used to decide at what time processes are started in a system. In other words, the long-term scheduler decides which (and how many) processes are visible to the short-term scheduler.
Explain the difference between a scheduler and a dispatcher.
A dispatcher implements the mechanism to switch threads. It is given a thread to switch to, and then performs the tasks necessary to make that thread run on the CPU, such as saving the old thread’s context to memory and restoring the new thread’s context.
A scheduler implements the scheduling policy. It is thus responsible for selecting the next thread to run after the previous thread has exhausted its time slice or blocked for I/O. After making a selection, the scheduler typically calls into the dispatcher, passing an identifier for the selected thread.
Discuss the advantages and disadvantages of short over long time slices in preemptive round-robin scheduling (not virtual round-robin). Use the criteria presented in the lecture for assessing scheduling policies.
+improved interactivity
+shorter response time
+Improved fairness for I/O-bound jobs
-increased turnaround time
-increased scheduling and dispatching overhead
-decreased throughput
Explain the concept of priority inheritance.
Priority inheritance (or priority donation) can be useful if a high-priority task A depends on the completion of a low-priority task B, in which case A donates its priority to B so B completes faster. Otherwise, A effectively gets a lower priority than B, because A cannot run, until B has finished (i.e., the scheduler selected the low-priority task).
Name one data structure that is allocated in the user address space and one that is allocated in the kernel address space during the creation of a kernel-level thread.
User space: (User-)Stack
Kernel space: Thread control block (TCB)
Name three reasons for terminating a process.
> Voluntary termination: The process has finished the task it was started for.
> Fatal error: The process has encountered an error or the OS kills the process because of an exception (e.g., a segmentation fault).
> External termination: The process is killed by a signal from another process.
Explain the term preemption. Which feature must the hardware provide for preemption to be possible?
Preemption means that the OS can interrupt running threads, storing their state so their execution can be seamlessly resumed later.
For preemption to work, the hardware must provide a timer interrupt.
Do daemon processes in interactive systems typically have a high or low scheduling priority?
A low priority. Daemons are background processes that are not directly visible to the user. The user is less likely to notice slow-running daemons, which is
why resources should preferably be allocated to foreground processes that the user interacts with.
Can priority inversion occur in the many-to-one thread model?
Yes. Priority inversion is a scheduling problem and therefore unrelated to the type of thread used. If a high-priority thread A waits on a spinlock held by
low-priority thread B, A cannot continue if the scheduler constantly selects threads with a higher priority than B for execution. Whether that scheduler executes in user or kernel space makes no difference.
Which data about a zombie process must a POSIX-compatible operating system at least store until that process is terminated for good?
> Exit status: This is what is collected by waitpid()
> Process ID (PID): Since the parent process can wait for a specific child, the OS must know which exit status belongs to which process
> Parent process: Since processes can only wait for their own children, the operating system must store parent-child relations for zombies to allow for permission checking. This relationship can be stored either in the child (as a link to the parent process) or the parent (as a list of children)
Does it make sense to free all virtual memory in the user mode address space of a process when that process becomes a zombie?
Yes. A process can only become a zombie after it exits. This implies that no activity can take place in the address space of a zombie process, which is why the contents of the address space are not needed anymore.
Consider an OS using a round-robin scheduler with a fixed timeslice length. This OS attempts to give more CPU time to important threads by adding these threads to the scheduler’s queue multiple times. However, the system often crashes as soon as such a thread blocks. Which problem probably caused the crash? Suggest a solution for this problem that otherwise preserves the properties of the scheduler.
The problem with this scheme is that if one thread blocks for I/O, there will be more entries for this thread in the scheduler’s queue. The scheduler may thus select a thread to run that is not ready to run.
Possible solutions include:
* Whenever a thread blocks, scan the scheduler’s queue and remove all entries for this thread.
* Store a timeslice length in each thread’s TCB, and run each thread for its stored timeslice length once selected. This way, some threads can be granted more time without the need for multiple entries per thread in the scheduler’s queue.
* Have the scheduler check the thread’s state before actually scheduling it, and advance to the next queue entry if the current entry refers to a blocked thread.
Parallel execution can be implemented both with processes as well as threads. List and explain two advantages of threads compared to parallel processes.
+All threads of a process share the same address space which facilitates communication and coordination between the threads
+Switching between threads causes less overhead than switching between processes because the address space does not need to be switched.
Give one advantage and one disadvantage of user-level threads compared to kernel-level threads.
+User-level threads can be implemented on OS that don’t support kernel-level-threads.
+Switching between user-level threads causes less overhead than switching between kernel-level threads. As a consequence user-level-threads tend to be faster and more efficient.
-If one user-level thread invokes a blocking system call all other threads of the process are blocked as well.
-The process scheduler implemented as part of the kernel lacks knowledge about the number of threads per process which might lead to poor scheduling decisions.
Does each thread of a process have its own stack?
Yes, every thread needs its own stack to allow independent function calls.
Explain the “convoy effect” which can occur when a first come first serve (FCFS) scheduler is used.
If one long job arrives first, the other jobs have to wait for the first job to finish even if they are very short.
The convoy effect leads to a bad utilization of I/O devices and a poor average turnaround time.
Consider a scheduling policy which selects for execution the process with the shortest next CPU burst, in order to minimize average waiting time. Why does this policy, however, not necessarily result in the minimal average turnaround time?
If a long I/O intensive job with many short CPU bursts is executed, it is given priority over shorter jobs with longer CPU bursts. Due to their lower priority, those shorter jobs have to wait, which might increase the average turnaround time.
Explain the difference between kernel-level threads and kernel-mode threads?
Kernel-mode threads perform background tasks such as garbage collection, freeing page frames and swapping them out to disk, calculating checksums, mirroring disks in a software RAID, or distributing load to other processors. As the name suggests, kernel-mode threads are completely running in kernel-mode, meaning that privileged instructions can be executed without an additional system call.
Kernel level threads are threads running in user-mode and kernel-mode, which are managed and scheduled by the kernel – as opposed to user-level threads which are managed in user-mode.
Explain the term upcall?
An upcall is a call from a lower-level subsystem, such as a kernel to a higher-level subsystem, such as user code. Note: A system call is not an upcall but a downcall from user code to the kernel
Explain the importance of upcalls when using a M-to-N threading model?
In the many-to-many model there must be some way for the kernel to communicate with the user-level thread manager to maintain an appropriate number of kernellevel threads allocated to the process. This mechanism is called scheduler activations. The kernel notifies the user-level thread manager of important kernel events using upcalls from the kernel to the user-level thread manager. Examples of such events include a thread making a blocking system call and the kernel allocating a
new kernel-level thread to the process.