Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Operating Systems Assignment: Fair Traffic Control Policy for Multithreaded Programming, Assignments of Operating Systems

An assignment for a master's level computer science course at george mason university, where students are required to implement a 'fair' traffic control policy for multithreaded programming using mutex locks and condition variables. The goal is to limit the number of vehicles (threads) crossing the bridge in one direction while ensuring fairness and avoiding head-on collisions or bridge collapses. Students must write procedures for arrivebridge, crossbridge, and exitbridge, and provide debug messages for each step. The solution must not employ busy waiting and must use two queues for vehicles waiting to enter the bridge. The assignment includes three vehicle arrival schedules and requires submission of a write-up, pseudo-code, source code, and output for each schedule.

Typology: Assignments

Pre 2010

Uploaded on 02/12/2009

koofers-user-w1f
koofers-user-w1f 🇺🇸

4

(1)

10 documents

1 / 3

Toggle sidebar

Related documents


Partial preview of the text

Download Operating Systems Assignment: Fair Traffic Control Policy for Multithreaded Programming and more Assignments Operating Systems in PDF only on Docsity! GEORGE MASON UNIVERSITY Computer Science Department CS 571 – Operating Systems Fall 2003 Assignment 2 – Multithreaded Programming DUE DATE – Oct 6 Process Synchronization The goal of this exercise to give you some experience writing concurrent pro- grams. You have been hired by MetroTrans to synchronize traffic over a narrow light-duty bridge on a public highway. Traffic may only cross the bridge in one direction at a time, and if there are ever more than 3 vehicles on the bridge at one time, it will collapse under their weight. In this system, each vehicle is represented by one thread, which executes the procedure OneVehicle when it arrives at the bridge: OneVehicle(int vehicle_id,int direc, int time_to_cross) { ArriveBridge(direc); CrossBridge(vehicle_id,direc,time_to_cross); ExitBridge(vehicle_id,direc); } In the code above, vehicle id is an integer which uniquely identifies each vehicle (The vehicles arriving at the bridge should be assigned vehicle ids 1,2,3... and so on ). direc is either 0 or 1; it gives the direction in which the vehicle will cross the bridge. time to cross is the time it takes a vehicle to cross the bridge – assume that every vehicle takes 4 seconds to cross the bridge. Implement a “fair” traffic control policy that imposes a limit (say 5) on the number of vehicles that can cross the bridge in one direction, while vehicles travelling in the opposite direction are waiting to get on the bridge. NOTE: if there are no vehicles waiting to get on the bridge in the opposite direction, your policy should not switch directions. In other words, your policy should be smart enough to provide fairness if there is traffic in both directions but should never force a vehicle to wait to get on the bridge if there is no traffic in the opposite direction. Write the procedures ArriveBridge,CrossBridge and ExitBridge, using mutex locks and condition variables for synchronization1 . The ArriveBridge procedure must not return until it is safe for the car to cross the bridge in the given direction (it must guarantee that there will be no head-on collisions or bridge collapses). The CrossBridge procedure should just delay for time to cross seconds and print out a debug message. ExitBridge is called to indicate that the caller has finished crossing the bridge; ExitBridge should take steps to let additional cars cross the bridge. In addition, ExitBridge should update a shared variable departure index, which keeps track of the order in which the cars leave the bridge, i.e., the first car to leave the bridge has departure index 1, the second car has departure index 2, and so on. The ExitBridge procedure should also print out the departure index for that vehicle. 1If you use Java for the assignment, use the equivalent thread synchronization faciltities 1 NOTES: • Your solution must not employ busy waiting. • Your solution must use two queues on either end of the bridge where vehicles wait until it is safe for them to enter the bridge. In terms of threads, this means that you need two condition variables (or Java objects) where a thread is suspended if it cannot enter the CrossBridge procedure. • For full credit, your solution must not use the pthread cond broadcast() function (or if you’re using Java, the notify all() method), i.e. your solution must only use pthread cond signal() or notify() for signalling any threads blocked on a condition variable. NOTE: invoking pthread cond signal() or notify() repeatedly in a loop is equivalent to pthread cond broadcast() and notify all() • Occasionally, a vehicle (thread) may overtake another vehicle travelling in the same direction on the bridge. Do not worrry about this. You do not have to ensure that vehicles leave the bridge in the same order as they entered it. • The debug message printed out in CrossBridge should provide a snapshot of the bridge and the queues on the two ends of the bridge. It is probably also a good idea to print out debug messages in ArriveBridge and ExitBridge. In this assignment, you have to run your program for the three vehicle arrival schedules given below: (i) 5 : DELAY(10) : 5 : DELAY(10) : 5 : DELAY(10) : 5: DELAY(10) : 5 : DELAY(10) : 5 (ii) 10 : DELAY(10) : 10 : DELAY(10): 10 (iii) 30 Here the numbers indicate the number of vehicles arriving simultaneously at the bridge, while the numbers in parentheses indicate the delay before the next arrival(s). For example, under schedule (i) 5 vehicles arrive simultaneously at the bridge at the start of the experiment, five more vehicles arrive simultaneously 10 seconds after the arrival of the first five vehicles and so on. In each of the three schedules, thirty vehicles arrive at the bridge during the course of the experiment. Note that vehicles arriving simultaneously does not imply that they are all traveling in the same direction. Assume that the probability that a vehicle is traveling in direction “0” is 0.6 NOTES: 1. You can use either Java or the Pthreads multithreaded programming library for doing this assignment. Both Java and the Pthreads library are available on machines in the IT&E lab. 2. On Solaris, information on how a thread library call is to be invoked is provided by the corresponding man page, e.g., to see how pthread create is invoked type man pthread create. Additional docu- mentation on multi-threaded programming using the Pthreads library is also available on the Solaris documentation web site (see link on class web page). 3. If you are using Solaris, use the system call drand48() to determine the direction of the vehicle (accord- ing to the probability distribution specified above). The random number stream should be initialized by calling srand48() with the desired seed. If you are using Java for this assignment, use the methods in class java.util.Random to create and use the random number stream that determines the direction of a vehicle. 2
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved