Chapter 5 Concurrency Mutual Exclusion and Synchronization C

  • Slides: 44
Download presentation
Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

C Threading

C Threading

C Threads State Change in C 3 n n n The setjmp/longjmp set of

C Threads State Change in C 3 n n n The setjmp/longjmp set of macros implemented in the C provide the perfect platform to perform complex flow-control. The setjmp function saves the state of a program in a jmpbuf array. The state of a program are the register values, sp (stack pointer), fp (frame pointer), and pc (program counter). Executing a setjmp always returns 0 after saving the stack environment. Executing a longjmp jumps to a setjmp which then returns the value of the argument of the longjmp (0 is never returned). A call to longjmp restores the saved environment and returns control to the point just after the corresponding setjmp call. BYU CS 345 Mutual Exclusion

setjmp/longjmp setjmp / longjmp 4 n n #include <setjmp. h> jmp_buf n n n

setjmp/longjmp setjmp / longjmp 4 n n #include <setjmp. h> jmp_buf n n n setjmp(jmp_buf env); n n n An array capable of storing minimal CPU context (ie, stack pointer (sp), frame pointer (fp), and program counter (pc)). Contents are intentionally meaningless. saves the register contents, (ie, program state, sp, fp, pc) in a jmp_buf array (env) so that the function longjmp()can restore them later. returns 0 value. longjmp(jmp_buf env, int val); n n resets the registers to the values saved in env. longjmp() returns as if you have just called the setjmp() call that saved env with non-zero value. BYU CS 345 Mutual Exclusion

setjmp/longjmp 5 Multi-threading in C Start Here Sa. R v. Reet t. Cuur ornn

setjmp/longjmp 5 Multi-threading in C Start Here Sa. R v. Reet t. Cuur ornn t 0 e 0 Re xt sto re Re Co Rs nte Re ettour r xt st en. C 3 Re or o n t Re uren C tex o tu 3 nt t rn ex t 3 xt te Con tore 2 Res urn Ret ext BYU CS 345 C 0 ont etauvren RS // new threads jmp_buf k_context; t 0 ext n o r u C for (tid = 0; tid < 4; tid++) Sa. Rvet Switchint to tid; thread stack { struct t if (setjmp(k_context) == 0) {. . . ex nt o C { void* stack. End; 1 e r n o r t t temp = (int*)tcb[tid]. stack. End; jmp_buf context; u 0 ex s et et. C uron t Re R v R a S SET_STACK(temp); } tcb[4]; if (setjmp(tcb[tid]. context) == 0) { longjmp(k_context, 1); void my. Thread() // my thread } { my. Thread(); while (1) } { } if(!setjmp(tcb[tid]. context)) Enter Scheduler SWAP; longjmp(k_context, 2); while (1) // schedule threads { // body of thread tid = scheduler(); if (setjmp(k_context) == 0) } { } longjmp(tcb[tid]. context, 3); } Repeat 4 times } Mutual Exclusion

setjmp/longjmp 6 Multi-tasking in C SWAP; BYU CS 345 Mutual Exclusion

setjmp/longjmp 6 Multi-tasking in C SWAP; BYU CS 345 Mutual Exclusion

setjmp/longjmp 7 Multi-tasking in C // new cur. Task if ((code = setjmp(k_context)) ==

setjmp/longjmp 7 Multi-tasking in C // new cur. Task if ((code = setjmp(k_context)) == 0) { // change stack pointer to temp = (int*)tcb[cur. Task]. stack. End; SET_STACK(temp) // begin execution of new task result = (*tcb[cur. Task]. task) (tcb[cur. Task]. argc, tcb[cur. Task]. argv); longjmp(k_context), 1); } // scheduler while (1) { if (code == 1) return; cur. Task = scheduler(); // save kernel state in k_context if ((code = setjmp(k_context)) == 0) { // restore user context / resume task longjmp(tcb[cur. Task]. context, 3) } } BYU CS 345 int new. Task(int argc, char* argv) { while (1) { // save user context in tcb if (!setjmp(tcb[cur. Task]. context) { // restore kernel context longjmp(k_context, 2); } //. . . { return 0; } Mutual Exclusion

8 CS 345 Stalling’s Chapter # Project 1: Computer System Overview 2: Operating System

