vault backup: 2025-04-21 22:49:53
This commit is contained in:
@ -190,8 +190,34 @@ A light-weight process.
|
||||
- **Kernel Threads**
|
||||
- Managed by OS
|
||||
|
||||
### Relationship modles
|
||||
*
|
||||
### Relationship models
|
||||
#### Many-to-one
|
||||
|
||||
* User-level threads to one kernel treads
|
||||
* Management done by thread library in user space
|
||||
* The entire process blocks whenever a thread makes a blocking sycalls
|
||||
* Only **one** thread can access the kernel at a time (you can't run multiple threads in parallel on multiprocessors)
|
||||
|
||||
#### One-to-one
|
||||
Each user thread is mapped to a kernel thread
|
||||
- Provides more concurrency
|
||||
Unfortunately:
|
||||
- Creating a user thread requires creating the corresponding kernel thread
|
||||
- Overhead of creating kernel threads retricts the number of threads
|
||||
|
||||
|
||||
#### Many-to-many
|
||||
Multiplexes many user threads to a $\leq$ number of kernel threads.
|
||||
|
||||
- Allows creation of however many threads the user wants
|
||||
- The kernel can schedule another thread for execution whenever a thread performs a blocking system call
|
||||
|
||||
|
||||
#### Fork-join
|
||||
Parent creates forks (children threads) and then waits for the children to terminate, joining with them, at which point it can retrieve and combine results.
|
||||
|
||||
This is also called **synchronous threading**. Parent **cannot** continue until the work has been completed.
|
||||
|
||||
|
||||
[^1]: A batch job is a scheduled task or a set of commands that are executed without manual intervention - **cron**
|
||||
|
||||
@ -199,3 +225,21 @@ A light-weight process.
|
||||
|
||||
[^3]: sequence of programmed instructions that can be managed independently by a scheduler within a computer program
|
||||
|
||||
|
||||
##### Parallelism
|
||||
|
||||

|
||||
|
||||
|
||||
## Thread pool
|
||||
Issue wih threads:
|
||||
- Overhead when creating
|
||||
- Exhausting system resources
|
||||
Solution: thread pools - creating a number of threads at startup and place them into a pool where they sit and wait for work.
|
||||
|
||||
|
||||
This optimizes everything because:
|
||||
Sharing threads:
|
||||
- If a thread is blocked (e.g., waiting for I/O), it doesn't remain idle; it can be reassigned to another task
|
||||
- Each thread has its own task queue
|
||||
- Whenever a thread finishes its tasks it looks through the other threads' queues and "steals" tasks.
|
||||
|
123
Operating Systems/Scheduling.md
Normal file
123
Operating Systems/Scheduling.md
Normal file
@ -0,0 +1,123 @@
|
||||
---
|
||||
type: theoretical
|
||||
backlinks:
|
||||
- "[[Overview#Multitasking/Timesharing]]"
|
||||
- "[[Processes and Threads#Timesharing In-depth]]"
|
||||
---
|
||||
Processes take turns to use the CPU.
|
||||
|
||||
## Long Term Scheduling
|
||||
Processes that are waiting are put in a *ready* queue.
|
||||
|
||||
## Short Term Scheduling
|
||||
Which process in the ready queue should be assigned the CPU next
|
||||
|
||||
|
||||
## Criteria
|
||||
We want to:
|
||||
- Maximize CPU utilization
|
||||
- Minimize Average Turnaround time
|
||||
- Maximize throughput
|
||||
- Minimize waiting and response time
|
||||
|
||||
|
||||
### CPU utilization
|
||||
Each process spends $p$ fraction of its time wating for I/O, having $n$ processes in memory. The probability that all processes are waiting for I/O is $p^n$
|
||||
Hence, CPU utilization is:
|
||||
|
||||
$$
|
||||
1- p^n
|
||||
$$
|
||||
|
||||
### Average Turnaround Time
|
||||
The time since the process enters the ready queue until it terminates.
|
||||
|
||||
Average is literally the mean of the aforementioned but for many processes.
|
||||
|
||||
### Throughput
|
||||
Number of processes executed completely in a unit of time.
|
||||
|
||||
## Non-Preemptive scheduling
|
||||
|
||||
If the process executes to complete, the algorithm is non-preemptive.
|
||||
|
||||
- Long average turnaround time
|
||||
- Whenever a long process starts, short ones have to wait
|
||||
### FCFS
|
||||
Read title
|
||||
|
||||
## SJF
|
||||
Shortest job first. Choose the next process to execute from the processes currently in the ready queue, based on their execution time
|
||||
|
||||
|
||||
> [!IMPORTANT]
|
||||
> Starvation happens when a process waits for a resource for a long time
|
||||
|
||||
|
||||
|
||||
This could happen with SJF.
|
||||
|
||||
|
||||
### Attacking starvation by introducing compound priority[^1]
|
||||
|
||||
So, SJF uses `1/Execution time` priority. We just add `0.1*waiting time` to it.
|
||||
|
||||
[^1]: I made that shit the fuck up just now
|
||||
|
||||
## Preemptive scheduling
|
||||
Scheduler stops a process and reclaims the CPU after assigning it
|
||||
|
||||
|
||||
### SRTF
|
||||
Shortest remaining time first (as opposed to shortest job first which just takes the initial times, this one is dynamic)
|
||||
|
||||
We keep track of when the processes start and how much time they've taken, calculating how much time they have left. We pick the minimum based on that.
|
||||
|
||||
### RR
|
||||
Round robing. Just give everyone a small time window for them to do their jobs.
|
||||
|
||||
We need to find a "time quantum"[^2] by balancing minimizing overhead of context switching and maximizing response time
|
||||
|
||||
**no priority**
|
||||
|
||||
|
||||
## Process categorization
|
||||
|
||||
| Category | Description | Example |
|
||||
| ----------- | ------------------------------------------------------------ | ----------------------- |
|
||||
| Batch | No interaction with users, input/output read/written to file | Cron job |
|
||||
| Interactive | Requires input from user. Needs short response times. | Chat |
|
||||
| Real-time | Expects response from user | Industrial applications |
|
||||
|
||||
## Multi-queue scheduling
|
||||
Multiple priority levels and uses RR for each.
|
||||
Choses processes from highest level and recurses downwards whenever a level is exhausted.
|
||||
|
||||
### + feedback
|
||||
Processes can be moved from one queue to another based on the type of operations executed (i.e. I/O is high priority).
|
||||
|
||||
|
||||
## Lottery
|
||||
Random number. Set boundaries. e.g.:
|
||||
|
||||
$$
|
||||
|
||||
\begin{align*}
|
||||
p_a \leftarrow 20\% \\
|
||||
p_b \leftarrow 50\% \\
|
||||
p_c \leftarrow 30\%
|
||||
\end{align*}
|
||||
$$
|
||||
|
||||
So:
|
||||
|
||||
$$
|
||||
rand() \rightarrow 0.4 \implies S(\{p_i\}) = p_b
|
||||
$$
|
||||
|
||||
|
||||
## Real-time scheduling
|
||||
Dividing the program into a number of short-lived processes.
|
||||
|
||||
|
||||
[^2]: Terrible fucking name, why why why, this should be called a window or some shit.
|
Reference in New Issue
Block a user