The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Monday, August 29, 2011

Page Replacement Algorithm (Complete)



#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#define max 100
#define min 10

int ref[max],count,frame[min],n;

void input()
{
system("CLS");
int i,temp;
count=0;
printf("\n\n\tEnter the number of page frames : ");
scanf("%d",&n);
printf("\n\n\tEnter the reference string (-1 for end) : ");
scanf("%d",&temp);
while(temp != -1)
{
ref[count++]=temp;
scanf("%d",&temp);
}
}

void FIFO()
{
int i,j,fault=0;
system("CLS");
for(i=0;i<n;i++)
frame[i]=-1;
for(i=0;i<count;i++)
{
for(j=0;j<n;j++)
if(frame[j]==ref[i])
break;
if(j==n)
frame[fault%n]=ref[i], fault++;
printf("\n\nAfter inserting %d the frame status is : ",ref[i]);
for(j=0;j<n;j++)
printf("%d ",frame[j]);

getch();
}
printf("\n\n\tEnd to inserting the reference string.");
printf("\n\n\tTotal page fault is %d.",fault);
printf("\n\n\tPress any key to continue.");
getch();
}

void Second_Chance()
{
int i,j,ref_bit[min],cur=0,fault=0;
system("CLS");
for(i=0;i<n;i++)
ref_bit[i]=0, frame[i]=-1;
for(i=0;i<count;i++)
{
for(j=0;j<n;j++)
if(frame[j]==ref[i])
break;
if(j==n)
while(1)
{
if(cur==n)
cur=0;
if(ref_bit[cur]==0)
{
frame[cur]=ref[i];
ref_bit[cur]=1;
fault++;
cur++;
break;
}
else
ref_bit[cur]=0;
cur++;
}//while
printf("\n\nAfter inserting %d the frame status is : ",ref[i]);
printf("\nFrame Reference_bit\n");
for(j=0;j<n;j++)
printf(" %d %d\n",frame[j],ref_bit[j]);
getch();
}//for i
printf("\n\n\tEnd to inserting the reference string.");
printf("\n\n\tTotal page fault is %d.",fault);
printf("\n\n\tPress any key to continue.");
getch();
}

void LRU()
{
int i,j,k,stack[min],top=0,fault=0;
system("CLS");
for(i=0;i<count;i++)
{
if(top<n)
stack[top++]=ref[i],fault++;
else
{
for(j=0;j<n;j++)
if(stack[j]==ref[i])
break;
if(j<n)
{
for(k=j;k<n-1;k++)
stack[k]=stack[k+1];
stack[k]=ref[i];
}
else
{
for(k=0;k<n-1;k++)
stack[k]=stack[k+1];
stack[k]=ref[i];
fault++;
}
}
printf("\n\nAfter inserting %d the stack status is : ",ref[i]);
for(j=0;j<top;j++)
printf("%d ",stack[j]);
getch();
}//for i
printf("\n\n\tEnd to inserting the reference string.");
printf("\n\n\tTotal page fault is %d.",fault);
printf("\n\n\tPress any key to continue.");
getch();
}

void NRU()
{
int i,j,Class[min],min_class,rb,mb,cl,index,fault=0;
system("CLS");
for(j=0;j<n;j++)
Class[j]=-1;
for(i=0;i<count;i++)
{
for(j=0;j<n;j++)
if(frame[j]==ref[i])
break;
printf("\nEnter the referenced & modified bit status for %d : ",ref[i]);
scanf("%d%d",&rb,&mb);
if(rb==0 && mb==0)
cl=0;
else if(rb==0 && mb==1)
cl=1;
else if(rb==1 && mb==0)
cl=2;
else
cl=3;
if(j<n)
Class[j]=cl;
else
{
min_class=Class[0];
index=0;
for(j=1;j<n;j++)
if(min_class>Class[j])
min_class=Class[j], index=j;
frame[index]=ref[i];
Class[index]=cl;
fault++;
}//else
printf("\n\nAfter inserting %d the frame status is : ",ref[i]);
printf("Frame Class");
for(j=0;j<n;j++)
printf("%d %d\n",frame[j],Class[j]);
getch();
}//for i
printf("\n\n\tEnd to inserting the reference string.");
printf("\n\n\tTotal page fault is %d.",fault);
printf("\n\n\tPress any key to continue.");
getch();
}

