This homework is due on Tuesday, March 10, at 11:59:59 PM (Eastern daylight time). You must use the submit to turn in your homework like so: submit cs421_jtang hw3 hw3.c
Your program must be named hw3.c, and it will be compiled on Ubuntu 14.04 as follows:
gcc --std=c99 -Wall -O2 -pthread -o hw3 hw3.cThere must not be any compilation warnings in your submission; warnings will result in grading penalties. In addition, your code must be properly indented and have a file header comment, as described on the coding conventions page.
In this homework, you are a programmer for the UberLyftCar ride-sharing company. In town are four hotels popular with college students on Spring Break. Students are summoning rides to take them from one hotel to another; it is your responsibility to efficiently process the ride requests.
Your program will simulate a day of driving passengers between the four hotels. These jobs are stored in a data file that your program reads and parses. Each job has four components: the source hotel, the destination hotel, number of passengers requesting travel, and the amount of time spent traveling between the hotels. As long as there are enough people at the source hotel, the job may be taken. When the job is taken, subtract from the source hotel the number of passengers. After spending time driving, increment the destination hotel by that number of passengers.
To get started, download the stub file hw3-stub.c. Do this by opening a Terminal within your Ubuntu VM, and then entering this command:
wget http://www.csee.umbc.edu/~jtang/cs421.s15/homework/hw3-stub.c
In addition, there are two data files associated with this assignment: spring_break1.dat and spring_break2.dat. Use the wget command to fetch them as well.
The UberLyftCar company has a fleet of cars to handle all of these jobs. Your program takes two command-line argument. The first argument gives the name of the data file to use. The second argument gives the number of cars at your disposal. Your program spawns a number of threads for each car. Each thread handles each job independently.
When a job begins, subtract the requested number of people from the
source hotel. Simulate travel time by having your thread sleep (as
per sleep()
function) for a number of seconds equal to
the travel time. Afterwards, increment the destination hotel by the
number of people.
As proof that your program has correctly handled all jobs, it must display how many people are at each hotel at the end of the day.
Here is a sample outputs from running the program, against spring_break1.dat and with 3 threads. This program displays the jobs assigned to each car.
$ ./hw3 spring_break1.dat 3 Car 0: Driving 5 from hotel 1 to 0 (6 seconds) Car 1: Driving 7 from hotel 2 to 3 (3 seconds) Car 2: Driving 8 from hotel 0 to 2 (9 seconds) Car 0: Driving 3 from hotel 1 to 3 (4 seconds) Car 1: Driving 2 from hotel 3 to 1 (3 seconds) Car 2: Driving 9 from hotel 3 to 2 (6 seconds) Car 1: Driving 1 from hotel 0 to 1 (2 seconds) Car 0: Driving 4 from hotel 3 to 1 (7 seconds) Car 0: Driving 2 from hotel 2 to 0 (6 seconds) Car 1: Driving 4 from hotel 1 to 3 (7 seconds) Car 2: Driving 3 from hotel 2 to 0 (5 seconds) End of day check: Hotel 0: 11 1: 5 2: 15 3: 9
For spring_break2.dat and 4 threads, a simple threading implementation will complete within 22 seconds. You may earn an additional 25% on this homework if your program can consistently complete under 22 seconds. Furthermore, the fastest submission for the entire class will gain another 25% (for a total of 50%).
Each students' submission will be timed via the time command, like so:
$ time ./hw3 spring_break2.dat 4 ... End of day check: Hotel 0: 10 1: 17 2: 3 3: 10 real 0m22.003s user 0m9.004s sys 0m0.000s
Your implementation may not cheat, including but not limited to:
#define sleep usleep