0% found this document useful (0 votes)
40 views

Lab Programs

The document contains code for implementing different disk scheduling algorithms - First Come First Serve (FCFS), SCAN, CSCAN and LOOK. FCFS calculates the total seek count by moving the disk head to service requests in the order they arrive. SCAN and CCAN improve on FCFS by serving requests in ascending or descending order of cylinder numbers. LOOK serves the nearest request and skips others. The code takes input of request sequence, initial head position and disk size, then calculates and prints the total disk head movement for each algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Lab Programs

The document contains code for implementing different disk scheduling algorithms - First Come First Serve (FCFS), SCAN, CSCAN and LOOK. FCFS calculates the total seek count by moving the disk head to service requests in the order they arrive. SCAN and CCAN improve on FCFS by serving requests in ascending or descending order of cylinder numbers. LOOK serves the nearest request and skips others. The code takes input of request sequence, initial head position and disk size, then calculates and prints the total disk head movement for each algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

FCFS

void main()
{
int Pid[10],AT[10],BT[10],i,j,n,t,CT=0;
printf("Enter Number of Processes\n");
scanf("%d",&n);
printf("\nEnter Process Id : \n");
for(i=0;i<n;i++)
scanf("%d",&Pid[i]);
printf("\nEnter Arrival Time : \n");
for(i=0;i<n;i++)
scanf("%d",&AT[i]);
printf("\nEnter Burst Time : \n");
for(i=0;i<n;i++)
scanf("%d",&BT[i]);
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
{
if(AT[j]>AT[j+1])
{
t=Pid[j];
Pid[j]=Pid[j+1];
Pid[j+1]=t;

t=AT[j];
AT[j]=AT[j+1];
AT[j+1]=t;

t=BT[j];
BT[j]=BT[j+1];
BT[j+1]=t;
}
}
printf("\nPROCESS\t\tARRIVAL TIME\tBURST TIME\tCOMPLETION TIME\tTURN
AROUND TIME\tWAITING TIME");
CT=AT[0];
for(i=0;i<n;i++)
{
CT+=BT[i];
printf("\nprocess[%d]\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n",Pid[i],
AT[i],BT[i],CT,CT-AT[i],CT-AT[i]-BT[i]);
}}
SJF
#include<stdio.h>
int n,i,j,bt[30],ct[30],tat[30],wt[30],p[30],t;
void main()
{
printf("Enter no.of processes\n");
scanf("%d",&n);
printf("Enter process id\n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter burst time for each process\n");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(bt[j]>bt[j+1])
{
t=bt[j];
bt[j]=bt[j+1];
bt[j+1]=t;

t=p[j];
p[j]=p[j+1];
p[j+1]=t;
}
}
}
ct[0]=bt[0];
for(i=1;i<n;i++)
ct[i]=ct[i-1]+bt[i];
for(i=0;i<n;i++)
tat[i]=ct[i];
for(i=0;i<n;i++)
wt[i]=tat[i]-bt[i];
printf("PROCESSID\tBURSTTIME\tCOMPLETIONTIME\tWAITINGTIME\tTURNAROUN
DTIME\n");

for(i=0;i<n;i++)
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n",p[i],bt[i],ct[i],wt[i],t
at[i]);
}
Priority
#include<stdio.h>
int n,i,j,prio[30],bt[30],ct[30],tat[30],wt[30],p[30],t;
void main()
{
printf("Enter no.of processes\n");
scanf("%d",&n);
printf("Enter process id\n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter priority of each process\n");
for(i=0;i<n;i++)
scanf("%d",&prio[i]);
printf("Enter burst time for each process\n");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(prio[j]>prio[j+1])
{
t=prio[j];
prio[j]=prio[j+1];
prio[j+1]=t;

t=bt[j];
bt[j]=bt[j+1];
bt[j+1]=t;

t=p[j];
p[j]=p[j+1];
p[j+1]=t;
}
}
}
ct[0]= bt[0];
for(i=1;i<n;i++)
ct[i]= ct[i-1]+bt[i];
for(i=0;i<n;i++)
tat[i]=ct[i];
for(i=0;i<n;i++)
wt[i]=tat[i]-bt[i];
printf("PROCESSID\tPRIORITY\tBT\tCT\tWT\tTAT\n")
for(i=0;i<n;i++)
printf("%d\t\t%d\t\t%d\t%d\t%d\t%d\n",p[i],prio[i],bt[i],ct[i],wt[
i],tat[i]);
}
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d
:",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[
count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);return 0;}
BANKERS
#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here
int avail[10], alloc[10][10], max[10][10], need[10][10],
maxres[10], m, n, i,j,k,sum;

