Notes/Operating Systems/Scheduling.md

124 lines
3.4 KiB
Markdown

---
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.