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 ;)
Showing posts with label Operating Systems. Show all posts
Showing posts with label Operating Systems. Show all posts

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();
}

Friday, June 3, 2011

Memory Management - First Fit, Best Fit and Worst Fit (Simple)


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

struct memory
{
 int ind;
 int prn;
 int stpt;
 int space;
 struct memory *nxt;
}*start,*ptr,*ptrc,*loc,*locp,*np;

int ttl_space,rem_space;

int chk_avail(int num,int mem)
{
 if(mem > rem_space)
   return 0;
 else
 {
  ptr = start;
  while(ptr != NULL)
  {
   if(ptr->ind == 1 && ptr->prn == num)
    return 0;
   if(ptr->ind == 0 && ptr->space >= mem)
    return 1;
   ptr = ptr->nxt;
  }
  return 0;
 }
}

void getstats()
{
  ptr = start;
  printf("\n Process           Starting Point        Memory Allocated \n");
  printf(" ---------------------------------------------------------- \n");
  while(ptr != NULL)
  {
    if(ptr->ind == 1)
     printf("\n\t%d\t\t",ptr->prn);
    else
     printf("Free Space\t\t\n",ptr->prn);
    printf("%d\t\t\t%d\n",ptr->stpt,ptr->space);
   ptr = ptr->nxt;
  }
 printf("\n\t\t     Total Memory Space : %d",ttl_space);
 printf("\n\t\t Total Allocated Memory : %d",ttl_space-rem_space);
 printf("\n\t\t Total Available Memory : %d",rem_space);
}

void first_fit(int mem)
{
 loc = start;
 locp = NULL;
 while(loc != NULL)
 {
   if(loc->ind == 0 && loc->space >= mem)
    break;
   locp = loc;
   loc = loc->nxt;
 }
}

void best_fit(int mem)
{
 int small = ttl_space;
 loc = start;
 locp = NULL;
 while(loc != NULL)
 {
   if(loc->ind == 0 && loc->space >= mem && loc->space - mem  < small )
   {
    ptrc = locp;
    ptr = loc;
    small = loc->space - mem;
   }
   locp = loc;
   loc = loc->nxt;
 }
 locp = ptrc;
 loc = ptr;
}

void worst_fit(int mem)
{
 int big = 0;
 loc = start;
 locp = NULL;
 while(loc != NULL)
 {
   if(loc->ind == 0 && loc->space >= mem && loc->space - mem  > big )
   {
    ptrc = locp;
    ptr = loc;
    big = loc->space - mem;
   }
   locp = loc;
   loc = loc->nxt;
 }
 locp = ptrc;
 loc = ptr;
}

void allocate()
{
  int cho,res,nm,mm;
  do
  {
  printf("\n Enter process number : ");
  scanf("%d",&nm);
  printf("\n Enter memory space reqd. : ");
  scanf("%d",&mm);
  res = chk_avail(nm,mm);
  }while(res == 0);
  printf("\n Therefore, Process is unique and Reqd. Memory is availiable. ");
  printf("\n\n\t By which method you want to allocate Memory ?? ");
  printf("\n 1. First Fit\n 2. Best Fit\n 3. Worst Fit");
  printf("\n\n\t Choose your Option : ");
  scanf("%d",&cho);
  switch(cho)
  {
   case 1 : first_fit(mm); break;
   case 2 : best_fit(mm); break;
   case 3 : worst_fit(mm); break;
   default: printf("\n\n\t Try again later !! "); break;
  }
  if(cho>=1&&cho<=3)
  {
   np = new memory;
   np->ind = 1;
   np->prn = nm;
   np->stpt = loc->stpt;
   np->space = mm;
   np->nxt = NULL;
   if(loc == start)
   {
    np->nxt = start;
    start = np;
   }
   else
   {
    locp->nxt = np;
    np->nxt = loc;
   }
   if(loc->space != np->space)
   {
   loc->stpt = np->stpt + np->space;
   loc->space = loc->space - np->space;
   }
   else
   {
    np->nxt = loc->nxt;
    delete loc;
   }
   rem_space -= np->space;
   getstats();
  }
}

