Synchronization 1 InterProcess Communication IPC IPC needed in

  • Slides: 27
Download presentation
Synchronization 1

Synchronization 1

Inter-Process Communication (IPC) IPC needed in multiprogramming – management of multiple processes within a

Inter-Process Communication (IPC) IPC needed in multiprogramming – management of multiple processes within a uni-processor system multiprocessing – management of multiple processes within a multi-processor system Pass information to each other via shared resource Mutual exclusion & Synchronization Proper sequencing 2

Spooling Example: Correct Process 1 int next_free; Process 2 int next_free; … 1 next_free

Spooling Example: Correct Process 1 int next_free; Process 2 int next_free; … 1 next_free = in; Shared memory 4 abc 2 Stores F 1 into next_free; 5 Prog. c 3 in=next_free+1 6 7 Prog. n F 1 F 2 out in 4 next_free = in 5 Stores F 2 into next_free; … 6 in=next_free+1 3

Spooling Example: Data Races Process 1 int next_free; 3 Stores F 1 into next_free;

Spooling Example: Data Races Process 1 int next_free; 3 Stores F 1 into next_free; 4 in=next_free+1 … 1 next_free = in; Shared memory 4 abc 5 Prog. c 6 7 Prog. n F 1 F 2 Process 2 int next_free; ou t 2 next_free = in /* value: 7 */ in 5 Stores F 2 into next_free; … 6 in=next_free+1 4

