Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread Definitions #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 61 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,75 @@ This repo was created to collect and share important Operating System concepts f

## Study Topics

#### [Unit 1: Threads](https://www.student.cs.uwaterloo.ca/~cs350/F19/notes/threads-1up.pdf)
### [Unit 1: Threads](https://www.student.cs.uwaterloo.ca/~cs350/F19/notes/threads-1up.pdf)

* Define Thread
#### What is a Thread?

* A thread is *a sequence of instructions.* They provide a way for programmers to express concurrency. All threads **share** access to the program's global variables and heap, but each thread's stack frames are **private**. A thread can:
* Create new threads
* Terminate itself
* Yield execution to another (unspecified) thread.
* 5 reasons why use threads?
* Resource Utilization
* Resource Utilization - blocked threads can give up CPU resources
* Parallelism
* Responsiveness
* Priority
* Modularization
* Responsiveness - a thread only for UI
* Priority - allocate CPU time differently
* Modularization - organization of tasks

**Recall**: Sequential programs follow the *Fetch/Execute Cycle*, where instruction \#`PC` is fetched, decoded, and executed, and then `PC` is incremented.

**Recall**: The stack starts at address `0xFFFF FFFF` and grows towards `Ox0`.

Conceptually, multiple threads should execute sequentially with their own private register contents and stack.

#### Implementing Concurrent Threads

* 3 ways to implement concurrent threads
* Hardware Support
* Timesharing
* Hardware Support - `P` processors, `C` cores, `M` multithreading per core means we can have `PCM` simultaneous threads running.
* Timesharing - multiple threads rapidly switching on the same hardware. Each thread gets a small amount of time to execute, and when that time expires, a context switch occurs.
* Hardware + Timesharing
* Define Timesharing
* Define Context Switch
* Define Interrupts
* Define Preemption
* 4 ways to cause context switching
* Thread_yield (voluntary)
* Thread_exit (voluntary)
* Thread blocks following Wchan_sleep() (volunatry)
* Thread preempted (involuntary)

**Context Switch**: the switch from one thread to another in timesharing. During a context switch:

1. decide which thread runs next (scheduling)
1. save current thread's registers
1. load register contents of next thread

Context switches are caused by:

* the running thread calling `thread_yield`
* the running thread calling `thread_exit`
* the running thread **blocks** by calling `wchan_sleep`
* the running thread is **preempted** (involuntary switch)

**Preemption** forces a running thread to stop running so another thread can have a chance. To "gain control" (run thread library code without calling a thread lib function), **interrupts** are used. If a thread exceeds the scheduling quantum (time limit), the thread is preempted.

**Interrupts** are an event that occurs during the execution of a program. When an interrupt occurs, the hardware transfers control to a fixed location in memory, to an *interrupt handler*. The interrupt handler:

1. Creates a trapframe to record thread context
1. Determines which device caused the interrupt and performs device-specific processing.
1. Restores the saved thread context from the trap frame and resumes execution of the thread.

In terms of stack frames, it looks like this (Thread 1 and 2, where 1 is running and 2 has **Thread Yield**, **Thread Switch**, and **Switchframe** on its stack):

1. An interrupt is created (not a stack frame)
1. Add **Trap Frame** - capture the current state of the thread
1. Add **Interrupt Handler Stack Frames**
1. Add **Thread Yield** - exceeded quantum, so yield
1. Add **Thread Switch** - (high-level context switch) choose a new thread and save caller-save registers
1. Add **Switchframe** - (low-level context switch) save callee-save registers

Then, switch to the other thread and pop off the **Switchframe**, **Thread Switch**, and **Thread Yield**, and resume execution.

![thread state flowchart](thread-states.png)

* 3 thread states
* Running (on CPU)
* Blocked (on wait channels)
* Ready (waiting to run on CPU)
* Know how to draw 2 thread stacks with context switches between them

#### [Unit 2: Syncronization](https://www.student.cs.uwaterloo.ca/~cs350/F19/notes/synchronization-1up.pdf)
### [Unit 2: Syncronization](https://www.student.cs.uwaterloo.ca/~cs350/F19/notes/synchronization-1up.pdf)

* Define Critical Sections
* Define Race Conditions
Expand All @@ -54,7 +94,7 @@ This repo was created to collect and share important Operating System concepts f
* __Resource Ordering__
* Volatile keyword and how it works

#### [Unit 3: Processes, Kernel, System Calls](https://www.student.cs.uwaterloo.ca/~cs350/F19/notes/processes-1up.pdf)
### [Unit 3: Processes, Kernel, System Calls](https://www.student.cs.uwaterloo.ca/~cs350/F19/notes/processes-1up.pdf)

* Define Process
* Define Kernel
Expand All @@ -75,7 +115,8 @@ This repo was created to collect and share important Operating System concepts f
* Distinguish between user (application) and kernel stack
* __Know how to draw processes and system calls with details regarding user and kernel stack__

#### [Unit 4: Virtual Memory](https://www.student.cs.uwaterloo.ca/~cs350/F19/notes/virtualmemory-1up.pdf)
### [Unit 4: Virtual Memory](https://www.student.cs.uwaterloo.ca/~cs350/F19/notes/virtualmemory-1up.pdf)

* Define physical memory
* Define virtual memory and why we need it
* Understand address translation and 3 ways to do it
Expand Down
Binary file added thread-states.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.