void deallocate()
{
  int res,nm;
  printf("\n Enter process number to be deallocated : ");
  scanf("%d",&nm);
  res = chk_avail(nm,ttl_space);
  if(res != 0)
   printf("\n\t\t Sorry !! Can't be deleted !!! \n");
  else
  {
   locp = NULL;
   loc = start;
   while(loc != NULL)
   {
    if(loc->ind == 1 && loc->prn == nm)
      break;
    locp = loc;
    loc = loc->nxt;
   }
   rem_space += loc->space;
   loc->ind = 0;
   locp = start;
   loc = start->nxt;
   while(loc != NULL)
   {
    if(locp->ind == 0 && loc->ind == 0 )
    {
     locp-> space += loc->space;
     locp->nxt = loc->nxt;
     delete loc;
     loc = locp->nxt;
    }
    else
    {
     locp = loc;
     loc = loc->nxt;
    }
   }
   printf("\n\t Therefore, The Given Memory Space has been deallocated \n");
   getstats();
  }
}

void main()
{
 int cho;
 do
 {
 clrscr();
 printf("\n Enter the maximum space in memory : ");
 scanf("%d",&ttl_space);
 }while(ttl_space<=0 || ttl_space >= 10000);
 rem_space = ttl_space;
 np = new memory;
 np->stpt = np->ind = 0;
 np->space = ttl_space;
 np->nxt = NULL;
 start = np;
 do
 {
  getch();
  clrscr();
  printf("\n\t What would you like to do now in ?? \n");
  printf("\n1. Allocate Memory by Inserting Process ");
  printf("\n2. Deallocate Memory by Removing Process ");
  printf("\n\n\t Choose your option : ");
  scanf("%d",&cho);
  switch(cho)
  {
   case 1 :  allocate();  break;
   case 2 :  deallocate(); break;
   default:  printf("\n\t\t Thanks for Using The Program \n");  break;
  }
 }while(cho>=1&&cho<=2);
 getch();
}

Thursday, June 2, 2011

Memory Management - First Fit, Best Fit and Worst Fit (Complex)


/*
 * Auther: Jayarathina Madharasan. Y
 * Website: http://www.madharasan.com/
 * Date: Oct, 10, 2008
 * This program performs animation of first fit, best fit, worst fit operating system concepts.
*/

#include<stdio.h>
#include<conio.h>
#define maxmem_no 100

int mem[maxmem_no], maxmem = maxmem_no;

void initmem()
{
int i = 0;
for(; i<maxmem; i++)
mem[i] = 0;
}

void comp()
{
int mem_temp[maxmem_no], i = 0, k;
for(; i<maxmem; i++)
mem_temp[i] = mem[i];
initmem();
for(i=0, k=0; i<maxmem; i++)
if(mem_temp[i] != 0)
{
mem[k] = mem_temp[i];
k++;
}
}

void removejob()
{
int jn, i=0;
printf("\nEnter Job Number to remove: "); scanf("%d", &jn);
if(jn<0)
{
printf("Error!");
getch();
return;
}
for (; i<maxmem; i++)
if (mem[i]== jn)
mem[i] =0;
}

void printmem()
{
int i =0;
for (printf("\n"); i<maxmem; i++)
{
(mem[i]==0)?printf("%c ", 176):printf("%c ", 219);
}
printf("\n\n");

for (i=0; i<maxmem; i++)
{
if(mem[i]!=0)
printf("%d ", mem[i]);
else
printf("  ");
}
printf("\n\n");
}

void memalloc(int jn, int jm, int mem_srt)
{
int i = mem_srt;
for(; i<=mem_srt+jm-1; i++)
mem[i] = jn;
}