void main()
{
int x;
//freopen("in.cpp","r",stdin);
while(1)
{
system("CLS");
printf("\n\n\t-----MENU-----");
printf("\n\t1. Input ");
printf("\n\t2. FIFO Algorithm");
printf("\n\t3. Second Chance Algorithm");
printf("\n\t4. LRU (Least Recently Used) Algorithm");
printf("\n\t5. NRU (Not Recently Used) Algorithm");
printf("\n\t0. Exit.");
printf("\n\n\tEnter your choice.");
scanf("%d",&x);
switch(x)
{
case 1:
input();
break;
case 2:
FIFO();
break;
case 3:
Second_Chance();
break;
case 4:
LRU();
break;
case 5:
NRU();
break;
case 0:
exit(0);
}
}
}

Monday, August 22, 2011

LRU Page Replacement Algorithm


#include<stdio.h>
#include<conio.h>
#define max 100
#define min 10

int ref[max],count,frame[min],n;

void input()
{
 int temp;
 do
 {
  clrscr();
  printf("\n\n\tEnter the number of page frames : ");
  scanf("%d",&n);
 }while(n>min || n <= 0);
 count = 0;
 printf("\n\n\tEnter the reference string (-1 for end) : ");
 scanf("%d",&temp);
 while(temp != -1 && count <= max)
 {
  ref[count++]=temp;
  scanf("%d",&temp);
 }
}

int find_pos(int nm)
{
 int ps,l,small=nm;
 for(int k=0;k<n;k++)
 {
  l = nm-1;
  while(ref[l] != frame[k] && l > 0)
    l--;
  if(l<small)
  {
   small = l;
   ps = k;
  }
 }
return ps;
}

void LRU()
{
 int i,j,fault=0;
 for(i=0;i<n;i++)
  frame[i] = -1;
 int ind,pos=0;
 printf("\n\nPage       Page Status          Page Fault      Frame Status\n");
 printf("-------------------------------------------------------------------------------");
 for(i=0;i<count;i++)
 {
  ind = 0;
  printf("\n %d\t",ref[i]);
  for(j=0;j<n;j++)
  {
   if(frame[j] == -1)
   {
    ind = 1;
    pos = j;
    break;
   }
   if(frame[j] == ref[i])
   {
   printf("Already Inserted\t   No\t\t");
   ind = 2;
   break;
   }
  }
  if(ind != 2)
  {
   printf("Inserting in Frame\t   Yes\t\t");
   if(ind == 0)
    pos = find_pos(i);
   frame[pos] = ref[i];
   fault ++;
  }
  for(j=0;j<n;j++)
  {
   if(frame[j] == -1)
    printf(" -");
   else
    printf(" %d",frame[j]);
  }
 }
 printf("\n\n\n\n\tEnd to inserting the reference string.");
 printf("\n\n\tTotal page fault is %d.",fault);
}


void main()
{
input();
LRU();
getch();
}

Wednesday, August 17, 2011

Optimal Page Replacement Algorithm



#include<stdio.h>
#include<conio.h>
#define max 100
#define min 10

int ref[max],count,frame[min],n;

void input()
{
 int temp;
 do
 {
  clrscr();
  printf("\n\n\tEnter the number of page frames : ");
  scanf("%d",&n);
 }while(n>min || n <= 0);
 count = 0;
 printf("\n\n\tEnter the reference string (-1 for end) : ");
 scanf("%d",&temp);
 while(temp != -1 && count <= max)
 {
  ref[count++]=temp;
  scanf("%d",&temp);
 }
}

