PA 8001 2015 Practical 2
Contents
[hide]Objectives
There are three main objectives for this practical:
- Implement the mutual exclusion primitives,
- Use a concurrency library,
- Learn to perform typical I/O operations using PiFace,
- Use all the ingredients to implement a simple access control protocol
Instructions
Submit a single .zip file on blackboard with four folders, each containing the solutions to the below-specified parts.
For each function in the code, you need to write a piece of comment, describing its pre- and post-condition and a few test-cases.
Part 1: I/O on PiFace
In this exercise, we familiarize ourselves with the PiFace Control and Display hardware and its software library: you need both libmcp23s17 and libpifacecad. Use the documentation of the library or the example code on the internet to find your way through the different functionalities and implement the following exercise:
Download both libmcp23s17 and libpifacecad and rename them as libmcp23s17 and libpifacecad. To build the library run the flowing command,
$ cd libmcp23s17/ && make && cd -
$ cd libpifacecad/ && make
Enable the SPI interface
$ sudo raspi-config
You will see a blue screen with options. Select ADVANCED OPTIONS, then 'Enable/Disable SPI interfaces' and YES to the SPI kernel module.
Now you can restart the Raspberry Pi and work on the PiFace!
Part 2: Train Controller
The exercise is about two ticket machines, such as the one used in bakeries for taking turns. Customers may arrive at each of the two ticket machines, they can press a button to take a turn (by which the latest number between the two ticket machines is determined) and after a small delay, they will get a ticket, which is the number from the previous step incremented by one . The tickets should be issued uniquely, i.e., no two customers should get the same number, even if they got their tickets from different machines.
You use the navigation switch to have a customer arrived at ticket machine 1. Ticket machine 2 has customers arriving at a fixed period (3 seconds). Each ticket machine has a delay of 1 second between reading the last ticket from both machines and issuing the next one.
The current tickets of ticket machines 1 and 2 are displayed on the left- and the right-hand-sides of the LCD display of PiFace, respectively. The initial state of both ticket machines indicates number 0. Hence, by pressing right, releasing before 3 seconds, a customer arrives at ticket machine 1 and 1 second later will receive ticket number 1 displayed on the left-most corner of the LCD. Exactly after 3 seconds from the start of the system, a customer will arrive at ticket machine 2 and one second later will get the next available ticket displayed on the left-most corner.
If two customers arrive at the two ticket machines simultaneously, a mutual exclusion mechanism should ensure that they get different tickets.
Use the POSIX Thread (pthread) library to implement concurrency. More information about pthread with some examples can be found here.