Sie sind auf Seite 1von 19

Assignment 2(Lab)

Submitted To :
Mam Fatima

Submitted By:
Syed Muhammad Ali

Section:
A
Sap Id:
70068893
Question No 1:
Round Robin Scheduling ?

Answer:
Code:( Part a, b c and d solution):
#include<stdio.h>
int at[100],bt[100],rt[100],temp[100];
float wait_time=0,turn_time=0;
void main()
{
int c,j,n,time,r,flag=0,time_q,ltt,i,wt=0;
printf("Enter no.of process:");
scanf("%d",&n);
r=n;
for(c=0;c<n;c++)
{
printf("Enter arrival time of p%d:\t",c+1);
scanf("%d",&at[c]);
printf("Enter burst time of p%d: \t",c+1);
scanf("%d",&bt[c]);
rt[c]=bt[c];
temp[c]=bt[c];
printf("\n");
}
printf("Enter time quantum:\t");
scanf("%d",&time_q);
printf("\n\n\tprocess\tAT\tTAT\tWT \n\n");
for(time=0,c=0;r!=0;)
{
if(rt[c]<=time_q && rt[c]>0)
{
time=time+rt[c];
rt[c]=0;
flag=1;
}
else if (rt[c]>0)
{
rt[c]=rt[c]-time_q;
time=time+time_q;
}
if(rt[c]==0 && flag==1)
{
wt=0;
wt=time-at[c]-bt[c];
r--;
printf("\tP%d\t%d\t%d\t%d\n",c+1,at[c],time-at[c],wt);
ltt=time-at[c];
wait_time=wait_time+time-at[c]-bt[c];
turn_time=turn_time+time-at[c];
flag=0;
}
if(c==n-1)
c=0;
else if(at[c+1]<=time)
c++;
else
c=0;
}
j=0;
printf("\n\n\n");
printf("Gantt Chart ");
printf("\n\n\n");
printf("\t");
for(i=at[0];i<time;)
{
if(bt[j]>=time_q)
{
printf("P%d\t",j+1);
i+=time_q;
bt[j]=bt[j]-time_q;
}
else if(bt[j]>0)
{
printf("p%d\t",j+1);
i+=bt[j];
bt[j]=0;
}
j++;
if(j>=n)
{
j=0;
}
}
printf("\n");

j=0;
printf("\t");
for(i=at[0];i<time;)
{
if(temp[j]>=time_q)
{
printf("%d\t",i+time_q);
i+=time_q;
temp[j]=temp[j]-time_q;
}
else if(temp[j]>0)
{
printf("%d\t",i+temp[j]);
i+=temp[j];
temp[j]=0;
}
j++;
if(j>=n)
{
j=0;
}
}

printf("\n\n\n");
printf("\nAverage_waiting_time=%f\n",wait_time/n);
printf("Average_turn_around_time=%f\n",turn_time/n);
printf("\n\n");
}

Part E Solution:
In Round Robin Scheduling the time quantum is fixed and then processes are
scheduled such that no process get CPU time more than one time quantum in one go. If
time quantum is too large, the response time of the processes is too much which may
not be tolerated in interactive environment. If time quantum is too small, it causes
unnecessarily frequent context switch leading to more overheads resulting in less
throughput. In this paper a method using fuzzy logic has been proposed that decides a
value that is neither too large nor too small such that every process has got reasonable
response time and the throughput of the system is not decreased due to unnecessarily
context switches.

Output:(part a,b,c,d)
Question NO 2:
Shortest Remaining First
Solution:(Code Part a,b,c)
#include<stdio.h>
struct sjf
{
int bt,at,wt,st,pno,tt,cbt;
};

int get(struct sjf arr[],int t,int n)


{int imin,min=9999,i;
for(i=0;i<n;i++)
{
if(arr[i].at<=t&&arr[i].st==0)
if(min>arr[i].bt)
{
min=arr[i].bt;
imin=i;
} }
return imin;
}
void gantt_chart(struct sjf arr[],int p[],int n,int nop)
{
int i,a[100],s=0;
float avgtt=0,avgwt=0;
printf("***************************************\n");
printf("GANTT CHART\n");
printf("0");
for(i=0;i<n-1;i++)
{
while(i<n-1&&p[i]==p[i+1])
{
s++;
i++;
}
s++;
printf(" -> [P%d] <- %d",arr[p[i]].pno,s);
arr[p[i]].wt=s-arr[p[i]].at-arr[p[i]].tt;
}
for(i=0;i<nop;i++)
{
arr[i].tt+=arr[i].wt;
avgwt+=arr[i].wt;
avgtt+=arr[i].tt;
}
printf("\n***************************************\n");
printf("Pro\tArTi\tBuTi\tTaTi\tWtTi\n");
printf("***************************************\n");
for(i=0;i<nop;i++)
{
printf("[P%d]\t%d\t%d\t%d\t%d\n",arr[i].pno,arr[i].at,arr[i].cbt,arr[i].tt,arr[i].wt);
}
printf("***************************************\n");
avgwt = avgwt/nop;
avgtt = avgtt/nop;
printf("Average Waiting Time : %.2f\n",avgwt);
printf("Average Turnaround Time : %.2f\n",avgtt);
return;
}

int iscomplite(struct sjf arr[],int n)