int find_pos(int nm)
{
 int ps,l,big=nm;
 for(int k=0;k<n;k++)
 {
  l = nm+1;
  while(ref[l] != frame[k] && l < count)
    l++;
  if(l>big)
  {
   big = l;
   ps = k;
  }
 }
return ps;
}

void OPT()
{
 int i,j,fault=0;
 for(i=0;i<n;i++)
  frame[i] = -1;
 int ind,pos=0;
 printf("\n\nPage       Page Status          Page Fault      Frame Status\n");
 printf("-------------------------------------------------------------------------------");
 for(i=0;i<count;i++)
 {
  ind = 0;
  printf("\n %d\t",ref[i]);
  for(j=0;j<n;j++)
  {
   if(frame[j] == -1)
   {
    ind = 1;
    pos = j;
    break;
   }
   if(frame[j] == ref[i])
   {
   printf("Already Inserted\t   No\t\t");
   ind = 2;
   break;
   }
  }
  if(ind != 2)
  {
   printf("Inserting in Frame\t   Yes\t\t");
   if(ind == 0)
    pos = find_pos(i);
   frame[pos] = ref[i];
   fault ++;
  }
  for(j=0;j<n;j++)
  {
   if(frame[j] == -1)
    printf(" -");
   else
    printf(" %d",frame[j]);
  }
 }
 printf("\n\n\n\n\tEnd to inserting the reference string.");
 printf("\n\n\tTotal page fault is %d.",fault);
}


void main()
{
input();
OPT();
getch();
}

Monday, August 15, 2011

FIFO Page Replacement Algorithm


#include<stdio.h>
#include<conio.h>
#define max 100
#define min 10

int ref[max],count,frame[min],n;

void input()
{
 int temp;
 do
 {
  clrscr();
  printf("\n\n\tEnter the number of page frames : ");
  scanf("%d",&n);
 }while(n>min || n <= 0);
 count = 0;
 printf("\n\n\tEnter the reference string (-1 for end) : ");
 scanf("%d",&temp);
 while(temp != -1 && count <= max)
 {
  ref[count++]=temp;
  scanf("%d",&temp);
 }
}

void FIFO()
{
 int i,j,fault=0;
 for(i=0;i<n;i++)
  frame[i] = -1;
 int ind,pos=0;
 printf("\n\nPage       Page Status          Page Fault      Frame Status\n");
 printf("-------------------------------------------------------------------------------");
 for(i=0;i<count;i++)
 {
  ind =0;
  printf("\n %d\t",ref[i]);
  for(j=0;j<n;j++)
  {
   if(frame[j] == ref[i])
   {
   printf("Already Inserted\t   No\t\t");
   ind = 1;
   break;
   }
  }
  if(ind == 0)
  {
   printf("Inserting in Frame\t   Yes\t\t");
   frame[pos] = ref[i];   pos = (pos+1)%n;
   fault ++;
  }
  for(j=0;j<n;j++)
  {
   if(frame[j] == -1)
    printf(" -");
   else
    printf(" %d",frame[j]);
  }
 }
 printf("\n\n\n\n\tEnd to inserting the reference string.");
 printf("\n\n\tTotal page fault is %d.",fault);
}


void main()
{
input();
FIFO();
getch();
}

Wednesday, August 10, 2011

Multilevel Queue Feedback CPU Scheduling Algorithm


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

float avg_wt,avg_tt;
int i = 0,ttl_wt=0,ttl_tt=0,qt1=4,qt2=8;

struct process
{
 int prn;
 char type;
 int bt;
 int wt;
 int tt;
 struct process *nxt;
}*stfor1,*stfor2,*stbck,*np,*endfor1,*endfor2,*endbck,*temp;

void ins_node(struct process *np,int num)
{
 if(num == 1)
 {
  if(stfor1 == NULL)
    stfor1 = endfor1 = np;
  else
  {
   endfor1->nxt = np;
   endfor1 = np;
  }
 }
 else if(num == 2)
 {
  if(stfor2 == NULL)
    stfor2 = endfor2 = np;
  else
  {
   endfor2->nxt = np;
   endfor2 = np;
  }
 }
 else
 {
  if(stbck == NULL)
    stbck = endbck = np;
  else
  {
   endbck->nxt = np;
   endbck = np;
  }
 }
}

void del_node(struct process *np,int num,int choice)
{
 if(num == 1)
 {
  if(stfor1 == endfor1)
   stfor1 = NULL;
  else
   stfor1 = stfor1->nxt;
  if(choice == 1)
   delete np;
 }
 else if(num == 2)
 {
  if(stfor2 == endfor2)
   stfor2 = NULL;
  else
   stfor2 = stfor2->nxt;
  if(choice == 1)
   delete np;
 }
 else
 {
  if(stbck == endbck)
   stbck = NULL;
  else
   stbck = stbck->nxt;
  delete np;
 }
}

void ins_dat()
{
  char ch;
  int j=0,n;
  stfor1 = endfor1 = stfor2 = endfor2 = stbck = endbck = NULL;
  do
  {
  np = new process;
  np->nxt = NULL;
  np->prn = ++j;
  printf("\n Enter the Burst time of Process #%d : ",np->prn);
  scanf("%d",&np->bt);
  printf("\n Enter the type of Process #%d (f-foreground or b-background): ",np->prn);
  np->type = getche();

  if(np->type == 'f')
   np->nxt = stfor1;
  else
   np->nxt = NULL;

  np->wt = np->tt = 0;
  if(np->type == 'f')
   ins_node(np,1);
  else
   ins_node(np,3);
  printf("\n\n Continue ?? : ");
  ch = getche();
  }while(ch == 'y' || stfor1 == NULL);
}

void Round_Robin()
{
 getch();
 printf("\n\n Foreground RR Process Scheduling (Quantum Size = %d)\n\n",qt1);
 printf("\n\n Process   Burst Time   Waiting Time   Turnaround Time \n");
 printf(" ------------------------------------------------------- \n");
 temp = stfor1;
  do
  {
   printf(" %d \t\t %d \t\t",stfor1->prn,stfor1->bt);
   stfor1->wt += (temp->tt - stfor1->tt);
   if(qt1 >= stfor1->bt)
   {
    stfor1->tt = temp->tt + stfor1->bt;
    stfor1->bt = 0;
    printf(" %d \t\t %d\n\n",stfor1->wt,stfor1->tt);
    ttl_wt += stfor1->wt;
    ttl_tt += stfor1->tt;
    temp = np = stfor1;
    del_node(np,1,1);
    ++i;
   }
   else
   {
    stfor1->bt -= qt1;
    stfor1->tt = temp->tt + qt1;
    printf(" %d \t\t %d\n\n",stfor1->wt,stfor1->tt);
    temp = np = stfor1;
    del_node(np,1,2);
    if(temp->bt > qt1)
     ins_node(temp,2);
    else
     ins_node(temp,1);
   }
  }while(stfor1 != NULL);


  if(stfor1 == NULL && stfor2 != NULL)
  {
  getch();
  printf("\n Foreground RR Process Scheduling (Quantum Size = %d)\n",qt2);
  printf("\n\n Process   Burst Time   Waiting Time   Turnaround Time \n");
  printf(" ------------------------------------------------------- \n");
  do
  {
   printf(" %d \t\t %d \t\t",stfor2->prn,stfor2->bt);
   stfor2->wt += (temp->tt - stfor2->tt);
   if(qt2 >= stfor2->bt)
   {
    stfor2->tt = temp->tt + stfor2->bt;
    stfor2->bt = 0;
    printf(" %d \t\t %d\n\n",stfor2->wt,stfor2->tt);
    ttl_wt += stfor2->wt;
    ttl_tt += stfor2->tt;
    temp = np = stfor2;
    del_node(np,2,1);
    ++i;
   }
   else
   {
    stfor2->bt -= qt2;
    stfor2->tt = temp->tt + qt2;
    printf(" %d \t\t %d\n\n",stfor2->wt,stfor2->tt);
    np = temp = stfor2;
    del_node(np,2,2);
    if(temp->bt > qt2)
     ins_node(temp,3);
    else
     ins_node(temp,2);
   }
  }while(stfor2 != NULL);
  }
}

void FCFS()
{
 getch();
 printf("\n\n Background FCFS Process Scheduling \n\n");
 printf("\n\n Process   Burst Time   Waiting Time   Turnaround Time \n");
 printf(" ------------------------------------------------------- \n");
 do
 {
   printf(" %d \t\t %d \t\t",stbck->prn,stbck->bt);
   stbck->wt += (temp->tt-stbck->tt);
   stbck->tt = temp->tt + stbck->bt;
   printf(" %d \t\t %d\n\n",stbck->wt,stbck->tt);
   ttl_wt += stbck->wt;
   ttl_tt += stbck->tt;
   temp = np = stbck;
   del_node(np,3,1);
   ++i;
 }while(stbck != NULL);

}
void main()
{
 clrscr();
 ins_dat();
 if(stfor1 != NULL)
  Round_Robin();
 if(stfor1 == NULL && stfor2 == NULL && stbck != NULL)
  FCFS();
 avg_wt = (float) ttl_wt/i;
 avg_tt = (float) ttl_tt/i;
 printf("\n\n Average Waiting Time  : %f",avg_wt);
 printf("\n Average Turnaround Time : %f",avg_tt);
 getch();
}

Tuesday, August 9, 2011

Multilevel Queue CPU Scheduling Algorithm


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

float avg_wt,avg_tt;
int i = 0,ttl_wt=0,ttl_tt=0,qt;

struct process
{
 int prn;
 char type;
 int bt;
 int wt;
 int tt;
 struct process *nxt;
}*stfor,*stbck,*np,*endfor,*endbck,*temp;

void ins_node(struct process *np)
{
 if(np->type == 'f')
 {
  if(stfor == NULL)
    stfor = endfor = np;
  else
  {
   endfor->nxt = np;
   endfor = np;
  }
 }
 else
 {
  if(stbck == NULL)
    stbck = endbck = np;
  else
  {
   endbck->nxt = np;
   endbck = np;
  }
 }
}

void del_node(struct process *np,int choice)
{
 if(np->type == 'f')
 {
  if(stfor == endfor)
   stfor = NULL;
  else
   stfor = stfor->nxt;
  if(choice == 1)
   delete np;
  }
  else
  {
   if(stbck == endbck)
    stbck = NULL;
   else
    stbck = stbck->nxt;
   delete np;
  }
}

void ins_dat()
{
  char ch;
  int j=0;
  stfor = endfor = stbck = endbck = NULL;
  do
  {
  np = new process;
  np->prn = ++j;
  printf("\n Enter the Burst time of Process #%d : ",np->prn);
  scanf("%d",&np->bt);
  printf("\n Enter the type of Process #%d (f-foreground or b-background): ",np->prn);
  np->type = getche();
  if(np->type == 'f')
   np->nxt = stfor;
  else
   np->nxt = NULL;
  np->wt = np->tt = 0;
  ins_node(np);
  printf("\n\n Continue ?? : ");
  ch = getche();
  }while(ch == 'y' || stfor == NULL);
}

void Round_Robin()
{
 getch();
 printf("\n\n Foreground RR Process Scheduling (Quantum Size = %d)\n\n",qt);
 printf("\n\n Process   Burst Time   Waiting Time   Turnaround Time \n");
 printf(" ------------------------------------------------------- \n");
 temp = stfor;
 do
 {
   printf(" %d \t\t %d \t\t",stfor->prn,stfor->bt);
   stfor->wt += (temp->tt - stfor->tt);
   if(qt >= stfor->bt)
   {
    stfor->tt = temp->tt + stfor->bt;
    stfor->bt = 0;
    printf(" %d \t\t %d\n\n",stfor->wt,stfor->tt);
    ttl_wt += stfor->wt;
    ttl_tt += stfor->tt;
    temp = np = stfor;
    del_node(np,1);
    ++i;
   }
   else
   {
    stfor->bt -= qt;
    stfor->tt = temp->tt + qt;
    printf(" %d \t\t %d\n\n",stfor->wt,stfor->tt);
    np = temp = stfor;
    del_node(np,2);
    ins_node(temp);
   }
 }while(stfor != NULL);
}

