Process Synchronization Chapter 5

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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?

  • 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?

  • 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?

<p>Protecting critical sections by ensuring mutual exclusion. (D)</p> Signup and view all the answers

Which of the following is NOT a necessary condition for a solution to the critical section problem?

<p>Fairness: All processes must have equal priority in accessing the critical section. (C)</p> Signup and view all the answers

What distinguishes counting semaphores from binary semaphores?

<p>Counting semaphores can have integer values beyond 0 and 1, while binary semaphores are limited to 0 and 1. (D)</p> Signup and view all the answers

In the context of process synchronization, what is a deadlock?

<p>A state where multiple processes are blocked indefinitely, waiting for each other. (A)</p> Signup and view all the answers

How does a preemptive kernel handle critical sections differently from a non-preemptive kernel?

<p>A preemptive kernel allows process preemption while running in kernel mode, requiring protection of critical sections. (A)</p> Signup and view all the answers

Which of the following scenarios can lead to starvation in process synchronization?

<p>A process with low priority repeatedly being denied access to a shared resource. (B)</p> Signup and view all the answers

Priority inversion presents a challenge in process scheduling. What is the problem?

<p>A high-priority process indefinitely waits for a low-priority process to release a lock. (C)</p> Signup and view all the answers

What does the turn variable indicate in Peterson's solution?

<p>Which process's turn it is to enter the critical section (B)</p> Signup and view all the answers

In the producer-consumer problem, what is the purpose of the 'counter' variable?

<p>To track the number of available buffers (B)</p> Signup and view all the answers

What is a 'spinlock'?

<p>A lock that causes a process to repeatedly check for availability. (B)</p> Signup and view all the answers

What is the purpose of the wait() operation on a semaphore?

<p>To decrement the semaphore value and possibly wait. (D)</p> Signup and view all the answers

What is the purpose of the entry section in a critical section problem solution?

<p>To request permission to enter the critical section (A)</p> Signup and view all the answers

Which of the following is the simplest lock?

<p>Mutex Lock (B)</p> Signup and view all the answers

What is the purpose of the signal() operation on a semaphore?

<p>To indicate that a process has finished a task (B)</p> Signup and view all the answers

What is mutual exclusion in the context of critical sections?

<p>Ensuring that no two processes are in their critical sections at the same time (B)</p> Signup and view all the answers

Which of the following is a solution to the critical section problem that does not involve busy waiting?

<p>Counting semaphores (A)</p> Signup and view all the answers

What is a race condition?

<p>A situation where multiple processes access and manipulate shared data concurrently. (C)</p> Signup and view all the answers

Flashcards

Cooperating Processes

Cooperater process may affect or be affected by other processes in execution in the system.

Peterson's Solution

Only synchronizes 2 processes competing for a shared resource, using busy waiting and shared variables like turn and flag.

Counter Variable

Integer variable to keep track of available buffers. Producer increments, consumer decrements.

Race Condition

Multiple processes access and manipulate the same data concurrently, outcome depends on the order of execution.

Signup and view all the flashcards

Mutex Locks

Protects critical section using a boolean variable. Provides acquire() to get a lock and release() to free it.

Signup and view all the flashcards

Semaphore

More sophisticated way to synchronize processes via integer variable and wait() & signal() operations.

Signup and view all the flashcards

Critical Section solution

If one process enters no other allowed. If a process exits notify others. If some processes want to enter it cannot be postponed indefinity.

Signup and view all the flashcards

Priority Inversion

Prevent priority inversion where low prioirty process holds a lock needed by higher priority process.

Signup and view all the flashcards

Deadlock

A concurrency problem where two or more processes are waiting indefinitely for an event that can only be caused by one of the waiting processes.

Signup and view all the flashcards

Starvation

A situation where a process is indefinitely blocked and never removed from a semaphore queue.

Signup and view all the flashcards

Turn variable

Indicates which process has priority to enter the critical section, helping to avoid indefinite postponement.

Signup and view all the flashcards

Flag array

Boolean array indicating if a process is ready to enter the critical section.

Signup and view all the flashcards

Critical Section

A region of code that needs to be protected so multiple threads do not access the same resource.

Signup and view all the flashcards

Wait() operation

A synchronization tool where a process waits for a condition to become true. Avoids busy waiting by blocking the process until a signal is received.

Signup and view all the flashcards

Signal() operation

A synchronization tool where a process signals that a condition is true. Wakes up a process from the wait queue.

Signup and view all the flashcards

Preemptive Kernel

Kernel allows preemption of processes, even when running in kernel mode. CPU can be taken at any time or it leaves CPU giving control before using all time

Signup and view all the flashcards

Non-preemptive Kernel

Kernel runs until it exits, blocks, or voluntarily yields CPU. CPU cannot be taken from process running. No race condition in kernel mode.

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 to turn.
  • 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 if flag[j] is false or turn is i. It can't be both.
  • Progress is satisfied due to flag[i] being false in the while(true) loop. Pi can execute any time.
  • Bounded waiting is satisfied because of flag[i] being false and the while(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() and signal().
  • Process with try to enter a critical section (CS)
  • If S>0 it will be decremented & enter CS; if S<=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 and P2 require S1 to happen before S2
  • Create a semaphore "synch" initialized to 0.
  • P1 will will enter
  • P2 will wait until P1 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.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser