Deadline: |
Hand in by 5pm on Friday 3rd May 2024 |
Evaluation: |
20 marks – which is 20% of your final grade |
Late Submission: |
1 mark off per day late |
Work: |
This assignment is to be done individually – your submission may be checked for plagiarism against other assignments and against Internet repositories. If you adapt material from the internet you must acknowledge your source. |
Purpose: |
Solving a nontrivial synchronisation problem. |
An incomplete C implementation of a lift simulator is available (for both Windows and UNIX) on the course stream site (a2_handout.c). This program simulates passengers moving between different floors in a building using lifts. Each person and each lift are controlled by a different thread.
Currently there are some missing sections of code (marked by ---) and variety of race-conditions, you must complete the code then identify and protect against these errors.
Description:
The program will create a thread for each lift and for each person in the building. Lifts go up and down picking people up and taking them to where they want to go.
Each person starts on a certain floor (from) and thinks of a random floor they want to go to (to). Then they wait for a lift going in the correct direction by waiting on a semaphore (up_arrow or down_arrow) associated with the floor they are on (from). When a lift signals this semaphore, the person will get into the lift, possibly press the button for the floor they want to go to and wait for the lift to arrive at their destination floor (to) by waiting on another semaphore. When they reach their destination, they will get out, wait for a while and then start another journey.
There are two semaphores for each floor, one for people going up and one for people going down. In each lift there is a semaphore for each floor. The people in the lift can wait on these before getting in or out of a lift.Each floor in the building is described by the following data structure with two semaphores that people can wait on – up_arrow and down_arrow.
(continued over)
Each lift in the building are described by the following data structure which has one semaphore for each floor that the passengers inside can wait on.
The algorithm that each person should follow is:
The algorithm that each lift should follow is:
(continued over)
You must complete the program by adding semaphores (and semaphore calls) to synchronise between the threads.
The lines marked with a --- need to be completed before the program will compile, most just need wait or signal calls. You will need to use a global variable (of type lift_info*) to tell people which lift to get into.
Once these lines have been completed, the program should work in some form but occasionally the screen will not be drawn properly (parts of the building may be overwritten). This is because the threads are all printing to the screen at the same time.
Add a mutual exclusion semaphore to prevent threads printing at the same time.
Try changing the settings LIFTSPEED, GETINSPEED and GETOUTSPEED to 10. This may work reasonably well for a while but eventually the lifts will stop picking people up and the program may even crash. This is caused by threads accessing the same shared data.
Try changing the settings LIFTSPEED, GETINSPEED and GETOUTSPEED to 0 and PEOPLESPEED to 1.
This will make things go at top speed and make your program even more unreliable.
Add the necessary semaphores to the program to allow it to work reliably at top speed. Think about everywhere the program accesses shared data.
The threads in your assignment should not hold a mutex lock around a Sleep() call (with the possible exception of the print function).
Details:
Take note: you need to pass the address of the semaphores to these functions.
Compilation:
1. Place the following comments at the top of your program code and provide the appropriate information:
Marks will be allocated for correctness, fitness of purpose, sensible use of data structures/algorithms, use of language features and sensible comments. Good comments will help me to award you marks even if your code is not quite perfect.
If you have any questions about this assignment, please ask the lecturer.