void FCFS()
{
 getch();
 printf("\n\n Background FCFS Process Scheduling \n\n");
 printf("\n\n Process   Burst Time   Waiting Time   Turnaround Time \n");
 printf(" ------------------------------------------------------- \n");
 do
 {
   printf(" %d \t\t %d \t\t",stbck->prn,stbck->bt);
   stbck->wt = temp->tt;
   stbck->tt = temp->tt + stbck->bt;
   printf(" %d \t\t %d\n\n",stbck->wt,stbck->tt);
   ttl_wt += stbck->wt;
   ttl_tt += stbck->tt;
   temp = np = stbck;
   del_node(np,1);
   ++i;
 }while(stbck != NULL);

}
void main()
{
 clrscr();
 printf("\n Enter the Quantum time for Foreground Process : ");
 scanf("%d",&qt);
 ins_dat();
 if(stfor != NULL)
  Round_Robin();
 if(stfor == NULL && stbck != NULL)
  FCFS();
 avg_wt = (float) ttl_wt/i;
 avg_tt = (float) ttl_tt/i;
 printf("\n\n Average Waiting Time  : %f",avg_wt);
 printf("\n Average Turnaround Time : %f",avg_tt);
 getch();
}

Sunday, August 7, 2011

Priority CPU Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#define max 10

int i,j,nop;

struct process
{
 int prn;
 int prin;
 int bt;
 int wt;
 int tt;
}pr[max],temp;

void ins_dat()
{
 for(i=0;i<nop;i++)
 {
  pr[i].prn = i+1;
  printf("\n Enter the Burst time and Priroty Number of Process #%d : ",pr[i].prn);
  scanf("%d %d",&pr[i].bt,&pr[i].prin);
 }
}

void Priority_scheduling()
{
 for(i=0;i<nop;i++)
 {
  for(j=0;j<(nop-1)-i;j++)
  {
   if(pr[j].prin > pr[j+1].prin)
   {
    temp = pr[j];
    pr[j] = pr[j+1];
    pr[j+1] = temp;
   }
  }
 }
 for(i=0;i<nop;i++)
 {
  pr[i].wt = i == 0 ? 0 : pr[i-1].tt ;
  pr[i].tt = pr[i].wt + pr[i].bt;
 }
}

void disp_dat()
{
 int ttl_tt=0,ttl_wt=0;
 float avg_tt,avg_wt;
 printf("\n\n Process   Waiting Time   Burst Time   Turnaround Time \n");
 printf(" ------------------------------------------------------- \n");
 for(i=0;i<nop;i++)
 {
  printf(" %d \t\t %d \t\t %d \t\t %d\n\n",pr[i].prn,pr[i].wt,pr[i].bt,pr[i].tt);
  ttl_wt += pr[i].wt;
  ttl_tt += pr[i].tt;
 }
 avg_wt = (float) ttl_wt/nop;
 avg_tt = (float) ttl_tt/nop;
 printf("\n\n Average Waiting Time  : %f",avg_wt);
 printf("\n Average Turnaround Time : %f",avg_tt);
}

void main()
{
 do
 {
 clrscr();
 printf("\n Enter the number of processes : ");
 scanf("%d",&nop);
 }while(nop > max || nop <= 0);
 ins_dat();
 Priority_scheduling();
 disp_dat();
 getch();
}

Thursday, August 4, 2011

Round Robin CPU Scheduling Algorithm (Pointers)


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


int qt;

struct process
{
 int prn;
 int bt;
 int wt;
 int tt;
 struct process *nxt;
}*start,*np,*end,*temp;

void ins_node(struct process *np)
{
 if(start == NULL)
    start = end = np;
  else
  {
   end->nxt = np;
   end = np;
  }
}

