Anda di halaman 1dari 13

1. Write a program for first come first serve (FCFS) CPU scheduling algorithm.

#include<stdio.h>
#include<conio.h>
void main()
{
int t[50],p[50],w[50],i,n;
float a,s=0.0;
clrscr();
printf("Enter number of processes:");
scanf("%d",&n);
printf("Enter process and their burst time:");
for(i=0;i<n;i++)
{
scanf("%s%d",p[i],&t[i]);
if(t[i]==0)
{
printf("Invalid process burst time please enter again:");
i--;
}
}
w[0]=0;
for(i=0;i<n;i++)
{
w[i+1]=w[i]+t[i];
printf("\nWating time for process %d is %d",i,w[i]);
}

printf("\nAverage waiting time is:");


for(i=0;i<n;i++)
s=s+w[i];
printf("%f",a=s/n);
getch();
}

2. Write a program for shortest job First (SJF) CPU Scheduling algorithm.

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

void main()
{
int i, j, n, process[10], total=0, wtime[10], ptime[10], temp, ptemp;
float avg=0;
clrscr();
printf("\nEnter number of Processes:");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Process %d ID:",i+1);
scanf("%d", &process[i]);
printf("\nEnter Process %d Time:",i+1);
scanf("%d",&ptime[i]);
}

for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(ptime[i]>ptime[j])
{
temp = ptime[i];

ptime[i] = ptime[j];
ptime[j] = temp;
ptemp = process[i];
process[i] = process[j];
process[j] = ptemp;
}
}
}
wtime[0]=0;
for(i=1;i<n;i++)
{
wtime[i]=wtime[i-1]+ptime[i-1];
total=total+wtime[i];
}
avg=(float)total/n;
printf("\nP_ID\t P_TIME\t W_TIME\n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\n",process[i],ptime[i],wtime[i]);
printf("\nTotal Waiting Time: %d \nAverage Waiting Time: %f", total, avg);
getch();
}

3. Write a program for Priority Based CPU Scheduling algorithm.


#include<stdio.h>

int main()
{
int p[30],pr[30],key,loc,bt[30],temp,max,wt[30],ta[30],sum=0,i,j,n;
wt[0]=0;
printf("enter the number of processes=");
scanf("%d",&n);
printf("enter the burst time for each process");
for(i=0;i<n;i++)
{
p[i]=i+1;
printf("\np%d=",i+1);
scanf("%d",&bt[i]);
printf("\t priority of p%d=",i+1);
scanf("%d",&pr[i]);
}

for(i=0;i<n;i++)
{ max=i;
for(j=i+1;j<n;j++)
{
if(pr[j] <pr[max])
max=j;
}
temp=pr[max];
pr[max]=pr[i];
pr[i]=temp;

temp=bt[max];
bt[max]=bt[i];
bt[i]=temp;
temp=p[max];
p[max]=p[i];
p[i]=temp;
}

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

wt[i+1]=bt[i]+wt[i];

ta[i]=bt[i]+wt[i];
sum+=ta[i];
}
for(i=0;i<n;i++)
{
printf("\n waiting time for p[%d]=%d",p[i],wt[i]);
printf("\t turn around time for p[%d]=%d",p[i],ta[i]);
}
printf("\n\n average turn around=%d",sum/n);
return 1;
}

4. Write a program for Round Robin (RR) CPU Scheduling algorithm.


Round Robin Scheduling Algorithm
1. The queue structure in ready queue is of First In First Out (FIFO) type.
2. A fixed time is allotted to every process that arrives in the queue. This fixed time is
known as time slice or time quantum.
3. The first process that arrives is selected and sent to the processor for execution. If it
is not able to complete its execution within the time quantum provided, then an
interrupt is generated using an automated timer.
4. The process is then stopped and is sent back at the end of the queue. However, the
state is saved and context is thereby stored in memory. This helps the process to
resume from the point where it was interrupted.
5. The scheduler selects another process from the ready queue and dispatches it to the
processor for its execution. It is executed until the time Quantum does not exceed.
6. The same steps are repeated until all the process are finished.
The round robin algorithm is simple and the overhead in decision making is very low. It
is the best scheduling algorithm for achieving better and evenly distributed response
time.

Example

Lets take one example to understand it.


Time Quantum = 2

Process

Burst Time

Execution Time

P1

P2

P3

P4

5.

Normalized
Arrival

Burst

Turnaround

Turnaround

Waiting

Process

Time

Time (x)

Time(t)

Time(t/x)

Time

P1

21

2.34

12

P2

17

3.4

12

P3

11

3.67

P4

12

Average Turnaround Time = 15.25


Average Normalized Turnaround Time = 3.10
Average Waiting Time = 10

#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;
}

#include<stdio.h>
#include<conio.h>
main()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
float awt=0.0,atat=0.0;
clrscr();
printf("Enter number of processes:");
scanf("%d",&n);
printf("Enter burst time for sequences:");
for(i=0;i<n;i++)
{

scanf("%d",&bt[i]);
st[i]=bt[i];
}
printf("Enter time quantum:");
scanf("%d",&tq);
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}

if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("Process_no

Burst time

Wait time

Turn around time

");
for(i=0;i<n;i++)
printf("%d

%d

%d

%d

",i+1,bt[i],wt[i],tat[i]);
printf("Avg wait time is %f
Avg turn around time is %f",awt,atat);
getch();
}
http://www.sourcecodesworld.com/source/show.asp?ScriptID=839

Anda mungkin juga menyukai