#include<stdio.h>
#define MAX 20
int frames[MAX],ref[MAX],mem[MAX][MAX],faults,
sp,m,n,count[MAX];
void accept()
{
int i;
printf("Enter no.of frames:");
scanf("%d", &n);
printf("Enter no.of references:");
scanf("%d", &m);
printf("Enter reference string:\n");
for(i=0;i<m;i++)
{
printf("[%d]=",i);
scanf("%d",&ref[i]);
}
}
void disp()
{
int i,j;
for(i=0;i<m;i++)
printf("%3d",ref[i]);
printf("\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf(" ");
}
printf("\n");
}
printf("Total Page Faults: %d\n",faults);
}
int search(int pno)
{
int i;
for(i=0;i<n;i++)
{
if(frames[i]==pno)
return i;
}
return -1;
}
int get_lfu(int sp)
{
int i,min_i,min=9999;
i=sp;
do
{
if(count[i]<min)
{
min = count[i];
min_i = i;
}
i=(i+1)%n;
}while(i!=sp);
return min_i;
}
void lfu()
{
int i,j,k;
for(i=0;i<m && sp<n;i++)
{
k=search(ref[i]);
if(k==-1)
{
frames[sp]=ref[i];
count[sp]++;
faults++;
sp++;
for(j=0;j<n;j++)
mem[j][i]=frames[j];
}
else
count[k]++;
}
sp=0;
for(;i<m;i++)
{
k = search(ref[i]);
if(k==-1)
{
sp = get_lfu(sp);
frames[sp] = ref[i];
count[sp]=1;
faults++;
sp = (sp+1)%n;
for(j=0;j<n;j++)
mem[j][i] = frames[j];
}
else
count[k]++;
}
}
int main()
{
accept();
lfu();
disp();
return 0;
}
#define MAX 20
int frames[MAX],ref[MAX],mem[MAX][MAX],faults,
sp,m,n,count[MAX];
void accept()
{
int i;
printf("Enter no.of frames:");
scanf("%d", &n);
printf("Enter no.of references:");
scanf("%d", &m);
printf("Enter reference string:\n");
for(i=0;i<m;i++)
{
printf("[%d]=",i);
scanf("%d",&ref[i]);
}
}
void disp()
{
int i,j;
for(i=0;i<m;i++)
printf("%3d",ref[i]);
printf("\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf(" ");
}
printf("\n");
}
printf("Total Page Faults: %d\n",faults);
}
int search(int pno)
{
int i;
for(i=0;i<n;i++)
{
if(frames[i]==pno)
return i;
}
return -1;
}
int get_lfu(int sp)
{
int i,min_i,min=9999;
i=sp;
do
{
if(count[i]<min)
{
min = count[i];
min_i = i;
}
i=(i+1)%n;
}while(i!=sp);
return min_i;
}
void lfu()
{
int i,j,k;
for(i=0;i<m && sp<n;i++)
{
k=search(ref[i]);
if(k==-1)
{
frames[sp]=ref[i];
count[sp]++;
faults++;
sp++;
for(j=0;j<n;j++)
mem[j][i]=frames[j];
}
else
count[k]++;
}
sp=0;
for(;i<m;i++)
{
k = search(ref[i]);
if(k==-1)
{
sp = get_lfu(sp);
frames[sp] = ref[i];
count[sp]=1;
faults++;
sp = (sp+1)%n;
for(j=0;j<n;j++)
mem[j][i] = frames[j];
}
else
count[k]++;
}
}
int main()
{
accept();
lfu();
disp();
return 0;
}
6 Comments
WmenbaWrie_da Troy Schorfheide https://www.kadirajenningsart.com/profile/wicahpeepekericonchita/profile
ReplyDeletetebanbankca
Optimal page replacement algorithm
ReplyDeleteExample- Reference string 2 3 4 2 1 3 7 5 4 3 .using 3 frames ,define page faults.
#include
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter page reference string: ");
for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i){
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i){
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
flag3 =0;
for(j = 0; j < no_of_frames; ++j){
temp[j] = -1;
for(k = i + 1; k < no_of_pages; ++k){
if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
}
for(j = 0; j < no_of_frames; ++j){
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0){
max = temp[0];
pos = 0;
for(j = 1; j < no_of_frames; ++j){
if(temp[j] > max){
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
LRU .
ReplyDeleteExample- Enter reference string: 5 7 5 6 7 3 using 3 frames. calculate page faults
#include
int findLRU(int time[], int n){
int i, minimum = time[0], pos = 0;
for(i = 1; i < n; ++i){
if(time[i] < minimum){
minimum = time[i];
pos = i;
}
}
return pos;
}
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j, pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter reference string: ");
for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i){
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i){
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == pages[i]){
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
ReplyDelete2.Round robin
Example:
Following is the example of round robin scheduling.
Process Id Arrival Time Burst Time
P1 0 10
P2 1 8
P3 2 7
Time Slot is 5 Sec.
Execution of all processes completed
Process Id Burst Time Wait Time Turn Around Time
P1 10 20 10
P2 8 22 14
P3 7 23 16
Average Waiting Time = (20 + 22 + 23)/3 = 65/3 = 21.666666
Average Turnaround Time = (10 + 14 + 16)/3 = 40/3 = 13.333333
Program/Source Code
Here is the source code of the C program to implement Round Robin scheduling. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
1. /*
2. * Round Robin Scheduling Program in C
3. */
4.
5. #include
6.
7. int main()
8. {
9. //Input no of processed
10. int n;
11. printf("Enter Total Number of Processes:");
12. scanf("%d", &n);
13. int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n], temp_burst_time[n];
14. int x = n;
15.
16. //Input details of processes
17. for(int i = 0; i < n; i++)
18. {
19. printf("Enter Details of Process %d \n", i + 1);
20. printf("Arrival Time: ");
21. scanf("%d", &arr_time[i]);
22. printf("Burst Time: ");
23. scanf("%d", &burst_time[i]);
24. temp_burst_time[i] = burst_time[i];
25. }
26.
27. //Input time slot
28. int time_slot;
29. printf("Enter Time Slot:");
30. scanf("%d", &time_slot);
31.
32. //Total indicates total time
33. //counter indicates which process is executed
34. int total = 0, counter = 0,i;
35. printf("Process ID Burst Time Turnaround Time Waiting Time\n");
36. for(total=0, i = 0; x!=0; )
37. {
38. // define the conditions
39. if(temp_burst_time[i] <= time_slot && temp_burst_time[i] > 0)
40. {
41. total = total + temp_burst_time[i];
42. temp_burst_time[i] = 0;
43. counter=1;
44. }
45. else if(temp_burst_time[i] > 0)
46. {
47. temp_burst_time[i] = temp_burst_time[i] - time_slot;
48. total += time_slot;
49. }
50. if(temp_burst_time[i]==0 && counter==1)
51. {
52. x--; //decrement the process no.
53. printf("\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d", i+1, burst_time[i],
54. total-arr_time[i], total-arr_time[i]-burst_time[i]);
55. wait_time = wait_time+total-arr_time[i]-burst_time[i];
56. ta_time += total -arr_time[i];
57. counter =0;
58. }
59. if(i==n-1)
60. {
61. i=0;
62. }
63. else if(arr_time[i+1]<=total)
64. {
65. i++;
66. }
67. else
68. {
69. i=0;
70. }
71. }
72. float average_wait_time = wait_time * 1.0 / n;
73. float average_turnaround_time = ta_time * 1.0 / n;
74. printf("\nAverage Waiting Time:%f", average_wait_time);
75. printf("\nAvg Turnaround Time:%f", average_turnaround_time);
76. return 0;
77. }
FIFO- Take page reference strings 4, 1, 2, 4, 5 and three page slots as an example. Define page faults
ReplyDelete1. #include < stdio.h >
2. int main()
3. {
4. int incomingStream[] = {4 , 1 , 2 , 4 , 5};
5. int pageFaults = 0;
6. int frames = 3;
7. int m, n, s, pages;
8. pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
9. printf(" Incoming \ t Frame 1 \ t Frame 2 \ t Frame 3 ");
10. int temp[ frames ];
11. for(m = 0; m < frames; m++)
12. {
13. temp[m] = -1;
14. }
15. for(m = 0; m < pages; m++)
16. {
17. s = 0;
18. for(n = 0; n < frames; n++)
19. {
20. if(incomingStream[m] == temp[n])
21. {
22. s++;
23. pageFaults--;
24. }
25. }
26. pageFaults++;
27. if((pageFaults <= frames) && (s == 0))
28. {
29. temp[m] = incomingStream[m];
30. }
31. else if(s == 0)
32. {
33. temp[(pageFaults - 1) % frames] = incomingStream[m];
34. }
35. printf("\n");
36. printf("%d\t\t\t",incomingStream[m]);
37. for(n = 0; n < frames; n++)
38. {
39. if(temp[n] != -1)
40. printf(" %d\t\t\t", temp[n]);
41. else
42. printf(" - \t\t\t");
43. }
44. }
45. printf("\nTotal Page Faults:\t%d\n", pageFaults);
46. return 0;
47. }
Output:
Incoming Frame 1 Frame 2 Frame 3
4 4 - -
1 4 1 -
2 4 1 2
4 4 1 2
5 5 1 2
Total Page Faults: 4
ReplyDeleteSJF Example:
Process Arrival Time Burst Time
P1 0 5
P2 0 4
P3 0 12
P4 0 7
Average Waiting Time = (4 + 0 + 16 + 9)/4 = 29/4 = 7.25
Program/Source Code
Here is the source code of the C Program to Implement SJF Scheduling. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
1. /*
2. * C Program to Implement SJF Scheduling
3. */
4.
5. #include
6. int main()
7. {
8. int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,totalT=0,pos,temp;
9. float avg_wt,avg_tat;
10. printf("Enter number of process:");
11. scanf("%d",&n);
12.
13. printf("\nEnter Burst Time:\n");
14. for(i=0;i<n;i++)
15. {
16. printf("p%d:",i+1);
17. scanf("%d",&bt[i]);
18. p[i]=i+1;
19. }
20.
21. //sorting of burst times
22. for(i=0;i<n;i++)
23. {
24. pos=i;
25. for(j=i+1;j<n;j++)
26. {
27. if(bt[j]<bt[pos])
28. pos=j;
29. }
30.
31. temp=bt[i];
32. bt[i]=bt[pos];
33. bt[pos]=temp;
34.
35. temp=p[i];
36. p[i]=p[pos];
37. p[pos]=temp;
38. }
39.
40. wt[0]=0;
41.
42. //finding the waiting time of all the processes
43. for(i=1;i<n;i++)
44. {
45. wt[i]=0;
46. for(j=0;j<i;j++)
47. //individual WT by adding BT of all previous completed processes
48. wt[i]+=bt[j];
49.
50. //total waiting time
51. total+=wt[i];
52. }
53.
54. //average waiting time
55. avg_wt=(float)total/n;
56.
57. printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
58. for(i=0;i<n;i++)
59. {
60. //turnaround time of individual processes
61. tat[i]=bt[i]+wt[i];
62.
63. //total turnaround time
64. totalT+=tat[i];
65. printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
66. }
67.
68. //average turnaround time
69. avg_tat=(float)totalT/n;
70. printf("\n\nAverage Waiting Time=%f",avg_wt);
71. printf("\nAverage Turnaround Time=%f",avg_tat);
72. }