Showing posts with label Operating System Algorithms. Show all posts
Showing posts with label Operating System Algorithms. Show all posts

Saturday, June 29, 2013

CPU Scheduling Algorithm : Shortest Remaining Time [Version 2] C++


Hi guys, after the the first version of C implementation of CPU Scheduling Algorithm Shortest Remaining Time by my friend, here I present another version of it in C++, which is also a much shorter program than previous one..


#include<iostream.h>
#include<conio.h>
class pro
{
public:
    int id;
    int burstTime;
    float arrivalTime;
    int roundat;
};
class sch
{
public:
    int nop,rqsize,btsum,g[100],gt[100],tt[100];
    pro min;
    pro max;
    void getdata(pro p[100]);
    void putdata(pro p[100]);
    void round(pro p[100]);
    void srt(pro p[100],pro readyq[100],int minid);
    void gantt(pro p[100],int gt[100]);
};
void sch::getdata(pro p[100])
{
       cout<<"\nEnter No. of Processes=\t";
       cin>>nop;
       for(int i=1;i<=nop;i++)
       {
        cout<<"\nEnter burst time for P"<<i<<" =\t";
        cin>>p[i].burstTime;
        btsum=btsum+p[i].burstTime;
        cout<<"\nArrival Time for P"<<i<<" =\t";
        cin>>p[i].arrivalTime;
        p[i].id=i;
       }
 }

Sunday, January 22, 2012

C implementation of FCFS, SJF, ROUND ROBIN

#include<stdio.h>

typedef struct process
{int id;
int wait;
int burst;
};
struct process pro[10];
struct process temp[20];

table(struct process pro[],int nop);
gantt(struct process pro[],int nop);
int getdata();
incwait(int tempburst,int id);
init();
int nop;

main()
{
struct process tmppro;
int choice,quantum;
int i,j,x,z,ret;
int ans,rrwt;
float rrawt,rratrt,fcfsawt,fcfsatrt,sjfatrt,sjfawt;
int procount,t,tburst[10];
do{printf("\t\t* * * * * * MENU * * * * * * \n");
printf("\t\t1.FCFS\n\t\t2.SJF\n\t\t3.Round Robin");
printf("\n\nPlease enter your choice : ");

Tuesday, April 12, 2011

Java Implementation of Page Replacement Algorithm -FIFO,LRU,OPT

import java.io.*;
public class Paging {
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
int n, page[], f, frames[], faults, count;
double rate;

public Paging() throws IOException{
System.out.println("Enter number of pages");
n=Integer.parseInt(input.readLine());
page=new int[n];
System.out.println("Enter number of page frames");
f=Integer.parseInt(input.readLine());
frames=new int[f];
count=1;
}

void reset()
{
int j;
for(j=0;j<f;j++)
frames[j]=0;
faults=0;
count=1;
}



void read() throws IOException
{
int i;
System.out.println("Enter the pages");
for(i=0;i<n;i++)
{
System.out.println("Enter page number "+(i+1));
page[i]=Integer.parseInt(input.readLine());
}
for(i=0;i<f;i++)
frames[i]=-1;
}

void fifo()
{
int i,j,k=0;
reset();
boolean found=false;
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(page[i]==frames[j])
found=true;
}
if(found==false)
{
frames[k]=page[i];
if(k==f-1)
k=0;
else
k++;
faults++;

}
display();
found=false;
}
System.out.println("Number of page faults = "+faults);
System.out.println("Fault rate = "+(faults*1.0/n));
}

void lru()
{
int i,j,duration[],max;
reset();
duration=new int[f];
boolean found=false;
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
duration[j]++;
for(j=0;j<f;j++)
{
if(page[i]==frames[j])
{
found=true;
duration[j]=0;
}
}
if(found==false)
{
max=0;
for(j=0;j<f;j++)
{
if(duration[j]>duration[max])
max=j;
}
frames[max]=page[i];
duration[max]=0;
faults++;
}

display();
found=false;

}
System.out.println("Number of page faults = "+faults);
System.out.println("Fault rate = "+(faults*1.0/n));
}

void opt()
{
int i,j=0,k,duration[],max,flag[];
reset();
duration=new int[f];
flag=new int[f];
boolean found=false;

for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
flag[j]=0;
duration[j]=n;
}

for(k=i+1;k<n;k++)
{
for(j=0;j<f;j++)
if(page[k]==frames[j]&&flag[j]==0)
{
duration[j]=k;
flag[j]=1;
}
}

for(j=0;j<f;j++)
if(page[i]==frames[j])
found=true;
if(found==false)
{
max=0;
for(j=0;j<f;j++)
{
if(duration[j]>duration[max])
max=j;
if(frames[j]<0)
{
max=j;
break;
}
}
frames[max]=page[i];
faults++;
}

display();
found=false;

}
System.out.println("Number of page faults = "+faults);
System.out.println("Fault rate = "+(faults*1.0/n));
}

void display()
{
int i;
System.out.print("Page frame "+count+" :");
for(i=0;i<f;i++)
{
if(frames[i]==-1)
System.out.print(" -");
else
System.out.print(" "+frames[i]);
}
System.out.print("\n");
count++;
}
public static void main(String[] args) throws IOException{
int option;
String choice;
Paging p=new Paging();
p.read();
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("Menu");
System.out.println("1. FIFO");
System.out.println("2. LRU");
System.out.println("3. OPT");
System.out.println("Enter option");
option=Integer.parseInt(input.readLine());
switch(option)
{
case 1: p.fifo();
break;
case 2: p.lru();
break;
case 3: p.opt();
break;
default: System.out.println("Invalid input");
}
System.out.println("Press Y to continue");
choice=input.readLine();
}
while(choice.compareToIgnoreCase("y")==0);
// TODO Auto-generated method stub

}

}



You would be interested in:

1. Java Implementation of First-Fit, Best-Fit and Worst-Fit
2. Java Implementation of Bankers Algorithm
3. C implementation of CPU Scheduling Algorithm Shortest Remaining Time
4. C Implementation of CPU Scheduling Algorithm FCFS, SJF, Round Robin

Java Implementation of First-Fit,Best-Fit and Worst-Fit

import java.io.*;
public class Fitting {
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
int process[], rprocess[], block[], rblock[], usage[], rusage[];
int p, b, free, used, rfree, rused, c, c1;

public Fitting() throws IOException{
System.out.println("Enter number of blocks");
b=Integer.parseInt(input.readLine());
System.out.println("Enter number of processes");
p=Integer.parseInt(input.readLine());
process=new int[p];
rprocess=new int[p];
block=new int[b];
rblock=new int[b];
usage=new int[b];
rusage=new int[b];
c=0;
// TODO Auto-generated constructor stub
}




void read() throws IOException
{
int i;
System.out.println("Enter block sizes");
for(i=0;i<b;i++)
{
System.out.print("Block "+(i+1)+" : ");
rblock[i]=Integer.parseInt(input.readLine());
}
System.out.println("Enter block usage scenario");
for(i=0;i<b;i++)
{
System.out.println("Is block "+(i+1)+" used (1) or free (0)?");
rusage[i]=Integer.parseInt(input.readLine());
if(rusage[i]==1)
{
rused=rused+rblock[i];
c1++;
}
else
rfree=rfree+rblock[i];
}
System.out.println("Enter process demand");
for(i=0;i<p;i++)
{
System.out.print("Process "+(i+1)+" : ");
rprocess[i]=Integer.parseInt(input.readLine());
}
}

void reset()
{
int i;
for(i=0;i<b;i++)
{
block[i]=rblock[i];
usage[i]=rusage[i];
}
for(i=0;i<p;i++)
process[i]=rprocess[i];
used=rused;
free=rfree;
c=c1;
}

void display()
{
int total;
total=rused+rfree;
System.out.println("Blocks used = "+c);
System.out.println("Total used space = "+used);
System.out.println("Blocks free = "+(b-c));
System.out.println("Total free space = "+(total-used));
}

void f_fit()
{
int i,j;
for(i=0;i<p;i++) //Processes.
for(j=0;j<b;j++) //Blocks.
{
if(process[i]<=block[j]&&usage[j]==0)
{
usage[j]=1;
used=used+block[j];
c++;
System.out.println("Process "+(i+1)+" is in block "+(j+1));
break;
}
}
}

void b_fit()
{
int i,j,size,best;
for(i=0;i<p;i++)
{
size=32967;
best=-1;
for(j=0;j<b;j++)
{
if(process[i]<=block[j]&&usage[j]==0&&(block[j]-process[i])<size)
{
size=block[j]-process[i];
best=j;
}
}
if(size<32967&&best!=-1) //Ensuring a best fit.
{
usage[best]=1;
used=used+block[best];
c++;
System.out.println("Process "+(i+1)+" is in block "+(best+1));
}
}
}

void w_fit()
{
int i,j,size,worst;
for(i=0;i<p;i++)
{
size=0;
worst=-1;
for(j=0;j<b;j++)
{
if(process[i]<=block[j]&&usage[j]==0&&(block[j]-process[i])>size)
{
size=block[j]-process[i];
worst=j;
}
}
if(worst!=-1) //Ensuring a worst fit.
{
usage[worst]=1;
used=used+block[worst];
c++;
System.out.println("Process "+(i+1)+" is in block "+(worst+1));
}
}
}



/**
* @param args
*/
public static void main(String[] args) throws IOException{
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
int option;
String choice;
Fitting f=new Fitting();
f.read();
do
{
f.reset();
System.out.println("Menu");
System.out.println("1. First fit");
System.out.println("2. Best fit");
System.out.println("3. Worst fit");
System.out.println("4. Block information");
System.out.println("Enter option");
option=Integer.parseInt(input.readLine());

switch(option)
{
case 1: f.f_fit();
break;
case 2: f.b_fit();
break;
case 3: f.w_fit();
break;
case 4: f.display();
break;
default: System.out.println("Invalid input");
}
f.display();
System.out.println("Press Y to continue");
choice=input.readLine();
}
while(choice.compareToIgnoreCase("y")==0);
// TODO Auto-generated method stub
}
}

You would be interested in:
1. Java Implementation of Page Replacement Algorithm -FIFO, LRU, OPT
2. Java Implementation of Bankers Algorithm
3. C implementation of CPU Scheduling Algorithm Shortest Remaining Time
4. C Implementation of CPU Scheduling Algorithm FCFS, SJF, Round Robin

Java Implementation of Bankers Algorithm