int memfreeFF(int memreq)
{
int i=0, start = -1, end = -1;
calc:
start=-1, end=-1;
for(; i<maxmem; i++)
{
if(mem[i] == 0)
{
start = i;
break;
}
}
for(; i<maxmem; i++)
{
if(mem[i]==0)
{
end = i;
}
else
{
break;
}
}
if ((end - start +1) >= memreq)
{
return start;
}
else if(i<maxmem)
{
goto calc;
}
return -1;
}

int memfreeBF(int memreq)
{
int i=0, start = -1, end = -1;
int start_temp = -1, tot_free = -1;
calc:
start=-1, end=-1;
for(; i<maxmem; i++)
{
if(mem[i] == 0)
{
start = i;
break;
}
}
for(; i<maxmem; i++)
{
if(mem[i]==0)
{
end = i;
}
else
{
break;
}
}
if ((end - start +1) >= memreq)
{
if (start_temp != -1)
{
if(tot_free <= (end - start + 1))
{
start = -1; end = -1;
}
else
{
start_temp = start;
tot_free = end - start + 1;
start = -1; end = -1;
}
}
else{
start_temp = start;
tot_free = end - start + 1;
start = -1; end = -1;
}
}
if(i<maxmem)
{
goto calc;
}
return start_temp;
}


int memfreeWF(int memreq)
{
int i=0, start = -1, end = -1;
int start_temp = -1, tot_free = -1;
calc:
start=-1, end=-1;
for(; i<maxmem; i++)
{
if(mem[i] == 0)
{
start = i;
break;
}
}
for(; i<maxmem; i++)
{
if(mem[i]==0)
{
end = i;
}
else
{
break;
}
}
if ((end - start +1) >= memreq)
{
if (start_temp != -1)
{
if(tot_free >= (end - start + 1))
{
start = -1; end = -1;
}
else
{
start_temp = start;
tot_free = end - start + 1;
start = -1; end = -1;
}
}
else{
start_temp = start;
tot_free = end - start + 1;
start = -1; end = -1;
}
}
if(i<maxmem)
{
goto calc;
}
return start_temp;
}

int checkname(int num)
{
int i;
for(i=0;i<maxmem;i++)
if(mem[i] == num)
return 1;
return 0;
}

void fit(int cho)
{
int ch, jn, jm, temp;
char fnam [5][25] = {"First Fit", "Best Fit", "Worst Fit"};
clrscr();
printf("Enter Total Memory Size: ");
scanf("%d", &maxmem);
do{
clrscr();
printmem(); fflush(stdin);
printf("%s :\n\n1. Enter Job\n2. Remove Job\n3. Compaction\n4. Back\nEnter Your Choise: ", fnam[cho-1]);
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("\nEnter Job Number: "); scanf("%d", &jn);
if((jn<0) || (checkname(jn) == 1))
{
printf("Error, Please enter a valid number!");
getch();
break;
}
printf("Enter required memory: "); scanf("%d", &jm);

switch(cho)
{
case 1:
temp = memfreeFF(jm); break;
case 2:
temp = memfreeBF(jm); break;
case 3:
temp = memfreeWF(jm); break;
}
if(temp == -1)
{
printf("\nInsufficient Memory!\n");
getch();
}
else
memalloc(jn, jm, temp);
break;
case 2:
removejob();
break;
case 3:
comp();
break;
case 4:
return;
default:
printf("Wrong Choise");
getch();
break;
}
}while(ch !=4);
getch();
}

void main()
{
int ch=0;
initmem();
do{
initmem();
fflush(stdin); clrscr();
printf("\nMenu\n");
printf("\n1. First Fit\n2. Best Fit\n3. Worst Fit\n4. Exit\n");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
if ( (ch > 4) || (ch <1) )
printf("Wrong Choise!\n Enter Again : ");
else if (ch != 4)
fit(ch);
else
{
printf("Thanks!");
break;
}
}while(ch != 4);
getch();
}

Tuesday, May 31, 2011

Disk Scheduling Algorithm (Complete)


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

