Programs, Processes, Processors, and OS Flashcards
Concurrent Programming
The three central concepts in concurrent programming
- Processor
- Hardware device that executes machine instructions
- Program
- Instruction sequence defining potential execution path Passive description of what you would like to happen
- Stored on disk/secondary memory
- Process
- Active system entity executing associate program(s) or; Program in execution on a processor
- Resides in primary memory, removed on reboot
(We’ll assume process, thread, and task are equivalent for now)
Processes and Programs
Why is a process not a program?
1) Program may be executed by multiple processes at the same time
– Wordpad opened twice as separate processes of the same program
– 1 program, multiple processes
2) Process might run one program, and then another
– gcc pre-processor is a different program (executable file) from the compilers syntax analyser and code generator
– The pre-processor, syntax and analyser, and code generator are executed one after the other by the same process
– 1 process, multiple programs
Processes
Processes abstract over…
1) Processors
– Can have multiple processes regardless number of processors
2) “Disturbed” sequential execution
– Interrupt processing: Handling external events (keypresses, clock ticks)
– Context switch: Execution jumps to another part of memory
Context Switching
Need to store context information for a switched (What is a thread’s Context?) -out thread so can later resume exactly where it left off
A thread’s context (minimally) comprises
– Program Counter (PC): Instruction in the program (or function) that the thread will execute next
– Stack Pointer (SP): Address at the top of the thread’s call stack. The call stack remembers the sequence of function calls the thread is making as it executes program (each thread needs own stack)
(Three-step context-switching sequence)
- De-schedule currently-running thread
–Save PC and SP CPU registers of current running thread
–Required so that thread resumes execution exactly where left off - Scheduler selects ‘best’ ready thread to run next
– Time-slicing, priority, starvation
– Hardware architecture, etc. - Resume newly-selected thread
– Restore register contents back to PC & SP registers
– Thread resumes where it last left off (PC is loaded last)
Concurrency vs. Parallelism
– Processes are always concurrent
– …but are not always parallel
- Parallel
- Multiple (n > 1) processes executing simultaneously. All executing at a given instant.
- Concurrent
- n > 1 processes are underway
simultaneously. < n may execute at given instant
- n > 1 processes are underway
Which processes finishes first?
We cannot know, the answer might change
“Safe” Concurrency
When do we not have to worry about concurrency?
1) No shared data or communication
2) Read only data (constant)
Risky Concurrency: Contention for Resources
When should we worry about concurrency?
- Threads use a shared resource without synchronization
- One or more threads modify the shared resource
Concurrency Context: Leveraging Hardware and Software
Leveraging Hardware and Software
Multi-core processors: Each core can run a thread concurrently with other cores
Leveraging Hardware and Software 2
Knowing concurrency is a requirement for efficient multi-thread and multi-core systems
Limit to Performance: Amdahl’s law
Program speedup by adding more processors is limited by the serial part of the program
e.g. If 95% of program (its runtime) can be parallelized, theoretical maximum speedup is x20
1/(1-p) where p is proportion of the program that can be parallelized.
Complexities of Concurrency
– Accidental complexity
– Low-level APIs
– Tedious, error-print, time consuming
– Non-portable
– Limited debugging tools
– Actual behaviour vs. debug environment
– Lack of tools to identify and rectify race conditions
Some System Calls for Unix Processes