Mong mọi người giúp đỡ bài thực hành Lab4 này ,thanks nhiều ạ…
3. Chạy thử chương trình mô phỏng giải thuật FCFS sau:
Yêu cầu:
- Thực thi đoạn chương trình trên : Nhập dữ liệu input và xuất các giải trị ouput ra màn hình.
- Vẽ sơ đồ giải thuật và giải thích ý nghĩa đoạn code
- Thêm vào đoạn code trên để tính Average waiting time và Aveage Turn Around time
#include<stdio.h>
#include<conio.h>
void main()
{
char pn[10][10]; //process name
int arr[10], //arrival time
bur[10], //burst time
star[10], //start time
finish[10], //finish time
tat[10] //turn around time
,wt[10],i,n; //waiting time
int totwt=0, // total of waiting times
tottat=0; // total of turn around times
clrscr();
printf(“Enter the number of processes:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“Enter the Process Name, Arrival Time & Burst Time:”);
scanf(“%s %d %d”,&pn*,&arr*,&bur*);
}
for(i=0;i<n;i++)
{
if(i==0)
star*=arr*;
else
star*=finish[i-1];
wt*=star*-arr*;
finish*=star*+bur*;
tat*=finish*-arr*;
}
printf("
PName Arrtime Burtime Start TAT Finish");
for(i=0;i<n;i++)
{
printf("
%s %6d %6d %6d %6d %6d",pn*,arr*,bur*,star*,tat*,finish*);
totwt+=wt*;
tottat+=tat*;
}
- Viết chương trình mô phỏng giải thuật định thời còn lại với các yêu cầu input và output sau:
- Với giải thuật SJF, Priority :
Input:
Nhập: Số Process
Nhập: Process Name, Arrival Time & Burst Time
Output:
Process Name, Arrival time, Execution time, Waitingtime, Tatime
Average Waiting Time
Average Turn Around Time - Với giải thuật RR:
Input:
Nhập: Số Process
Nhập: Thời gian quantum
Nhập: Tên process & estimated time
Output:
Estimated Time
// SJF.cpp : Defines the entry point for the console application.
//
#include “stdafx.h”
#include<stdio.h>
#include<conio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf(“Enter number of process:”);
scanf_s(“%d”,&n);
printf(“nEnter Burst Time:n”);
for(i=0;i<n;i++)
{
printf(“p%d:”,i+1);
scanf_s(“%d”,&bt*);
p*=i+1; //contains process number
}
//sorting burst time in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt*;
bt*=bt[pos];
bt[pos]=temp;
temp=p*;
p*=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process will be zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt*=0;
for(j=0;j<i;j++)
wt*+=bt[j];
total+=wt*;
}
avg_wt=(float)total/n; //average waiting time
total=0;
printf("
Process Burst Time Waiting Time Turnaround Time");
for(i=0;i<n;i++)
{
tat*=bt*+wt*; //calculate turnaround time
total+=tat*;
printf("
p%d %d %d %d
",p*,bt*,wt*,tat*);
}
avg_tat=(float)total/n; //average turnaround time
printf("Average Waiting Time=%f
“,avg_wt);
printf(“Average Turnaround Time=%f
“,avg_tat);
_getch();
}
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
void main()
{
char p[10][5];
int et[10],wt[10],timer=3,count,pt[10],rt,i,j,totwt=0,t,n=5,found=0,m;
float avgwt;
clrscr();
for(i=0;i<n;i++)
{
printf(“enter the process name : “);
scanf(”%s”,&p*);
printf(“enter the processing time : “);
scanf(”%d”,&pt*);
}
m=n;
wt[0]=0;
i=0;
do
{
if(pt*>timer)
{
rt=pt*-timer;
strcpy(p[n],p*);
pt[n]=rt;
et*=timer;
n++;
}
else
{
et*=pt*;
}
i++;
wt*=wt[i-1]+et[i-1];
}while(i<n);
count=0;
for(i=0;i<m;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(p*,p[j])==0)
{
count++;
found=j;
}
}
if(found!=0)
{
wt*=wt[found]-(counttimer);
count=0;
found=0;
}
}
for(i=0;i<m;i++)
{
totwt+=wt;
}
avgwt=(float)totwt/m;
for(i=0;i<m;i++)
{
printf(”
%s %d %d”,p*,pt*,wt*);
}
printf(”
total waiting time %d
",totwt);
printf(“total avgtime %f”,avgwt);
}