














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















UNIVERSITY of SAN FRANCISCO department of computer science
CS 326 Operating Systems Greg Benson
We need to understand how an OS kernel implements processes and threads
We will look at the following issues:
How do the kernel and processes execute
How to implement processes and threads
Scheduling processes and threads
Synchronizing processes and threads
Time A B Process
At all times a CPU is executing some code:
It executes user code in processes/threads
It executes kernel code
Think about this:
The CPU PC (program counter, instruction pointers) is always pointing to an instruction
In order to switch from one process to another we must execute some kernel code
‣ How and when do we transfer control from user processes to the kernel? ‣ When a process makes a system call (e.g., write(...)) ‣ In response to a hardware interrupt ‣ Timer, keyboard, mouse, network, hard disk, etc. ‣ Hardware exceptions such as divide by zero and memory address fault ‣ Anytime control is transferred to the kernel a process switch can occur ‣ This is called a^ mode switch^ as we are switched from user mode to kernel mode ‣ Note: a process cannot directly switch to another process; control must be transferred to the kernel first
Time A B Process Kernel timer syscall network
How to pick the quantum length?
What is good and bad about a short quantum?
What is good and bad about a long quantum?
In practice, I/O bound processes do not use an entire quantum, they just run frequently
Although Linux has a default quantum of 100ms, the system can give lower or higher values depending on the nature of the process
A process can be in one of three states:
running - executing instructions
ready - waiting to execute
blocked - waiting for something Running Ready Blocked
Recall that a process consists of
Memory for code, data, and stack in user space
Kernel state and stack
The kernel represents each process as a C struct called the process control block (PCB)
State, stack pointer, register values
Process id, CPU time used, memory map
Open file descriptors
The kernel maintains lists of processes
The ready list consists of all ready, but not running processes
Blocked processes are put onto special lists for particular event, e.g. a timer list
A big part of kernel process management is maintaining the kernel process lists
intr_entry: save registers (push) setup kernel env push stack pointer call intr_handler intr_exit: restore registers (pop) iret
Not all mode switches cause a context switch
A mode switch allows the kernel to run:
Handle interrupts (e.g., incoming data)
Check to see if scheduling is required
A context switch (done in kernel):
Changes to new process/thread context
Processes
n Threads + resources (mem, files, etc.)
In modern OSes, n >= 1
Linux, Mac OS X, Solaris, Windows etc.
In Pintos, n = 1, a process is a thread (as in traditional UNIX)
However, we could change this if we wanted
If a computer has more than one processor or “core” in a multi-core system, the OS needs to schedule threads onto processors
Not too much different than single processor schedule, except
Need to deal with multiple threads in the kernel
Need synchronization (more later)
Need a more complex scheduling policy