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

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

66 19 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

Thông tin cơ bản

Định dạng
Số trang 66
Dung lượng 1,92 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th EditionChapter 5: Process Synchronization... Critical Section Problem Consider system of n processes {p 0 , p 1 ,

Trang 1

Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Chapter 5: Process

Synchronization

Trang 2

Chapter 5: Process Synchronization

Trang 3

5.3 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Trang 4

 Processes can execute concurrently

 May be interrupted at any time, partially completing execution

Trang 5

5.5 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Producer

while (true) {

/* produce an item in next produced */

while (counter == BUFFER_SIZE) ;

Trang 7

5.7 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

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

 Consider this execution interleaving with “count = 5” initially:

S0: producer execute register1 = counter {register1 = 5}

S1: producer execute register1 = register1 + 1 {register1 = 6} 

S2: consumer execute register2 = counter {register2 = 5} 

S3: consumer execute register2 = register2 – 1 {register2 = 4} 

S4: producer execute counter = register1 {counter = 6 }  S5: consumer execute counter = register2 {counter = 4}

Trang 8

Critical Section Problem

Consider system of n processes {p 0 , p 1 , … p n­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

Trang 9

5.9 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Critical Section

General structure of process P i  

Trang 10

Algorithm for Process Pi

do {

while (turn == j);

critical section turn = j;

remainder section } while (true);

Trang 11

5.11 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Solution to Critical-Section Problem

1.   Mutual Exclusion ­ If process P i 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

3.  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

Trang 12

Essentially free of race conditions in kernel mode

Trang 13

5.13 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

 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!

Trang 14

Algorithm for Process Pi

remainder section } while (true);

Trang 15

5.15 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Peterson’s Solution (Cont.)

Trang 17

5.17 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Solution to Critical-section Problem Using Locks

do {

acquire lock

critical section release lock

remainder section } while (TRUE);

Trang 19

5.19 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Solution using test_and_set()

 Shared Boolean variable lock, initialized to FALSE

 Solution:

do {

while (test_and_set(&lock)) ; /* do nothing */

Trang 20

compare_and_swap Instruction

Definition:

int compare _and_swap(int *value, int expected, int new_value) {

int temp = *value;

Trang 21

5.21 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Solution using compare_and_swap

 Shared integer  “lock”  initialized to 0; 

 Solution:

do {

while (compare_and_swap(&lock, 0, 1) != 0) ; /* do nothing */

Trang 22

Bounded-waiting Mutual Exclusion with test_and_set

do { waiting[i] = true;

else waiting[j] = false;

Trang 23

5.23 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

 Simplest is mutex lock

 Protect a critical section  by first acquire() a lock then 

Trang 24

acquire() and release()

Trang 25

5.25 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Semaphore

 Synchronization tool that provides more sophisticated ways (than Mutex locks)  for  process to synchronize their activities.

}

 Definition of  the signal() operation

signal(S) { S++;

}

Trang 26

wait(synch);

S 2 ;

Trang 27

5.27 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Trang 28

Semaphore Implementation with no Busy waiting

wakeup – remove one of processes in the waiting queue and place it in  the ready queue

typedef struct{

int value;

struct process *list;

} semaphore;

Trang 29

5.29 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Implementation with no Busy waiting (Cont.)

wait(semaphore *S) { S->value ;

if (S->value < 0) { add this process to S->list;

block();

} }

signal(semaphore *S) { S->value++;

if (S->value <= 0) { remove a process P from S->list;

wakeup(P);

} }

Trang 30

Deadlock and Starvation

Priority Inversion – Scheduling problem when lower­priority process 

Trang 31

5.31 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Classical Problems of Synchronization

Trang 32

Bounded-Buffer Problem

n buffers, each can hold one item

 Semaphore mutex initialized to the value 1

 Semaphore full initialized to the value 0

 Semaphore empty initialized to the value n

Trang 33

5.33 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Bounded Buffer Problem (Cont.)

Trang 34

Bounded Buffer Problem (Cont.)

Trang 35

5.35 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Semaphore rw_mutex initialized to 1

 Semaphore mutex initialized to 1

 Integer read_count initialized to 0

Trang 36

Readers-Writers Problem (Cont.)

Trang 37

5.37 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Readers-Writers Problem (Cont.)

signal(mutex);

} while (true);

       

Trang 38

Readers-Writers Problem Variations

Trang 39

5.39 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Trang 40

Dining-Philosophers Problem Algorithm

 The structure of Philosopher i:

do { wait (chopstick[i] );

Trang 41

5.41 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Dining-Philosophers Problem Algorithm (Cont.)

 Deadlock handling

  Allow at most 4 philosophers to be sitting simultaneously at  the table

  Allow a philosopher to pick up  the forks only if both are available (picking must be done in a critical 

section

  Use an asymmetric solution  ­­ an odd­numbered  philosopher picks  up first the left chopstick and then the right chopstick. Even­numbered  philosopher picks 

 up first the right chopstick and then the left chopstick. 

Trang 42

Problems with Semaphores

Trang 43

5.43 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

// shared variable declarations procedure P1 (…) { … }

procedure Pn (…) {……}

Initialization code (…) { … } }

}

Trang 44

Schematic view of a Monitor

Trang 45

5.45 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Condition Variables

condition x, y;

 Two operations are allowed on a condition variable:

x.wait() –  a process that invokes the operation is suspended until x.signal()

x.signal() – resumes one of processes (if any) that 

invoked x.wait()

 If no x.wait()  on the variable, then it has no effect on the variable

Trang 46

Monitor with Condition Variables

Trang 47

5.47 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Condition Variables Choices

 Both Q and P cannot execute in paralel. If Q is resumed, then P must wait

Trang 48

Monitor Solution to Dining Philosophers

monitor DiningPhilosophers {

enum { THINKING; HUNGRY, EATING) state [5] ; condition self [5];

void pickup (int i) { state[i] = HUNGRY;

Trang 49

5.49 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Solution to Dining Philosophers (Cont.)

void test (int i) {

if ((state[(i + 4) % 5] != EATING) &&

(state[i] == HUNGRY) &&

(state[(i + 1) % 5] != EATING) ) { state[i] = EATING ;

self[i].signal () ; }

}

initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING;

} }

Trang 50

Each philosopher i invokes the operations pickup() and 

Trang 51

5.51 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Monitor Implementation Using Semaphores

 Variables 

semaphore mutex; // (initially = 1) semaphore next; // (initially = 0) int next_count = 0;

Each procedure F  will be replaced by

wait(mutex);

… body of F;

if (next_count > 0) signal(next)

else signal(mutex);

 Mutual exclusion within a monitor is ensured

Trang 52

Monitor Implementation – Condition Variables

else signal(mutex);

wait(x_sem);

x_count ;

Trang 53

5.53 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Monitor Implementation (Cont.)

The operation x.signal can be implemented as:

if (x_count > 0) { next_count++;

signal(x_sem);

wait(next);

next_count ;

}

Trang 54

Resuming Processes within a Monitor

Trang 55

5.55 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

 Allocate a single resource among competing processes using 

priority numbers that specify the maximum time a process  plans to use the resource

Trang 56

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;

Trang 57

5.57 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Trang 59

5.59 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

 Dispatcher objects either signaled­state (object available) 

or non­signaled state (thread will block)

Trang 60

Linux Synchronization

 Linux:

 Prior to kernel Version 2.6, disables interrupts to implement short critical sections

Trang 61

5.61 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Trang 62

Alternative Approaches

 Transactional Memory

 OpenMP

 Functional Programming Languages

Trang 63

5.63 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Trang 64

}

The code contained within the #pragma omp critical directive 

is treated as a critical section and performed atomically

OpenMP

Trang 65

5.65 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th  Edition

Trang 66

End of Chapter 5

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

TỪ KHÓA LIÊN QUAN