OS Lab Import
OS Lab Import
)
// C program for implementation of
FCFS scheduling
#include<stdio.h>
// Function to find the waiting time
for all processes
void findWaitingTime(int processes[],
int n, int bt[], int wt[])
{
// waiting time for first process is 0
wt[0] = 0;
// calculating waiting time
for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}
// Function to calculate turn around
time
void findTurnAroundTime(int
processes[], int n, int bt[], int wt[],
int tat[])
{
// calculating turnaround time by
adding bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
//Function to calculate average time
void findavgTime( int processes[], int
n, int bt[])
{
int wt[n], tat[n], total_wt = 0,
total_tat = 0;
//Function to find waiting time of all
processes
findWaitingTime(processes, n, bt,
wt);
//Function to find turn around time
for all processes
findTurnAroundTime(processes, n,
bt, wt, tat);
//Display processes along with all
details
printf("Processes Burst time Waiting
time Turn around time\n");
// Calculate total waiting time and
total turn around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ",(i+1));
printf(" %d ", bt[i]);
printf(" %d",wt[i]);
printf(" %d\n",tat[i]);
}
float s=(float)total_wt / (float)n;
float t=(float)total_tat / (float)n;
printf("Average waiting time = %f",s);
printf("\n");
printf("Average turn around time =
%f ",t);
}
// Driver code
int main()
{
//process id's
int processes[] = {1, 2, 3};
int n = sizeof processes / sizeof
processes[0];
//Burst time of all processes
int burst_time[] = {30, 6, 8};
findavgTime(processes, n,
burst_time);
return 0;
}
Output:-
/tmp/Wkg32Wl0eK.o
Processes Burst time Waiting time
Turn around time
1 30 0 30
2 6 30 36
3 8 36 44
Average waiting time = 22.000000
Average turn around time =
36.666668
=== Code Execution Successful ===
2.)
//Program for shortest job first in C
#include <stdio.h>
int main()
{
// Matrix for storing Process Id, Burst
Time, Average Waiting Time &
Average Turn Around Time.
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
printf("Enter number of process: ");
scanf("%d", &n);
printf("Enter Burst Time:\n");
// User Input Burst Time and alloting
Process Id.
for (i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
}
// Sorting process according to their
Burst Time.
for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j][1] < A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
// Calculation of Waiting Times
for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
// Calculation of Turn Around Time
and printing the data.
for (i = 0; i < n; i++) {
A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
printf("P%d %d %d %d\n", A[i][0],
A[i][1], A[i][2], A[i][3]);
}
avg_tat = (float)total / n;
printf("Average Waiting Time= %f",
avg_wt);
printf("\nAverage Turnaround Time=
%f", avg_tat);
}
Output:-
Enter number of process: 4
Enter Burst Time:
P1: 2
P2: 1
P3: 3
P4: 7
P BT WT TAT
P2 1 0 1
P1 2 1 3
P3 3 3 6
P4 7 6 13
Average Waiting Time= 2.500000
Average Turnaround Time= 5.750000
3.)
/* Round Robin Scheduling Program
in C */
#include<stdio.h>
int main()
{
//Input no of processed
int n;
printf("Enter Total Number of
Processes:");
scanf("%d", &n);
int wait_time = 0, ta_time = 0,
arr_time[n], burst_time[n],
temp_burst_time[n];
int x = n;
//Input details of processes
for(int i = 0; i < n; i++)
{
printf("Enter Details of Process %d
\n", i + 1);
printf("Arrival Time: ");
scanf("%d", &arr_time[i]);
printf("Burst Time: ");
scanf("%d", &burst_time[i]);
temp_burst_time[i] = burst_time[i];
}
//Input time slot
int time_slot;
printf("Enter Time Slot:");
scanf("%d", &time_slot);
//Total indicates total time
//counter indicates which process is
executed
int total = 0, counter = 0,i;
printf("Process ID Burst Time
Turnaround Time Waiting Time\n");
for(total=0, i = 0; x!=0; )
{
// define the conditions
if(temp_burst_time[i] <= time_slot
&& temp_burst_time[i] > 0)
{
total = total + temp_burst_time[i];
temp_burst_time[i] = 0;
counter=1;
}
else if(temp_burst_time[i] > 0)
{
temp_burst_time[i] =
temp_burst_time[i] - time_slot;
total += time_slot;
}
if(temp_burst_time[i]==0 &&
counter==1)
{
x--; //decrement the process no.
printf("\nProcess No %d \t\t
%d\t\t\t\t %d\t\t\t %d", i+1,
burst_time[i],
total-arr_time[i], total-arr_time[i]-
burst_time[i]);
wait_time = wait_time+total-
arr_time[i]-burst_time[i];
ta_time += total -arr_time[i];
counter =0;
}
if(i==n-1)
{
i=0;
}
else if(arr_time[i+1]<=total)
{
i++;
}
else
{
i=0;
}
}
float average_wait_time = wait_time
* 1.0 / n;
float average_turnaround_time =
ta_time * 1.0 / n;
printf("\nAverage Waiting Time:%f",
average_wait_time);
printf("\nAvg Turnaround Time:%f",
average_turnaround_time);
return 0;
}
Output:-
Enter Total Number of Processes:2
Enter Details of Process 1
Arrival Time: 3
Burst Time: 5
Enter Details of Process 2
Arrival Time: 3
Burst Time: 7
Enter Time Slot:2
Process ID Burst Time Turnaround
Time Waiting Time
Process No 1 5
4 -1
Process No 2 7
9 2
Average Waiting Time:0.500000
Avg Turnaround Time:6.500000
Producer producesitem 2
Enter your choice:2
Producer producesitem 1
Enter your choice:6
Producer producesitem 1
Enter your choice:8
Producer producesitem 1
Enter your choice:4
available resources:
-5
Allocated matrix Max need
5| 4| -1
2| 2| 0
The system is in an unsafe state!!
--------------------------------
Process exited after 89.8 seconds
with return value 52
Press any key to continue . . .
9.)
#include<stdio.h>
int main()
{
int
i,j,n,a[50],frame[10],no,k,avail,count
=0;
printf("\n ENTER THE NUMBER OF
PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER
:\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF
FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page
frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
Output:-
ENTER THE NUMBER OF PAGES:
2
/* single level*/
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
// clrscr();
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5.
Exit\nEnter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\n Directory Empty");
else
{
printf("\n The Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
getch();
}
Output:-
--------------------------------
Process exited after 32.49 seconds with return value 0
Press any key to continue . . .
17.)
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,n,req[50],mov=0,cp;
printf("enter the current position\n");
scanf("%d",&cp);
printf("enter the number of requests\n");
scanf("%d",&n);
printf("enter the request order\n");
for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
}
mov=mov+abs(cp-req[0]); // abs is used to calculate the absolute value
printf("%d -> %d",cp,req[0]);
for(i=1;i<n;i++)
{
mov=mov+abs(req[i]-req[i-1]);
printf(" -> %d",req[i]);
}
printf("\n");
printf("total head movement = %d\n",mov);
}
Output:-
enter the current position
2
enter the number of requests
3
enter the request order
1
1
2
2 -> 1 -> 1 -> 2
total head movement = 2
--------------------------------
Process exited after 26.54 seconds with return value 24
Press any key to continue . . .
18.)
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]<queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
for(i=temp1+2,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[i]=0;
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek
%d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}
Output:-
Enter the max range of disk
2
Enter the initial head position
4
Enter the size of queue request
2
Enter the queue of disk positions to be read
1
3
Disk head moves from 4 to 2 with seek 2
Disk head moves from 2 to 3 with seek 1
Disk head moves from 3 to 1 with seek 2
Disk head moves from 1 to 0 with seek 1
Total seek time is 6
Average seek time is 3.000000
--------------------------------
Process exited after 41.68 seconds with return value 0
Press any key to continue . . .