Programming Assignment IV
Due on Friday, April 4, 2014 @ 11pm
100 points
This is a ThreadMentor
programming assignment, and you can only use semaphores and locks.
The use of any other system and/or synchronization primitives is unacceptable.
Party Room Problem
A landlord has a big apartment with a fantastic social room.
Many students stay in this apartment.
Since the neighbors frequently complained about party noise,
the landlord came and checked his apartment frequently
to ensure his fantastic social room is being used properly,
and break up the party if there are too many students in the room.
Here is a list of rules:
- Any number of students can be in this room at the same time.
- The landlord enters this room if there are more than
n students in the room (to break up the party),
where n is an input.
- If there are no students or there are no more than n
students in the room, the landlord leaves and comes back later.
- While the landlord is in the room, no additional students may enter,
but students may leave.
Therefore, students may leave the room at any time.
- The landlord may not leave the room until all students have left
(party is over).
- There is only one landlord!
The landlord is a thread with the following pattern.
The landlord only checks the room m times
and retires
because he feels it is too tedious to handle this situation.
After m iterations, he sends all students home,
sells his apartment, and goes to Bahama for a long long vacation.
for (i = 0; i < m; i++) { // m is an input value
Delay(); // take some rest
CheckRoom(...); // check the social room
Delay(); // take some rest
}
CheckRoom() is a function
for the landlord to check the room based on the above rules.
Each student is also a thread:
while (1) {
Delay(); // study for a while
GoToParty(...); // go to the party
Delay(); // well, everyone needs some sleep
}
Your job is to fill all the needed synchronization and other activities
into two functions CheckRoom()
and GoToParty().
Your implementation must satisfy
the stated conditions.
Otherwise, your implementation is not a correct one.
Write a C++ program using
ThreadMentor
to simulate these activities.
Note that you can only use mutex locks and semaphores.
Your program will not be counted as
a correct one if any other synchronization primitives are used.
Input and Output
The input of your program consists of the following:
- There are three command line arguments:
- m: the number of iterations
- n: the max number of students in the room
without being broken up by the landlord
- k: the total number of students in the apartment
Note that k must be equal to or larger than
n.
- These three command line arguments are supplied to your program
as follows:
prog4 m n k
Thus,
prog4 7 9 20 means
the landlord checks the room 7 times, if there are
more than 9 students in the room the landlord breaks up the party,
and there are 20 students in his apartment.
If m is 0, the default value is 5;
if n is 0, the default value is 5; and if
k is 0, the default value is 10.
For example,
prog4 0 6 0
means that the landlord checks the room 5 times,
if there are more than 6 students in the room the landlord
breaks up the party,
and there are 10 students in total.
- You many assume all command line arguments being non-negative and
are no more than 30.
Furthermore, you may assume that n is always less than
or equal to k.
- Your program should generate an output similar to the following:
Student 5 starts
.....
Landlord thread starts
.....
Landlord checks the room the YY-th time (XX students in the party) // XX is the number of students currently in the room
Landlord leaves because the room is empty // there should be no messages between these two messages
.....
Student 3 starts
.....
Student 4 joins the party
.....
Student 4 enters the room (XX students in the party) // XX is the number of students currently in the room
.....
Landlord checks the room the YY-th time (XX students in the party) // YY is the iteration number
.....
Student 2 leaves the room (XX students in the party)
Landlord enters and breaks up the party due to too many students
.....
Student 7 leaves the room (XX students in the party)
.....
Student 8 joins the party // because the landlord is checking room, he cannot enter
Landlord finishes checking room and leaves. // room should be empty now
.....
Student 2 joins the party
.....
Student 8 enters the room (XX students in the party) // Now he can enter because the landlord leaves
.....
Landlord checks the room the YY-th time (XX students in the party) // XX is the number of students currently in the room
Landlord leaves because condition is good // there should be no messages between these two messages
.....
After checking the room XX times, the landlord retires and is on vacation!
Due to the dynamic behavior of multithreaded programs, you will not
be able to generate an output that is exactly identical to the above.
- All messages from the landlord start on column 1; all messages
generated by students start on column 6. Without observing
this rule, you may risk lower grade.
- The output below illustrates that student 4 makes a decision to
join the party, and then enters the room.
This student shows his intention (join the party), and
enters the room if the landlord is not checking.
Student 4 shows the number of students currently in the party.
Note that the first student entering the room after the landlord finishes
checking must report 1 because that student is the first
one enters the room.
Student 4 joins the party
.....
Student 4 enters the room (XX students in the party) // XX is the number of students currently in the room
- The following illustrates the interaction between the landlord
and student 7 and student 8.
Student 7 leaves the room while the landlord is checking,
and reports the remaining number of students in the room.
Student 8 joins the party while the landlord is checking room.
As a result, student 8 cannot enter the room, and must wait until
the landlord finishes checking and leaves.
Landlord enters and breaks up the party due to too many students
.....
Student 7 leaves the room (XX students in the party)
.....
Student 8 joins the party // because the landlord is checking room, he cannot enter
Landlord finishes checking room and leaves. // room should be empty now
.....
Student 8 enters the room (XX students in the party) // Now he can enter because the landlord leaves
- If the room is empty, the landlord leaves immediately.
The two lines of message must be displayed together.
Landlord checks the room the YY-th time (XX students in the party) // XX is the number of students currently in the room
Landlord leaves because the room is empty // there should be no messages between these two messages
- If there are no more than n students in the room, the landlord leaves immediately.
The two lines of message must be displayed together.
Landlord checks the room the YY-th time (XX students in the party) // XX is the number of students currently in the room
Landlord leaves because condition is good // there should be no messages between these two messages
- After a specific number of room checking, the landlord retires,
and the whole system ends.
Make sure that this is the
last. message your program
will print.
After checking the room XX times, the landlord retires and is on vacation!
Submission Guidelines
General Rules
- All programs must be written in C++.
- Use the submit command to
submit your work. You can submit as many times as you want, but
only the last on-time one will be graded.
- Unix filename is case sensitive,
THREAD.cpp,
Thread.CPP,
thread.CPP, etc
are not the same.
- We will use the following approach to compile and test your
programs:
make <-- make your program
prog4 8 4 20 <-- test your program
This procedure may be repeated a number of times with different
input files to see if your program works correctly.
- Your implementation should fulfill the program specification
as stated. Any deviation from the specification will cause you to
receive zero point.
- A README file is always required.
- No late submission will be graded.
-
Programs submitted to wrong class and/or wrong section
will not be graded.
Compiling and Running Your Programs
This is about the way of compiling and running your program.
If we cannot compile your program due to syntax errors, wrong file names, etc,
we cannot test your program,
and, as a result, you receive 0 point.
If your program compiles successfully but fails to run,
we cannot test your program,
and, again, you receive 0 point.
Therefore, before submitting your work,
make sure your program can compile and run properly.
-
Not-compile programs receive 0 point.
By not-compile, I mean any reason that could cause an
unsuccessful compilation, including missing files, incorrect
filenames, syntax errors in your programs,
and so on. Double check your files before you submit, since I
will not
change your program.
Note again: Unix filenames are
case sensitive.
-
Compile-but-not-run programs receive 0 point.
Compile-but-not-run usually means you have attempted
to solve the problem to some degree but you failed to
make it working properly.
- A meaningless or vague program receives
0 point even though it compiles successfully.
This usually means your program does not solve the problem but
serves as a placeholder or template just making it to compile and run.
Program Style and Documentation
This section is about program style and documentation.
- For each file, the first piece should be a program
header to identify yourself like this:
// -----------------------------------------------------------
// NAME : John Smith User ID: xxxxxxxx
// DUE DATE : mm/dd/yyyy
// PROGRAM ASSIGNMENT #
// FILE NAME : xxxx.yyyy.zzzz (your unix file name)
// PROGRAM PURPOSE :
// A couple of lines describing your program briefly
// -----------------------------------------------------------
Here, User ID is the one you
use to login. It is not your social security number nor your M number.
For each function in your program, include a simple description
like this:
// -----------------------------------------------------------
// FUNCTION xxyyzz : (function name)
// the purpose of this function
// PARAMETER USAGE :
// a list of all parameters and their meaning
// FUNCTION CALLED :
// a list of functions that are called by this one
// -----------------------------------------------------------
- Your programs must contain enough concise and to-the-point comments.
Do not write a novel!
- Your program should have good indentation.
- Do not use global variables!
Program Specification
Your program must follow exactly the requirements of this programming assignment.
Otherwise, you receive 0 point even though your program runs and produces
correct output.
The following is a list of potential problems.
- Your program does not use the indicated algorithms/methods to solve
this problem.
- Your program does not follow the structure given in the specification.
For example, your program is not divided into functions and files, etc
when the specification says you should.
- Any other significant violation of the given program specification.
- Incorrect output format.
This will cost you some points depending on how serious the violations are.
The grader will make a decision.
Hence, carefully check your program output against the required one.
-
Your program does not achieve the goal of maximum parallelism.
Program Correctness
If your program compiles and runs, we will check its correctness.
We normally run your program with two sets of input data,
one posted on this programming assignment page (the public one)
and the other prepared by the grader (the private one).
You program must deliver correct results for both data sets.
Depending on the seriousness of the problem(s), significant deduction
may be applied.
For example, if your program delivers all wrong results for the public data set,
you receive 0 point for that component.
The README File
A file named README is required
to answer the following questions:
- Question:
How did you make sure that no students can enter while the landlord
is in the room and checking?
Explain your approach in details.
- Question:
How did you make sure that the landlord will not leave until all students
have left the room?
Explain your approach in details.
- Question:
How did you make sure the message
"After checking the room XX times, the landlord retires and is on vacation!" is the last message printed by your program?
You should elaborate your answer and provide details.
When answering the above questions, make sure
each answer starts with a new line and have the question number
(e.g., Question X:) clearly shown.
Separate two answers with a blank line.
Note that the file name has to be
README rather than
readme or
Readme.
Note also that there is no
filename extension, which means filename such as
README.TXT
is NOT acceptable.
README must
be a plain text file. We do not accept files produced
by any word processor. Moreover, watch for very long lines.
More precisely, limit the length of each line to no more than
80 characters with the
Return/Enter key
for line separation.
Missing this file, submitting
non-text file, file with long lines, or providing
incorrect and/or vague answers will cost you many points.
Suggestion:
Use a Unix text editor to prepare your
README
rather than a word processor.
Final Notes
- Your submission should include the following files:
- File thread.h
that contains all class definitions of your threads.
- File thread.cpp
contains all class implementations of your threads.
- File thread-support.cpp
contains all supporting functions such as
CheckRoom(),
GoToParty(),
other functions designed and implemented by you as needed.
- File thread-main.cpp
contains the main program.
- File Makefile
is a makefile that compiles the above three files
to an executable file
prog4.
Since we may use any lab machine
to grade your programs, your makefile should make
sure all paths are correct. Note also that
without following this file structure your program
is likely to fall into the compile-but-not-run
category, and, as a result, you may get a low grade.
Before submission, check if you have the
proper file structure and correct makefile.
Note that your Makefile
should not activate
the visualization system.
- The README file.
Note also that without following this file structure your
program is likely to fall into the compile-but-not-run
category, and, as a result, you may get low grade.
Therefore, before submission, check if you have the
proper file structure and a correct makefile.
-
Always start early, because I will not grant any extension if
your home machine, network connection, your phone line or the
department machines crash in the last minute.
- Since the rules are all clearly stated,
no leniency will be given and none of the above conditions
is negotiable. So, if you have anything in doubt,
please ask for clarification.
-
Click here to see how your
program will be graded.