Process synchronization

24
LASSICAL PROCESS SYNCHRONIZATIO OBLEM IN OPERATING SYSTEM: A REV PRESENTED BY RAMESH KUMAR PROFESSOR, CSE BIT DURG

Transcript of Process synchronization

Page 1: Process synchronization

CLASSICAL PROCESS SYNCHRONIZATION PROBLEM IN OPERATING SYSTEM: A REVIEW

PRESENTED BYRAMESH KUMARPROFESSOR, CSE

BIT DURG

Page 2: Process synchronization

A World War I Fighter Aircraft•Germans: Flew high to gain altitude, turned off engine (propeller stopped) to fire machine guns ~ coarse-grained synchronizaion ~ mutual exclusion•Germans later developed technology to synchronize between gun firing and whirling propeller blades ~ fine-grained synchronization

Fighter Aircraft Problem

Page 3: Process synchronization

Bakery Algorithm get a ticket number to purchase baked goods if two processes pick the number at the same time, it does not guarantee two processes have different #if number( Pi ) < number ( Pj ) serve Pi firstif number ( Pi ) = number ( Pj ), then check i, j ( unique process ids ); if i < j, then serve Pi firstThe notation (a,b) < (c,d) is defined as (a < c) or (a = c and b < d) Operational principles behind Lamport’s bakery algorithm are very simple. All process threads must take a number and wait their turn to use a shared computing resource or to enter their critical section. The number can be any of the global variables, and processes with the lowest number will be processed first. If there is a tie or similar number shared by both processes, it is managed through their process ID. If a process terminates before its turn, it has to start over again in the process queue.

Lamport’s Bakery Problem

Page 4: Process synchronization
Page 5: Process synchronization

Dining Philosophers ProblemThere is a dining room containing a circular table with five chairs. At each chair is a plate, and between each plate is a single chopstick. In the middle of the table is a bowl of spaghetti. Near the room are five philosophers who spend most of their time thinking, but who occasionally get hungry and need to eat so they can think some more.In order to eat, a philosopher must sit at the table, pick up the two chopsticks to the left and right of a plate, then serve and eat the spaghetti on the plate.

Page 6: Process synchronization

The Dining Philosophers have worked up a solution Before eating, each philosopher will flip a coin to decide whether to pick up the left fork or the right fork first. If the second fork is taken, the philosopher will put the first fork down, then flip the coin again.

Flipping Coin Philosophers Problem

Page 7: Process synchronization

The barber has one barber chair and a waiting room with a number of chairs in it. When the barber finishes cutting a customer's hair, he dismisses the customer and then goes to the waiting room to see if there are other customers waiting. If there are, he brings one of them back to the chair and cuts his hair. If there are no other customers waiting, he returns to his chair and sleeps in it.Each customer, when he arrives, looks to see what the barber is doing. If the barber is sleeping, then the customer wakes him up and sits in the chair. If the barber is cutting hair, then the customer goes to the waiting room. If there is a free chair in the waiting room, the customer sits in it and waits his turn. If there is no free chair, then the customer leaves.

Sleeping Barber Problem

Page 8: Process synchronization

Customer One process requiring the services of a bartender.Barber An ongoing process that cuts customers hair.Cashier An ongoing process that accepts payment from customers.ResourcesStanding area Floor space for customer processes waiting for an empty space on the couch. Up to 10 customers may be in the standing area.Sofa A sitting area for those waiting to be served by a bar tender. Four customers may sit on the sofa at any given time.Chair Customers must sit in a barber’s chair in order to be served.Cash register Payment transactions take place at the cash register between the cashier and customer processes.

Sleeping Barber Floor Plan Problem

Page 9: Process synchronization
Page 10: Process synchronization

There is a shared resource which should be accessed by multiple processes. There are two types of processes in this context. They are reader and writer. Any number of readers can read from the shared resource simultaneously, but only one writer can write to the shared resource. When a writer is writing data to the resource, no other process can access the resource. A writer cannot write to the resource if there are non zero number of readers accessing the resource.

Reader Writer Problem

Page 11: Process synchronization

Producer Consumer/Bounded Buffer Problem