printf("\nEnter the number of processes and the number of


resources:\n");
scanf("%d%d", &n, &m);
printf("\nEnter maximum instances of resources\n");
for (j = 0; j < m; j++)
{
scanf("%d", &maxres[j]);
avail[j] = maxres[j];
}
printf("\nEnter the Allocated Matrix:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
scanf("%d", &alloc[i][j]);
}
printf("\nEnter the Max Matrix:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
scanf("%d", &max[i][j]);
need[i][j] = max[i][j] - alloc[i][j];
}
}
printf("\nThe Need Matrix is:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
printf("%d ", need[i][j]);
printf("\n");
}
for (j = 0; j < m; j++) //calculating available matrix after
allocation
{
sum = 0;
for (i = 0; i < n; i++)
sum += alloc[i][j];
avail[j] -= sum;
}
int finish[10],safeseq[10], ind = 0;
for (k = 0; k < n; k++) {
finish[k] = 0;}

int y = 0;
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
if (finish[i] == 0)
{
int flag = 0;
for (j = 0; j < m; j++)
{
if (need[i][j] > avail[j])
{
flag = 1;
break;
}
}

if (flag == 0)
{
safeseq[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
finish[i] = 1;
//printf("i=%d\n",i);
}
}
}
}

for (i = 0; i < n; i++)


if (finish[i] == 0)
{
printf("system is in unsafe state.");
return(0);
}
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++)
printf(" P%d ->", safeseq[i]);
printf(" P%d", safeseq[n - 1]);
return(0);
}
DISK SCHEDULING

FCFS
#include<stdio.h>
#include<stdlib.h>
int main()
{
int Q[100],i,n,seekcount=0,head;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
{
scanf("%d",&Q[i]);
}
printf("Enter initial head position\n");
scanf("%d",&head);
for(i=0;i<n;i++)
{
seekcount+=abs(Q[i]-head);
head=Q[i];
}
printf("Total seekcount is %d",seekcount);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[500],i,j,n,TotalHeadMoment=0,initial,size;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
int index;
for(i=0;i<n;i++)
if(initial<RQ[i])
{
index=i;
break;
}
for(i=index;i<n;i++)
{
TotalHeadMoment = TotalHeadMoment+abs(RQ[i]-initial);
printf("\n%d",TotalHeadMoment);
initial=RQ[i];
}
// last movement for max size
TotalHeadMoment = TotalHeadMoment+abs(size-RQ[i-1]-1);
initial = size-1;
for(i=index-1;i>=0;i--)
{
TotalHeadMoment = TotalHeadMoment+abs(RQ[i]-initial);
printf("\n%d",TotalHeadMoment);
initial=RQ[i];
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}
CSCAN
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,t,temp,head,n,size,Q[100],seek=0,index;
printf("\nEnter the number of requests :");
scanf("%d",&n);
printf("\nEnter the requests :");
for(i=0;i<n;i++)
scanf("%d",&Q[i]);
printf("\nEnter the head pointer :");
scanf("%d",&head);
printf("\nEnter the disk size :");
scanf("%d",&size);
temp=size-1;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(Q[i]>Q[j])
{
t=Q[i];
Q[i]=Q[j];
Q[j]=t;
}
}
}
for(i=0;i<n;i++)
{
if(Q[i]>head)
{
index=i;
break;
}
}
for(i=index;i<n;i++)
{
seek=seek+abs(Q[i]-head);
head=Q[i];
}
seek=seek+abs(Q[i-1]-temp)+temp+Q[0];
temp=Q[0];
for(i=1;i<index;i++)
{
seek=seek+abs(temp-Q[i]);
temp=Q[i];
}
printf("\nTotal seek time = %d",seek);
return 0;}

You might also like