void del_node(struct process *np,int choice)
{
  if(start == end)
   start = NULL;
  else
   start = start->nxt;
  if(choice == 1)
   delete np;
}

void ins_dat()
{
  char ch;
  int i=0;
  start = end = NULL;
  do
  {
  np = new process;
  np->nxt = NULL;
  np->prn = ++i;
  printf("\n Enter the Burst time of Process #%d : ",np->prn);
  scanf("%d",&np->bt);
  np->nxt = start;
  np->wt = np->tt = 0;
  ins_node(np);
  printf("\n\n Continue ?? : ");
  ch = getche();
  }while(ch == 'y');
}

void Round_Robin()
{
 float avg_wt,avg_tt;
 int i = 0,ttl_wt=0,ttl_tt=0;
 getch();
 printf("\n\n Process   Burst Time   Waiting Time   Turnaround Time \n");
 printf(" ------------------------------------------------------- \n");
 temp = start;
 do
 {
   printf(" %d \t\t %d \t\t",start->prn,start->bt);
   start->wt += (temp->tt - start->tt);
   if(qt >= start->bt)
   {
    start->tt = temp->tt + start->bt;
    start->bt = 0;
    printf(" %d \t\t %d\n\n",start->wt,start->tt);
    ttl_wt += start->wt;
    ttl_tt += start->tt;
    temp = np = start;
    del_node(np,1);
    ++i;
   }
   else
   {
    start->bt -= qt;
    start->tt = temp->tt + qt;
    printf(" %d \t\t %d\n\n",start->wt,start->tt);
    np = temp = start;
    del_node(np,2);
    ins_node(np);
   }
 }while(start != NULL);
    avg_wt = (float) ttl_wt/i;
    avg_tt = (float) ttl_tt/i;
   printf("\n\n Average Waiting Time  : %f",avg_wt);
   printf("\n Average Turnaround Time : %f",avg_tt);
}

void main()
{
 clrscr();
 printf("\n Enter the Quantum time : ");
 scanf("%d",&qt);
 ins_dat();
 Round_Robin();
 getch();
}

Round Robin CPU Scheduling Algorithm (Arrays)


#include<stdio.h>
#include<conio.h>
#define max 10

int i,j,qt,nop;

struct process
{
 int ind;
 int prn;
 int bt;
 int wt;
 int tt;
}pr[max],temp;

void ins_dat()
{
 for(i=0;i<nop;i++)
 {
  pr[i].prn = i+1;
  printf("\n Enter the Burst time of Process #%d : ",pr[i].prn);
  scanf("%d",&pr[i].bt);
  pr[i].ind = pr[i].wt = pr[i].tt = 0;
 }
}

void Round_Robin()
{

 printf("\n\n Process   Burst Time   Waiting Time   Turnaround Time \n");
 printf(" ------------------------------------------------------- \n");
 int done = 0,ttl_tt=0,ttl_wt=0;
 float avg_tt,avg_wt;
 temp = pr[0];
 i = 0;
 do
 {
   if(pr[i].ind == 0)
   {
   printf(" %d \t\t %d \t\t",pr[i].prn,pr[i].bt);
   pr[i].wt += (temp.tt - pr[i].tt);
   if(qt >= pr[i].bt)
   {
    pr[i].tt = temp.tt + pr[i].bt;
    pr[i].bt = 0;
    pr[i].ind = 1;
    done ++;
    ttl_wt += pr[i].wt;
    ttl_tt += pr[i].tt;
   }
   else
   {
    pr[i].bt -= qt;
    pr[i].tt = temp.tt + qt;
   }
   temp = pr[i%nop];
   printf(" %d \t\t %d\n\n",pr[i].wt,pr[i].tt);
   }
  i = (i+1)%nop;
 }while(done != nop);
 avg_wt = (float) ttl_wt/nop;
 avg_tt = (float) ttl_tt/nop;
 printf("\n\n Average Waiting Time  : %f",avg_wt);
 printf("\n Average Turnaround Time : %f",avg_tt);
}

void main()
{
 do
 {
 clrscr();
 printf("\n Enter the number of processes : ");
 scanf("%d",&nop);
 }while(nop > max || nop <= 0);
 printf("\n Enter the Quantum time : ");
 scanf("%d",&qt);
 ins_dat();
 Round_Robin();
 getch();
}

Tuesday, August 2, 2011

SJFS CPU Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#define max 10

int i,j,nop;

struct process
{
 int prn;
 int bt;
 int wt;
 int tt;
}pr[max],temp;

void ins_dat()
{
 for(i=0;i<nop;i++)
 {
  pr[i].prn = i+1;
  printf("\n Enter the Burst time of Process #%d : ",pr[i].prn);
  scanf("%d",&pr[i].bt);
 }
}

void SJF()
{
 for(i=0;i<nop;i++)
 {
  for(j=0;j<(nop-1)-i;j++)
  {
   if(pr[j].bt > pr[j+1].bt)
   {
    temp = pr[j];
    pr[j] = pr[j+1];
    pr[j+1] = temp;
   }
  }
 }
 for(i=0;i<nop;i++)
 {
  pr[i].wt = i == 0 ? 0 : pr[i-1].tt ;
  pr[i].tt = pr[i].wt + pr[i].bt;
 }
}

void disp_dat()
{
 int ttl_tt=0,ttl_wt=0;
 float avg_tt,avg_wt;
 printf("\n\n Process   Waiting Time   Burst Time   Turnaround Time \n");
 printf(" ------------------------------------------------------- \n");
 for(i=0;i<nop;i++)
 {
  printf(" %d \t\t %d \t\t %d \t\t %d\n\n",pr[i].prn,pr[i].wt,pr[i].bt,pr[i].tt);
  ttl_wt += pr[i].wt;
  ttl_tt += pr[i].tt;
 }
 avg_wt = (float) ttl_wt/nop;
 avg_tt = (float) ttl_tt/nop;
 printf("\n\n Average Waiting Time  : %f",avg_wt);
 printf("\n Average Turnaround Time : %f",avg_tt);
}

void main()
{
 do
 {
 clrscr();
 printf("\n Enter the number of processes : ");
 scanf("%d",&nop);
 }while(nop > max || nop <= 0);
 ins_dat();
 SJF();
 disp_dat();
 getch();
}

Monday, August 1, 2011

FCFS CPU Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#define max 10

int i,nop;

struct process
{
 int prn;
 int bt;
 int wt;
 int tt;
}pr[max];

void ins_dat()
{
 for(i=0;i<nop;i++)
 {
  pr[i].prn = i+1;
  printf("\n Enter the Burst time of Process #%d : ",pr[i].prn);
  scanf("%d",&pr[i].bt);
 }
}

void FCFS()
{
 for(i=0;i<nop;i++)
 {
  pr[i].wt = pr[i].prn == 1 ? 0 : pr[i-1].tt ;
  pr[i].tt = pr[i].wt + pr[i].bt;
 }
}

void disp_dat()
{
 int ttl_tt=0,ttl_wt=0;
 float avg_tt,avg_wt;
 printf("\n\n Process   Waiting Time   Burst Time   Turnaround Time \n");
 printf(" ------------------------------------------------------- \n");
 for(i=0;i<nop;i++)
 {
  printf(" %d \t\t %d \t\t %d \t\t %d\n\n",pr[i].prn,pr[i].wt,pr[i].bt,pr[i].tt);
  ttl_wt += pr[i].wt;
  ttl_tt += pr[i].tt;
 }
 avg_wt = (float) ttl_wt/nop;
 avg_tt = (float) ttl_tt/nop;
 printf("\n\n Average Waiting Time  : %f",avg_wt);
 printf("\n Average Turnaround Time : %f",avg_tt);
}

void main()
{
 do
 {

 printf("\n Enter the number of processes : ");
 scanf("%d",&nop);
 }while(nop > max || nop <= 0);
 ins_dat();
 FCFS();
 disp_dat();
 getch();
}