In: Computer Science
1. Practice Tasks
Aims: to simulate the principles and key technology in operating systems. You need to analyze, design, implement and debug a program which simulates the chosen principles or technology. You need to finish the following tasks to do the practice:
1) CPU scheduling algorithms (including FCFS, SJF, Priority Scheduling, Round Robin)
2) Process Synchronization (including producer and consumer problem, reader and writer problem, Dining-Philosophers Problem)
3) Page replacement algorithm (including FIFO, LRU, Optimal Algorithm)
4) Disk scheduling algorithm (including FCFS, SSTF, SCAN)
You need to implement the above 4 tasks to do the practice. You need to implement the input, algorithms and the output. The output will be different when you switch to different algorithms.
2. Requirements
1) Requirement analysis
2) Function design
3) Code preferebly in c/c++ or java
4) Report write-up
(Please do rate the answer if you found useful - Solution for first question is only provided )
1) FCFS Program code:
#include<stdio.h>
int n,i,b[10],wt[10],tat[10];
void main()
{
printf("enter no of process");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter burst time of process
%d :",i);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{ printf("\nthe waiting time of process %d
:",i);
wt[i]=b[i-1]+wt[i-1];
printf("%d",wt[i]);
printf("\nthe turn around time of
process %d :",i);
tat[i]=wt[i]+b[i];
printf("%d",tat[i]);
}
}
Output
2) SJF Program Code:
#include<stdio.h>
int n,i,b[10],wt[10],tat[10],j,temp;
void main()
{
printf("enter no of process");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter burst time of process
%d :",i);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(b[j+1]<b[j])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
printf("%d",b[i]);
printf("\n");
for(i=0;i<n;i++)
{ printf("\nthe waiting time of process %d
:",i);
wt[i]=b[i-1]+wt[i-1];
printf("%d",wt[i]);
printf("\nthe turn around time of
process %d :",i);
tat[i]=wt[i]+b[i];
printf("%d",tat[i]);
}
}
Output
#include<stdio.h>
int main()
{
int i, limit, total = 0, x, counter = 0, time;
int wait_time = 0, turnaround_time = 0, arrival_time = 0,
burst_time[10], temp[10];
printf("\nEnter Total Number of Processes:\t");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++)
{
printf("\nEnter Details of Process %d \n", i + 1);
printf("Burst Time:\t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
printf("\nEnter Time Slot:\t");
scanf("%d", &time);
for(total = 0, i = 0; x != 0;)
{
if(temp[i] <= time && temp[i] > 0)
{
total = total + temp[i];
temp[i] = 0;
counter = 1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - time;
total = total + time;
}
if(temp[i] == 0 && counter == 1)
{
x--;
printf("Process %d Burst Time %d
Turnaround Time %d Waiting Time %d\n",i + 1, burst_time[i], total -
arrival_time, total - arrival_time
- burst_time[i]);
wait_time = wait_time + total - arrival_time - burst_time[i];
turnaround_time = turnaround_time + total - arrival_time;
counter = 0;
}
if(i == limit - 1)
{
i = 0;
}
else if(arrival_time <= total)
{
i++;
}
else
{
i = 0;
}
}
return 0;
}
Output
Priority Program Code:
#include<stdio.h>
int n,i,b[10],temp,temp2,j,wt[10],tat[10],p[10];
void main()
{
printf("enter no of process");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter burst time of process
%d :",i);
scanf("%d",&b[i]);
printf("enter priority of process
%d :",i);
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(p[j+1]<p[j])
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
temp2=b[j];
b[j]=b[j+1];
b[j+1]=temp2;
}
}
}
for(i=0;i<n;i++)
printf("%d",b[i]);
printf("\n");
for(i=0;i<n;i++)
printf("%d",p[i]);
for(i=0;i<n;i++)
{ printf("\nthe waiting time of process %d
:",i);
wt[i]=b[i-1]+wt[i-1];
printf("%d",wt[i]);
printf("\nthe turn around time of
process %d :",i);
tat[i]=wt[i]+b[i];
printf("%d",tat[i]);
}
}
Output