Two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue. The producer's job is to generate data, put it into the buffer, and start again. At the same time, the consumer is consuming the data (i.e., removing it from the buffer), one piece at a time. The problem is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer won't try to remove data from an empty buffer.

Page 12: Process synchronization

Cigarette Smoking ProblemAssume a cigarette requires three ingredients to make and smoke: tobacco, paper, and matches. There are three smokers around a table, each of whom has an infinite supply of one of the three ingredients — one smoker has an infinite supply of tobacco, another has paper, and the third has matches.There is also a non-smoking agent who enables the smokers to make their cigarettes by arbitrarily selecting two of the supplies to place on the table. The smoker who has the third supply should remove the two items from the table, using them (along with their own supply) to make a cigarette, which they smoke for a while. Once the smoker has finished his cigarette, the agent places two new random items on the table. This process continues forever.

Page 13: Process synchronization

Tanenbaum] A student majoring in anthropology and minoring in computer science has embarked on a research project to see if African baboons can be taught about deadlocks. He locates a deep canyon and fastens a rope across it, so the baboons can cross hand-over-hand. Several baboons can cross at the same time, provided that they are all going in the same direction. If eastward moving and westwardmoving baboons ever get onto the rope at the same time, a deadlock will result (the baboons will get stuck in the middle) because it is impossible for one baboon to climb over another one while suspended over the canyon. If a baboon wants to cross the canyon, it must check to see that no other baboon is currently crossing in the opposite direction. Write a program using semaphores that avoids deadlock. Do not worry about a series of eastward moving baboons holding up the westward moving baboons indefinitely.

Baboons Crossing Canyoan Problem

Page 14: Process synchronization

This demonstration shows a college consisting of five philosophers, a chef and a canteen. The chef and the philosophers are "active" objects. The canteen is a "passive" object through which the philosophers and the chef interact. The canteen is implemented in the style of the CubbyHole.The philosophers think for a while and then go to the canteen for food.Except for one of them ... who is plain greedy, never thinks and just keeps going to the canteen.The chef cooks in batches of four, replenishing the canteen when each batch is ready. The greedy philosopher always misses out! He gets there too early the first time (no food yet) and is put on hold. When released, he is put on the *back* of the canteen queue again ... behind his colleagues who arrived whilst he was on hold. His colleagues take the whole batch just arrived from the kitchen and he gets put on hold again. He never gets out of this cycle.

Starving Philosphers Problem

Page 15: Process synchronization

Late-Night Pizza. A group of students are studying for a CPS 110 exam. The students can study only while eating pizza. Each student executes the following loop: while (true) { pick up a piece of pizza;study while eating the pizza}. If a student finds that the pizza is gone, the student goes to sleep until another pizza arrives. The first student to discover that the group is out of pizza phones Satisfactions atBrightleaf to order another pizza before going to sleep. Each pizza has S slices. Write code to synchronize the student threads and the pizza delivery thread. Your solution should avoid deadlock and phone Satisfactions (i.e., wake up the delivery thread) exactly once each time a pizza is exhausted. No piece of pizza may be consumed by more than one student. [Andrews91]

Pizza Distribution Problem

Page 16: Process synchronization

John is waiting in his car at a red traffic light with his children. His children start fighting with each other while waiting, and he leans back to scold them. Once their fighting stops, John checks the light again and notices that it's still red. However, while he was focusing on his children, the light had changed to green, and then back again. John doesn't think the light ever changed, but the people waiting behind him are very mad and honking their horns now.In this scenario, the 'A' state is when the traffic light is red, and the 'B' state is when it's green. Originally, the traffic light starts in 'A' state. If John looked at the light he would have noticed the change. But he only looked when the light was red (state 'A'). There is no way to tell if the light turned green during the time of no observation.

ABA Model Problem

Page 17: Process synchronization

Expensive Candy. Three engineering professors have gone to the faculty club to eat licorice. Each piece of licorice costs 36 cents. To buy a piece of licorice, a professor needs a quarter, a dime, and apenny (they do not give change, and they don’t take American Express). The first professor has a pocket full of pennies, the second a supply of quarters, and the third a supply of dimes. A wealthy alum walks up to the bar and lays down at random two of the three coins needed for a piece of licorice. The professor with the third coin takes the money and buys the licorice. The cycle then repeats. Show how to synchronize the professors and the alum. [Patil71, Parnas75, Andrews91]

