Parallel output order changes between runs, because console access is random.
Single-Core
- während waiting for I/O ist CPU auf idle, was man vermeiden möchte
- CPU scheduler regelt das, verteilt CPU zwischen Prozessen (Prozesse umschalten)
- processes share CPU, but each process has own memory space
Every process needs context (PCB)
-
hardware context
-
memory context
-
os-level context
-
changing to another process requires a context switch, resulting in large overhead
- swap memory (green) is a lot slower
OS manages processes
- starts processes
- ends processes
- controls resource usage
- schedules cpu time
- allows inter-process communication
Context switching
- P1 is executing
- capture PCB(1)
- load PCB(2)
- P2 is executing
Prallelism and Concurrency
Threads
General
- independent sequences of execution on the same process
- Threads are not shielded from each other
- memory shared
- Context switching between threads is efficient
They
- share heap (memory space)
- have own execution stack
- have own instruction screen
Thread Attributes
Threads in Java
Option 1
Option 2
t1.start();
t2.start();- Threads can continue to run even if main() returns already
- threads need to be started
- always at least one thread (first one calls
main())
- the console is shared between all threads, internal mechanisms make sure only one thread read/writes at the same time. who gets access is random
Example
