0% found this document useful (0 votes)
13 views5 pages

Lec 3-3 CENG328 IPC-2

Uploaded by

dekud9556
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Lec 3-3 CENG328 IPC-2

Uploaded by

dekud9556
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Classical mutual exclusion problems

 Bounded(N places)-Buffer
 Readers and Writers
Classical Synchronization
 Dining-Philosophers
Problems
 Sleeping Barber
 Dining-Philosophers Problem
Solution using monitors

Operating Systems Operating Systems

Implementation of Producer-consumer Shared


Bounded-Buffer Problem Using Semaphore Bounded Buffer Problem (Cont.)

 Each of N buffer places can hold one data item  The structure of the producer process
 Implementation: do {
 Use binary semaphore mutex to establish // produce an item
mutual exclusion on buffer update, initialized wait (empty); // queued if 0
to 1 wait (mutex);
 Use a multi-value semaphore full to // add the item to the buffer
implement item consumption, initialized to 0 signal (mutex);
 Use a multi-value semaphore empty to signal (full); //allow consumer to consume if any
implement item production, initialized N. } while (true);

Operating Systems Operating Systems

Implement Readers-Writers Problem using


Bounded Buffer Problem (Cont.) Semaphore

 The structure of the consumer process  A data set is shared among a number of concurrent reader and writer
processes
do {  Readers – only read the data set; they do not perform any updates
wait (full); //queue if 0  Writers – write the data item to be read by the readers
 Design algorithm:
wait (mutex);
 multiple readers can read an item, if exist, concurrently with no protection
// remove an item from buffer  Writer(s) can only write data item in mutual exclusion
signal (mutex);  A writer and a reader can write and read in mutual exclusion
 Modeling Shared Data
signal (empty); //allow producer to produce, if any
 Data set: item
// consume the removed item  Semaphore mutex initialized to 1
} while (true);  Semaphore wrt initialized to 1
 Integer readcount is readers shared memory, initialized to 0: it counts
number of readers in the process of reading.

Operating Systems Operating Systems

1
Readers-Writers Problem (Cont.) Readers-Writers Problem (Cont.)

 writer process: should write only if there is no active  The structure of a reader process

reader do {
wait (mutex) ;
do { readcount ++ ;
wait (wrt) ; // no limit on number of items if (readercount == 1) wait (wrt) ;
signal (mutex)

// writing item is performed // reading item is performed

wait (mutex) ;
signal (wrt) ; readcount - - ;
} while (true) if redacount == 0) signal (wrt) ;
signal (mutex) ;
} while (true)

Operating Systems Operating Systems

Dining-Philosophers Problem: 5
philosopher dine and think
Dining Philosophers: First Try

5 Chinese philosophers dine and


think randomly.

 Modeling: functions: think(), eat(), take_fork(), put_fork()


 Share Data set:
 Bowl of rice Is this solution correct? No control over the state of
5 chopsticks: Semaphore fork [5], initialized to 1

the forks!

Operating Systems Operating Systems

Dining Philosophers: Correct Try: Control over the


state of the foks Dining Philosophers

Solution to dining philosophers problem (part 1)


Solution to dining philosophers problem (part 2)
Note that mutex controls all CSs; S[i] are initially set to 0

Operating Systems Operating Systems

2
The Sleeping Barber Problem (2)
Sleeping Barber

waiting is shared data item.

Operating Systems Operating Systems

Problems with Semaphores Monitors: A higher level synchronization


construct
 Correct use of semaphore operations is not  A high-level abstraction that provides a convenient and
effective mechanism for process synchronization
easy.
 Only one process may be active within the monitor at a
time
 Omitting of wait (mutex) or signal (mutex) or monitor monitor-name
both is cause of incorrect solutions. {
// shared variable declarations
procedure P1 (…) { …. }

procedure Pn (…) {……}
Initialization code ( ….) { … }

}
}
Operating Systems Operating Systems

Schematic view of a Monitor Condition Variables and dining


philosophers
 condition x, y;
 Two operations on a condition
variable:
 x.wait () – a process that invokes
the operation is suspended.
 x.signal () – resumes one of
processes (if any) that invoked
x.wait ()

Operating Systems Operating Systems

3
Solution to Dining Philosophers: Monitor
Monitor with Condition Variables Solution
monitor DiningPhilosopher
{
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
void pickup (int i) {
state[i] = HUNGRY;
test(i);
if (state[i] != EATING) self [i].wait;
}
void putdown (int i) {
state[i] = THINKING;
// test left and right neighbors
test((i + 4) % 5);
test((i + 1) % 5);
}

Operating Systems Operating Systems

Monitor Solution to Dining Philosophers (cont) Thread examples: MUTEX


/* mutex are only valid within the same process */
void test (int i) { pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
if ( (state[(i + 4) % 5] != EATING) && int counter=0;
(state[i] == HUNGRY) &&
(state[(i + 1) % 5] != EATING) ) {
state[i] = EATING ; /* Function C */
self[i].signal () ; void functionC()
} {
} pthread_mutex_lock( &mutex1 );
initialization_code() {
counter++
for (int i = 0; i < 5; i++) pthread_mutex_unlock( &mutex1 );
state[i] = THINKING; }
}
}
Operating Systems Operating Systems

Mutex example program: mutex1.c Compile mutex1.c and run


#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;

main()
{
Compile: gcc -lpthread mutex1.c
int rc1, rc2;

Run:
pthread_t thread1, thread2;

/* Create independent threads each of which will execute functionC */

if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) )


./a.out
{
printf("Thread creation failed: %d\n", rc1);
}

if( (rc2=pthread_create( &thread2, NULL, &functionC, NULL)) )


{
printf("Thread creation failed: %d\n", rc2);
Results:
Counter value: 1
}

/* Wait till threads are complete before main continues. Unless we */


/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */

pthread_join( thread1, NULL);


pthread_join( thread2, NULL);
Counter value: 2
exit(0);
}

void *functionC()
{
pthread_mutex_lock( &mutex1 );
counter++;
printf("Counter value: %d\n",counter);
pthread_mutex_unlock( &mutex1 );
}

Operating Systems Operating Systems

4
THIS IS ALL ABOUT THREADS!

Operating Systems

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy