Skip to main content

Write the program to simulate Round Robin (RR) scheduling. The arrival time and first CPU-burst for different n number of processes should be input to the algorithm. Also give the time quantum as input. Assume the fixed IO waiting time (2 units). The next CPU-burst should be generated randomly. The output should give Gantt chart, turnaround time and waiting time for each process. Also find the average waiting time and turnaround time.

 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Structure to represent a process
typedef struct Process {
    int pid;        // Process ID
    int arrival;    // Arrival time
    int burst;      // CPU burst time
    int remaining;  // Remaining burst time
    int waiting;    // Waiting time
    int turnaround; // Turnaround time
} Process;

// Function to swap two processes
void swap(Process *a, Process *b) {
    Process temp = *a;
    *a = *b;
    *b = temp;
}

// Function to perform Round Robin (RR) scheduling
void roundRobinScheduling(Process *processes, int n, int timeQuantum) {
    // Sort processes based on arrival time
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (processes[j].arrival > processes[j + 1].arrival) {
                swap(&processes[j], &processes[j + 1]);
            }
        }
    }

    // Initialize variables
    int currentTime = 0;
    int totalWaiting = 0;
    int totalTurnaround = 0;
    int completed = 0;

    printf("\nGantt Chart:\n");
    printf("--------------------------------------------------\n");
    printf("Time   |");
    for (int i = 0; i < n; i++) {
        printf("  P%d   |", processes[i].pid);
    }
    printf("\n--------------------------------------------------\n");

    // Perform scheduling
    while (completed < n) {
        for (int i = 0; i < n; i++) {
            if (processes[i].remaining > 0) {
                // Execute the process for the time quantum or until it completes
                int timeToExecute = (processes[i].remaining < timeQuantum) ? processes[i].remaining : timeQuantum;
                processes[i].remaining -= timeToExecute;
                currentTime += timeToExecute;

                // Print Gantt chart
                printf("%3d    |", currentTime);

                // Check if the process has completed
                if (processes[i].remaining == 0) {
                    // Calculate turnaround time
                    processes[i].turnaround = currentTime - processes[i].arrival;
                    totalTurnaround += processes[i].turnaround;

                    // Calculate waiting time
                    processes[i].waiting = processes[i].turnaround - processes[i].burst;
                    totalWaiting += processes[i].waiting;

                    completed++;
                }
            }
        }
    }

    // Print turnaround time and waiting time for each process
    printf("\nProcess  | Turnaround Time | Waiting Time\n");
    printf("----------------------------------------\n");
    for (int i = 0; i < n; i++) {
        printf("   P%d        %5d               %5d\n", processes[i].pid, processes[i].turnaround, processes[i].waiting);
    }

    // Print average turnaround time and waiting time
    printf("\nAverage Turnaround Time: %.2f\n", (float)totalTurnaround / n);
    printf("Average Waiting Time: %.2f\n", (float)totalWaiting / n);
}

int main() {
    srand(time(NULL));

    int n, timeQuantum;
    printf("Enter the number of processes: ");
    scanf("%d", &n);
    printf("Enter the time quantum: ");
    scanf("%d", &timeQuantum);

    // Initialize an array of processes
    Process processes[n];
    for (int i = 0; i < n; i++) {
        processes[i].pid = i + 1;
        printf("Enter arrival time and burst time for P%d: ", i + 1);
        scanf("%d %d", &processes[i].arrival, &processes[i].burst);
        processes[i].remaining = processes[i].burst;
    }

    // Perform Round Robin scheduling
    roundRobinScheduling(processes, n, timeQuantum);

    return 0;
}

Comments

Popular posts from this blog

Write a program to read book information (bookid, bookname, bookprice, bookqty) in file “book.dat”. Write a menu driven program to perform the following operations using Random access file: i. Search for a specific book by name. ii. Display all book and total cost

  import java . io .*; import java . util .*; class Book {       String name , id ;       int qty ;       double price , total ;       Book ( String i , String n , String p , String q )      {               name = n ;               id = i ;               qty = Integer . parseInt ( q );               price = Double . parseDouble ( p );               total = qty * price ;      }       public String toString ()      {               System . out . println ( "name\t id\t qty\t price\t total" );               String s = name + "\t" + id + "\t" + qty + "\t" + price + "\t" + total ;           ...

Define a class MyDate (day, month, year) with methods to accept and display a MyDate object. Accept date as dd, mm, yyyy. Throw user defined exception “InvalidDateException” if the date is invalid. Examples of invalid dates : 03 15 2019, 31 6 2000, 29 2 2021

  import java . io .*; import java . util .*; class InvalidDateException extends Exception {       InvalidDateException ()       {               System . out . println ( "Invalid Date" );       } } class MyDate {       int day , mon , yr ;       void accept ( int d , int m , int y )       {             day = d ;             mon = m ;             yr = y ;       }       void display ()       {             System . out . println ( "Date is valid : " + day + "/" + mon + "/" + yr );       } } class Date {       public static void main ( String args []) throws Exception       {             Scanner sc = new Sca...

Write a Java program to design a screen using Awt that will take a user name and password. If the user name and password are not same, raise an Exception with appropriate message. User can have 3 login chances only. Use clear button to clear the TextFields.

  import java . awt .*; import java . awt . event .*; import javax . swing .*; class InvalidPasswordException extends Exception {       InvalidPasswordException ()       {             System . out . println ( "Username and password is not same" );       } } public class Password extends Frame implements ActionListener {       Label uname , upass ;       TextField nametext ;       TextField passtext , msg ;       Button login , Clear ;       Panel p ;       int attempt = 0 ;       char c = '*' ;             public void login ()       {             p = new Panel ();             uname = new Label ( "Username : " , Label . CENTER );             upass = n...