HELP , with operating system related program in c.

Joined
Mar 27, 2023
Messages
1
Reaction score
0
Implement the scheduling algorithms SRTF (pre-emptive), SJF(non-pre-emptive) and Round Robin
CPU Scheduling (quantum=5) and Virtual Round Robin algorithm (quantum=5) for the sample data
given below. Assume all processes are CPU bound.
All programs should print various performance measures (turnaround time, waiting time, response
time for each process and system throughput)
Assume that there is only one CPU and one I/O device in the system. The I/O device can be assumed
to be sequential i.e. it serves only one process at a time. Save the above data in a file as shown below and read data from that file. File Data Format
Line 1: P0;0;24;2;5
Line 2: P1;3;17;3;6
Line 3: P2;8;50;2;5
Line 4: P3;15;10;3;6
You can choose any programming language of your choice. NOTE:
1. You need to submit one zip file containing four code, input, output files for eac
 
Joined
Apr 6, 2023
Messages
21
Reaction score
0
I tried to implement Shortest Job First Scheduling algorithm (non-pre-emptive) using Java language. It takes the number of processes, arrival time and burst time of each process as the input and prints the turnaround time and waiting time for each process.
Code:
import java.util.*;

public class SJF1 {
  public static void main(String args[])
  {
    Scanner sc = new Scanner(System.in);
    System.out.println ("Enter number of processes : ");
    int n = sc.nextInt();
    int pid[] = new int[n];
    int at[] = new int[n];
    int bt[] = new int[n];
    int ct[] = new int[n];
    int ta[] = new int[n];
    int wt[] = new int[n]; 
    int f[] = new int[n]; 
    int st=0, tot=0;
    float avgwt=0, avgta=0;
 
    for(int i=0;i<n;i++) {
     System.out.println ("Enter process " + (i+1) + " arrival time:");
     at[i] = sc.nextInt();
     pid[i] = i+1;
     f[i] = 0;
    }
    
    for(int i=0;i<n;i++) {
     System.out.println ("Enter process " + (i+1) + " burst time:");
     bt[i] = sc.nextInt();
     pid[i] = i+1;
     f[i] = 0;
    }
    
    while(true)
    {
       int c=n, min=999;
       if (tot == n)
         break;
       for (int i=0; i<n; i++)
       {
      
       if ((at[i] <= st) && (f[i] == 0) && (bt[i]<min)) 
       {
         min=bt[i];
         c=i;
       }
      }
      
       if (c==n)
         st++;
       else {
         ct[c]=st+bt[c];
         st+=bt[c];
         ta[c]=ct[c]-at[c];
         wt[c]=ta[c]-bt[c];
         f[c]=1;
         tot++;
       }
     }
       System.out.println("Process  Arrival time  Burst time  Finish time  Turnaround time "
               + " Waiting time");
       for(int i=0;i<n;i++)
       {
         avgwt+= wt[i];
         avgta+= ta[i];
         System.out.println(pid[i]+"\t   "+at[i]+"\t\t"+bt[i]+"\t\t"+ct[i]+
                 "\t\t"+ta[i]+"\t\t"+wt[i]);
       }
       System.out.println("Average Turnaround Time is "+ (float)(avgta/n) + " ms");
       System.out.println("Average Waiting Time is "+ (float)(avgwt/n) + " ms");
       sc.close();
    }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top