Academic Year 2022/2023
1 Introduction The project simulates the traffic of cargo ships for transporting goods divided into lots. Ships perform loading and unloading operations in ports distributed on a map of SO_LATO^2. The main objective is to delve into the use of the C language and how it can be exploited in the UNIX environment, enhancing knowledge related to this OS.
2 Master The Master is responsible for creating and managing the program simulation. It handles:
- Declaration and initialization of variables and populating them (Map, Ports, Ships, Lots)
- Creation and management of processes (Port, Ship)
- Simulation state printing (Dump)
- Creation and management of shared memory
- Process state tracking and global execution time
- Updating goods lifespan as time progresses
- Simulation start and termination
3 Port Process The port process has demand and supply of goods, varying by type. There is a port at each corner of the map; the rest, if present, are randomly distributed. Each port has a number of docks (SO_DOCKS), within which loading and unloading operations are managed. To perform these operations with as much parallelism as possible, threads are used, managed by a semaphore, limiting the execution to at most SO_DOCKS threads simultaneously. The total number of ports is defined by SO_PORTS. Ship docking and undocking are managed through inter-process communication (IPC) and the use of semaphores.
4 Ship Process The ship process transports various goods, meeting the demands of the ports. Ships (SO_SHIPS) are born without cargo and are randomly distributed on the map. They have all the information about the ports, and during cargo loading, a random port satisfying specific conditions, such as offering available goods, is chosen. During unloading, they choose the nearest port accepting the type of goods on board, minimizing wasted goods. Once the destination port is identified, the ship, after completing the journey, communicates its arrival via inter-process communication (IPC). It waits until the port signals the end of the operation, after which it resumes its journey. Ships do not perform loading operations until all onboard goods are completely unloaded or expired.
5 Goods Goods are divided into lots, generated at the ports, each with a type randomly chosen from SO_GOODS different types, a quantity (SO_SIZE), and a lifespan expressed in days. Additionally, each lot has a status indicating where the goods are located (ship or port) and whether they have expired or not.
6 Dump Dump prints the simulation state before, during, and at the end. Prints during the simulation occur every second, equivalent to one day of simulation. The report is generated in a .txt file for each simulation.
7 Conclusion For the project development, modular code techniques were adopted, creating the datalib.h and merce.h libraries. To achieve maximum parallelism among processes, execve, fork, and threads were utilized. Process start and termination for ships and ports were performed through signals. The 'make' utility and a bash file were used to start the simulation.
8 References Course Material stackoverflow.com "The C Programming Language: Fundamentals and Techniques" (Paul Deitel, Harvey Deitel) ChatGPT cplusplus.com (C library)