Click here to load reader

Operating Systems - · PDF file Operating Systems Synchronisation Stephan Sigg Distributed and Ubiquitous Systems Technische Universit at Braunschweig February 10, 2011 Stephan Sigg

  • View
    5

  • Download
    0

Embed Size (px)

Text of Operating Systems - · PDF file Operating Systems Synchronisation Stephan Sigg Distributed and...

  • Operating Systems Synchronisation

    Stephan Sigg

    Distributed and Ubiquitous Systems Technische Universität Braunschweig

    February 10, 2011

    Stephan Sigg Operating Systems 1/79

  • Overview and Structure

    Introduction to operating systems History Architectures

    Processes Processes, Threads, IPC, Scheduling Synchronisation Deadlocks

    Memory management Paging Segmentation

    Filesystems

    Security and Protection

    Distributed systems

    Cryptography

    Stephan Sigg Operating Systems 2/79

  • Outline Synchronisation

    1 Synchronisation The critical section problem Synchronisation hardware Semaphores Classic synchronisation problems Monitors Atomic transactions

    Stephan Sigg Operating Systems 3/79

  • Synchronisation Introduction

    Definition

    A cooperating process is one that can affect or be affected by other processes executing in the system

    Cooperating processes can either directly share a logical address space (code and data)

    Or share data through files or messages

    Concurrent access to shared data may result in data inconsistency

    Stephan Sigg Operating Systems 4/79

  • Synchronisation Introduction

    The producer-consumer-problem

    Example

    T0 : producer execute register1 = counter {register1 = 5} T1 : producer execute register1 = register1 + 1 {register1 = 6} T2 : consumer execute register2 = counter {register2 = 5} T3 : consumer execute register2 = register2 − 1 {register2 = 4} T4 : producer execute counter = register1 {counter = 6} T5 : consumer execute counter = register2 {counter = 4}

    Stephan Sigg Operating Systems 5/79

  • Synchronisation Introduction

    The producer-consumer-problem

    Definition

    A situation where processes access and manipulate the same data concurrently and the outcome of the execution depends on the order in which access takes place is called a race condition

    To guard against race condition we need to ensure that only one process at a time can be manipulating the shared data.

    Synchronisation among processes is required

    Stephan Sigg Operating Systems 6/79

  • Outline Synchronisation

    1 Synchronisation The critical section problem Synchronisation hardware Semaphores Classic synchronisation problems Monitors Atomic transactions

    Stephan Sigg Operating Systems 7/79

  • Synchronisation The critical section problem

    The critical section problem

    Consider a system of n processes {P0,P1, . . . ,Pn−1} Each process has a segment of code called its critical section

    In the critical section, processes change common variables, update a table, write a file, . . .

    Only one process is allowed to execute its critical section at a time

    The critical section problem is to design a protocol that enables this property

    Stephan Sigg Operating Systems 8/79

  • Synchronisation The critical section problem

    The critical section problem

    Structure of a typical process Pi :

    1: while true do 2: entry section 3: critical section 4: exit section 5: remainder section 6: end while

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    Stephan Sigg Operating Systems 9/79

  • Synchronisation The critical section problem

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

    Mutual exclusion

    Progress

    Bounded waiting

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    Stephan Sigg Operating Systems 10/79

  • Synchronisation The critical section problem

    Mutual exclusion

    If process Pi is executed in its critical section, no other processes are allowed to be executed in their critical sections

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    Stephan Sigg Operating Systems 11/79

  • Synchronisation The critical section problem

    Progress

    If no process is executing in its critical section

    Only those processes that are not executing in their remainder section participate in deciding wether to enter the critical section

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    Stephan Sigg Operating Systems 12/79

  • Synchronisation The critical section problem

    Bounded waiting

    It exists a bound on the number of times that other processes are allowed to enter their critical section after a request to enter the critical section of a specific process is answered.

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    remainder section

    critical section

    exit section

    entry section

    Stephan Sigg Operating Systems 13/79

  • Synchronisation The critical section problem

    Peterson’s solution

    A software solution

    Synchronisation of two processes Pi and Pj

    The processes share the two data items

    int turn; boolean flag[2];

    Process A

    Kernel

    Process B

    turn flag[2]

    Stephan Sigg Operating Systems 14/79

  • Synchronisation The critical section problem

    Peterson’s solution

    Structure of process Pi

    1: while (true) do 2: flag [i ] = true; 3: turn = j ; 4: while (flag [j ]&&turn == j) do 5: nothing 6: end while 7: critical section 8: flag [i ] = false; 9: remainder section

    10: end while

    Process A

    Kernel

    Process B

    turn flag[2]

    Stephan Sigg Operating Systems 15/79

  • Synchronisation The critical section problem

    Peterson’s solution 1 This implementation preserves

    1 Mutual exclusion 2 Progress requirement 3 Bounded-waiting requirement

    2 Software-based solutions are not guaranteed to work on modern computer architectures

    Modern CPUs reorder memory accesses to improve execution efficiency

    3 Any solution to solve the critical section problem requires a lock

    Process A

    Kernel

    Process B

    turn flag[2]

    Stephan Sigg Operating Systems 16/79

  • Outline Synchronisation

    1 Synchronisation The critical section problem Synchronisation hardware Semaphores Classic synchronisation problems Monitors Atomic transactions

    Stephan Sigg Operating Systems 17/79

  • Synchronisation Synchronisation hardware

    Synchronisation hardware

    Since software-based solutions are not guaranteed to work on modern computer architectures, locks are utilised

    Race conditions are prevented as critical sections are protected by locks

    Example: lock

    1: while true do 2: acquire lock 3: critical section 4: release lock 5: remainder section 6: end while

    Stephan Sigg Operating Systems 18/79

  • Synchronisation Synchronisation hardware

    Synchronisation hardware

    Many modern computer systems provide special hardware instructions

    to test and modify the content of a word to swap contents of two words as one uninterruptable unit

    We can use these instructions to solve the critical section problem

    Stephan Sigg Operating Systems 19/79

  • Synchronisation Synchronisation hardware

    TestAndSet()

    1: boolean TestAndSet(boolean target){ 2: boolean rv = target; 3: target = true; 4: return rv; 5: }

    Swap()

    1: void Swap(boolean a, boolean b){ 2: boolean temp = a; 3: a = b; 4: b = temp; 5: }

    Stephan Sigg Operating Systems 20/79

  • Synchronisation Synchronisation hardware

    Mutual exclusion with TestAndSet()

    1: while true do 2: while TestAndSet(&lock) do 3: ; // do nothing 4: end while 5: // critical section 6: lock = false; 7: // remainder section 8: end while

    Stephan Sigg Operating Systems 21/79

  • Synchronisation Synchronisation hardware

    Mutual exclusion with Swap()

    1: while true do 2: key = true; 3: while key do 4: Swap(&lock, &key); 5: end while 6: // critic

Search related