{
int i;
for(i=0;i<n;i++)
if(arr[i].st==0)
return 0;
return 1;
}
int main()
{
int n,i,a,t=0;
int p[100];
float avgwt=0,avgtt=0;
struct sjf arr[100];
printf("SJF (Shortest Job First) - Preemptive\n");
printf("Note -\n1. Arrival Time of at least one process should be 0\n2. CPU should never be
idle\n");
printf("Enter Number of Processes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Arrival Time & Burst Time for Process [P%d]\n",i);
scanf("%d%d",&arr[i].at,&arr[i].bt);
arr[i].pno = i;
arr[i].cbt = arr[i].bt;
arr[i].st=0;
arr[i].tt=arr[i].bt;
arr[i].wt=0;
}
i=0;
while(1)
{
if(iscomplite(arr,n))
break;
a=get(arr,t,n);
p[i]=a;
arr[a].bt-=1;
if(arr[a].bt==0)
arr[a].st=1;
t=t+1;
i++;
}
gantt_chart(arr,p,i,n);
return 0;
}

Output(Part a,b,c):
Question No 3:
Priority Scheduling:
Solution for a, b ,c and d(Code):
#include<stdio.h>
void main()
{
int n,i,j,TEMP,TEMP1,TEMP2,TEMP3,TEMP4;
float WTSUM=0,TATSUM=0;
int bt[10],at[10],P[10],ct[10],tat[10],wt[10],pt[10];
printf("enter the no of process\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the arrival time of p%d:\t",i);
scanf("%d",&at[i]);
printf("Enter burst time of p%d: \t",i);
scanf("%d",&bt[i]);
printf("Enter the priority :\t");
scanf("%d",&pt[i]);
P[i]=i;
printf("\n");
}
ct[0]=0;
if(bt[1]>=at[n])
{
ct[1]=bt[1]+at[1];
tat[1]=ct[1]-at[1];
wt[1]=tat[1]-bt[1];
WTSUM=wt[1];
TATSUM=tat[1];
for(i=2;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(pt[j]<pt[i])
{
TEMP4=pt[i];
pt[i]=pt[j];
pt[j]=TEMP4;
TEMP1=bt[i];
bt[i]=bt[j];
bt[j]=TEMP1;
TEMP2=at[i];
at[i]=at[j];
at[j]=TEMP2;
TEMP3=P[i];
P[i]=P[j];
P[j]=TEMP3;
}
}
if(ct[i-1]<at[i])
{
TEMP=at[i]-ct[i-1];
ct[i]=ct[i-1]+bt[i]+TEMP;
TEMP1=bt[i];
}
else
{
ct[i]=ct[i-1]+bt[i];
}
tat[i]=ct[i]-at[i];
wt[i]=tat[i]-bt[i];
WTSUM=WTSUM+wt[i]+wt[1];
TATSUM=TATSUM+tat[i]+tat[1];
}
}
if(at[n]==0)
{
ct[0]=0;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(pt[j]<pt[i])
{
TEMP4=pt[i];
pt[i]=pt[j];
pt[j]=TEMP4;
TEMP1=bt[i];
bt[i]=bt[j];
bt[j]=TEMP1;
TEMP2=at[i];
at[i]=at[j];
at[j]=TEMP2;
}
}
if(ct[i-1]<at[i])
{

TEMP=at[i]-ct[i-1];
ct[i]=ct[i-1]+bt[i]+TEMP;
TEMP1=bt[i];
}
else
{
ct[i]=ct[i-1]+bt[i];
}
tat[i]=ct[i]-at[i];
wt[i]=tat[i]-bt[i];
WTSUM=WTSUM+wt[i];
TATSUM=TATSUM+tat[i];
}
}
printf("\n\n\n");
printf("\tPROCESS\tBT\tAT\tPT\tTAT\tWT\n");
for(i=1;i<=n;i++)
{
printf("\tP%d\t%d\t%d\t%d \t%d\t%d\n\n",P[i],bt[i],at[i],pt[i],tat[i],wt[i]);
}
printf("Average_waiting_time = %f\n",WTSUM/n);
printf("Average_turn_around_time= %f\n",TATSUM/n);
printf("\n\n");
printf("Gantt chart\n");
for(i=1;i<=n;i++)
{
printf("\tP%d\t",P[i]);
}
printf("\n");
for(i=1;i<=n;i++)
{
printf("\t%d\t",ct[i]);
}
}

Output(Part a,b,c,d)
Question No 4:
MultipleLevel Queue:
Soluition(for a,b,c)Code:
#include<stdio.h>
#include<conio.h>
#include<unistd.h>
#include<stdlib.h>
main()
{
int p[20],bt[20], su[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
system("clear");
printf("Enter the number of processes --- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i] = i;
printf("Enter the Burst Time of Process %d --- ", i);
scanf("%d",&bt[i]);
printf("System/User Process (0/1) ? --- ");
scanf("%d", &su[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(su[i] > su[k])
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=su[i];
su[i]=su[k];
su[k]=temp;
}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\t SYSTEM/USER PROCESS \tBURST TIME\tWAITING TIME\tTURNAROUND
TIME");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],su[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);
printf("\nAverage Turnaround Time is --- %f",tatavg/n);
getch();
}

Output(Part a,b,c)

Das könnte Ihnen auch gefallen