A multi process application that simulates a commercial port.
Open a terminal in the parent directory and type:
$ make
This will call all three makefiles in directories myport, port-master and vessel.
You can also use:
$ make clean
This removes all executable and object files.
Open a terminal in the parent directory and type:
$./myport/myport -l myport/configfile
This process is the "anchor" of the port and is responsible for the following tasks:
- Checks given arguments from the tty and reads data within the given configuration file. Three types of vessels (LARGE, MEDIUM, SMALL) must be present in the file.
- Opens the logFile and records a part of its implementation.
- Allocates memory for the port corresponding to the data presented within the configuration file and initializes the
sharedMemory_t
type struct. - Creates the shared memory segment and loads it with the
sharedMemory_t
type struct. forks
the child process port-master and hands over the shared memory key.- Awaits the termination of port-master process.
- Destroys the created semaphores and detaches the shared memory from its address space.
- Marks the shared memory segment for destruction. The segment will actually get destroyed when the last child process detaches the shared memory from its address space.
- Frees any allocated memory.
The port-master process is responsible for the following tasks:
-
Checks its given argument which is the key to the shared memory.
-
Initiliazes the signal handler for
SIGUSR1 (10)
andSIGUSR2 (12)
. -
Opens logFile for appending.
-
Attaches the shared memory to its address space.
-
Initializes the semaphores to be used.
-
Creates the child process vessel-creator.
-
Awaits for ships using
semA
and serves them according to the following occurences: - Ships that just reached the approach lane. - Ships that are already in the port and want to leave. - Ships that tried to port but there wasn't any free space at the time.Ships that want to leave are given priority. The ship departure is achieved using
semA
,semD
andsemE
. The port-master commands the exit maneuver and deletes the ship's data from the shared memory. Ships that are waiting at the approach lane are checked next. The semaphores used aresemA
andsemC
. If the approaching ship can port (namely there is a free space) the port-master allows it to proceed. If there isn't any free space the port-master calculates how much time the ship must wait in the approach lane until it can ask to port again.Open a terminal and type:
$kill -12 port-master-pid $kill -10 port-master-pid vessel-creator-pid
The
port-master-pid
andvessel-creator-pid
can be found in the tty while the application is running. When the first command is executed the port-master process changes "speed" using thesleep
system calls. Speeds of 0.5, 1, 2, 3 seconds are supported. The second command is used to flag the port-master and the vessel-creator processes for termination. When the port-master receives theSIGUSR1
it completes the last entering maneuver and prevents other ships from entering. Then it waits for ships to complete their tasks. -
When the last vessel exits the port and after the vessel-creator process terminates the port-master detaches the shared memory and frees any allocated memory.
The vessel-creator process is port-master's child and is responsible for random vessel creation at regular intervals (2 seconds).
The vessel process:
- Checks its arguments and attaches the shared memory at its address space.
- Using the
semB
awaits for either docking or departing from the port. Each time a vessel locks thesemB
semaphore it writes its data (type of vessel, mooring position to pay, mooring duration and maneuver duration) in the shared memory. - Waits for port-master's "OK" to start its entering maneuver or goes back to the approach lane and waits for a specified time.
- When a vessel finishes its tasks within the port it exits the port by using semaphores
semA
,semD
andsemE
and detaches the shared memory from its address space. - Frees any allocated memory and terminates.
The vessel-creator process stops generating vessels when the SIGUSR1
signal is received. At this point it waits for its children (vessels) to terminate and detaches the shared memory. Lastly it frees any allocated memory and terminates.
The application was checked for memory leaks using the tool Valgrind and is completely free from such errors. To run the application using Valgrind open a terminal in the parent directory and type:
$valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --trace-children=yes ./myport/./myport -l myport/configfile
To stop the application use:
$kill -10 port-master-pid vessel-creator-pid
After termination the application was also checked for correct shared memory deallocation using:
$ipcs -mi 5668888
Where 5668888 is the shared memory id. The command's result was:
ipcs: id 5668888 not found.