Podcast
Questions and Answers
Which of the following is NOT a characteristic of Peterson's Solution for synchronization?
Which of the following is NOT a characteristic of Peterson's Solution for synchronization?
- It assumes atomic load and store instructions.
- It uses busy waiting to check access to the critical section.
- It uses shared variables to indicate process turn and readiness.
- It is applicable to synchronize multiple processes. (correct)
In the context of the producer-consumer problem, how does a counter variable introduce potential inconsistency?
In the context of the producer-consumer problem, how does a counter variable introduce potential inconsistency?
- It ensures exclusive access to the buffer.
- It may lead to race conditions when incremented/decremented concurrently. (correct)
- It prevents simultaneous read and write operations.
- It eliminates the need for synchronization primitives.
What is a critical section?
What is a critical section?
- A memory region protected by the operating system.
- A section of code where multiple processes can execute concurrently.
- A segment of code where shared variables may be accessed and updated. (correct)
- A part of the program that handles input/output operations.
What is the primary purpose of a mutex lock?
What is the primary purpose of a mutex lock?
Which of the following is NOT a necessary condition for a solution to the critical section problem?
Which of the following is NOT a necessary condition for a solution to the critical section problem?
What distinguishes counting semaphores from binary semaphores?
What distinguishes counting semaphores from binary semaphores?
In the context of process synchronization, what is a deadlock?
In the context of process synchronization, what is a deadlock?
How does a preemptive kernel handle critical sections differently from a non-preemptive kernel?
How does a preemptive kernel handle critical sections differently from a non-preemptive kernel?
Which of the following scenarios can lead to starvation in process synchronization?
Which of the following scenarios can lead to starvation in process synchronization?
Priority inversion presents a challenge in process scheduling. What is the problem?
Priority inversion presents a challenge in process scheduling. What is the problem?
What does the turn
variable indicate in Peterson's solution?
What does the turn
variable indicate in Peterson's solution?
In the producer-consumer problem, what is the purpose of the 'counter' variable?
In the producer-consumer problem, what is the purpose of the 'counter' variable?
What is a 'spinlock'?
What is a 'spinlock'?
What is the purpose of the wait()
operation on a semaphore?
What is the purpose of the wait()
operation on a semaphore?
What is the purpose of the entry section in a critical section problem solution?
What is the purpose of the entry section in a critical section problem solution?
Which of the following is the simplest lock?
Which of the following is the simplest lock?
What is the purpose of the signal() operation on a semaphore?
What is the purpose of the signal() operation on a semaphore?
What is mutual exclusion in the context of critical sections?
What is mutual exclusion in the context of critical sections?
Which of the following is a solution to the critical section problem that does not involve busy waiting?
Which of the following is a solution to the critical section problem that does not involve busy waiting?
What is a race condition?
What is a race condition?
Flashcards
Cooperating Processes
Cooperating Processes
Cooperater process may affect or be affected by other processes in execution in the system.
Peterson's Solution
Peterson's Solution
Only synchronizes 2 processes competing for a shared resource, using busy waiting and shared variables like turn and flag.
Counter Variable
Counter Variable
Integer variable to keep track of available buffers. Producer increments, consumer decrements.
Race Condition
Race Condition
Signup and view all the flashcards
Mutex Locks
Mutex Locks
Signup and view all the flashcards
Semaphore
Semaphore
Signup and view all the flashcards
Critical Section solution
Critical Section solution
Signup and view all the flashcards
Priority Inversion
Priority Inversion
Signup and view all the flashcards
Deadlock
Deadlock
Signup and view all the flashcards
Starvation
Starvation
Signup and view all the flashcards
Turn variable
Turn variable
Signup and view all the flashcards
Flag array
Flag array
Signup and view all the flashcards
Critical Section
Critical Section
Signup and view all the flashcards
Wait() operation
Wait() operation
Signup and view all the flashcards
Signal() operation
Signal() operation
Signup and view all the flashcards
Preemptive Kernel
Preemptive Kernel
Signup and view all the flashcards
Non-preemptive Kernel
Non-preemptive Kernel
Signup and view all the flashcards
Study Notes
- CH 5 - Process Synchronization
Recap
- Cooperating processes can affect or be affected by other processes in execution.
- Processes can share data in two ways: sharing a logical address space (done by threads) or sharing data through messages/files which requires concurrent access.
- Running processes can be interrupted, leading to data inconsistency.
- Data consistency is needed to ensure cooperating processes run in order.
- To solve the producer-consumer problem when all buffers are full, an integer counter tracks the number of full buffers.
- The counter is initially set to 0, incremented by the producer, and decremented by the consumer.
Peterson's Solution
- Peterson's Solution synchronizes two processes competing for a shared resource.
- It solves the critical section problem without special hardware.
- Peterson's solution uses busy waiting (spinlock).
- It assumes atomic load and store instructions to avoid interruptions.
- It has two shared variables: 'turn' (indicates whose turn it is) and 'flag' (indicates if a process is ready).
- If
flag[i]
is true, process i is ready; otherwise, it's not.
Algorithm
- The
flag[i]
gives priority toturn
. - Process can only enter critical section one time.
Solution
- Solution ensures mutual exclusion
- The create an integer counter that will keep track of the number of full buffers
- The counter is set to zero initially
- The producer produces and item, counter is incremented by 1.
- Consumer consumes item, counter decremented by 1.
Mutual Exclusion, Progress, and Bounded Waiting
- Peterson's solution satisfies the critical section requirements.
- Mutual exclusion is satisfied because
Pi
can enter ifflag[j]
is false or turn isi
. It can't be both. - Progress is satisfied due to
flag[i]
being false in thewhile(true)
loop.Pi
can execute any time. - Bounded waiting is satisfied because of
flag[i]
being false and thewhile(true)
loop.
Components of the Producer-Consumer Problem
- Producer and consumer are two separate processes writing into and reading out of a bounded buffer.
- Bounded buffer, in pointer, out pointer, and a counter are required.
- The counter, if not handled correctly, can lead to a race condition.
Race Condition
- counter++ could be implemented as register1 = counter, register1 = register1 + 1, counter = register1
- counter-- could be implemented as register2 = counter, register2 = register2 - 1, counter = register2
- If there is interleaving between these blocks there might data inconsistancy.
- Race condition occurs when multiple processes access and manipulate the same data, and the outcome depends on the order of access.
Critical Section Problem
- Involves 'n' processes, each with a critical section segment that can change shared variables, update tables, or write files.
- If a process is in its critical section, no other process should be in theirs.
- The process asks for permission to enter the critical section in the entry section, executes in critical section, exits in exit section, and loops again.
- Rules of critical section problem include the different sections of the process (entry, critical, exit and remainder).
- Solution to critical section problems must satisfy mutual exclusion, progress and bounded waiting
Mutual Exclusion
- If a process is executing in its critical section, no other processes can be in their critical sections.
Progress
- If no process is in a critical section and some processes want to enter, the selection of which process enters next cannot be indefinitely postponed.
Bounded Waiting
- There is a bound on how many times other processes can enter their critical sections after a process has requested entry and before its request is granted.
Synchronization Hardware
- It needs hardware support for critical section code
- Solutions protect critical regions via locks.
- For example, uniprocessors could disable interrupts for the critical section to run without preemption.
- Disabling interupts are inefficient for multiprocessor and not scalable
- Modern machines use atomic hardware instructions.
Atomic Instructions
- Atomic instructions are non-interruptible sequences that either test and set a memory word or swap the contents of two memory words.
- CPU can be taken at any time during an execution.
Types of Locks
Mutex Locks
- Mutex locks provide mutual exclusion, protecting critical sections using a boolean variable.
- Processes acquire() a lock before the critical section and release() it afterward.
- Acquire & release calls are atomic & implemented via atomic hardware instructions
- This solution might require busy waiting (spinlock).
Semaphore
- Semaphores are a more sophisticated way to synchronize process actions.
- Most used solutions
- It has an integer variable
S
and two atomic operations:wait()
andsignal()
. - Process with try to enter a critical section (CS)
- If
S>0
it will be decremented & enter CS; ifS<=0
it will wait
Wait() Operation
- Definition of the
wait()
operation wait(S)
operation decrements the semaphore (S), and if S becomes negative, the process is blocked.- The process busy waits or must until S > 0
Signal() Operation
- Definition of signal operation
- The
signal(S)
operation increments (S) semaphore; which can allow other processes to continue.
Types of Semaphores
- Counting semaphore integer values range is unrestricted domain
- Binary semaphore integer balues range restrict between 0 & 1 (Mutex lock)
Synchronization example
- Suppose
P1
andP2
requireS1
to happen beforeS2
- Create a semaphore "synch" initialized to 0.
P1
will will enterP2
will wait untilP1
finish- Solve synchronization problems.
Assumptions
- Assume that processes execute at non-zero speed.
- No assumption to the relative speed of the processes.
Critical Section Handling
- Critical Section Handling in OS depends on whether it's preemptive or non-preemptive.
Preemptive Kernel
- Preemptive kernels allow preemption of processes when running in kernel mode i.e. CPU is taken at any time
- It relinquishes control before using all the available time.
Non-Preemptive Kernel
- Non-preemptive kernels run until they exit, block, or voluntarily yield the CPU.
- CPU cannot be taken from a process running in kernel mode until it completes with no race conditions in kernel mode.
Deadlock & Starvation
Deadlock
- Deadlock occurs when two or more processes are waiting for each other to release resources.
Example Deadlock
- Let S and Q be two semaphores initialized to 1.
- P0 is waiting(S)
- Then P0 tries to acquires Q but it cant be so it waits(S)
- P1 tries to acquire (Q) after acquiring P0.
- Deadlock happens because the operations will not execute and P0 and P1 are deadlocks.
Starvation
- Starvation refers to indefinite blocking, where a process may never be removed from a semaphore queue, possibly due to priorities or scheduling issues.
Priority Inversion
- Priority Inversion is a scheduling issue where a low-priority process holds a lock needed by a high-priority process.
Classical Problems
- Classical Problems if solved then synchronization scheme proposed works can be: Bound-Buffer Problem (Producer-Consumer Problem) Readers-writers problem Dining philosphers problem
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.