Critical Region (Critical Section) Process { while (true) { ENTER CRITICAL SECTION Access shared

Critical Region (Critical Section) Process { while (true) { ENTER CRITICAL SECTION Access shared variables; // Critical Section; LEAVE CRITICAL SECTION Do other work } } 5

Critical Region Requirements Mutual Exclusion Progress Bounded Wait No Speed and Number of CPU

Critical Region Requirements Mutual Exclusion Progress Bounded Wait No Speed and Number of CPU assumptions 6

Critical Region Requirements Mutual Exclusion: No other process must execute within the critical section

Critical Region Requirements Mutual Exclusion: No other process must execute within the critical section while a process is in it. Progress: If no process is waiting in its critical section and several processes are trying to get into their critical section, then entry to the critical section cannot be postponed indefinitely. 7

Critical Region Requirements Bounded Wait: A process requesting entry to a critical section should

Critical Region Requirements Bounded Wait: A process requesting entry to a critical section should only have to wait for a bounded number of other processes to enter and leave the critical section. Speed and Number of CPUs: No assumption may be made about speeds or number of CPUs. 8

visual learning aid: ‘bmp’ in alphabetical order Bounded Wait Mutual Exclusion Progress Oversimplifying Assumptions

visual learning aid: ‘bmp’ in alphabetical order Bounded Wait Mutual Exclusion Progress Oversimplifying Assumptions 9

Bounded Wait Mutual Exclusion Progress No cutting in! Oversimplifying Assumptions 10

Bounded Wait Mutual Exclusion Progress No cutting in! Oversimplifying Assumptions 10

Bounded Wait Mutual Exclusion Progress Are there door No cutting in! locks? Oversimplifying Assumptions

Bounded Wait Mutual Exclusion Progress Are there door No cutting in! locks? Oversimplifying Assumptions 11

Bounded Wait Mutual Exclusion Progress Are there door No cutting in! locks? We'll be

Bounded Wait Mutual Exclusion Progress Are there door No cutting in! locks? We'll be first if we sprint Oversimplifying Assumptions 12

Bounded Wait Progress Mutual Exclusion Are there door No cutting in! locks? Well, Did

Bounded Wait Progress Mutual Exclusion Are there door No cutting in! locks? Well, Did you see anybody go in? We'll be first if we sprint Oversimplifying Assumptions 13

Critical Regions (2) Mutual exclusion using critical regions 14

Critical Regions (2) Mutual exclusion using critical regions 14

Mutual Exclusion With Busy Waiting Possible Solutions Software-only candidate solutions (Two-Process Solutions) Hardware solutions

Mutual Exclusion With Busy Waiting Possible Solutions Software-only candidate solutions (Two-Process Solutions) Hardware solutions Lock Variables Turn-Based Mutual Exclusion Other Flag Mutual Exclusion Two Flag and Turn Mutual Exclusion Disabling Interrupts; Test-and-set; Swap (Exchange) Semaphores 15

Semaphore Concept Fundamental Principle: two or more processes want to cooperate by means of

Semaphore Concept Fundamental Principle: two or more processes want to cooperate by means of simple signals For signaling introduce Special Variable : Semaphore s Primitives: sem. Signal(s) – transmit signal via semaphore s sem. Wait(s) – receive signal via semaphore s Primitives sem. Signal and sem. Wait must be ATOMIC!!! Note: Different notation is used for sem. Signal and sem. Wait (P for sem. Wait, V for sem. Signal, ‘wait’ for sem. Wait, ‘signal’ for sem. Signal) 16

Definition of Semaphore Primitives (Counting Semaphore) struct semaphore{ int count; queue. Type queue; };

Definition of Semaphore Primitives (Counting Semaphore) struct semaphore{ int count; queue. Type queue; }; void sem. Signal(semaphore s) { s. count++; if (s. count ≤ 0) { remove a process P from s. queue; place process P on ready list; } void sem. Wait(semaphore s) { s. count--; if (s. count < 0) { place this process in s. queue; block this process; } } 17

Definition of Binary Semaphore Primitives struct binary_semaphore { enum {0, 1} value; queue. Type

Definition of Binary Semaphore Primitives struct binary_semaphore { enum {0, 1} value; queue. Type queue; }; void sem. Signal. B(binary_semaphore s) { void sem. Wait. B(binary_semaphore s) { if (s. value == 1) s. value = 0; else { place this process in s. queue; } block this process; } } if (s. queue is empty()) s. value = 1; else { remove a process P from s. queue; place process P on ready list; } 18

Mutual Exclusion Using Semaphores semaphore s = 1; Pi { while(1) { sem. Wait(s);

Mutual Exclusion Using Semaphores semaphore s = 1; Pi { while(1) { sem. Wait(s); /* Critical Section */ sem. Signal(s); /* remainder */ } } lock = 0; Pi { while(1) { while(Test_And_Set(lock)) { }; /* Critical Section */ lock =0; /* remainder */ } } 19

Value of Semaphore lock Queue 1 Process A Process B Critical Region Normal Execution

Value of Semaphore lock Queue 1 Process A Process B Critical Region Normal Execution Blocked on semaphore lock sem. Wait(lock) 0 sem. Wait(lock) B -1 sem. Signal(lock) 0 sem. Signal(lock) 1 20

Implementation of Semaphores in POSIX: SEM semaphore is variable of type sem_t Atomic Operations:

Implementation of Semaphores in POSIX: SEM semaphore is variable of type sem_t Atomic Operations: int sem_init(sem_t *sem, int pshared, unsigned value); int sem_destroy(sem_t *sem); int sem_post(sem_t *sem); Int sem_trywait(sem_t *sem); Int sem_wait(sem_t *sem); Use <semaphore. h> 21

Unnamed Semaphores Ch 14 pp 491 -501 #include <semaphore. h> Sem_t sem; You cannot

Unnamed Semaphores Ch 14 pp 491 -501 #include <semaphore. h> Sem_t sem; You cannot make a copy of a semaphore variable!!! #include <semaphore. h> int sem_init(sem_t *sem, int pshared, unsigned value); pshared == 0: only threads of process creating semaphore can use semaphore. 22

Sharing Semaphores Sharing semaphores between threads within a process is easy, use pshared==0 Forking

Sharing Semaphores Sharing semaphores between threads within a process is easy, use pshared==0 Forking a process creates copies of any semaphore it has… this does not share the semaphore Making pshared non zero allows any process that can access the semaphore to use it. This places the semaphore in global environment. 23

sem_init can fail!!! In unsuccessful, sem_init returns -1 and sets errno. Example: sem_t sem.

sem_init can fail!!! In unsuccessful, sem_init returns -1 and sets errno. Example: sem_t sem. A; if (sem_init(&sem. A, 0, 1) == -1) perror(“Failed to initialize semaphore sem. A”); errno EINVAL ENOSPC EPERM cause Value > sem_value_max Resources exhausted Insufficient privileges 24

Semaphore Operations #include <semaphore. h> int sem_destroy(sem_t *sem); • Destroying a semaphore that’s been

Semaphore Operations #include <semaphore. h> int sem_destroy(sem_t *sem); • Destroying a semaphore that’s been destroyed gives undefined result. • Destroying a semaphore on which a thread is blocked gives undefined results. 25

Semaphore Operations #include <semaphore. h> int sem_post(sem_t *sem); Unlocks the semaphore. If the semaphore

Semaphore Operations #include <semaphore. h> int sem_post(sem_t *sem); Unlocks the semaphore. If the semaphore value resulting from this operation is: positive, then no threads were blocked waiting for the semaphore. zero, then one of the threads blocked waiting for the semaphore will be allowed to return successfully from its call to sem_wait(). if unsuccessful, returns -1 and sets errno == EINVAL if semaphore doesnt exist 26

Semaphore Operations int sem_trywait(sem_t *sem); locks the semaphore if it is currently not locked

Semaphore Operations int sem_trywait(sem_t *sem); locks the semaphore if it is currently not locked (>0). doesn’t block returns -1 and errno==EAGAIN if semaphore zero can be interrupted by signal – errno == EINTR int sem_wait(sem_t *sem); locks the semaphore. If the semaphore value is currently zero, then the calling thread will block. Can be interrupted by signal – errno == EINTR 27