import java.io.*;
class Bankersalgorithm
{
void disp(int m,int n,int alloc[][],int max[][],int avble[],int need[][])throws Exception
{
char res[]={'A','B','C','D','E','F','G','H','I','J'};
// BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
int i,j;
System.out.println("\n\tALLOCATION\tMAX\tNEED\tAVAILABLE");
System.out.println("\n\t\t");
for(i=0;i<4;i++)
{
for(j=0;j<m;j++)
System.out.println(" "+res[j]);
System.out.println(" ");
}


for(i=0;i<n;i++)
{
System.out.println("\n\t\t"+i);
for(j=0;j<m;j++)
System.out.println(" "+alloc[i][j]);
System.out.println(" ");
for(j=0;j<m;j++)
System.out.println(" "+max[i][j]);
System.out.println("\t");
for(j=0;j<m;j++)
System.out.println(" "+need[i][j]);
System.out.println(" ");
if(i==0)
{
for(j=0;j<m;j++)
System.out.println(" "+avble[j]);
}
}
}
void safesq(int m,int n,int alloc[][],int avble[],int need[][])throws Exception
{
int i,j,k=0,l,flag=0,flag1=0;
int work[]=new int[5];
int work1[]=new int[5];
int fin[]=new int[5];
int safesq[]=new int[6];
for(i=0;i<m;i++)
work[i]=avble[i];
for(i=0;i<n;i++)
fin[i]=0;
for(l=0;l<n;l++)
{
for(i=0;i<n;i++)
{
flag1=0;
if(fin[i]==0)
{
for(j=0; j<m; j++)
{
if(need[i][j] > work[j])
{
flag1=1;
break;
}
}
if(flag1==0)
{
for(j=0;j<m;j++)
work[j]=work[j]+alloc[i][j];
fin[i]=1;
safesq[k]=i;
k++;
}
}
}
}
for(i=0;i<n;i++)
{
if(fin[i]==0)
{
System.out.println("\n\tFOR THE GIVEN REQUIREMENT THE SYSTEM IS");
System.out.println(" NOT IN A SAFE STATE.\n");
flag=1;
break;
}
}
if(flag==0)
{
System.out.println("\n\tTHE SAFE SEQUENCE IS:\t");
for(i=0;i<n;i++)
System.out.println(" "+safesq[i]);
}
}
void resreq(int m,int n,int alloc[][],int avble[],int need[][])throws Exception
{
char res[]={'A','B','C','D','E','F','G','H','I','J'};
BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
int i,j,num,flag=0,flag1=0;
int alloc1[][]=new int[5][5];
int avble1[]=new int[5];
int req[]=new int[5];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
alloc1[i][j] = alloc[i][j];
for(j=0;j<m;j++)
avble1[j]=avble[j];
System.out.println("\n\tENTER THE PROCESS NO. THAT REQUIRES EXTRA RESOURCES:\t");
num=Integer.parseInt(d.readLine());
System.out.println("\n\tENTER THE INSTANCE REQUIREMENT OF PROCESS \n"+num);
for(i=0;i<m;i++)
{
System.out.println("\n\tRESOURCE :\t"+res[i]);
req[i]=Integer.parseInt(d.readLine());
}
for(i=0;i<m;i++)
{
if(req[i]>need[num][i])
{
flag=1;
break;
}
}
if(flag==0)
{
for(i=0;i<m;i++)
{
if(req[i] > avble1[i])
{
flag1=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<m;i++)
{
avble1[i]=avble1[i]-req[i];
alloc1[num][i]=alloc1[num][i]+req[i];
need[num][i]=need[num][i]-req[i];
}
safesq(m,n,alloc1,avble1,need);
}
else
{
System.out.println("\n\tTHE PROCESS P%d HAS TO WAIT AS RESOURCES ");
System.out.println("ARE NOT AVAILABLE.\n");
}
}
else
System.out.println("\n\t REQUIREMENT EXCEEDS MAXIMUM CLAIM.\n");
}
}
class Result
{
public static void main(String args[])throws Exception
{
char res[]={'A','B','C','D','E','F','G','H','I','J'};
int i,j,ch=0,n,m;
int work1[]=new int[5];
int avble[]=new int[5];
int max[][]=new int[5][];
int alloc[][]=new int[5][5];
int need[][]=new int[5][5];
Bankersalgorithm b=new Bankersalgorithm();
BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nENTER THE NUMBER OF PROCESSES : \t");
n=Integer.parseInt(d.readLine());
System.out.println("\nENTER THE NUMBER OF RESOURCES : \t");
m=Integer.parseInt(d.readLine());
System.out.println("\nENTER THE MAXIMUM INSTANCES OF EACH RESOURCE\n");
for(i=0;i<m;i++)
{
System.out.println("\n\tRESOURCE %c:\t"+res[i]);
avble[i]=Integer.parseInt(d.readLine());
}
System.out.println("\nENTER THE MAXIMUM DEMAND OF EACH PROCESS FOR A RESOURCE\n");
for(i=0;i<n;i++)
{
System.out.println("\n\tFOR PROCESS \n"+i);
for(j=0;j<m;j++)
{
System.out.println("\n\tRESOURCE : \t"+res[j]);
max[i][j]=Integer.parseInt(d.readLine());
}
}
System.out.println("\nENTER THE MAX NO. OF INSTANCES OF A RESOURCE ALLOCATED TO");
System.out.println(" A PROCESS.\n");
for(i=0;i<n;i++)
{
System.out.println("\n\tFOR PROCESS \n"+i);
for(j=0;j<m;j++)
{
System.out.println("\n\tRESOURCE : \t"+res[j]);
alloc[i][j]=Integer.parseInt(d.readLine());
}
}
for(i=0;i<m;i++)
{
work1[i]=0;
for(j=0;j<n;j++)
work1[i]+=alloc[j][i];
avble[i]=avble[i] - work1[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(ch<5)
{
System.out.println("\n\n\tMENU:\n\t1]DISPLAY DATA\n\t2]GENERATE SAFE SEQUENCE");
System.out.println("\n\t3]RESOURCE REQUEST\n\t4]EXIT\n\tENTER YOUR CHOICE:\t");
ch=Integer.parseInt(d.readLine());
switch(ch)
{
case 1:
b.disp(m,n,alloc,max,avble,need);
break;
case 2:
b.safesq(m,n,alloc,avble,need);
break;
case 3:
b.resreq(m,n,alloc,avble,need);
break;
case 4:
default:
System.out.println("\n\tINVALID CHOICE ENTERED.\n");
}
}
System.out.println("\n\t\tThank You");
//return true;
}
}

You would be interested in:
1. Java Implementation of Page Replacement Algorithm -FIFO, LRU, OPT
2. Java Implementation of First-Fit, Best-Fit and Worst-Fit
3. C implementation of CPU Scheduling Algorithm Shortest Remaining Time
4. C Implementation of CPU Scheduling Algorithm FCFS, SJF, Round Robin

C implementation of CPU Scheduling Algorithm Shortest Remaining Time

Hello Friends, here's C implementation of CPU Scheduling Algorithms(Shortest Remaining Time).This is a .To run this program in windows simply copy and paste the program into notepad save it with any name you want with extension ".c".To run this program under linux just remove '#include,getch(); and clrscr();' from the program and save it with extension ".c".Don't just copy ,understand it.Program developed and tested by Niraj.
For any queries,mistakes mail to rustamengg@live.com


#include<stdio.h>

typedef struct process
{
int id;
int arrival;
int burst;
int wait;
};
struct process pool[10];
struct process ready[10];
struct process exe[20];
struct process temp,current,tmppro;

gantt(struct process pool[],int nop);
int scanpro(int time,int nop);
arrange();
execute();
incwait1();
incwait2();

int time,x,y,z;
float at[10],at1[10];

int main()
{

int tmp,nop,no;
float at_float,awt,atrt;
int j,i,maxarrtime;
int counter=0;
time=0;
x=1;
y=0;
z=0;
clrscr();
printf("Please enter the number of processes : ");
scanf("%d",&nop);
for(i=0;i<nop;i++)
at[i]=0;
printf("\nPlease enter the ID, Burst time & Arrival Time of each process : ");
printf("\n");
for(i=0;i<nop;i++)
{
printf("\nID : P%d",i+1);
pool[i].id=i+1;
printf("\nBurst Time : ");
scanf("%d",&pool[i].burst);
printf("Arrival Time : ");
scanf("%f",&at[i]);
}
for(i=0;i<nop;i++)
{
at1[i]=at[i];
at_float=at[i]+0.999;
pool[i].arrival=(int)at_float;
at[i]=(float)pool[i].arrival-at1[i];
}
printf("\n* * * * * * * PROCESS TABLE * * * * * * *\n");
printf("\nP ID Burst Time Arrival Time");
for(i=0;i<nop;i++)
{
if(pool[i].id==0)
break;
printf("\nP%d\t\t%d\t%f",pool[i].id,pool[i].burst,at1[i]);
}
maxarrtime=pool[nop-1].arrival;
while(time<=maxarrtime)
{
printf("\n\nTime : %d ",time);
printf("\tpool[%d].arr : %d ",z,pool[z].arrival);
if(time==pool[z].arrival)
{
no=scanpro(time,nop);
printf("\n\nNo of Process with same arrival time : %d",no);
printf("\nMoving Arrived process from Pool of Process to Ready Queue...");
for(counter=0;counter<no;counter++)
{
ready[x]=pool[z];
printf("\npool[%d].id = P%d ",z,pool[z].id);
printf("\tready[%d].id = P%d",x,ready[x].id);
x++;
z++;
}
execute();
}
else
{
if(current.burst>0)
{
current.burst--;
incwait1();
printf("\nCurrent : P%d and Burst : %d",current.id,current.burst);
time++;
}
else
{
if(current.burst==0)
{
printf("\nProcess P%d completes.",current.id);
execute();
}
}
}
}
counter=temp.burst-current.burst;
tmp=0;
while(current.burst!=0)
{
current.burst--;
counter++;
tmp++;
}
i=1;
printf("\nIncrementing Wait Time of processes in Ready Queue by %d...",tmp);
printf("\nAfter Incrementing ...");
while(ready[i].id>0 && ready[i].burst!=0)
{
ready[i].wait=ready[i].wait+tmp;
printf("\nP%d waiting time : %d",ready[i].id,ready[i].wait);
i++;
}
temp.burst=counter;
exe[y++]=temp;
for(i=0;i<10;i++)
ready[i]=ready[i+1];
arrange();
incwait2();
x=0;
while(ready[x].id>0)
exe[y++]=ready[x++];
for(i=0;i<20;i++)
exe[i]=exe[i+1];
for(i=0;i<nop;i++)
{
for(j=0;j<20;j++)
{
if(pool[i].id==exe[j].id)
{
pool[i].wait=exe[j].wait;
}
}
at[i]=at[i]+pool[i].wait;
printf("\nP%d Total Wait Time -->%f\tTotal Turn Around Time -->%f",i+1,at[i],(at[i]+pool[i].burst));
}
awt=0;
atrt=0;
for(i=0;i<nop;i++)
{
awt=awt+at[i];
atrt=atrt+(at[i]+pool[i].burst);
}
awt=awt/nop;
atrt=atrt/nop;
printf("\nAverage Waiting Time : %f",awt);
printf("\nAverage Turn Around Time : %f",atrt);
gantt(exe,nop);
return 0;
}

execute()
{
ready[0]=current;
printf("\nMoving current process to first location in Ready Queue.");
arrange();
temp.burst=temp.burst-current.burst;
exe[y++]=temp;
current=ready[0];
printf("\nCurrent :: ID = P%d Burst = %d",current.id,current.burst);
temp=ready[0];
current.burst--;
incwait1();
printf("\nDecrementing Current Burst by 1. Current burst : %d",current.burst);
time++;
}

incwait1()
{
int i=1;
printf("\n\nIncrementing wait time of processes in Ready Queue ...");
while(ready[i].id>0 && ready[i].burst!=0)
{
ready[i].wait++;
printf("\nP%d wait : %d",ready[i].id,ready[i].wait);
i++;
}
}

incwait2()
{
int i=1;
int wt=0;
printf("\n\nIncrementing wait time of processes in Ready Queue ...");
wt=ready[0].burst;
while(ready[i].id>0 && ready[i].burst!=0)
{
ready[i].wait=ready[i].wait+wt;
wt=wt+ready[i].burst;
printf("\nP%d wait : %d",ready[i].id,ready[i].wait);
i++;
}
}

int scanpro(int time,int nop)
{
int i;
int counter=0;
for(i=0;i<nop;i++)
if(pool[i].arrival==time)
counter++;
return counter;
}

arrange()
{
int a,i,j,counter=0;
for(i=1;i<10;i++)
{
for(j=0;j<10-i;j++)
{
if(ready[j].burst>ready[j+1].burst)
{
tmppro=ready[j];
ready[j]=ready[j+1];
ready[j+1]=tmppro;
}
}
}
printf("\nReady Queue after arranging : ");
for(i=0;i<10;i++)
{
if(ready[i].id!=0 && ready[i].burst>0)
{
printf("\nP ID : P%d",ready[i].id);
printf("\tP Burst : %d",ready[i].burst);
}
}
i=0;
while(ready[i].burst==0)
{
counter++;
i++;
}
a=0;
for(i=counter;i<10;i++)
{
ready[a++]=ready[i];
ready[i].id=0;
ready[i].burst=0;
}
}


gantt(struct process pool[],int nop)
{
int t=0,i;
int procount=0;
printf("\n\n\n* * * * * * * GANTT CHART * * * * * * *\n\n");
i=0;
while(pool[i].id>0)
{
procount++;
i++;
}
printf("\n");
for(i=0;i<procount;i++)
printf("--------");
printf("\n|");
for(i=0;i<procount;i++)
printf(" P%d |",pool[i].id);
printf("\n");
for(i=0;i<procount;i++)
printf("--------");
printf("\n");
for(i=0;i<procount;i++)
{
printf("%d\t",t);
t=t+pool[i].burst;
}
printf("%d",t);
}

OUTPUT:

Please enter the number of processes : 6

Please enter the ID, Burst time & Arrival Time of each process :

ID : P1
Burst Time : 7
Arrival Time : 0.000

ID : P2
Burst Time : 3
Arrival Time : 0.001

ID : P3
Burst Time : 1
Arrival Time : 5.001

ID : P4
Burst Time : 5
Arrival Time : 6.002

ID : P5
Burst Time : 4
Arrival Time : 6.020

ID : P6
Burst Time : 2
Arrival Time : 7.001

* * * * * * * PROCESS TABLE * * * * * * *

P ID Burst Time Arrival Time
P1 7 0.000000
P2 3 0.001000
P3 1 5.001000
P4 5 6.002000
P5 4 6.020000
P6 2 7.001000

Time : 0 pool[0].arr : 0

No of Process with same arrival time : 1
Moving Arrived process from Pool of Process to Ready Queue...
pool[0].id = P1 ready[1].id = P1
Moving current process to first location in Ready Queue.
Ready Queue after arranging :
P ID : P1 P Burst : 7
Current :: ID = P1 Burst = 7

Incrementing wait time of processes in Ready Queue ...
Decrementing Current Burst by 1. Current burst : 6

Time : 1 pool[1].arr : 1

No of Process with same arrival time : 1
Moving Arrived process from Pool of Process to Ready Queue...
pool[1].id = P2 ready[2].id = P2
Moving current process to first location in Ready Queue.
Ready Queue after arranging :
P ID : P2 P Burst : 3
P ID : P1 P Burst : 6
Current :: ID = P2 Burst = 3

Incrementing wait time of processes in Ready Queue ...
P1 wait : 1
Decrementing Current Burst by 1. Current burst : 2

Time : 2 pool[2].arr : 6

Incrementing wait time of processes in Ready Queue ...
P1 wait : 2
Current : P2 and Burst : 1

Time : 3 pool[2].arr : 6

Incrementing wait time of processes in Ready Queue ...
P1 wait : 3
Current : P2 and Burst : 0

Time : 4 pool[2].arr : 6
Process P2 completes.

Moving current process to first location in Ready Queue.
Ready Queue after arranging :
P ID : P1 P Burst : 6
Current :: ID = P1 Burst = 6
Incrementing wait time of processes in Ready Queue ...
Decrementing Current Burst by 1. Current burst : 5

Time : 5 pool[2].arr : 6

Incrementing wait time of processes in Ready Queue ...
Current : P1 and Burst : 4

Time : 6 pool[2].arr : 6

No of Process with same arrival time : 1
Moving Arrived process from Pool of Process to Ready Queue...
pool[2].id = P3 ready[3].id = P3
Moving current process to first location in Ready Queue.
Ready Queue after arranging :
P ID : P3 P Burst : 1
P ID : P1 P Burst : 4
Current :: ID = P3 Burst = 1

Incrementing wait time of processes in Ready Queue ...
P1 wait : 4
Decrementing Current Burst by 1. Current burst : 0

Time : 7 pool[3].arr : 7

No of Process with same arrival time : 2
Moving Arrived process from Pool of Process to Ready Queue...
pool[3].id = P4 ready[4].id = P4
pool[4].id = P5 ready[5].id = P5
Moving current process to first location in Ready Queue.
Ready Queue after arranging :
P ID : P1 P Burst : 4
P ID : P5 P Burst : 4
P ID : P4 P Burst : 5
Current :: ID = P1 Burst = 4

Incrementing wait time of processes in Ready Queue ...
P5 wait : 1
P4 wait : 1
Decrementing Current Burst by 1. Current burst : 3

Time : 8 pool[5].arr : 8

No of Process with same arrival time : 1
Moving Arrived process from Pool of Process to Ready Queue...
pool[5].id = P6 ready[6].id = P6
Moving current process to first location in Ready Queue.
Ready Queue after arranging :
P ID : P6 P Burst : 2
P ID : P1 P Burst : 3
P ID : P5 P Burst : 4
P ID : P4 P Burst : 5
Current :: ID = P6 Burst = 2

Incrementing wait time of processes in Ready Queue ...
P1 wait : 5
P5 wait : 2
P4 wait : 2
Decrementing Current Burst by 1. Current burst : 1
Incrementing Wait Time of processes in Ready Queue by 1...
After Incrementing ...
P1 waiting time : 6
P5 waiting time : 3
P4 waiting time : 3

Ready Queue after arranging :
P ID : P1 P Burst : 3
P ID : P5 P Burst : 4
P ID : P4 P Burst : 5

Incrementing wait time of processes in Ready Queue ...
P5 wait : 6
P4 wait : 10
P1 Total Wait Time -->6.000000 Total Turn Around Time -->13.000000
P2 Total Wait Time -->0.999000 Total Turn Around Time -->3.999000
P3 Total Wait Time -->0.999000 Total Turn Around Time -->1.999000
P4 Total Wait Time -->10.998000 Total Turn Around Time -->15.998000
P5 Total Wait Time -->6.980000 Total Turn Around Time -->10.980000
P6 Total Wait Time -->0.999000 Total Turn Around Time -->2.999000

Average Waiting Time : 4.495833

Average Turn Around Time : 8.162500


* * * * * * * GANTT CHART * * * * * * *

┌────┬────┬────┬────┬────┬────┬────┬────┬────┬
│ P1 │ P2 │ P1 │ P3 │ P1 │ P6 │ P1 │ P5 │ P4 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴
0 1 4 6 7 8 10 13

You would be interested in:
1. Java Implementation of Page Replacement Algorithm -FIFO, LRU, OPT
2. Java Implementation of First-Fit, Best-Fit and Worst-Fit
3. Java Implementation of Bankers Algorithm
4. C Implementation of CPU Scheduling Algorithm FCFS, SJF, Round Robin

Do leave a comment if u appreciate the output style
:-)

Thursday, April 1, 2010

C Implementation of Shortest Remaining Time CPU Scheduling Algorithm

Hello Friends, here's C implementation of Shortest Remaining Time CPU Scheduling Algorithm.To run this program in windows simply copy and paste the program into notepad save it with any name you want with extension ".c".To run this program under linux just remove '#include<conio.h>,getch(); and clrscr();' from the program and save it with extension ".c".Program developed and tested by Niraj.

#include<conio.h>
#include<stdio.h>

typedef struct process
{
int id;
int arrival;
int burst;
int wait;
};
struct process pool[10];
struct process ready[10];
struct process exe[20];
struct process temp,current,tmppro;

gantt(struct process pool[],int nop);
int scanpro(int time,int nop);
arrange();
execute();
incwait1();
incwait2();

int time,x,y,z;
float at[10],at1[10];
int main()
{
int tmp,nop,no;
float at_float,awt,atrt;
int j,i,maxarrtime;
int counter=0;
time=0;
x=1;
y=0;
z=0;
clrscr();
printf("Please enter the number of processes : ");
scanf("%d",&nop);
for(i=0;i<nop;i++)


at[i]=0;
printf("\nPlease enter the ID, Burst time & Arrival Time of each process : ");
printf("\n");
for(i=0;i<nop;i++)
{
printf("\nID : P%d",i+1);
pool[i].id=i+1;
printf("\nBurst Time : ");
scanf("%d",&pool[i].burst);
printf("Arrival Time : ");
scanf("%f",&at[i]);
}
for(i=0;i<nop;i++)
{
at1[i]=at[i];
at_float=at[i]+0.999;
pool[i].arrival=(int)at_float;
at[i]=(float)pool[i].arrival-at1[i];
}
printf("\n* * * * * * * PROCESS TABLE * * * * * * *\n");
printf("\nP ID Burst Time Arrival");
for(i=0;i<nop;i++)
{
if(pool[i].id==0)
break;
printf("\nP%d\t\t%d\t%f",pool[i].id,pool[i].burst,at1[i]);
}
printf("\n\n");

//SRT Logic

maxarrtime=pool[nop-1].arrival;
while(time<=maxarrtime)
{

if(time==pool[z].arrival)
{
no=scanpro(time,nop);
for(counter=0;counter<no;counter++)
{
ready[x]=pool[z];
x++;
z++;
}

execute();
}
else
{
if(current.burst>0)
{
current.burst--;
incwait1();

time++;

}
else
{
if(current.burst==0)
{
execute();
}
}
}
}
counter=temp.burst-current.burst;
tmp=0;
while(current.burst!=0)
{
current.burst--;
counter++;
tmp++;
}
i=1;

while(ready[i].id>0 && ready[i].burst!=0)
{
ready[i].wait=ready[i].wait+tmp;
i++;
}

temp.burst=counter;
exe[y++]=temp;
for(i=0;i<10;i++)
ready[i]=ready[i+1];
arrange();
incwait2();
x=0;
while(ready[x].id>0)
exe[y++]=ready[x++];
for(i=0;i<20;i++)
exe[i]=exe[i+1];

//setting wait time in pool
for(i=0;i<nop;i++)
{
for(j=0;j<20;j++)
{
if(pool[i].id==exe[j].id)
{
pool[i].wait=exe[j].wait;
}
}
at[i]=at[i]+pool[i].wait;
}

awt=0;
atrt=0;
for(i=0;i<nop;i++)
{
awt=awt+at[i];
atrt=atrt+(at[i]+pool[i].burst);
}
awt=awt/nop;
atrt=atrt/nop;
printf("\nAverage Waiting Time : %f",awt);
printf("\nAverage Turn Around Time : %f",atrt);
gantt(exe,nop);
getch();
return 0;
}

execute()
{
ready[0]=current;

arrange();
temp.burst=temp.burst-current.burst;
exe[y++]=temp;
current=ready[0];
temp=ready[0];
current.burst--;
incwait1();

time++;

}

incwait1()
{
int i=1;

while(ready[i].id>0 && ready[i].burst!=0)
{
ready[i].wait++;

i++;
}

}

incwait2()
{
int i=1;
int wt=0;

wt=ready[0].burst;
while(ready[i].id>0 && ready[i].burst!=0)
{
ready[i].wait=ready[i].wait+wt;
wt=wt+ready[i].burst;

i++;
}

}

int scanpro(int time,int nop)
{
int i;
int counter=0;
for(i=0;i<nop;i++)
if(pool[i].arrival==time)
counter++;
return counter;
}

arrange()
{
int a,i,j,counter=0;
for(i=1;i<10;i++)
{
for(j=0;j<10-i;j++)
{
if(ready[j].burst>ready[j+1].burst)
{
tmppro=ready[j];
ready[j]=ready[j+1];
ready[j+1]=tmppro;
}
}
}
i=0;
while(ready[i].burst==0)
{
counter++;
i++;
}
a=0;
for(i=counter;i<10;i++)
{
ready[a++]=ready[i];
ready[i].id=0;
ready[i].burst=0;
}
getch();
}


/*gantt chart*/
gantt(struct process pool[],int nop)
{
int t=0,i;
int procount=0;
printf("\n\n\n* * * * * * * GANTT CHART * * * * * * *\n\n");
i=0;
while(pool[i].id>0)
{
procount++;
i++;
}
printf("\n");
for(i=0;i<procount;i++)
printf("--------");
printf("\n|");
for(i=0;i<procount;i++)
printf(" P%d |",pool[i].id);
printf("\n");
for(i=0;i<procount;i++)
printf("--------");
printf("\n");
for(i=0;i<procount;i++)
{
printf("%d\t",t);
t=t+pool[i].burst;
}
printf("%d",t);
}
You would be interested in:
1. Java Implementation of Page Replacement Algorithm -FIFO, LRU, OPT
2. Java Implementation of First-Fit, Best-Fit and Worst-Fit
3. Java Implementation of Bankers Algorithm
4. C Implementation of CPU Scheduling Algorithm FCFS, SJF, Round Robin

C Implementation of CPU Sceduling Algorithm FCFS,SJF,Round Robin

Hello Friends, here's C implementation of CPU Scheduling Algorithms(First Come First Served ,Shortest Job First , Round Robin).This is a menu driven C program allowing you to select type of scheduling you want to implement .To run this program in windows simply copy and paste the program into notepad save it with any name you want with extension ".c".To run this program under linux just remove '#include,getch(); and clrscr();' from the program and save it with extension ".c".
Don't just copy ,understand it.Program developed and tested by Niraj.
For any queries,mistakes mail to rustamengg@live.com

#include<stdio.h>
typedef struct process
{
int id;
int wait;
int burst;
};
struct process pro[10];
struct process temp[20];

table(struct process pro[],int nop);
gantt(struct process pro[],int nop);
int getdata();
incwait(int tempburst,int id);
init();

int nop;

main()
{
struct process tmppro;
int choice,quantum;
int i,j,x,z,ret;
int ans,rrwt;
float rrawt,rratrt,fcfsawt,fcfsatrt,sjfatrt,sjfawt;
int procount,t,tburst[10];
do
{
printf("\t\t* * * * * * MENU * * * * * * \n");
printf("\t\t1.FCFS\n\t\t2.SJF\n\t\t3.Round Robin");
printf("\n\nPlease enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
{
init();
nop=getdata();
table(pro,nop);
gantt(pro,nop);
fcfsawt=0;
fcfsatrt=0;
for(i=nop-1;i>=0;i--)
{
pro[i].wait=0;
for(j=0;j<i;j++)
{
pro[i].wait=pro[i].wait+pro[j].burst;
}
}
for(i=0;i<nop;i++)
{
printf("\nP%d Wait Time : %d",pro[i].id,pro[i].wait);
printf("\nP%d Turn Around Time : %d",pro[i].id,(pro[i].wait+pro[i].burst));
fcfsawt=fcfsawt+pro[i].wait;
fcfsatrt=fcfsatrt+(pro[i].wait+pro[i].burst);
}
printf("\n\nAWT of FCFS : %f",fcfsawt/nop);
printf("\n\nATRT of FCFS : %f",fcfsatrt/nop);
break;
}
case 2:
{
init();
nop=getdata();
printf("\nTable before arranging ...\n");
table(pro,nop);
for(i=1;i<nop;i++)
{
for(j=0;j<nop-i;j++)
{
if(pro[j].burst>pro[j+1].burst)
{
tmppro=pro[j];
pro[j]=pro[j+1];
pro[j+1]=tmppro;
}
}
}
printf("\nTable after arranging ...\n");
table(pro,nop);
gantt(pro,nop);
sjfawt=0;
sjfatrt=0;
for(i=nop-1;i>=0;i--)
{
pro[i].wait=0;
for(j=0;j<i;j++)
{
pro[i].wait=pro[i].wait+pro[j].burst;
}
}
for(i=0;i<nop;i++)
{
printf("\nP%d Wait Time : %d",pro[i].id,pro[i].wait);
printf("\nP%d Turn Around Time : %d",pro[i].id,(pro[i].wait+pro[i].burst));
sjfawt=sjfawt+pro[i].wait;
sjfatrt=sjfatrt+(pro[i].wait+pro[i].burst);
}
printf("\n\nAWT of SJF : %f",sjfawt/nop);
printf("\n\nATRT of SJF : %f",sjfatrt/nop);
break;
}
case 3:
{
init();
j=0;
ret=0;
nop=getdata();
for(i=0;i<nop;i++)
tburst[i]=pro[i].burst;
printf("\nPlease enter the time quantum : ");
scanf("%d",&quantum);
for(i=0;i<nop;i++)
{
if(pro[i].burst>0)
{
if(pro[i].burst>quantum)
{
temp[j].id=pro[i].id;
temp[j].burst=quantum;
pro[i].burst=pro[i].burst-quantum;
j++;
incwait(quantum,pro[i].id);
}
else
{
temp[j]=pro[i];
j++;
incwait(pro[i].burst,pro[i].id);
pro[i].burst=0;
}
}
if(i==nop-1)
{
for(z=0;z<nop;z++)
{
if(pro[z].burst!=0)
{
i=-1;
break;
}
else
if(z==(nop-1)) ret=1;
}
}
if(ret==1) break;
}
table(temp,40);
gantt(temp,40);
printf("\n\nRound Robin awt and atrt : ");
rrawt=0;
rratrt=0;
for(i=0;i<nop;i++)
{
printf("\nP%d Wait Time : %d",pro[i].id,pro[i].wait);
printf("\nP%d Turn Around Time : %d",pro[i].id,(pro[i].wait+tburst[i]));
rrawt=rrawt+pro[i].wait;
rratrt=rratrt+(pro[i].wait+tburst[i]);
}
printf("\n\nAWT of RR : %f",rrawt/nop);
printf("\n\nATRT of RR : %f",rratrt/nop);
break;
}
}
printf("\n\nPress 1 to cont ...");
scanf("%d",&ans);
}
while(ans==1);
}

incwait(int tempburst,int id)
{
int i=0;
for(i=0;i<nop;i++)
{
if(pro[i].id!=id && pro[i].burst>0)
pro[i].wait=pro[i].wait+tempburst;
}
}

init()
{
int i;
nop=0;
for(i=0;i<10;i++)
{
pro[i].burst=0;
pro[i].id=0;
pro[i].wait=0;
}
for(i=0;i<20;i++)
{
temp[i].burst=0;
temp[i].id=0;
temp[i].wait=0;
}
}



table(struct process pro[],int nop)
{
int i;
printf("\n\n\n* * * * * * * PROCESS TABLE * * * * * * *\n\n");
printf("\nProcess Name Burst Time");
for(i=0;i<nop;i++)
{
if(pro[i].id==0)
break;
printf("\nP%d\t\t%d\t\t%d",pro[i].id,pro[i].burst);
}
}


gantt(struct process pro[],int nop)
{
int t=0,i;
int procount=0;
printf("\n\n\n* * * * * * * GANTT CHART * * * * * * *\n\n");
i=0;
while(pro[i].id>0)
{
procount++;
i++;
}
printf("\nÚ");
for(i=0;i<procount;i++)
printf("ÄÄÄÄÄÄÄÂ");
printf("\n³");
for(i=0;i<procount;i++)
printf(" P%d ³",pro[i].id);
printf("\nÀ");
for(i=0;i<procount;i++)
printf("ÄÄÄÄÄÄÄÁ");
printf("\n");
for(i=0;i<procount;i++)
{
printf("%d\t",t);
t=t+pro[i].burst;
}
printf("%d",t);
}


int getdata()
{
int i,nop;
printf("\n\nPlease enter the number of processes : ");
scanf("%d",&nop);
printf("\nPlease enter the ID and Burst time of each process : ");
printf("\n");
for(i=0;i<nop;i++)
{
printf("\nID : P%d",i+1);
pro[i].id=i+1;
printf("\nBurst Time : ");
scanf("%d",&pro[i].burst);
}
return nop;
}

You would be interested in:
1. Java Implementation of Page Replacement Algorithm -FIFO, LRU, OPT
2. Java Implementation of First-Fit, Best-Fit and Worst-Fit
3. Java Implementation of Bankers Algorithm
4. C implementation of CPU Scheduling Algorithm Shortest Remaining Time