main()
{
int gd=DETECT,gm,x,y,a1,x1,y1,a,b;
int req[30],head,i,k,move,n,ch,t1,t2,max,min,g[30],c,h;
initgraph(&gd,&gm,"z:\tcpp\bgi");
/*int req[30],head,i,k,move,n,ch,t1,t2,max,min,g[30],c,h;*/
clearviewport();
printf("(1) LOOK (2) CLOOK (3) FCFS (0) EXIT
Enter your choice:
");
scanf("%d",&ch);

while(ch)
{
printf("Enter the current head position: ");
scanf("%d",&head); h=head;
printf("Enter the number of requests: ");
scanf("%d",&n); k=0; move=0;
printf("Total number of cylinders: 200.
");
printf("Enter the requests (1 to 200):
");
req:
for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
if(req[i]>200||req[i]<1)
{ printf("Enter requests only between 1-200."); goto req; }
}

switch(ch)
{
case 1:
outtextxy(270,10,"SEEK PATTERN FOR LOOK");
max=req[0]; min=req[0]; t2=1; c=0;
for(i=1;i<n;i++)
{
max=max>req[i]?max:req[i];
min=min<req[i]?min:req[i];
}
if(max<=head) t2=0;
while(k<n)
{
t1=0;
for(i=0;i<n;i++)
{
if(t2)
{
if(req[i]&&req[i]>=head)
{
if(req[i]<req[t1]) t1=i;
if(req[i]==max)
{ t2=0; g[c]=1; c++; }
}
}

else
{
if(req[i]&&req[i]<=head)
{
if(req[i]>req[t1]) t1=i;
if(req[i]==min)
{ t2=1; g[c]=200; c++; }
}
}
}/*for*/
printf("%d ",req[t1]);
g[c]=req[t1]; c++;
move+=abs(head-req[t1]);
req[t1]=0; k++;
head=req[t1];
if(head==max) t2=0;
}/*while*/
break;

case 2:
outtextxy(270,10,"SEEK PATTERN FOR CLOOK");
max=req[0]; min=req[0]; t2=1; c=0;
for(i=1;i<n;i++)
{
max=max>req[i]?max:req[i];
min=min<req[i]?min:req[i];
}
while(k<n)
{
t1=0;
if(!t2) head=min;
for(i=0;i<n;i++)
{
if(req[i]&&req[i]>=head)
{
if(req[i]<req[t1]) t1=i;
else if(req[i]==max)
{ head=1; g[c]=1; c++; }
}
}/*for*/
printf("%d ",req[t1]);
move+=abs(head-req[t1]);
head=req[t1];
g[c]=req[t1]; c++;
req[t1]=0; k++;
if(head==max) t2=0;
}/*while*/
break;

case 3:
outtextxy(270,10,"SEEK PATTERN FOR FCFS");
for(i=0,c=0;i<n;i++,c++)
{
printf("%d ",req[i]);
move+=abs(head-req[i]);
head=req[i];
g[c]=req[i];
}
break;
}/*switch*/
printf("Order of servicing:
");
printf("%d: ",h);
for(i=0;i<n;i++)
printf("%d ",g[i]);
printf("
Total Head Movements: %d
",move);
x=getmaxx();
y=getmaxy();
rectangle(0,20,x-5,y-5);

a1=(x-30)/10;

b=110; a=h+(3*a1); y1=125;
fillellipse(a,b,2,2);


for(i=0;i<n;i++)
{
int x1=g[i]+(3*a1);
fillellipse(x1,y1,2,2);
line(a,b,x1,y1);
a=x1; b=y1;
y1+=15;
}

getch();
clrscr(); clearviewport();
printf("
(1) LOOK (2) CLOOK (3) FCFS (0) EXIT
Enter your choice:
");
scanf("%d",&ch);
}/*while*/
getch();
}/*main*/

Wednesday, May 25, 2011

C-LOOK Disk Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 20
#define cymax 199

int i,j,req,ttl_tracks=0,cp,np,cposn,nposn;
int cyposn[max],temp;

void input()
{
 do
 {
  clreol();
  printf("\n Enter the current header position : ");
  scanf("%d",&cposn);
 }while(cposn>cymax || cposn <=0);
 printf("\n Enter the %d I/O Requests : ",req);
 cyposn[0] = cposn;
 for(i=1;i<=req;i++)
  scanf("%d",&cyposn[i]);
}

void CLOOK()
{
 for(i=0;i<=req;i++)
 {
  for(j=0;j<req-i;j++)
  {
   if(cyposn[j] > cyposn[j+1])
   {
    temp = cyposn[j];
    cyposn[j] = cyposn[j+1];
    cyposn[j+1] = temp;
   }
  }
 }
 cp=0;
 do
 {
  if(cyposn[cp] == cposn)
   break;
  cp++;
 }while(cp!=req);
 printf("\nS.No.  Current Position    Next Position   Displacement \n");
 printf("---------------------------------------------------------- \n\n");
 i=0,j=cp;
 cposn = cyposn[cp];
 do
 {
  if(cp == req)
  { nposn = cyposn[0]; cp = 0; }
  else
   nposn = cyposn[++cp];
  printf(" %d\t\t%d\t\t%d\t\t%d\n",++i,cposn,nposn,abs(cposn-nposn));
  ttl_tracks += (abs(cposn-nposn));
  cposn = nposn == cyposn[req] ? cyposn[0] : nposn ;
 }while(nposn != cyposn[j-1]);
 printf("---------------------------------------------------------- \n\n");
 printf(" Total Tracks Displaced : %d",ttl_tracks);
}

void main()
{
 do
 {
  clrscr();
  printf("\n Enter the number of requests : ");
  scanf("%d",&req);
 }while(req>max || req <=0);
 input();
 CLOOK();
 getch();
}

Saturday, May 21, 2011

FCFS Disk Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 20
#define cymax 199

int i,req,ttl_tracks=0,cposn;
int cyposn[max];

void input()
{
 do
 {
  clreol();
  printf("\n Enter the current header position : ");
  scanf("%d",&cposn);
 }while(cposn>cymax || cposn <=0);
 printf("\n Enter the %d I/O Requests : ",req);
 cyposn[0] = cposn;
 for(i=1;i<=req;i++)
  scanf("%d",&cyposn[i]);
}

void FCFS()
{
 printf("\nS.No.  Current Position    Next Position   Displacement \n");
 printf("---------------------------------------------------------- \n\n");
 for(i=0;i<req;i++)
 {
  printf(" %d\t\t%d\t\t%d\t\t%d\n",i+1,cyposn[i],cyposn[i+1],abs(cyposn[i]-cyposn[i+1]));
  ttl_tracks += (abs(cyposn[i]-cyposn[i+1]));
 }
 printf("---------------------------------------------------------- \n\n");
 printf(" Total Tracks Displaced : %d",ttl_tracks);
}

void main()
{
 do
 {
  clrscr();
  printf("\n Enter the number of requests : ");
  scanf("%d",&req);
 }while(req>max || req <=0);
 input();
 FCFS();
 getch();
}

SSTF Disk Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#include<limits.h>
#include<math.h>
#define max 20
#define cymax 199

int i,j,req,ttl_tracks=0,cp,np,cposn;
int cyposn[max],ind[max],temp;

void input()
{
 do
 {
  clreol();
  printf("\n Enter the current header position : ");
  scanf("%d",&cposn);
 }while(cposn>cymax || cposn <=0);
 printf("\n Enter the %d I/O Requests : ",req);
 cyposn[0] = cposn;
 for(i=1;i<=req;i++)
 {
  scanf("%d",&cyposn[i]);
  ind[i] = 0;
 }

}

void search_short()
{
 int npl,npr,tmpr,tmpl;
 if(cp == 0)
 {
  np = cp;
  while(ind[np] == 1 && np < req)
   np++;
 }
 else if(cp == req)
 {
  np = cp;
  while(ind[np] == 1 && np > 0)
   np--;
 }
 else
 {
  npl = cp;
  while(ind[npl] == 1 && npl > 0)
   npl--;
  if(npl == 0 && ind[npl] == 1)
   tmpl = INT_MAX;
  else
   tmpl = cyposn[npl];
  npr = cp;
  while(ind[npr] == 1 && npr < req)
   npr++;
  if(npl == req && ind[npr] == 1)
   tmpr = INT_MAX;
  else
   tmpr = cyposn[npr];
  if(abs(cyposn[cp] - tmpl) < abs(cyposn[cp] - tmpr))
   np = npl;
  else
   np = npr;
 }
 ind[np] = 1;
}

void SSTF()
{
 for(i=0;i<=req;i++)
 {
  for(j=0;j<req-i;j++)
  {
   if(cyposn[j] > cyposn[j+1])
   {
    temp = cyposn[j];
    cyposn[j] = cyposn[j+1];
    cyposn[j+1] = temp;
   }
  }
 }
 cp=0;
 do
 {
  if(cyposn[cp] == cposn)
   break;
  cp++;
 }while(cp!=req);
 ind[cp] = 1;
 printf("\nS.No.  Current Position    Next Position   Displacement \n");
 printf("---------------------------------------------------------- \n\n");
 i = 0 ;
 do
 {
  search_short();
  printf(" %d\t\t%d\t\t%d\t\t%d\t\t%d\n",i+1,cyposn[cp],cyposn[np],abs(cyposn[cp]-cyposn[np]),np);
  ttl_tracks += (abs(cyposn[cp]-cyposn[np]));
  cp = np;
  i++;
 }while(i!=req);
 printf("---------------------------------------------------------- \n\n");
 printf(" Total Tracks Displaced : %d",ttl_tracks);
}

void main()
{
 do
 {
  clrscr();
  printf("\n Enter the number of requests : ");
  scanf("%d",&req);
 }while(req>max || req <=0);
 input();
 SSTF();
 getch();
}

Thursday, May 12, 2011

LOOK Disk Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 20
#define cymax 199

int i,j,req,ttl_tracks=0,cp,np,cposn,nposn;
int cyposn[max],temp;

void input()
{
 do
 {
  clreol();
  printf("\n Enter the current header position : ");
  scanf("%d",&cposn);
 }while(cposn>cymax || cposn <=0);
 printf("\n Enter the %d I/O Requests : ",req);
 cyposn[0] = cposn;
 for(i=1;i<=req;i++)
  scanf("%d",&cyposn[i]);
}

void LOOK()
{
 int ind = 0;
 for(i=0;i<=req;i++)
 {
  for(j=0;j<req-i;j++)
  {
   if(cyposn[j] > cyposn[j+1])
   {
    temp = cyposn[j];
    cyposn[j] = cyposn[j+1];
    cyposn[j+1] = temp;
   }
  }
 }
 cp=0;
 do
 {
  if(cyposn[cp] == cposn)
   break;
  cp++;
 }while(cp!=req);
 int tmp = cp;
 printf("\nS.No.  Current Position    Next Position   Displacement \n");
 printf("---------------------------------------------------------- \n\n");
 i=0;
 cposn = cyposn[cp];
 do
 {
  if(ind == 0)
  {
   if(cp == 0)
   {
    cp = tmp;
    nposn = cyposn[++cp]; ind = 1;
   }
   else
    nposn = cyposn[--cp];
  }
  else
    nposn = cyposn[++cp];

  printf(" %d\t\t%d\t\t%d\t\t%d\n",++i,cposn,nposn,abs(cposn-nposn));
  ttl_tracks += (abs(cposn-nposn));
  cposn = nposn;
 }while(nposn!=cyposn[req]);
 printf("---------------------------------------------------------- \n\n");
 printf(" Total Tracks Displaced : %d",ttl_tracks);
}

void main()
{
 do
 {
  clrscr();
  printf("\n Enter the number of requests : ");
  scanf("%d",&req);
 }while(req>max || req <=0);
 input();
 LOOK();
 getch();
}

Wednesday, May 11, 2011

C-SCAN Disk Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 20
#define cymax 199

int i,j,req,ttl_tracks=0,cp,np,cposn,nposn;
int cyposn[max],temp;

void input()
{
 do
 {
  clreol();
  printf("\n Enter the current header position : ");
  scanf("%d",&cposn);
 }while(cposn>cymax || cposn <=0);
 printf("\n Enter the %d I/O Requests : ",req);
 cyposn[0] = cposn;
 for(i=1;i<=req;i++)
  scanf("%d",&cyposn[i]);
}

void CSCAN()
{
 for(i=0;i<=req;i++)
 {
  for(j=0;j<req-i;j++)
  {
   if(cyposn[j] > cyposn[j+1])
   {
    temp = cyposn[j];
    cyposn[j] = cyposn[j+1];
    cyposn[j+1] = temp;
   }
  }
 }
 cp=0;
 do
 {
  if(cyposn[cp] == cposn)
   break;
  cp++;
 }while(cp!=req);
 printf("\nS.No.  Current Position    Next Position   Displacement \n");
 printf("---------------------------------------------------------- \n\n");
 i=0,j=cp;
 cposn = cyposn[cp];
 do
 {
  if(cposn == cyposn[req])
  { nposn = 199; cp = -1; }
  else
   nposn = cyposn[++cp];
  printf(" %d\t\t%d\t\t%d\t\t%d\n",++i,cposn,nposn,abs(cposn-nposn));
  ttl_tracks += (abs(cposn-nposn));
  cposn = nposn == 199 ? 0 : nposn;
 }while(nposn != cyposn[j-1]);
 printf("---------------------------------------------------------- \n\n");
 printf(" Total Tracks Displaced : %d",ttl_tracks);
}

void main()
{
 do
 {
  clrscr();
  printf("\n Enter the number of requests : ");
  scanf("%d",&req);
 }while(req>max || req <=0);
 input();
CSCAN();
 getch();
}

Monday, May 2, 2011

SCAN Disk Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 20
#define cymax 199

int i,j,req,ttl_tracks=0,cp,np,cposn,nposn;
int cyposn[max],temp;

void input()
{
 do
 {
  clreol();
  printf("\n Enter the current header position : ");
  scanf("%d",&cposn);
 }while(cposn>cymax || cposn <=0);
 printf("\n Enter the %d I/O Requests : ",req);
 cyposn[0] = cposn;
 for(i=1;i<=req;i++)
  scanf("%d",&cyposn[i]);



}

void SCAN()
{
 int ind = 0;
 for(i=0;i<=req;i++)
 {
  for(j=0;j<req-i;j++)
  {
   if(cyposn[j] > cyposn[j+1])
   {
    temp = cyposn[j];
    cyposn[j] = cyposn[j+1];
    cyposn[j+1] = temp;
   }
  }
 }
 cp=0;
 do
 {
  if(cyposn[cp] == cposn)
   break;
  cp++;
 }while(cp!=req);
 int tmp = cp;
 printf("\nS.No.  Current Position    Next Position   Displacement \n");
 printf("---------------------------------------------------------- \n\n");
 i=0;
 cposn = cyposn[cp];
 do
 {
  if(ind == 0)
  {
   if(cp == 0)
   { nposn = 0; ind = 1; }
   else
    nposn = cyposn[--cp];
  }
  else
  {
   if(cp == 0)
    cp = tmp;
   nposn = cyposn[++cp];
  }


  printf(" %d\t\t%d\t\t%d\t\t%d\n",++i,cposn,nposn,abs(cposn-nposn));
  ttl_tracks += (abs(cposn-nposn));
  cposn = nposn;
 }while(nposn!=cyposn[req]);
 printf("---------------------------------------------------------- \n\n");
 printf(" Total Tracks Displaced : %d",ttl_tracks);
}

void main()
{
 do
 {
  clrscr();
  printf("\n Enter the number of requests : ");
  scanf("%d",&req);
 }while(req>max || req <=0);
 input();
 SCAN();
 getch();
}