Skip to content

A multi-threaded Java simulation of a Car Wash and Gas Station implementing the classic Producer-Consumer problem. Features custom Semaphore and Mutex classes to manage shared resources and prevent race conditions, developed for the CS241 Operating Systems 1 course at Cairo University.

License

Notifications You must be signed in to change notification settings

AliRadwan1/Car-Wah-Simulation-Synchronization-Assignment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

37 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš— Car Wash Simulation

Java JavaFX Course License


πŸ“– Overview

This project is a Java 25 simulation of a Car Wash and Gas Station, built to model the Producer–Consumer Problem using custom semaphores and mutexes.
It demonstrates synchronization and safe concurrent access to shared resources in multithreaded environments.

  • Cars (Producers) continuously arrive seeking service.
  • Pumps (Consumers) represent service bays working in parallel.
  • Synchronization ensures cars wait when the queue is full and pumps operate only when cars are available.

🧩 Repository Structure

Car-Wash-Simulation/
β”‚
β”œβ”€β”€ GUI/
β”‚   └── ServiceStation.java        # JavaFX 25 GUI version with animations
β”‚
β”œβ”€β”€ GUI_TXT/
β”‚   └── ServiceStation.java        # JavaFX 25 GUI version (text-based, no animations)
β”‚
β”œβ”€β”€ src/
β”‚   └── ServiceStation.java        # Console version (terminal-based)
β”‚
β”œβ”€β”€ .gitignore
└── README.md

🧠 Note:
Each version includes only one file named ServiceStation.java (as required by the assignment).
All synchronization logic (Semaphores, Cars, Pumps) is contained in that file.


🧠 Concept & Objective

The project applies Operating-Systems synchronization principlesβ€”particularly the Bounded Buffer patternβ€”to simulate limited waiting space and concurrent servicing.

It ensures:

  • Cars cannot enter a full waiting area.
  • Pumps only serve cars when available.
  • Only one thread modifies the queue at a time.
  • The number of serviced cars never exceeds the number of pumps.

βš™οΈ Implementation Details

🧱 ServiceStation (Main Controller)

Initializes shared resources (Queue, Mutex, Empty, Full, Pumps semaphores), reads user input, and launches producer and consumer threads.

πŸš— Car (Producer)

  • Represents an arriving car.
  • Waits if the queue is full (Empty.wait()).
  • Adds itself safely under mutex protection.
  • Signals a pump that a car is available (Full.signal()).

β›½ Pump (Consumer)

  • Waits for available cars (Full.wait()) and bays (Pumps.wait()).
  • Removes a car, services it, and releases the bay.

πŸ”’ Semaphore

Custom counting semaphore with wait() (P) and signal() (V), preventing race conditions and ensuring proper synchronization.


πŸ–₯️ Versions & Execution

🧾 Console Version β€” src/ServiceStation.java

Run from terminal:

cd src
javac ServiceStation.java
java ServiceStation

Example Input

Enter waiting area capacity: 5
Enter number of service bays: 3

Example Output

C1 arrived
C2 arrived
C3 arrived
Pump 1: C1 occupied
Pump 2: C2 occupied
Pump 3: C3 occupied
C4 arrived and waiting
C5 arrived and waiting
Pump 1: C1 finishes service
Pump 1: Bay 1 is now free
Pump 1: C4 begins service
Pump 2: C5 begins service
All cars processed; simulation ends

🎨 GUI Version β€” GUI/ServiceStation.java

  • JavaFX 25 animated version showing cars moving between waiting areas and pumps.
  • Real-time visual updates for car movement and pump usage.

Run

  1. Open the folder in VS Code or IntelliJ with JavaFX configured.
  2. Run ServiceStation.java inside the GUI folder.

πŸ’¬ Text-Based GUI Version β€” GUI_TXT/ServiceStation.java

  • Simplified JavaFX 25 version with textual logs only (no animations).
  • Useful for lightweight visualization.

Run

  1. Open in your IDE with JavaFX configured.
  2. Run ServiceStation.java inside the GUI_TXT folder.

🧭 Getting Started β€” JavaFX 25 Setup in VS Code

πŸͺœ Step 1: Install Required Extensions

  1. Open VS Code.
  2. Go to Extensions (Ctrl + Shift + X).
  3. Install:
    • βœ… Extension Pack for Java (Microsoft)
    • βœ… Optional: JavaFX Support

πŸͺœ Step 2: Download JavaFX 25 SDK

  1. Visit https://openjfx.io.
  2. Download JavaFX 25 SDK for your OS.
  3. Extract it, e.g.:
    C:\javafx-sdk-25\
    

πŸͺœ Step 3: Configure VS Code Launch Settings

Add VM arguments to include JavaFX libraries.

For Windows

--module-path "C:\javafx-sdk-25\lib" --add-modules javafx.controls,javafx.fxml

For macOS/Linux

--module-path "/path/to/javafx-sdk-25/lib" --add-modules javafx.controls,javafx.fxml

Example .vscode/launch.json

{
  "configurations": [
    {
      "type": "java",
      "name": "Run GUI (JavaFX 25)",
      "request": "launch",
      "mainClass": "ServiceStation",
      "vmArgs": "--module-path \"C:\\javafx-sdk-25\\lib\" --add-modules javafx.controls,javafx.fxml"
    }
  ]
}

πŸͺœ Step 4: Run

  • Open GUI/ServiceStation.java or GUI_TXT/ServiceStation.java.
  • Click Run ▢️ in VS Code.
  • The JavaFX 25 window will launch showing the simulation.

🧰 Technologies Used

  • Java 25
  • JavaFX 25
  • Multithreading & Concurrency
  • Custom Semaphores & Mutexes
  • Producer–Consumer (Bounded Buffer)

πŸ§‘β€πŸ’» Contributors

Name Student ID
Ali Radwan Farouk 20231110
Adel Hefny 20230198
Mohamed Mahmoud 20230354
Asser Ahmed 20230655

Section: AI S5
Course: CS241 – Operating Systems 1
Faculty: Computers and Artificial Intelligence, Cairo University


🏁 Acknowledgements

Developed for CS241 Operating Systems (Synchronization Assignment) under TA Mena Asfour.
Demonstrates thread synchronization and bounded-buffer concurrency using Java 25 and JavaFX 25.

About

A multi-threaded Java simulation of a Car Wash and Gas Station implementing the classic Producer-Consumer problem. Features custom Semaphore and Mutex classes to manage shared resources and prevent race conditions, developed for the CS241 Operating Systems 1 course at Cairo University.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages