1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Process synchronization (hệ điều HÀNH NÂNG CAO SLIDE)

66 17 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Chapter 5: Process Synchronization Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Chapter 5: Process Synchronization  Background  The Critical­Section Problem  Peterson’s Solution  Synchronization Hardware  Mutex Locks  Semaphores  Classic Problems of Synchronization  Monitors  Synchronization Examples   Alternative Approaches Operating System Concepts – 9th Edition 5.2 Silberschatz, Galvin and Gagne ©2013 Objectives  To present the concept of process synchronization  To introduce the critical­section problem, whose solutions  can be used to ensure the consistency of shared data  To present both software and hardware solutions of the  critical­section problem  To examine several classical process­synchronization  problems  To explore several tools that are used to solve process  synchronization problems Operating System Concepts – 9th Edition 5.3 Silberschatz, Galvin and Gagne ©2013 Background  Processes can execute concurrently  May be interrupted at any time, partially completing  execution  Concurrent access to shared data may result in data  inconsistency  Maintaining data consistency requires mechanisms to ensure  the orderly execution of cooperating processes  Illustration of the problem: Suppose that we wanted to provide a solution to the  consumer­producer problem that fills all the buffers. We can  do so by having an integer counter that keeps track of the  number of full buffers.  Initially, counter 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 Operating System Concepts – 9th Edition 5.4 Silberschatz, Galvin and Gagne ©2013 Producer while (true) { /* produce an item in next produced */ while (counter == BUFFER_SIZE) ; /* nothing */ buffer[in] = next_produced; in = (in + 1) % BUFFER_SIZE; counter++; } Operating System Concepts – 9th Edition 5.5 Silberschatz, Galvin and Gagne ©2013 Consumer while (true) { while (counter == 0) ; /* nothing */ next_consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter ; /* consume the item in next consumed */ } Operating System Concepts – 9th Edition 5.6 Silberschatz, Galvin and Gagne ©2013 Race Condition  counter++ could be implemented as register1 = counter register1 = register1 + counter = register1  counter could be implemented as register2 = counter register2 = register2 - counter = register2  Consider this execution interleaving with “count = 5” initially: S0: producer execute register1 = counter S1: producer execute register1 = register1 + S2: consumer execute register2 = counter S3: consumer execute register2 = register2 – S4: producer execute counter = register1 S5: consumer execute counter = register2 Operating System Concepts – 9th Edition 5.7 {register1 = 5} {register1 = 6}  {register2 = 5}  {register2 = 4}  {counter = 6 }  {counter = 4} Silberschatz, Galvin and Gagne ©2013 Critical Section Problem  Consider system of n processes {p0, p1, … pn­1}  Each process has critical section segment of code  Process may be changing common variables, updating  table, writing file, etc  When one process in critical section, no other may be in its  critical section  Critical section problem is to design protocol to solve this  Each process must ask permission to enter critical section in  entry section, may follow critical section with exit section,  then remainder section Operating System Concepts – 9th Edition 5.8 Silberschatz, Galvin and Gagne ©2013 Critical Section  General structure of process Pi   Operating System Concepts – 9th Edition 5.9 Silberschatz, Galvin and Gagne ©2013 Algorithm for Process Pi { while (turn == j); critical section turn = j; remainder section } while (true); Operating System Concepts – 9th Edition 5.10 Silberschatz, Galvin and Gagne ©2013 Monitor Implementation – Condition Variables  For each condition variable x, we  have: semaphore x_sem; // (initially int x_count = 0; = 0)  The operation x.wait can be implemented as: x_count++; if (next_count > 0) signal(next); else signal(mutex); wait(x_sem); x_count ; Operating System Concepts – 9th Edition 5.52 Silberschatz, Galvin and Gagne ©2013 Monitor Implementation (Cont.)  The operation x.signal can be implemented as: if (x_count > 0) { next_count++; signal(x_sem); wait(next); next_count ; } Operating System Concepts – 9th Edition 5.53 Silberschatz, Galvin and Gagne ©2013 Resuming Processes within a Monitor  If several processes queued on condition x, and x.signal()  executed, which should be resumed?  FCFS frequently not adequate   conditional­wait construct of the form x.wait(c)  Where c is priority number  Process with lowest number (highest priority) is  scheduled next Operating System Concepts – 9th Edition 5.54 Silberschatz, Galvin and Gagne ©2013 Single Resource allocation  Allocate a single resource among competing processes using  priority numbers that specify the maximum time a process   plans to use the resource R.acquire(t); access the resurce; R.release;  Where R is an instance of  type ResourceAllocator         Operating System Concepts – 9th Edition 5.55 Silberschatz, Galvin and Gagne ©2013 A Monitor to Allocate Single Resource monitor ResourceAllocator { boolean busy; condition x; void acquire(int time) { if (busy) x.wait(time); busy = TRUE; } void release() { busy = FALSE; x.signal(); } initialization code() { busy = FALSE; } } Operating System Concepts – 9th Edition 5.56 Silberschatz, Galvin and Gagne ©2013 Synchronization Examples  Solaris  Windows  Linux  Pthreads Operating System Concepts – 9th Edition 5.57 Silberschatz, Galvin and Gagne ©2013 Solaris Synchronization  Implements a variety of locks to support multitasking, multithreading  (including real­time threads), and multiprocessing  Uses adaptive mutexes for efficiency when protecting data from short  code segments  Starts as a standard semaphore spin­lock  If lock held, and by a thread running on another CPU, spins  If lock held by non­run­state thread, block and sleep waiting for signal of  lock being released  Uses condition variables   Uses readers­writers locks when longer sections of code need access  to data  Uses turnstiles to order the list of threads waiting to acquire either an  adaptive mutex or reader­writer lock  Turnstiles are per­lock­holding­thread, not per­object  Priority­inheritance per­turnstile gives the running thread the highest of  the priorities of the threads in its turnstile Operating System Concepts – 9th Edition 5.58 Silberschatz, Galvin and Gagne ©2013 Windows Synchronization  Uses interrupt masks to protect access to global resources on  uniprocessor systems  Uses spinlocks on multiprocessor systems  Spinlocking­thread will never be preempted  Also provides dispatcher objects user­land which may act  mutexes, semaphores, events, and timers  Events  An event acts much like a condition variable  Timers notify one or more thread when time expired  Dispatcher objects either signaled­state (object available)  or non­signaled state (thread will block) Operating System Concepts – 9th Edition 5.59 Silberschatz, Galvin and Gagne ©2013 Linux Synchronization  Linux:  Prior to kernel Version 2.6, disables interrupts to  implement short critical sections  Version 2.6 and later, fully preemptive  Linux provides:  Semaphores  atomic integers  spinlocks  reader­writer versions of both  On single­cpu system, spinlocks replaced by enabling and  disabling kernel preemption Operating System Concepts – 9th Edition 5.60 Silberschatz, Galvin and Gagne ©2013 Pthreads Synchronization  Pthreads API is OS­independent  It provides:  mutex locks  condition variable  Non­portable extensions include:  read­write locks  spinlocks Operating System Concepts – 9th Edition 5.61 Silberschatz, Galvin and Gagne ©2013 Alternative Approaches  Transactional Memory  OpenMP  Functional Programming Languages Operating System Concepts – 9th Edition 5.62 Silberschatz, Galvin and Gagne ©2013 Transactional Memory  A memory transaction is a sequence of read­write operations  to memory that are performed atomically void update() { /* read/write memory */ } Operating System Concepts – 9th Edition 5.63 Silberschatz, Galvin and Gagne ©2013 OpenMP  OpenMP is a set of compiler directives and API that support  parallel progamming void update(int value) { #pragma omp critical { count += value } } The code contained within the #pragma omp critical directive  is treated as a critical section and performed atomically Operating System Concepts – 9th Edition 5.64 Silberschatz, Galvin and Gagne ©2013 Functional Programming Languages  Functional programming languages offer a different paradigm  than procedural languages in that they do not maintain state.   Variables are treated as immutable and cannot change state  once they have been assigned a value  There is increasing interest in functional languages such as  Erlang and Scala for their approach in handling data races Operating System Concepts – 9th Edition 5.65 Silberschatz, Galvin and Gagne ©2013 End of Chapter Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 ...Chapter 5: Process Synchronization  Background  The Critical­Section Problem  Peterson’s Solution  Synchronization? ?Hardware  Mutex Locks  Semaphores  Classic Problems of? ?Synchronization. .. critical­section problem  To examine several classical? ?process? ?synchronization? ? problems  To explore several tools that are used to solve? ?process? ? synchronization? ?problems Operating System Concepts – 9th Edition... Consider system of n processes {p0, p1, … pn­1}  Each? ?process? ?has critical section segment of code  Process? ?may be changing common variables, updating  table, writing file, etc  When one? ?process? ?in critical section, no other may be in its 

Ngày đăng: 29/03/2021, 08:37

Xem thêm:

TỪ KHÓA LIÊN QUAN

Mục lục

    Algorithm for Process Pi

    Solution to Critical-Section Problem

    Critical-Section Handling in OS

    Solution to Critical-section Problem Using Locks

    test_and_set Instruction

    Solution using test_and_set()

    compare_and_swap Instruction

    Solution using compare_and_swap

    Bounded-waiting Mutual Exclusion with test_and_set

    Semaphore Implementation with no Busy waiting

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN