Concurrency (并发)
Quote
Concurrency is about dealing with lots of things at once, but parallelism is about doing lots of things at once.
- A way to run multiple Thread at the same time, instead of running one thread after another thread is done . Thus, improving the overall performance
- Can be achieved with multiple Core(Parallelism (并行性)) or Context Switch when there is only one core
Resource utilisation
Maximise the use of resources on hand and handle multiple requests or events.
Time-Sharing
- A Implementation of Concurrency (并发) and a specific implementation of Multi-tasking when resources are shared by multiple users at the same time, achieved with quick Context Switch of Process (进程). This allow multiple users to run jobs on the same computer at the same time
- All Time-sharing systems are Multi-programming systems
Info
Multics - Wikipedia was the first time-sharing OS which inspires the creation of Unix.
Parallelism (并行性)
- A subset of Concurrency (并发), Thread run on its own Core, so it is concurrency running on multiple resources
Corporative Scheduling
- Rather than Kernel decides when to preempt Process (进程). Process gives up CPU and let other Process to run
CPU Hogging
Process can hog to CPU forever, modern OS adapts Preemptive Scheduling instead.
Preemptive Scheduling
- Before jumping to program code, the Kernel sets the Timer Chip to trigger an Hardware interrupts (外中断) after some period of time
- The kernel switches to User Mode and jumps to the next Instruction of the program
- When the Timer Chips elapses, it triggers a Hardware interrupts. The CPU receives the interrupt.
- The hardware interrupt stores Program Counter, so it can resume later
- The hardware interrupt invokes corresponding Interrupt Handler
- Interrupt passes control to Process Scheduler selects a process to run by restoring the state of the CPU for that process from the process’s PCB
- Repeat step 1 to step 7
No CPU Hogging
Ensure no Process (进程) can hog the CPU forever.