Expensive Candy Problem

Page 18: Process synchronization

Too Much Milk Problem

Page 19: Process synchronization

Cold coffee. The espresso franchise in the strip mall near my house serves customers FIFO in the following way. Each customer entering the shop takes a single “ticket” with a number from a “sequencer” on the counter. The ticket numbers dispensed by the sequencer are guaranteed to be unique and sequentially increasing. When a barrista is ready to serve the next customer, it calls out the “eventcount”, the lowest unserved number previously dispensed by the sequencer. Each customer waits until the eventcount reaches the number on its ticket. Each barrista waits until the customer with the ticket it called places an order. Show how to implement sequencers, tickets, and event counts using mutexes and condition variables.

Cold Coffee Problem

Page 20: Process synchronization

Highway 110 is a two-lane north-south road that passes through a one-lane tunnel. A car can safely enter the tunnel if and only if there are no oncoming cars in the tunnel. To prevent accidents, sensors installed at each end of the tunnel notify a controller computer when cars arrive or depart the tunnel in either direction The controller uses the sensor input to control signal lights at either end of the tunnel. Show how to implement the controller program correctly using mutexes and condition variables. You may assume that each car is represented by a thread that calls Arrive() and Depart() functions in the controller, passing an argument indicating the direction of travel. You may also assume that Arrive() can safely stop the arriving car by changing the correct signal light to red and blocking the calling thread. Your solution should correctly handle rush hour, during which most cars approach the tunnel from the same direction. (Translation: your solution should be free from starvation.)

Highway Tunnel Problem

Page 21: Process synchronization

24.[Tanenbaum] A distributed system using mailboxes has two IPC primitives, send and receive. The latter primitive specifies a process to receive from, and blocks if no message from that process is available,even though messages may be waiting from other processes. There are no shared resources, but processes need to communicate frequently about other matters. Is deadlock possible? Discuss.Deadlock is possible. Process A may query process B and await a response from B. B receives A’s message but must query process C before it can respond to A. Likewise process C must query processA in order to answer B’s question. A is only listening to B so C will never get an answer and thus cannot respond to B, which in turn cannot respond to A. Deadlock.

Mail Box Problem

Page 22: Process synchronization

Cinderella and Prince are getting a divorce. To divide their property, they have agreed on the following algorithm. Every morning, each one may send a letter to the other’s lawyer requesting one item of property. Since it takes a day for letters to be delivered, they have agreed that if both discover that they have requested the same item on the same day, the next day they will send a letter canceling the request.Among their property is their dog, Woofer, Woofer’s doghouse, their canary, Tweeter, and Tweeter’s cage. The animals lover their houses, so it has been agreed that any division of property separating an animal from its house is invalid, requiring the whole division to start over from scratch. Both Cinderella and Prince desperately want Woofer. So they can go on (separate) vacations, each spouse has programmed a personal computer to handle the negotiation. When they come back from vacation, the computers are still negotiating. Why? Is deadlock possible? Is starvation possible?If both parties always request the same item (eg Woofer), livelock occurs because the transactions are always cancelled.

Property Division Problem

Page 23: Process synchronization

The Sleeping Professor Problem. Once class is over, professors like to sleep — except when students bother them to answer questions. You are to write procedures to synchronize threads representing one professor and an arbitrary number of students.A professor with nothing to do calls IdleProf(), which checks to see if a student is waiting outside the office to ask a question. IdleProf sleeps if there are no students waiting, otherwise it signals one student to enter the office, and returns. A student with a question to ask calls ArrivingStudent(), which joins the queue of students waiting outside the office for a signal from the professor; if no students are waiting, then the student wakes up the sleeping professor. The idea is that the professor and exactly one studentwill return from their respective functions “at the same time”: after returning they discuss a topic of mutual interest, then the student goes back to studying and the professor calls IdleProf again. a) Implement IdleProf and ArrivingStudent using mutexes and condition variables. You may assume that mutexes and condition variables are fair (e.g., FIFO).

Sleeping Professor Problem

Page 24: Process synchronization

Thank you