Concurrency Flashcards
independent processes
can’t be affected by others and vice versa
cooperating processes
can be affected and can affect other processes
reasons for communication
- information sharing
- computational speedup (e.g. breaking down a big task to multiple small tasks)
- modularity (system is divided into modules that need to communicate)
- convenience (multitasking by the user, e.g. listening to music while writing a document)
Ctrl+Z
suspends a process, doesn’t terminate
signal types
Hardware-induced (e.g., SIGILL)
Software-induced (e.g., SIGQUIT or SIGPIPE)
actions
term
ign
core
stop
cont
catching signals
- Process registers signal handler
- OS delivers signal and allows process to run handler
- Current execution context needs to be saved/restored
kernel delivers the signal
→ Stops code currently executing (after saving contacts- states of registers- of the current program)
→ Saves context
→Executes signal handling code
→Restores original context
signal info pushed onto the stack
floating point state
ucontext: SP and IP
siginfo - info about the signal
sigreturn - SP; address to where to return after the signal handler finishes
upon sigreturn, the kernel
- can simply take all the state on top of stack
- and restore:RIP = …
RSP = …
RAX = …
RBX = …
etc. - vulnerability: not stored in the kernel so it can be modified by an user
reasons for synchronisation
- To account for dependencies
- To avoid processes getting in each other’s way
- Also applies to multithreaded execution
fork()
- creates a separate, duplicate process
- new PID
- continues after
exec()
- the program specified in the parameter will replace the entire process - including all threads
- same PID
- doesn’t continue after
race conditions
a situation in which several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place
spooler directory
a designated area where files are temporarily stored while they are being processed by a program or service, typically related to printing or job management
critical region
a code region with access to shared resources
requirements to avoid race conditions
- No two processes may be simultaneously in their critical regions
- No assumptions may be made about speeds or number of CPUs
- No process running outside its critical region may block others
- No process should have to wait forever to enter its critical region
a solution to the critical region problem must satisfy
- mutual exclusion: when one process is executing in its critical region, no other process is to be allowed to execute in its critical region
- progress: no process should have to wait forever to enter its critical region
- bounded waiting: there exists a bound on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before the request is granted
Peterson’s solution
when a process wants to enter its critical region it sets its flag to true and it will set the turn to the other process, i.e. it gives the turn to the other process
semaphore
- a nonnegative int variable shared between processes
- it can be accesses through two standard atomic operations: wait() and signal()
semaphore idea
- Idea: introduce a special semaphore integer type with 2 operations:
- down (wait):
- if semaphore ≤ 0 then block the calling process
- semaphore = semaphore - 1 otherwise
- up (signal):
- if there is a process blocking on semaphore, wake it up
- semaphore = semaphore + 1 otherwise
- OS guarantees all the operations are atomic by design
- Disable interrupts on single processors
- Spin locking on multiprocessors
- down (wait):
binary seamphores
mutex locks
only 0 or 1
spinlock semaphore
busy waiting wastes CPU cycles that some other process might be able to use - the process “spins” while waiting for the lock
Readers/Writers
- Idea: Build a queue of readers and writers
- Let several readers in at the same time
- Allow 1 writer when no readers are active
- How long may the writer have to wait?