Skip to main content

Write the program to simulate FCFS CPU-scheduling. The arrival time and first CPUburst for different n number of processes should be input to the algorithm. Assume that 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<string.h>
typedef struct process_info
{
   char pname[20];
   int at,bt,ct,bt1;
   struct process_info*next;
}NODE;
int n;
NODE*first,*last;
void accept_info()
{
   NODE*p;
   int i;
   printf("Enter no of processess:");
   scanf("%d",&n);
   for(i=0;i<n;i++)
   {
      p=(NODE*)malloc(sizeof(NODE));
      printf("Enter process name:");
      scanf("%s",p->pname);
      printf("Enter arrival time:");
      scanf("%d",&p->at);
      printf("Enter first CPU burst time:");
      scanf("%d",&p->bt);
      p->bt1=p->bt;
      p->next=NULL;
      if(first==NULL)
         first=p;
      else
         last->next=p;
      last=p;
   }
}
void print_output()
{
   NODE *p;
   float avg_tat=0,avg_wt=0;
   printf("pname\t at\t bt\t ct\t tat\t wt\t\n");
   p=first;
   while(p!=NULL)
   {
       int tat=p->ct-p->at;
       int wt=tat-p->bt;
       avg_tat +=tat;
       avg_wt +=wt;
       printf("%s\t %d\t %d\t %d\t %d\t %d\n",p->pname,p->at,p->bt,p->ct,tat,wt);
       p=p->next;
   }
   printf("Avg TAT=%f\t AVG WT=%f\n",avg_tat/n,avg_wt/n);
}
void print_input()
{
   NODE*p;
   p=first;
   printf("pname\t at\t bt\n");
   while(p!=NULL)
   {
      printf("%s\t %d\t %d\n",p->pname,p->at,p->bt1);
      p=p->next;
   }
}
void sort()
{
   NODE *p,*q;
   int t;
   char name[20];
   p=first;
   while(p->next!=NULL)
   {
      q=p->next;
      while(q!=NULL)
      {
         if(p->at>q->at)
         {
            strcpy(name,p->pname);
            strcpy(p->pname,q->pname);
            strcpy(q->pname,name);
            t=p->at;
            p->at=q->at;
            q->at=t;
            t=p->bt;
            p->bt=q->bt;
            q->bt=t;
            t=p->ct;
            p->ct=q->ct;
            q->ct=t;
            t=p->bt1;
            p->bt1=q->bt1;
            q->bt1=t;
         }
         q=q->next;
      }
      p=p->next;
   }
}
int time;
NODE*get_fcfs()
{
   NODE*p;
   p=first;
   while(p!=NULL)
   {
      if(p->at<=time && p->bt1!=0)
         return p;
      p=p->next;
   }
   return NULL;
}
struct gantt_chart
{
   int start;
   char pname[30];
   int end;
}s[100],s1[100];
int k;
void fcfs()
{
   int prev=0,n1=0;
   NODE*p;
   while(n1!=n)
   {
      p=get_fcfs();
      if(p==NULL)
      {
         time++;
         s[k].start=prev;
         strcpy(s[k].pname,"*");
         s[k].end=time;
         prev=time;
         k++;
      }
      else
      {
         time+=p->bt1;
         s[k].start=prev;
         strcpy(s[k].pname,p->pname);
         s[k].end=time;
         prev=time;
         k++;
         p->ct=time;
         p->bt1=0;
         n1++;
      }
     
      sort();
   }
}
void print_gantt_chart()
{
   int i,j,m;
   s1[0]=s[0];
   for(i=1,j=0;i<k;i++)
   {
      if(strcmp(s[i].pname,s1[j].pname)==0)
         s1[j].end=s[i].end;
      else
         s1[++j]=s[i];
   }
   printf("%d",s1[0].start);
   for(i=0;i<=j;i++)
   {
      m=(s1[i].end-s1[i].start);
      for(k=0;k<m/2;k++)
         printf("-");
      printf("%s",s1[i].pname);
      for(k=0;k<(m+1)/2;k++)
         printf("-");
      printf("%d",s1[i].end);
   }
}
int main()
{
   accept_info();
   sort();
   fcfs();
   print_output();
   print_gantt_chart();
   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...