Notes/UNB/Year 4/Semester 1/CS3418/10-23-2023.md
2024-01-22 10:12:48 -04:00

37 lines
1.7 KiB
Markdown

Lecture Topic: Critical Section
A solution to the critical section problem has to
- Ensure mutual exclusion
- Progress: If 2 resources are needed to progress, the program needs to properly assign resources, and not perform no progress work, such as assigning threads only 1 resource to each thread? Pen and form problem from notes
- Bounded waiting
Discusses this algorithm
https://en.wikipedia.org/wiki/Peterson%27s_algorithm
It is mentioned that the problem with this algorithm is that the initial implementation is only applicable to 2 threads
It also has problems with modern implementations that implement instruction or memory access reordering
Memory models
- Strongly ordered: Changes are immediately visible to all other processors
- Weakly ordered: Changes in memory are not immediately visible to all other processors
A memory barrier is a way to force a propagation of changes values in memory to all other processors
# Mutex Locks:
Before a critical section, acquire a mutex lock, and then release it after
For this to be functional you need atomic operations on acquire and release
This also requires busy waiting, because it needs to poll for the lock. Thus it is called a spinlock
# Semaphore
It is accesses with the wait and signal functions, these atomic functions perform these things
- wait: It waits while the semaphore is less than or equal to 0, and after it decrements the semaphore.
- signal: Simply increases the semaphore by 1
A semaphore can be thought of as a mutex lock that allows execution to more than one thread at once. The initial semaphore value is the number of threads that are allowed concurrent execution.
Semaphores can be also used to solve synchronization problems, not just the critical section problem