Project 3 - Jurassic Park
Project 3 - Jurassic Park
Project 3 - Jurassic Park
Purpose
Contemporary operating systems are built around the concept of processes or tasks. Tasks may or may not
be related, may or may not affect the state of another task, but usually always need to share resources in a
protected, prioritized, and equitable manner.
Jurassic Park is an inter-process communication and synchronization problem between multiple tasks.
Visitors to the park want to first get a ticket, visit the park museum, take a ride on a park tour car to see the
dinosaurs, and then visit the gift shop before leaving the park. Since the park is on a limited budget, drivers
must perform double duty when not sleeping; selling tickets to the park visitors and driving the tour cars
through the park. Visitors, drivers, and cars are represents by concurrent tasks while an additional task is
used to display the park status every second.
Attempting to coordinate the park activities without bringing about any race conditions is typical of many
queuing problems. A poorly implemented solution could lead to the inter-process communication problems
of starvation and deadlock. For example, the driver could end up waiting to sell a ticket to a visitor while a
loaded tour car could be waiting for a driver, resulting in deadlock. Alternatively, visitors may not decide to
take a tour car ride in an orderly manner, leading to process starvation as some visitors never get the chance
for a ride even though they have been waiting.
Jurassic Park is not unlike real world simulation and control problems such as found in airports, supply
houses, traffic systems, oil tank farms, etc. Each requires multitasking roles involving inter-process
coordination and communication.
Project Description
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. After successfully obtaining a ticket from a driver, the visitor gets in the museum line. (Again, a
limited number of visitors are allowed in the museum as well as the gift shop.) 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. When the touring car is filled with visitors and a driver is obtained, the car enters
Jurassic Park and follows a guided tour path through the park. When the tour car completes the ride and
pulls into the unloading station, the visitors depart and get into the gift shop line, the driver goes to sleep
awaiting new duties, and the tour car pulls forward to be loaded again. After visiting the gift shop, the
visitors exit the park.
Like the classical dining philosophers and sleeping barber problems, the Jurassic Park exercise shows what
problems can occur in a multitasking multi-user environment, when several tasks try to use the same
resources. For example, in practicality, you can think of the visitors as programs which want to print. The
tour cars and drivers are print spooler resources which have a finite number of slots for printing jobs (limited
memory). If they have nothing to do, they block. As print jobs arrive, print spooler tasks are awakened and
work until all slots are free.
1) Use the arguments to the project3 command to optionally specify the number of park visitors.
(The default is 45 visitors.)
2) Add a delta clock to your operating system. The delta clock ticks in tenth-of-a-second increments.
(See Delta Clock.)
3) Create a task for each park visitor (NUM_VISITORS), driver (NUM_DRIVERS), and tour car
(NUM_CARS). These tasks should all run at the same priority level.
4) Update the park data structure variables appropriately as visitor, driver, and car states change. The
park is displayed using the park data struct every second by the jurassicTask task.
5) Each task (visitor, driver, and car) should create its own timing semaphore, which is used for timing
functions (ie, arrival delay, standing in lines, time in gift shop or museum.) The delta clock should
be used to semSignal these semaphores.
6) Park visitors should randomly arrive at the park over a 10 second period. In addition, visitors should
stand in lines for a random time before requesting a ticket or entrance to the museum or gift shop (3
seconds maximum).
7) Use resource semaphores (counting) to control access to the park, the number of tickets available, and
the number of people allowed in the gift shop and museum.
8) Use mutex semaphores (binary) to protect any critical sections of code within your implementation,
such as when updating the delta clock, acquiring a driver to buy a ticket or drive a tour car,
accessing global data, or sampling the state of a semaphore.
9) Use semaphores (binary) to synchronize and communicate events between tasks, such as to awaken a
driver, signal data is valid, signal a mode change, etc.
10) Use at least one semTryLock function in your simulation.
11) The “SWAP” directive should be inserted between every line of code in your Jurassic Park
simulation. Park critical code must be protected by the parkMutex mutex.
12) The park simulation also creates a “lostVisitor” task which sums critical variables in the park to
detect any lost visitors. Beware!
13) Since all project 3 tasks need to be on the same priority level, it is necessary to let the park fully
initialize before proceeding to create your visitor, car, and driver tasks. To that end, execute a
"while (!parkMutex) SWAP;" loop just after the jurassicPark task is created.
14) You are to implement a fair algorithm that prevents deadlock and starvation rather than detect them.
Delta Clock
A delta clock is a mechanism used to improve performance and simplify the management of timed
semaphores. If a linked list (or the equivalent) is used to store pending timed semaphores, then all elements
of the list have to be checked every time there is a clock to see if that semaphore is to be signaled. For a
small number of semaphores, this might be OK, but certainly not a very scalable solution.
A delta clock structure stores pending timed events such that only the first entry needs to be examined (and
updated) when a clock occurs. All other timing events are a function or delta from that first entry.
Even One way to handle this is to create a linked list of events and run through the list on every
5 clock. However, notice that after 5 clocks, the first event to occur is Event2. Event1
t2
Even occurs 15 clocks later, Event6 comes 2 clocks after that, Event4 and Event5 occur 5
15
t1 clocks after Event6, and finally, Event3 happens 8 clocks after that. If we capture this
Even information in a delta clock structure, then only the top entry need be decremented on each
2
t6 clock interval. When it tics down to zero, the event is signaled and the delta stack is
Even
5 popped.
t4
Event
0 The delta clock structure is illustrated on the left.
5
Event
8
3
Inter-process Communication
Inter-Process Communication (IPC) refers to the exchange of data among two or more threads in one or more
processes. Among others, IPC techniques involve synchronization (events and semaphores), shared memory
(globals), message passing (pipes and message queues), sockets, and remote procedure calls (RPC). The
method of IPC used may vary based on the bandwidth and latency of communication between the threads,
and the type of data being communicated. For Jurassic Park, consider using semaphores and shared memory.
Semaphores are considered events and are usually one of the following types:
1. Resource semaphores. Typically counting, these semaphores are used to control access to the park,
the number of tickets available, and the number of people allowed in the gift shop and museum.
2. Mutex semaphores. Typically binary, these semaphores are used to protect any critical sections of
code within your implementation, such as when updating the delta clock, acquiring a driver to buy a
ticket or drive a tour car, accessing global data, or sampling the state of a semaphore.
Shared memory can be implemented using C global memory when protected with mutex semaphores.
Interfacing to “jurassicTask”
The “jurassicTask” task fills tour cars with passengers when a car is in the loading position, moves tour cars
one position forward (if possible) around the park, randomly chooses a route at the route cross roads, and
displays the status of Jurassic Park every second. The display is generated from the following C struct
variables defined in the os345park.h file:
typedef struct
{
int numOutsidePark; // # outside of park
int numInPark; // # in park (P=#)
int numTicketsAvailable; // # left to sell (T=#)
int numRidesTaken; // # of tour rides taken (S=#)
int numExitedPark; // # who have exited the park
int numInTicketLine; // # in ticket line
int numInMuseumLine; // # in museum line
int numInMuseum; // # in museum
int numInCarLine; // # in tour car line
int numInCars; // # in tour cars
int numInGiftLine; // # in gift shop line
int numInGiftShop; // # in gift shop
int drivers[NUM_DRIVERS]; // driver state (-1=T, 0=z, 1=A, 2=B, etc.)
BYU, CS 345 Project Three – Jurassic Park Page 4/8
CAR cars[NUM_CARS]; // cars in park
} JPARK;
Your visitor, driver, and tour car tasks need to update the appropriate variables at the appropriate times. The
following global signal semaphores are defined in “os345park.c”:
For example, to fill a seat in tour car carID, wait for available seat ready (fillSeat[carID]), then find
a visitor from the tour car waiting line, and then signal (seatFilled[carID]) that you are ready for the
next seat.
// wakeup attendant
SEM_SIGNAL(wakeupDriver); SWAP;
...
// got driver (mutex)
SEM_SIGNAL(needDriverMutex); SWAP;
}
Ticket Line
numInTicketLine
Driver Status
park.drivers[ ]
# Tickets Available
numTicketsAvailable
# in Park
numInPark
# Rides Taken
numRidesTaken
# Exited Park
numExitedPark
Your Jurassic Park assignment is to be demonstrated in person to a TA. The assignment is worth 20 points,
which will be awarded as follows:
2) 5 points – Have the project3 command schedule the jurassicTask. Also schedule 4 driver tasks, 4
car tasks, and 45 visitor tasks. All Jurassic Park tasks should execute at the same priority level.
Observe proper behavior as all visitors visit the museum, take a tour car ride, visit the gift shop, and
exit the park. Make sure that a SWAP directive is placed after every C instruction in your Jurassic
Park visitor, car, and driver simulation code (not kernel routines) as well as any non-kernel Delta
Clock code. These context switches will verify that mutual exclusion is properly implemented for a
truly pre-emptive environment.
• +2 points – bonus for early pass-off (at least one day before due date.)
• +1-6 points – bonus for approved changes to os345park.c, such as:
Improved interface to park
Different routes in park
Making dinosaurs individual tasks
Random lost (or consumed) park visitors (must be accounted for)
Gift shop expenditures
Having drivers do periodic maintenance on park ride
Something you think is clever…
• –2 points – penalty for not running visitor, car, and driver tasks at same priority level.
• –2 points – penalty for altering os345park.c without approval.
• –10 points – penalty for not having a SWAP after every C line of code.
• –2 points – penalty for each school day late.