Skip to main content

Write the program to simulate Non-preemptive Priority scheduling. The arrival time and first CPU-burst and priority for different n number of processes should be input to the algorithm. 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 priority;   // Priority
    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 non-preemptive priority scheduling
void priorityScheduling(Process *processes, int n) {
    // 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]);
            }
        }
    }

    // Variables to keep track of time and total waiting/turnaround time
    int currentTime = 0;
    int totalWaiting = 0;
    int totalTurnaround = 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
    for (int i = 0; i < n; i++) {
        if (currentTime < processes[i].arrival) {
            currentTime = processes[i].arrival;
        }

        // Calculate turnaround time
        processes[i].turnaround = currentTime - processes[i].arrival + processes[i].burst;
        totalTurnaround += processes[i].turnaround;

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

        // Print Gantt chart
        printf("%3d    |", currentTime);
        for (int j = 0; j < n; j++) {
            if (j == i) {
                printf("   %d*  |", currentTime + processes[i].burst);
                currentTime += processes[i].burst;
            } else {
                printf("       |");
            }
        }
        printf("\n");
    }

    // 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;
    printf("Enter the number of processes: ");
    scanf("%d", &n);

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

    // Perform non-preemptive priority scheduling
    priorityScheduling(processes, n);

    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 ;           ...

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...

) Create the following GUI screen using appropriate layout managers. Accept the name, class , hobbies of the user and apply the changes and display the selected options in a text box.

  import javax.swing.*; import java.awt.*; import java.awt.event.*; class Swing2 extends JFrame implements ActionListener {     JLabel l1 , l2 , l3 ;         JButton b ;         JRadioButton r1 , r2 , r3 ;         JCheckBox c1 , c2 , c3 ;         JTextField t1 , t2 ;         ButtonGroup b1 ;         JPanel p1 , p2 ;     static int cnt ;         private StringBuffer s1 = new StringBuffer ();                 Swing2 ()         {                             b1= new ButtonGroup ();                 p1= new JPanel ();                 p2= new JPanel ();               ...