Docsity

Lab 2 : The Elevator Lab - Computer Operating Systems | CS 481, Lab Reports of Operating Systems

Toggle sidebar
bg1
1
THE UNIVERSITY of
NEW MEXICO
Lab 2:
The Elevator Lab
Prof. Dorian Arnold
CS 481: Operating System Principles
Spring ‘09
2
Lab 2 Concepts
• (P)thread creation and synchronization
– pthread_mutex_lock()
– pthread_mutex_unlock()
– pthread_cond_wait()
– pthread_cond_signal()
3
Helper Files/Programs
• makefile: a makefile to build the various lab components
• elevator.h: important procedure and structure definitions
• elevator_skeleton.c: the driver program and procedures you
do not need to implement
• elevator_null.c: a blank solution that compiles
• reorder.c: Re-orders the simulation output events in ascending
time
• double-check.c: Verifies that your simulation ran correctly
– e.g. people get out on their destination floors with door open.
– You should reorder your output then validate it.
• dllist.h and dllist.c: Plank's doubly-linked list
implementation.
• jval.h and jval.c: Plank's generic data type implementation.
4
Additional Files
• fields.h and fields.c:
– Plank's library to simplify input processing
• jrb.h and jrb.c:
– Plank's red-black tree implementation
• names.h:
– header file used to generate random people names
• finesleep.h and finesleep.c:
– Plank's procedures for sub-second sleeping.
pf3
pf4

Partial preview of the text

Download Lab 2 : The Elevator Lab - Computer Operating Systems | CS 481 and more Lab Reports Operating Systems in PDF only on Docsity!

THE UNIVERSITY

of

NEW MEXICO^ Lab 2:The Elevator Lab^ Prof. Dorian ArnoldCS 481: Operating System PrinciplesSpring ‘

2

Lab 2 Concepts

• (P)thread creation and synchronization^ – pthread_mutex_lock()– pthread_mutex_unlock()– pthread_cond_wait()– pthread_cond_signal()

3

Helper Files/Programs • makefile: a makefile to build the various lab components • elevator.h: important procedure and structure definitions • elevator_skeleton.c

: the driver program and procedures you

do not need to implement • elevator_null.c

: a blank solution that compiles

-^ reorder.c

: Re-orders the simulation output events in ascendingtime

-^ double-check.c

: Verifies that your simulation ran correctly

  • e.g. people get out on their destination floors with door open.– You should reorder your output then validate it. • dllist.h^ and

dllist.c

:^ Plank's^

doubly-linked^

list

implementation. • jval.h^ and^ jval.c

: Plank's generic data type implementation.

4

Additional Files

• fields.h

and^ fields.c

– Plank's library to simplify input processing • jrb.h^ and^ jrb.c

– Plank's red-black tree implementation • names.h: – header file used to generate random people names • finesleep.h

and^ finesleep.c

– Plank's procedures for sub-second sleeping.

5

The Basics

-^ Prof. Jim Plank’s elevator simulation framework: >^ elevator^ nfloors

nelevators interarrival

opentime floor_to_floor

duration^ seed

-^ nfloors : Number of floors in the building•^ ( int^ greater than one). –^ nelevators : Number of elevators in the building•^ ( int^ greater than zero). –^ interarrival : Average interarrival time in seconds of people•^ ( double^ greater than zero). –^ opentime : Time in seconds for an elevator to open or close its doors•^ ( double^ greater than zero). –^ floor_to_floor

: Time in seconds for an elevator to to an adjacent floor• ( double greater than zero).

-^ duration : Time in seconds to simulate•^ ( double^ greater than zero). –^ seed : Seed for random number generator•^ ( int )

6

The Basics (cont’d) • Each elevator is a thread – elevator starts on floor 1 with door closed– can hold an infinite amount of people– door must be closed for elevator to move– door must be opened for people to get on • Each person is a thread – generated according to interarrival ratest^ • 1/3 from 1to random floor• 1/3 from random floor to 1

st

  • 1/3 from random floor to random floor

7

Elevator Simulation Struct typedef struct {int nfloors;int nelevators;double interarrival_time;

//people inter-arrival time double door_time;

//time to open/close door double floor_to_floor_time;

//time to go to adjacent floor int npeople_started;

// Stats int npeople_finished;pthread_mutex_t *lock;void *v;^

// For you to define

} Elevator_Simulation;^ One instance of this struct for the entire simulation

8

Elevator Struct

typedef struct {int id;^

/*^ Elevator id */ int onfloor;^

/*^ elevator’s current floor */ int door_open;

/*^ Whether the door is open */ int moving;^

/*^ Whether the elevator is moving */ Dllist people;

/*^ Dllist of people on the elevator */

pthread_mutex_t *lock;pthread_cond_t *cond;void *v;Elevator_Simulation *es;} Elevator;^ One instance of this struct for the elevator

13

Part 1: The Anti-Social Elevator • Global queue of people^ – insert person into queue upon generation • Elevators remove person from queue^ – service 1 person at a time

14

Part 1 Implementation • void initialize_simulation(Elevator_Simulation

*es):

-^ set up the global person list and a condition variable for blocking elevators • void wait_for_elevator(Person

*p):

-^ append the person to the global list–^ signal elevators that people are ready–^ block for elevator on the person's condition variable • void wait_to_get_off_elevator(Person *p)

-^ notify elevator that you are on it: signal your elevator's condition variable–^ wait for elevator to arrive: block on the person's condition variable • void person_done(Person

*p):

-^ Unblock the elevator's condition variable. • void *elevator(void *arg)

-^ check the global list;^ •^ if empty, block on condition variable for blocking elevators•^ if not empty:–^ take first person of global person’s list; put self in person’s “elevator” field–^ move to person’s floor–^ open door–^ notiyf person to get on and wait until the person is on–^ move to person's destination floor–^ opens door, notify person to get off–^ wait for person to get off–^ close door

15

Part 2: A Smarter Elevator • Elevators move floor by floor from bottomto top and back again• At each floor, elevator unloads/loads asappropriate• Multiple elevators

going in the same

direction^ should not be loading on thesame floor at the same time

16

Part 3: Bonus Exercise • Optionally implement even betteralgorithm• Top 5 scores get 25, 20, 15, 10 and 5bonus points Test scripts for Parts 1 and 2 availablefrom the lab 2 website!