8 CS 345 Stalling’s Chapter # Project 1: Computer System Overview 2: Operating System Overview 4 P 1: Shell 3: Process Description and Control 4: Threads 4 P 2: Tasking 5: Concurrency: ME and Synchronization 6: Concurrency: Deadlock and Starvation 6 P 3: Jurassic Park 7: Memory Management 8: Virtual memory 6 P 4: Virtual Memory 9: Uniprocessor Scheduling 10: Multiprocessor and Real-Time Scheduling 6 P 5: Scheduling 11: I/O Management and Disk Scheduling 12: File Management 8 P 6: FAT Student Presentations 6 BYU CS 345 Mutual Exclusion

Chapter 5 Learning Objectives 9 n n n Discuss basic concepts related to concurrency,

Chapter 5 Learning Objectives 9 n n n Discuss basic concepts related to concurrency, such as race conditions, OS concerns, and mutual exclusion requirements. Understand hardware approaches to supporting mutual exclusion. Define and explain semaphores. Define and explain monitors. Explain n n Producer/Consumer Bounded buffer Readers/writers problem Classical synchronization problems BYU CS 345 Mutual Exclusion

Mutual Exclusion Review… 10 n n The OS must keep track of active processes.

Mutual Exclusion Review… 10 n n The OS must keep track of active processes. The OS must allocate and deallocate resources. n n n Processor time Memory Files I/O devices The OS must protect the data and physical resources. The results of a process must be independent of the speed of execution relative to the speed of other concurrent processes. BYU CS 345 Mutual Exclusion

Cooperation 11 n n A cooperating process is one that can affect or be

Cooperation 11 n n A cooperating process is one that can affect or be affected by other processes executing in the system. A race condition occurs when the outcome depends on the particular order of execution, most often associated with shared resources. n n Share logical space (threads) Files or messages Concurrent or parallel execution Mutual exclusion prevents race conditions by synchronizing resource access. BYU CS 345 Mutual Exclusion

Resource Allocation 12 n Shared resources are n n n Produced Consumed Resource sharing

Resource Allocation 12 n Shared resources are n n n Produced Consumed Resource sharing presents problems of n Mutual Exclusion n Deadlock n n n Critical resource – a single nonsharable resource. Critical section – portion of the program that accesses a critical resource. Each process owns a resource that the other is waiting for. Two processes are waiting for communication from the other. Starvation n BYU CS 345 A process is denied access to a resource, even though there is no deadlock situation. Mutual Exclusion

Producer/Consumer 13 The Producer-Consumer Problem Shared variables: buffer[], counter = 0, n = 128;

Producer/Consumer 13 The Producer-Consumer Problem Shared variables: buffer[], counter = 0, n = 128; Producer Consumer repeat … produce an item in nextp … while (counter == n); buffer[in] = nextp in = (in + 1) mod n counter = counter + 1 repeat … while (counter == 0); nextc = buffer[out] out = (out + 1) mod n counter = counter - 1 … consume the item in nextc … until false BYU CS 345 Is there anything wrong here? Yes, possible race condition! Mutual Exclusion

Semaphores 14 Consider… Shared variables: S = Q = 1; P 0: P 1:

Semaphores 14 Consider… Shared variables: S = Q = 1; P 0: P 1: wait(S); wait(Q); wait(S); . . . signal(S); signal(Q); BYU CS 345 signal(Q); signal(S); Is there anything wrong here? Yes, possible deadlock! Mutual Exclusion

Semaphores 15 Consider… P 0: P 1: Enter classroom; Occupy seat; Take exam; Turnoff

Semaphores 15 Consider… P 0: P 1: Enter classroom; Occupy seat; Take exam; Turnoff lights; P 2: . . . Is there anything wrong here? Yes, possible indeterminate results! BYU CS 345 Mutual Exclusion

Semaphores 16 n What is a semaphore? n n How are semaphores produced? n

Semaphores 16 n What is a semaphore? n n How are semaphores produced? n n Semaphores are produced by SEM_SIGNAL. How are semaphores consumed? n n A semaphore is an autonomous synchronous abstract data type used for controlling access by multiple processes, to a common resource in a concurrent system. Semaphores are consumed by SEM_WAIT and SEM_TRYLOCK. What are the major uses of semaphores? n n n Synchronization Resource allocation Mutual Exclusion BYU CS 345 Mutual Exclusion

