Docsity

Process Synchronization - Lecture Slides | CSC 4103, Study notes of Operating Systems

Partial preview of the text

Download Process Synchronization - Lecture Slides | CSC 4103 and more Study notes Operating Systems in PDF only on Docsity!

1

CSC 4103 - Operating Systems Spring 2007

Tevfik Koşar

Louisiana State University February 6th, 2007

Lecture - VI

Process Synchronization

2

Roadmap

  • Process Synchronization
  • The Critical-Section Problem
  • Peterson’s Solution
  • Synchronization Hardware
  • Semaphores
  • Classic Problems of Synchronization
  • Monitors
  • Synchronization Examples
  • Atomic Transactions

3

Background

  • Concurrent access to shared data may result in data inconsistency
  • Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes
  • Consider consumer-producer problem:
    • Initially, count is set to 0
    • It is incremented by the producer after it produces a new buffer
    • and is decremented by the consumer after it consumes a buffer.

4

Producer

while (true) /* produce an item and put in nextProduced while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }

7

Critical Section

  • Critical section: segment of code in which the process may be changing shared data (eg. common variables)
  • No two processes should be executing in their critical sections at the same time
  • Critical section problem: design a protocol that the processes use to cooperate

8

Solution to Critical-Section Problem

A solution to the critical-section problem must satisfy the following requirements:

  1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections
  2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely

9

Solution to Critical-Section Problem

  1. Bounded Waiting - A bound must exist 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 that request is granted  Assume that each process executes at a nonzero speed  No assumption concerning relative speed of the N processes

10

Peterson’s Solution

  • Two process solution
  • Assume that the LOAD and STORE instructions are atomic; that is, cannot be interrupted.
  • The two processes share two variables:
    • int turn;
    • Boolean flag[2]
  • The variable turn indicates whose turn it is to enter the critical section.
  • The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready!

13

TestAndndSet Instruction

  • Definition:

boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv: }

14

Solution using TestAndSet

  • Shared boolean variable lock initialized to false.
  • Solution: do { while ( TestAndSet (&lock )) ; /* do nothing

// critical section

lock = FALSE;

// remainder section

} while ( TRUE);

15

Swap Instruction

  • Definition:

void Swap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp: }

16

Solution using Swap

  • Shared Boolean variable lock initialized to FALSE; Each process has a local Boolean variable key.
  • Solution: do { key = TRUE; while ( key == TRUE) Swap (&lock, &key ); // critical section lock = FALSE; // remainder section } while ( TRUE);

19

Semaphores as Synchronization Tool

  • Counting semaphore – integer value can range over an unrestricted domain
  • Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement - Also known as mutex locks
  • Provides mutual exclusion
    • Semaphore S; // initialized to 1
    • wait (S); Critical Section signal (S);

20

Deadlock and Starvation

  • Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes
  • Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); .. wait (Q); wait (S); .. .. signal (S); signal (Q); signal (Q); signal (S);
  • Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.

21

Classical Problems of Synchronization

  • Bounded-Buffer Problem
  • Readers and Writers Problem
  • Dining-Philosophers Problem

22

Bounded-Buffer Problem

  • N buffers, each can hold one item
  • Semaphore mutex for access to the buffer, initialized to 1
  • Semaphore full (number of full buffers) initialized to 0
  • Semaphore empty (number of empty buffers) initialized to N

25

Readers-Writers Problem

  • A data set is shared among a number of concurrent processes - Readers – only read the data set; they do not perform any updates - Writers – can both read and write.
  • Problem – allow multiple readers to read at the same time. Only one single writer can access the shared data at the same time.
  • Shared Data
    • Data set
    • Semaphore mutex initialized to 1. (for readcount)
    • Semaphore wrt initialized to 1. (for writers)
    • Integer readcount initialized to 0.

26

Readers-Writers Problem (Cont.)

  • The structure of a writer process

do { wait (wrt) ;

// writing is performed

signal (wrt) ; } while (true)

27

Readers-Writers Problem (Cont.)

  • The structure of a reader process do { wait (mutex) ; readcount ++ ; if (readercount == 1) wait (wrt) ; signal (mutex) // reading is performed wait (mutex) ; readcount - - ; if redacount == 0) signal (wrt) ; signal (mutex) ; } while (true)

28

Dining Philosophers Problem

  • Five philosophers spend their time eating and thinking.
  • They are sitting in front of a round table with spaghetti served. •There are five plates at the table and five forks set between the plates.
  • Eating the spaghetti requires the use of two forks which the philosophers pick up one at a time. •Semaphore chopstick [5] initialized to 1

31

Reading Assignment

  • Read chapter 6 from Silberschatz.

32

Acknowledgements

  • “Operating Systems Concepts” book and supplementary material by Silberschatz, Galvin and Gagne.