45.0k views
4 votes
Write a java program that simulates the CPU Scheduler and creates multiple threads to accomplish specific tasks in the simulation.

Consider a simple system with a single CPU. The system will serve several jobs that all are arrived at time 0. You are required to simulate three different scheduling algorithms:

1. First-Come-First-Serve (FCFS)

2. Shortest-Job-First (SJF)

3. Round-Robin (RR) – quantum = 10ms

Each job has a PCB that contains all required information to identify the job such as id, state, burst time, required memory, required statistics which include turnaround time for each job and waiting time for each job. You can add other fields to PCB to help you in programming.

The program will read process information from a file (job.txt) - this file will be provided by the instructor. File reading should be performed in an independent thread that creates the PCBs and put them in the job queue. Loading the jobs to ready queue should be performed in an independent thread that continuously checks the available space in memory to load the next job from the job queue to the ready queue. You must make sure that jobs can be loaded to ready queue only if enough space for the job is available in memory.

The main thread will perform the scheduling algorithms. Your program should let the user to choose the scheduling algorithm to be applied.

Structure of the input file:

A sample input file of three jobs is given as follows (Process ID: burst time in ms; Memory required in MB):

[Begin of job.txt]

1:25;500

2:13;700

3:20;100

[End of job.txt]

You can assume that

There are no more than 30 jobs in the input file (job.txt).

Processes arrive in the order they are read from the file for FCFS and RR-10.

All jobs arrive at time 0 for SJF.

Main memory is limited to 1024 MB

Context switching time is zero.

Your application must use threads (minimum two).

User OrdoDei
by
8.1k points

1 Answer

2 votes

Final answer:

To simulate the CPU Scheduler in Java, create multiple threads to handle different tasks. Implement First-Come-First-Serve (FCFS), Shortest-Job-First (SJF), and Round-Robin (RR) scheduling algorithms. Update the PCBs with relevant information and consider memory limitations.

Step-by-step explanation:

To simulate the CPU Scheduler in Java, you can create multiple threads to handle different tasks. Start by reading the process information from a file using one thread and creating the Process Control Blocks (PCBs) for each job. Another thread can continuously check the available memory to load jobs from the job queue to the ready queue. The main thread will handle the scheduling algorithms based on the user's choice.

For First-Come-First-Serve (FCFS) scheduling, simply process the jobs in the order they arrived. For Shortest-Job-First (SJF) scheduling, sort the jobs based on their burst time and process the shortest job first. For Round-Robin (RR) scheduling, set a quantum of 10ms and continuously process the jobs in a circular manner.

Throughout the simulation, make sure to update the PCBs with relevant information such as turnaround time and waiting time. Additionally, consider memory limitations and ensure that jobs can only be loaded when there is enough available memory.

You need to write a Java program that simulates a CPU Scheduler, implements three scheduling algorithms, manages jobs with a PCB, and uses threads to handle file reading and queue management.

The question you asked pertains to writing a Java program that simulates a CPU Scheduler using multiple threads. This means that the program will need to implement at least two separate threads: one for reading process information from a file and creating process control blocks (PCBs), and another for managing the job queue and ready queue based on the available memory. Additionally, the main thread will be responsible for executing the scheduling algorithms: First-Come-First-Serve (FCFS), Shortest-Job-First (SJF), and Round-Robin (RR) with a quantum of 10ms. The processes will have attributes like ID, state, burst time, memory requirement, and required statistics that include turnaround and waiting times.

When implementing the FCFS scheduling algorithm, processes will be executed in the order they arrive. For the SJF algorithm, the process with the shortest burst time will be scheduled next. In the RR algorithm, each process is given a fixed time slot (quantum) and is then placed back in the ready queue if it's not finished. The provided job.txt file will contain job information formatted as specified, and the program must ensure that a process is loaded into the ready queue only if there is enough memory available to accommodate it.

The core challenge of this simulation is to manage these different threads effectively, ensuring that shared resources such as the job queue and the ready queue are handled in a thread-safe manner to avoid race conditions and ensure the correct behavior of the scheduler.

The input will be provided by the instructor, and you will have to account for the constraints provided, such as the limited main memory and the fixed number of jobs.

User Galia
by
7.3k points