Semaphores 19 n What does it mean to be autonomous? n n n Disable

Semaphores 19 n What does it mean to be autonomous? n n n Disable interrupts (only works on uni-processor) Hardware: test-and-set, exchange Software solutions (Decker) What does it mean to be synchronous? What are the different types of semaphores? n Binary semaphore – 1 resource, 2 states n n n 0 = nothing produced, maybe tasks in queue 1 = something produced, no tasks in queue Counting semaphore – 1 resource, multiple copies n n n BYU CS 345 0 = nothing produced, nothing in queue -n = nothing produced, n tasks queued +n = n items produced, no tasks in queue Mutual Exclusion

Semaphores 20 SEM_SIGNAL - Producer void sem. Signal. Binary(Semaphore* semaphore) // signal binary semaphore

Semaphores 20 SEM_SIGNAL - Producer void sem. Signal. Binary(Semaphore* semaphore) // signal binary semaphore { semaphore->state = 1; // produce (signal) binary semaphore tid = de. Q(semaphore->queue, -1); // dequeue blocked task if (tid < 0) return; // if empty, return semaphore->state = 0; // blocked task consumes semaphore Produce tcb[tid]. state = S_READY; // ready task for execution en. Q(rq, tid, tcb[tid]. priority); // move task to ready. Consume queue return; void sem. Signal. Counting(Semaphore* semaphore) // signal counting }semaphore // end sem. Signal. Binary { if (++semaphore->state > 0) return; // return if nothing in queue tid = de. Q(semaphore->queue, -1); // dequeue task tcb[tid]. state = S_READY; // ready task for execution en. Q(rq, tid, tcb[tid]. priority); // move task to ready queue Produce & Consume return; } // end sem. Signal. Counting BYU CS 345 Mutual Exclusion

Semaphores 21 SEM_WAIT - Consumer void sem. Wait. Binary(Semaphore* semaphore) // wait binary semaphore

Semaphores 21 SEM_WAIT - Consumer void sem. Wait. Binary(Semaphore* semaphore) // wait binary semaphore { if (semaphore->state == 0) // signaled? { tcb[cur. Task]. state = S_BLOCKED; // n, change task state to blocked en. Q(semaphore->queue, de. Q(rq, cur. Task)); // move from ready to blocked queue swap. Task(); // reschedule the tasks } semaphore->state = 0; // consume semaphore return; // return w/no block sem. Wait. Counting(Semaphore* semaphore) // wait counting }void // end sem. Wait. Binary semaphore { semaphore->state--; // consume if (semaphore->state < 0) { tcb[cur. Task]. state = S_BLOCKED; // change task state to blocked en. Q(semaphore->queue, de. Q(rq, cur. Task)); // move from ready to blocked quueu swap. Task(); // reschedule the tasks } BYU CS 345 Mutual Exclusion return; // return w/semaphore

Project 2 Assignment Step 3: 5 -State Scheduling 22 n Add priority queue to

Project 2 Assignment Step 3: 5 -State Scheduling 22 n Add priority queue to semaphore struct n n n // semaphore // link to next semaphore // semaphore name (malloc) // state (count) // type (binary/counting) // tid of creator // blocked queue Malloc semaphore queue in create. Semaphore n n typedef struct semaphore { struct semaphore* sem. Link; char* name; int state; int type; int task. Num; PQueue q; } Semaphore; semaphore->q = (int*)malloc((MAX_TASKS + 1) * sizeof(int)); semaphore->q[0] = 0; // init queue sem. Wait: de. Queue current task from ready queue and en. Queue in semaphore block queue sem. Signal: de. Queue task from semaphore block queue and en. Queue in ready queue. BYU CS 345 Mutual Exclusion

P 2 - Tasking 5 -State Scheduler #define New SWAP SEM_WAIT(s) SEM_SIGNAL(s) SEM_TRYLOCK(s) create.

P 2 - Tasking 5 -State Scheduler #define New SWAP SEM_WAIT(s) SEM_SIGNAL(s) SEM_TRYLOCK(s) create. Task() swap. Task(); sem. Wait(s); sem. Signal(s); sem. Try. Lock(s); dispatch() Ready Queue Running swap. Task() kill. Task() Exit l() na sem Wa ig m. S se it( ) 23 Blocked Queues BYU CS 345 Mutual Exclusion

Scheduling 24 Task Scheduling Scheduler / Dispatcher Ready Priority Queue Executing SWAP SEM_SIGNAL Semaphore

Scheduling 24 Task Scheduling Scheduler / Dispatcher Ready Priority Queue Executing SWAP SEM_SIGNAL Semaphore Priority Queue SEM_WAIT … BYU CS 345 Mutual Exclusion

Project 2 Assignment Step 4 a: Counting Semaphore 25 n Add counting functionality to

Project 2 Assignment Step 4 a: Counting Semaphore 25 n Add counting functionality to semaphores n n n Add a 10 second timer (tics 10 sec) counting semaphore to the polling routine (os 345 interrupts. c). n n os 345 semaphores. c: sem. Signal, sem. Wait, sem. Try. Lock Replace goto temp; #include <time. h> header. Call the C function time(time_t *timer). sem. Signal the tics 10 sec semaphore every 10 seconds. Create a reentrant high priority timing task that n n blocks (SEM_WAIT) on the 10 second timer semaphore (tics 10 sec). when activated, outputs a message with the current task number and time and then blocks again. BYU CS 345 Mutual Exclusion

P 2 - Tasking 26 Task Control Block (tcb) State = { NEW, READY,

P 2 - Tasking 26 Task Control Block (tcb) State = { NEW, READY, RUNNING, BLOCKED, EXIT } // task control block Priority = { LOW, MED, HIGH, VERY_HIGH, HIGHEST } typedef struct // task control block { char* name; // task name int (*task)(int, char**); // task address int state; // task state (P 2) int priority; // task priority (P 2) int argc; // task argument count (P 1) char** argv; // task argument pointers (P 1) int signal; // task signals (P 1) // void (*sig. Cont. Handler)(void); // task my. SIGCONT handler void (*sig. Int. Handler)(void); // task my. SIGINT handler // void (*sig. Kill. Handler)(void); // task my. SIGKILL handler // void (*sig. Term. Handler)(void); // task my. SIGTERM handler Pending semaphore when blocked. // void (*sig. Tstp. Handler)(void); // task my. SIGTSTP handler TID parent; // task parent int RPT; // task root page table (P 4) int cdir; // task directory (P 6) Semaphore *event; // blocked task semaphore (P 2) void* stack; // task stack (P 1) jmp_buf context; // task context pointer (P 1) } TCB; BYU CS 345 Mutual Exclusion

Project 2 Assignment Step 4 b: List Tasks 27 n Modify the list tasks

Project 2 Assignment Step 4 b: List Tasks 27 n Modify the list tasks command to n n Display all tasks in all system queues in execution/priority order List task name, if the task is ready, paused, executing, or blocked, and the task priority. If the task is blocked, list the reason for the block. Use the project 2 command to schedule timer tasks 1 through 9, 2 signal tasks and 2 “Im. Alive” tasks. n n n The tics 10 sec task about the current time every 10 seconds in a round robin order. (Round Robin) The “Im. Alive” tasks will periodically say hello. (Blocking) The high priority “Signal” tasks should respond immediately when semaphore signaled. (Priority) BYU CS 345 Mutual Exclusion

Project 2 Assignment Step 4 c: Verification 28 Demo n # Task Name Priority

Project 2 Assignment Step 4 c: Verification 28 Demo n # Task Name Priority Time slice Blocking Semaphore 0 CLI w/pseudo-input interrupts 5 1 in. Buffer. Ready 1 -9 Ten. Seconds 10 1 tics 10 sec 10 s. Task 1 20 1 s. Task 10 11 s. Task 2 20 1 s. Task 11 12 Im. Alive 1 1 None 13 Im. Alive 1 1 None BYU CS 345 Mutual Exclusion

Bounded Buffer 29 Bounded Buffer Solution Shared semaphore: empty = n, full = 0,

Bounded Buffer 29 Bounded Buffer Solution Shared semaphore: empty = n, full = 0, mutex = 1; repeat produce an item in nextp wait(empty); wait(mutex); repeat wait(full); wait(mutex); remove an item from buffer place it in nextc add nextp to the buffer signal(mutex); signal(full); until false BYU CS 345 signal(mutex); signal(empty); consume the item in nextc until false Mutual Exclusion

Reader/Writer Readers and Writers Problem 30 n Data object is shared (file, memory, registers)

Reader/Writer Readers and Writers Problem 30 n Data object is shared (file, memory, registers) n n n Conditions needing to be satisfied: n n n many processes that only read data (readers) many processes that only write data (writers) many can read at the same time (patron of library) only one writer at a time (librarian) no one allowed to read while someone is writing Different from producer/consumer (general case with mutual exclusion of critical section) – possible for more efficient solution if only writers write and readers read. Solutions result in reader or writer priority BYU CS 345 Mutual Exclusion

Reader/Writer Shared Data 31 n What are some characteristics of shared data objects (files,

Reader/Writer Shared Data 31 n What are some characteristics of shared data objects (files, data bases, critical code)? n n What conditions / advantages / problems arise when there is concurrent reading and writing? n n n Many processes only need mutual exclusion of critical sections (producer/consumer, mutexes) many processes only read data (readers) many processes only write data (writers) many can read at the same time (patrons of library) only one writer at a time (librarians) no one allowed to read while someone is writing possible for more efficient solution than producer / consumer if only writers write and readers read. Who should have priority, readers or writers? BYU CS 345 Mutual Exclusion

Reader/Writer 32 Readers/Writers Semaphore rmutex=1, wmutex = 1; integer readcount = 0; Only one

Reader/Writer 32 Readers/Writers Semaphore rmutex=1, wmutex = 1; integer readcount = 0; Only one writer at a time while(true) { wait(wmutex); <write to the data object> Writer signal(wmutex); }; while(true) { Readers have priority! Who has priority (writersorsubject to Reader Writer? starvation!) Reader More than one reader at a time BYU CS 345 The first reader makes sure no one can write wait(rmutex); readcount++; if (readcount == 1) wait(wmutex); signal(rmutex); Last one out allows <read the data> writing again wait(rmutex); readcount--; if (readcount == 0) signal(wmutex); signal(rmutex); }; Mutual Exclusion

Reader/Writer 33 Writers/Readers Semaphore outer. Q, rsem, rmutex, wsem = 1; while(true) Additional readers

Reader/Writer 33 Writers/Readers Semaphore outer. Q, rsem, rmutex, wsem = 1; while(true) Additional readers queue here allowing { wait(outer. Q); { wait(wmutex); writers to jump ahead wait(rsem); writecnt++; of the readers wait(rmutex); if (writecnt == 1) readcnt++ wait(rsem); if (readcnt == 1) signal(wmutex); Disable wait(wsem); writers signal(rmutex); Wait here until signal(rsem); WRITE all readers done, signal(outer. Q); as well as Once a writer wants to signal(wsem); multiple writers write – no new readers READ wait(wmutex); allowed writecnt--; wait(rmutex); if (writecnt == 0) readcnt--; signal(rsem); if(readcnt == 0) signal(wmutex); signal(wsem); }; Last reader out Last writer out allows writers signal(rmutex); allows readers }; BYU CS 345 Mutual Exclusion

Barbershop Problem 34 n 3 barbers, each with a barber chair n n n

Barbershop Problem 34 n 3 barbers, each with a barber chair n n n Sofa can hold 4 customers Maximum of 20 in shop n n n Cashier Entrance Customers arrive randomly Customers wait outside if necessary Standing room area Exit Sofa When a chair is empty: n n n Barber chairs Haircuts vary in time Customer sitting longest on sofa is served Customer standing the longest sits down After haircut, customer pays cashier at cash register n n n Barbers have to cut hair and cashier Customer waits for receipt Upon exit, new customer allowed in shop BYU CS 345 Mutual Exclusion

Barbershop 35 Fair Barbershop procedure customer; var custnr: integer; begin wait ( max_capacity );

Barbershop 35 Fair Barbershop procedure customer; var custnr: integer; begin wait ( max_capacity ); // enter_shop wait( mutex 1 ); count : = count + 1; custnr : = count; signal( mutex 1 ); wait( sofa ); // sit on sofa wait( barber_chair ); // get up from sofa signal( sofa ); // sit in barber chair wait( mutex 2 ); enqueue 1( custnr ); signal( cust_ready ); signal( mutex 2 ); wait( finished[custnr] ); // leave barber chair signal( leave_b_chair ); // pay signal( payment ); wait( receipt ); // exit shop signal( BYU CS 345 max_capacity procedure barber; var b_cust: integer begin repeat // get customer wait( cust_ready ); wait( mutex 2 ); dequeue 1( b_cust ); signal( mutex 2 ); wait( coord ); // cut hair signal( coord ); signal( finished[b_cust] ); wait( leave_b_chair ); signal( barber_chair ); forever end; procedure cashier; begin repeat wait( payment ); wait( coord ); // accept payment signal( coord ); signal( receipt ); forever end; Barber chairs Cashier Entrance Standing room area Exit Sofa max_capacity: semaphore (: =20); sofa: semaphore (: =4); barber_chair, coord: semaphore (: =3); mutex 1, mutex 2: semaphore (: =1); cust_ready, leave_b_chair: semaphore (: =0) payment, receipt: semaphore (: =0) finished: array [1. . 50] of semaphore (: =0); count: integer; Mutual Exclusion

36 Jurassic Park Visitors try to enter the Jurassic Park at random times. (Only

36 Jurassic Park Visitors try to enter the Jurassic Park at random times. (Only a set number of visitors may be in the park at any one time – OSHA requirements!) Upon being allowed in the park, a visitor must get in line to purchase a ticket. When the tour car pulls into the unloading station, the visitors exit the tour car. and the driver goes to sleep awaiting new duties. The tour car pulls forward to be loaded again. After visiting the museum, the visitor gets in the tour car line to wait until permitted to board a tour car. (As a visitor boards a tour car, he returns his ticket. ) After visiting the gift shop, the visitors exit the park. BYU CS 345 When the touring car is filled with visitors and a driver is obtained, the car enters Jurassic Park and runs a guided tour through the park. After the visitors exit a tour car, they get into the gift shop line until they can visit the gift shop. After successfully obtaining a ticket from a driver, the visitor gets in the museum line and visits the museum. (A limited number of visitors are allowed in the museum as well as the gift shop. ) Mutual Exclusion

37 Jurassic Park procedure visitor; var visitor_id: integer; begin wait ( max_capacity ); //

37 Jurassic Park procedure visitor; var visitor_id: integer; begin wait ( max_capacity ); // enter_park wait ( park. Mutex ); num. Outside. Park--; num. In. Park-++; signal ( park. Mutex ); // get a ticket wait ( request. Ticket. Mutex ); signal ( need. Ticket ); signal ( wakeup. Driver ); wait ( take. Ticket ); signal ( request. Ticket. Mutex ); // visit museum // get in car line // wait for a seat wait ( need_visitor ); signal ( visitor_ready ); pass_to_car ( visitor_id ); wait ( ride_over ); // give back ticket // visit gift shop // exit park BYU CSsignal 345 ( max_capacity ); procedure car; var car. ID: integer begin repeat // fill 3 car seats for (NUM_SEATS) { wait ( fill. Seat[car. ID] ); signal ( need_visitor ); wait ( visitor_ready ); save_Visitor( visitor. ID ); signal ( seat. Filled[car. ID] ); } pass_to_park( car. Done ); signal ( car. Ready ); // enjoy ride wait ( car. Done ); signal ( each visitor. ID ); forever end; max_capacity: semaphore (: =20); park. Mutex, request. Ticket. Mutex: semaphore (: =1); need. Ticket, wait. Ticket: semaphore (: =0) procedure driver; var car. ID: integer begin repeat wait ( wakeup. Driver ); if ( trylock ( need_ticket )) { signal (take. Ticket); } else { signal ( driver_ready ); wait ( ride_over ); { forever Mutual Exclusion end;

Message Passing 38 n Shared memory is useful in a threaded environment n n

Message Passing 38 n Shared memory is useful in a threaded environment n n n Inter-process communication (IPC) used by processes n n n Single atomic variables (semaphores, modes) Memory mapping (data structures, messages) Test-and-set (atomic instructions) Fast – do not require data movement (reference) for processes inside the same computer for processes in a distributed system Message passing used by distributed systems (networks) n n n send(destination, message) post(destination, message) receive(source, message) BYU CS 345 Mutual Exclusion

Message Passing Synchronization 39 n For the sender: it is more natural not to

Message Passing Synchronization 39 n For the sender: it is more natural not to be blocked n n n can send several messages to multiple destinations sender usually expects acknowledgment of message receipt (in case receiver fails) Post. Message() is asynchronous – returns immediately Send. Message() is synchronous –block until message delivered and processed For the receiver: it is more natural to be blocked after issuing Receive. Message() n n the receiver usually needs the info before proceeding but could be blocked indefinitely if sender process fails before sending reply BYU CS 345 Mutual Exclusion

Message Passing Addressing 40 n Direct addressing: n n n when a specific process

Message Passing Addressing 40 n Direct addressing: n n n when a specific process identifier is used for source/destination but it might be impossible to specify the source ahead of time (ex: a print server) Indirect addressing (more convenient): n n messages are sent to a shared mailbox which consists of a queue of messages senders place messages in the mailbox, receivers pick them up BYU CS 345 Mutual Exclusion

Message Passing Mailboxes and Ports 41 n A mailbox can be private n n

Message Passing Mailboxes and Ports 41 n A mailbox can be private n n A mailbox can be shared among several senders and receivers n n one sender/receiver pair OS may then allow the use of message types (for selection) Port: a mailbox associated with one receiver and multiple senders n used for client/server application: the receiver is the server BYU CS 345 Mutual Exclusion

Message Passing 42 n n Port/Mailbox Ownership A port is usually owned and created

Message Passing 42 n n Port/Mailbox Ownership A port is usually owned and created by the receiving process The port is destroyed when the receiver terminates The OS creates a mailbox on behalf of a process (which becomes the owner) The mailbox is destroyed at the owner’s request or when the owner terminates BYU CS 345 Mutual Exclusion

Monitors 43 n A programming-language construct that provides equivalent functionality to that of semaphores

Monitors 43 n A programming-language construct that provides equivalent functionality to that of semaphores n n A monitor is a software module containing: n n n ME threads that wait (block) for true conditions. Thread-safe class (wrapped by mutual exclusive conditions) Concurrent Pascal, Pascal-Plus, Modula, Java one or more procedures, an initialization sequence, and local data variables Mutex (lock) object and condition variables Condition variable: n n n Container of threads that are waiting on a certain condition. Threads temporarily give up exclusive access in order to wait for some condition to be met. Local variables accessible only by monitor’s procedures A process enters the monitor by invoking one of its procedures Only one process can be in the monitor at any one time BYU CS 345 Mutual Exclusion

Monitor 44 Monitor for the P/C problem Monitor boundedbuffer: Make threading system buffer: array[0.

Monitor 44 Monitor for the P/C problem Monitor boundedbuffer: Make threading system buffer: array[0. . k-1] of items; release all locks and nextin: =0, nextout: =0, count: =0: integer; sleep until condition is met. notfull, notempty: condition; Produce(v): if (count = k) cwait(notfull); buffer[nextin] : = v; nextin : = nextin+1 mod k; count++; csignal(notempty); Signal a consumer thread or all consumer threads that are blocked waiting for non-empty buffer. Consume(v): if (count = 0) cwait(notempty); v : = buffer[nextout]; nextout : = nextout+1 mod k; count--; csignal(notfull); BYU CS 345 Mutual Exclusion

Conclusion 45 n n Semaphores are a powerful tool for enforcing mutual exclusion and

Conclusion 45 n n Semaphores are a powerful tool for enforcing mutual exclusion and to coordinate processes. When wait(S) and signal(S) are scattered among several processes n n Difficult to understand their effects. Difficult to test. Monitors make classes thread-safe and mutual exclusion more controllable. Usage must be correct in all the processes (everyone has to play by the rules). n One bad (or malicious) process can fail the entire collection of processes BYU CS 345 Mutual Exclusion

46 BYU CS 345 Mutual Exclusion

46 BYU CS 345 Mutual Exclusion