Sie sind auf Seite 1von 2

CS-407 OPERATING SYSTEMS ASSIGNMENT FOR BE CIS 2009-10 INSTRUCTIONS: Do this yourself! Copied assignment carries no marks.

. Submit the solution in CEW lab on 4th Mar 2013 to the lab staff. QUESTION 1: Consider the following pieces of code: CODE A: int get_half(int n){ int half; if(n%2!=0)n--; half=n/2; return (half);} CODE B: int get_odd(int n){ int odd; if(n%2==0)n++; odd=n; return (odd);} Can you write alternate codes that are reentrant? QUESTION 2: A factory makes glass bottles. A customer can place an order for either 10 transparent or 10 painted bottles. As long as an order is not completed and picked by a customer, a new order cannot be placed. There are glass blowers, painters and packers. As long as an order is not placed, no glass is blown. As long as a bottle is not blown, no bottle is painted. If the order is for transparent bottles, the painter does not work, and the bottles are passed to the packer. If the order is for painted bottles, the painter paints them and passes them on to the packer. The packer does not work as long as he does not receive bottles. The packer place 10 bottles in a crate, after which he tells the customer that the order is ready. The customer must not try to pick the order before it is ready. void customer() void blower() void painter() { { { while(true){ while(true){ while(true){ ord=place_order(); blow_bottle(); } } paint_bottle(); } } /*if ord=1 transparent bottles are ordered, if ord=2 painted bottles are ordered, no other value of ord is possible*/ go_home(); pick_order(); } } void packer() int ord; /*global variable for order type*/ { void main() int count=0; { while(true){ parbegin(customer,blower, painter, packer); } if(count==0) take_new_crate(); put_bottle_in_crate(); count++; if(count==10) count=0; } } Rewrite the above code, providing necessary synchronization and mutual exclusion with semaphores. Do not forget to initialize them. You can use if-else structures.

QUESTION 3: Let us think of a chocolate factory with two types of workers. Worker A makes heart shaped chocolates. Worker B makes chocolates with almonds. Both workers start work when the manager has provided chocolate and a new box. Both workers put the finished candy in a box such that each box of candy has 6 heart shaped chocolates and 6 chocolate sweets with almonds in them. The factory has an unlimited supply of chocolate, but it may run out of almonds. When there are no almonds, worker B cannot make candy, until the storekeeper brings him a fresh batch of nuts. The storekeeper works only when worker B asks him to, and sleeps the rest of the time. Each worker tells the manager when he has put in his 6 pieces of candy in the box. After both workers have completed a box, the manager provides more chocolate and a new box. At any one instant, only one worker may put a candy in a box. void manager() { while(true){ provide_chocolate(); provide_box(); } } void worker_A() { int heart=0; while(true){ make_candy(); heart++; put_in_box(); if(heart==6) heart=0; } } void worker_B() { int almond=0; while(true){ if(out_of_almonds) /*ask the storekeeper*/; make_candy(); almond++; put_in_box(); if(almond==6) almond=0; } }

void storekeeper() void main() { { while(true){ parbegin(manager, storekeeper, worker_A, worker_B); } give_almonds_to_worker_B(); }} Rewrite the above code, providing necessary synchronization and mutual exclusion with semaphores. Do not forget to initialize them. You can use if-else structures. QUESTION 4: Solve Q 5.24 from the 6th edition of Operating Systems by William Stallings. The question starts such: Consider a system of 6 threads {T_1, T_2, T_6} in which the sole mechanism of thread control is the semaphore. QUESTION 5: The barbershop problem: A barbershop consists of a waiting room with n chairs, and the barber room containing the barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy, but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the customers. Write pseudocode to solve this problem using semaphores. QUESTION 6: The cigarette smokers problem: There are four processes in this problem, three smoker processes and an agent process. Each of the smoker processes will make a cigarette and smoke it. To make a cigarette requires tobacco, paper, and matches. Each smoker process has one of the three items. I.e., one process has tobacco, another has paper, and a third has matches. The agent has an infinite supply of all three. The agent places two of the three items on the table, and the smoker that has the third item makes the cigarette. Synchronize the processes.

Das könnte Ihnen auch gefallen