0% found this document useful (0 votes)
21 views

OS Lab Import

The documents provide code examples for implementing different CPU scheduling algorithms in C including FCFS, SJF, Round Robin, Priority Scheduling and Producer-Consumer. The code snippets include functions for calculating waiting times, turnaround times and average times. Input from the user is taken for number of processes and their details. Output displays schedules, waiting times and average times.

Uploaded by

Vinay Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

OS Lab Import

The documents provide code examples for implementing different CPU scheduling algorithms in C including FCFS, SJF, Round Robin, Priority Scheduling and Producer-Consumer. The code snippets include functions for calculating waiting times, turnaround times and average times. Input from the user is taken for number of processes and their details. Output displays schedules, waiting times and average times.

Uploaded by

Vinay Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

1.

)
// 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

=== Code Execution Successful ===

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

=== Code Execution Successful ===


4.)
//Program for priority based
algorithm in C
#include<stdio.h>
// structure representing a structure
struct priority_scheduling {
// name of the process
char process_name;
// time required for execution
int burst_time;
// waiting time of a process
int waiting_time;
// total time of execution
int turn_around_time;
// priority of the process
int priority;
};
int main() {
// total number of processes
int number_of_process;
// total waiting and turnaround time
int total = 0;
// temporary structure for swapping
struct priority_scheduling
temp_process;
// ASCII numbers are used to
represent the name of the process
int ASCII_number = 65;
// swapping position
int position;
// average waiting time of the
process
float average_waiting_time;
// average turnaround time of the
process
float average_turnaround_time;
printf("Enter the total number of
Processes: ");
// get the total number of the
process as input
scanf("%d", & number_of_process);
// initializing the structure array
struct priority_scheduling
process[number_of_process];
printf("\nPlease Enter the Burst Time
and Priority of each process:\n");
// get burst time and priority of all
process
for (int i = 0; i < number_of_process;
i++) {
// assign names consecutively using
ASCII number
process[i].process_name = (char)
ASCII_number;
printf("\nEnter the details of the
process %c \n",
process[i].process_name);
printf("Enter the burst time: ");
scanf("%d", & process[i].burst_time);
printf("Enter the priority: ");
scanf("%d", & process[i].priority);
// increment the ASCII number to get
the next alphabet
ASCII_number++;
}
// swap process according to high
priority
for (int i = 0; i < number_of_process;
i++) {
position = i;
for (int j = i + 1; j <
number_of_process; j++) {
// check if priority is higher for
swapping
if (process[j].priority >
process[position].priority)
position = j;
}
// swapping of lower priority process
with the higher priority process
temp_process = process[i];
process[i] = process[position];
process[position] = temp_process;
}
// First process will not have to wait
and hence has a waiting time of 0
process[0].waiting_time = 0;
for (int i = 1; i < number_of_process;
i++) {
process[i].waiting_time = 0;
for (int j = 0; j < i; j++) {
// calculate waiting time
process[i].waiting_time +=
process[j].burst_time;
}
// calculate total waiting time
total += process[i].waiting_time;
}
// calculate average waiting time
average_waiting_time = (float) total /
(float) number_of_process;
// assigning total as 0 for next
calculations
total = 0;
printf("\n\nProcess_name \t Burst
Time \t Waiting Time \t Turnaround
Time\n");
printf("----------------------------------------
--------------------\n");
for (int i = 0; i < number_of_process;
i++) {
// calculating the turnaround time of
the processes
process[i].turn_around_time =
process[i].burst_time +
process[i].waiting_time;
// calculating the total turnaround
time.
total +=
process[i].turn_around_time;
// printing all the values
printf("\t %c \t\t %d \t\t %d \t\t
%d", process[i].process_name,
process[i].burst_time,
process[i].waiting_time,
process[i].turn_around_time);
printf("\n-------------------------------------
----------------------\n");
}
// calculating the average
turn_around time
average_turnaround_time = (float)
total / (float) number_of_process;
// average waiting time
printf("\n\n Average Waiting Time :
%f", average_waiting_time);
// average turnaround time
printf("\n Average Turnaround Time:
%f\n", average_turnaround_time);
return 0;
}
OutPut:-
Enter the total number of Processes:
2

Please Enter the Burst Time and


Priority of each process:

Enter the details of the process A


Enter the burst time: 1
Enter the priority: 3

Enter the details of the process B


Enter the burst time: 4
Enter the priority: 2

Process_name Burst Time


Waiting Time Turnaround Time
--------------------------------------------------
----------
A 1 0
1
--------------------------------------------------
---------
B 4 1
5
--------------------------------------------------
---------

Average Waiting Time : 0.500000


Average Turnaround Time: 3.000000

=== Code Execution Successful ===


5.)
// C program for the producer and
consumer approach
#include <stdio.h>
#include <stdlib.h>
// Initialize a mutex to 1
int mutex = 1;
// Number of full slots as 0
int full = 0;
// Number of empty slots as size of
buffer
int empty = 10, x = 0;
// Function to produce an item and
add it to the buffer
void producer()
{
// Decrease mutex value by 1
--mutex;
// Increase the number of full slots
by 1
++full;
// Decrease the number of empty
slots by 1
--empty;
// Item produced
x++;
printf("\nProducer produces" "item
%d", x);
// Increase mutex value by 1
++mutex;
}
// Function to consume an item and
remove it from buffer
void consumer()
{
// Decrease mutex value by 1
--mutex;
// Decrease the number of full slots
by 1
--full;
// Increase the number of empty
slots by 1
++empty;
printf("\nConsumer consumes "
"item %d", x);
x--;
// Increase mutex value by 1
++mutex;
}
// Driver Code
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer" "\n3.
Press 3 for Exit");
// Using '#pragma omp parallel for'
can give wrong value due to
synchronization issues.
// 'critical' specifies that code is
executed by only one thread at a
time i.e., only one thread enters the
critical section at a given time
#pragma omp critical
for (i = 1; i > 0; i++)
{
printf("\nEnter your choice:");
scanf("%d", &n);
// Switch Cases
switch (n)
{
case 1:
// If mutex is 1 and empty is non-
zero, then it is possible to produce
if ((mutex == 1) && (empty != 0))
{
producer();
}
// Otherwise, print buffer is full
else
{
printf("Buffer is full!");
}
break;
case 2:
// If mutex is 1 and full is non-zero,
then it is possible to consume
if ((mutex == 1) && (full != 0))
{
consumer();
}
// Otherwise, print Buffer is empty
else
{
printf("Buffer is empty!");
}
break;
// Exit Condition
case 3:
exit(0);
break;
}
}
}
Output:-
/tmp/hGfsmOmE5Z.o

1. Press 1 for Producer


2. Press 2 for Consumer
3. Press 3 for Exit
Enter your choice:1
Producer producesitem 1
Enter your choice:1

Producer producesitem 2
Enter your choice:2

Consumer consumes item 2


Enter your choice:4

Enter your choice:2

Consumer consumes item 1


Enter your choice:1

Producer producesitem 1
Enter your choice:6

Enter your choice:7

Enter your choice:2

Consumer consumes item 1


Enter your choice:7

Enter your choice:9


Enter your choice:1

Producer producesitem 1
Enter your choice:8

Enter your choice:4

Enter your choice:2

Consumer consumes item 1


Enter your choice:9

Enter your choice:1

Producer producesitem 1
Enter your choice:4

=== Code Execution Successful ===


6.)
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#define N 5
#define THINKING 2
#define HUNGRY 1
#define EATING 0
#define LEFT (phnum + 4) % N
#define RIGHT (phnum + 1) % N
int state[N];
int phil[N] = { 0, 1, 2, 3, 4 };
sem_t mutex;
sem_t S[N];
void test(int phnum)
{
if (state[phnum] == HUNGRY
&& state[LEFT] != EATING
&& state[RIGHT] != EATING) {
// state that eating
state[phnum] = EATING;
sleep(2);
printf("Philosopher %d takes fork %d
and %d\n",
phnum + 1, LEFT + 1, phnum + 1);
printf("Philosopher %d is Eating\n",
phnum + 1);
// sem_post(&S[phnum]) has no
effect
// during takefork
// used to wake up hungry
philosophers
// during putfork
sem_post(&S[phnum]);
}
}
// take up chopsticks
void take_fork(int phnum)
{
sem_wait(&mutex);
// state that hungry
state[phnum] = HUNGRY;
printf("Philosopher %d is Hungry\n",
phnum + 1);
// eat if neighbours are not eating
test(phnum);
sem_post(&mutex);
// if unable to eat wait to be
signalled
sem_wait(&S[phnum]);
sleep(1);
}
// put down chopsticks
void put_fork(int phnum)
{
sem_wait(&mutex);
// state that thinking
state[phnum] = THINKING;
printf("Philosopher %d putting fork
%d and %d down\n",
phnum + 1, LEFT + 1, phnum + 1);
printf("Philosopher %d is
thinking\n", phnum + 1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
void* philosopher(void* num)
{
while (1) {
int* i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
}
}
int main()
{
int i;
pthread_t thread_id[N];
// initialize the semaphores
sem_init(&mutex, 0, 1);
for (i = 0; i < N; i++)
sem_init(&S[i], 0, 0);
for (i = 0; i < N; i++) {
// create philosopher processes
pthread_create(&thread_id[i], NULL,
philosopher, &phil[i]);
printf("Philosopher %d is
thinking\n", i + 1);
}
for (i = 0; i < N; i++)
pthread_join(thread_id[i], NULL);
}
Output:-
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 5 is thinking
Philosopher 2 is Hungry
Philosopher 3 is Hungry
Philosopher 5 is Hungry
Philosopher 4 is Hungry
Philosopher 4 takes fork 3 and 4
Philosopher 4 is Eating
Philosopher 1 is Hungry
Philosopher 1 takes fork 5 and 1
Philosopher 1 is Eating
Philosopher 4 putting fork 3 and 4
down
Philosopher 4 is thinking
Philosopher 3 takes fork 2 and 3
Philosopher 3 is Eating
Philosopher 1 putting fork 5 and 1
down
Philosopher 1 is thinking
Philosopher 5 takes fork 4 and 5
Philosopher 5 is Eating
Philosopher 4 is Hungry
Philosopher 3 putting fork 2 and 3
down
Philosopher 3 is thinking
Philosopher 2 takes fork 1 and 2
Philosopher 2 is Eating
Philosopher 5 putting fork 4 and 5
down
Philosopher 5 is thinking
Philosopher 4 takes fork 3 and 4
7.)
/* Deadlock Avoidance*/
#include<stdio.h>
int main()
{
int n,r,i,j,k,p,u=0,s=0,m;
int
block[10],run[10],active[10],newreq[
10];
int
max[10][10],resalloc[10][10],resreq[
10][10];
int
totalloc[10],totext[10],simalloc[10];
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the no ofresource
classes:");
scanf("%d",&r);
printf("Enter the total existed
resource in each class:");
for(k=1; k<=r; k++)
scanf("%d",&totext[k]);
printf("Enter the allocated
resources:");
for(i=1; i<=n; i++)
for(k=1; k<=r; k++)
scanf("%d",&resalloc[i][k]);
printf("Enter the process making the
new request:");
scanf("%d",&p);
printf("Enter the requested
resource:");
for(k=1; k<=r; k++)
scanf("%d",&newreq[k]);
printf("Enter the process which are n
blocked or running:");
for(i=1; i<=n; i++)
{
if(i!=p)
{
printf("process %d:\n",i+1);
scanf("%d%d",&block[i],&run[i]);
}
}
block[p]=0;
run[p]=0;
for(k=1; k<=r; k++)
{
j=0;
for(i=1; i<=n; i++)
{
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
}
}
for(i=1; i<=n; i++)
{
if(block[i]==1||run[i]==1)
active[i]=1;
else
active[i]=0;
}
for(k=1; k<=r; k++)
{
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
}
for(k=1; k<=r; k++)
{
if(totext[k]-totalloc[k]<0)
{
u=1;
break;
}
}
if(u==0)
{
for(k=1; k<=r; k++)
simalloc[k]=totalloc[k];
for(s=1; s<=n; s++)
for(i=1; i<=n; i++)
{
if(active[i]==1)
{
j=0;
for(k=1; k<=r; k++)
{
if((totext[k]-simalloc[k])<(max[i][k]-
resalloc[i][k]))
{
j=1;
break;
}
}
}
if(j==0)
{
active[i]=0;
for(k=1; k<=r; k++)
simalloc[k]=resalloc[i][k];
}
}
m=0;
for(k=1; k<=r; k++)
resreq[p][k]=newreq[k];
printf("Deadlock willn't occur");
}
else
{
for(k=1; k<=r; k++)
{
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
}
printf("Deadlock will occur");
}
return 0;
}
Output:-
Enter the no of processes:2
Enter the no ofresource classes:1
Enter the total existed resource in
each class:2
Enter the allocated resources:3
3
Enter the process making the new
request:4
Enter the requested resource:2
Enter the process which are n
blocked or running:process 2:
2
1
process 3:
4
3
Deadlock will occur

=== Code Execution Successful ===


8.)
/* deadlock Prevention*/
#include<stdio.h>
#include<conio.h>
void main()
{
int
allocated[15][15],max[15][15],need[
15][15],avail[15],tres[15],work[15],fl
ag[15];
int pno,rno,i,j,prc,count,t,total;
count=0;
//clrscr();
printf("\n Enter number of
process:");
scanf("%d",&pno);
printf("\n Enter number of
resources:");
scanf("%d",&rno);
for(i=1;i<=pno;i++)
{
flag[i]=0;
}
printf("\n Enter total numbers of
each resources:");
for(i=1;i<= rno;i++)
scanf("%d",&tres[i]);
printf("\n Enter Max resources for
each process:");
for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i);
for(j=1;j<= rno;j++)
scanf("%d",&max[i][j]);
}
printf("\n Enter allocated resources
for each process:");
for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i);
for(j=1;j<= rno;j++)
scanf("%d",&allocated[i][j]);
}
printf("\n available resources:\n");
for(j=1;j<= rno;j++)
{
avail[j]=0;
total=0;
for(i=1;i<= pno;i++)
{
total+=allocated[i][j];
}
avail[j]=tres[j]-total;
work[j]=avail[j];
printf(" %d \t",work[j]);
}
do
{
for(i=1;i<= pno;i++)
{
for(j=1;j<= rno;j++)
{
need[i][j]=max[i][j]-allocated[i][j];
}
}
printf("\n Allocated matrix Max
need");
for(i=1;i<= pno;i++)
{
printf("\n");
for(j=1;j<= rno;j++)
{
printf("%4d",allocated[i][j]);
}
printf("|");
for(j=1;j<= rno;j++)
{
printf("%4d",max[i][j]);
}
printf("|");
for(j=1;j<= rno;j++)
{
printf("%4d",need[i][j]);
}
}
prc=0;
for(i=1;i<= pno;i++)
{
if(flag[i]==0)
{
prc=i;
for(j=1;j<= rno;j++)
{
if(work[j]< need[i][j])
{
prc=0;
break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\n Process %d completed",i);
count++;
printf("\n Available matrix:");
for(j=1;j<= rno;j++)
{
work[j]+=allocated[prc][j];
allocated[prc][j]=0;
max[prc][j]=0;
flag[prc]=1;
printf(" %d",work[j]);
}
}
}
while(count!=pno&&prc!=0);
if(count==pno)
printf("\nThe system is in a safe
state!!");
else
printf("\nThe system is in an unsafe
state!!");
getch();
}
Output:-
Enter number of process:2

Enter number of resources:1

Enter total numbers of each


resources:2

Enter Max resources for each


process:
for process 1:4

for process 2:2


Enter allocated resources for each
process:
for process 1:5

for process 2:2

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

ENTER THE PAGE NUMBER :


3
2

ENTER THE NUMBER OF FRAMES :3


ref string page frames
3 3 -1 -1
2 3 2 -1
Page Fault Is 2
--------------------------------
Process exited after 59.51 seconds
with return value 0
Press any key to continue . . .
10.)
#include<stdio.h>
void optimal(int string[20],int n,int
size)
{
//Creating array for block storage
int frames[n];
//Initializing each block with -1
for (int i=0;i<n;i++)
frames[i]=-1;
//Index to insert element
int index=-1;
//Counters
int page_miss=0;
int page_hits=0;
//Pointer to indicate initially frames
filled or not
int full=0;
//Traversing each symbol in fifo
for (int i=0;i<size;i++)
{
int symbol=string[i];
int flag=0;
for(int j=0;j<n;j++)
{
if (symbol==frames[j])
{
flag=1;
break;
}
}
if (flag==1) {
printf("\nSymbol: %d Frame:
",symbol);
for (int j=0;j<n;j++)
printf("%d ",frames[j]);
page_hits+=1;
}
else
{
//Frames are still empty
if (full==0)
{
index=(index+1)%n;
frames[index]=symbol;
page_miss+=1;
printf("\nSymbol: %d Frame:
",symbol);
for (int j=0;j<n;j++)
printf("%d ",frames[j]);
//Frames filled or not
if (i==n-1)
full=1;
}
//Frames are full, now we can apply
optimal page replacement
else
{
//First find the index to replace with
int pos=-1;
int index=-1;
//Traversing each symbol and
checking their optimal possibility
for(int j=0;j<n;j++)
{
//Whether symbol in frame found or
not in future cached frame
int found=0;
for (int k=i+1;k<size;k++)
{
//If symbol exists in cached string
if (frames[j]==string[k])
{
found=1;
if (pos<k)
{
pos=k;
index=j;
}
break;
}
}
//Symbol does not exist in cached
frame
if (found==0)
{
pos=size;
index=j;
}
}
//Now assign symbol in lru position
frames[index]=symbol;
printf("\nSymbol: %d Frame:
",symbol);
for (int j=0;j<n;j++)
printf("%d ",frames[j]);
}
}
}
printf("\nPage hits:
%d",page_hits);
printf("\nPage misses:
%d",page_miss);
}
//Main function
int main(void)
{
int string[]={2, 3, 4, 2, 1, 3, 7, 5, 4, 3};
int no_frames=3;
int size=sizeof(string)/sizeof(int);
optimal(string,no_frames,size);
return 0;
}
Output:-
Symbol: 2 Frame: 2 -1 -1
Symbol: 3 Frame: 2 3 -1
Symbol: 4 Frame: 2 3 4
Symbol: 2 Frame: 2 3 4
Symbol: 1 Frame: 1 3 4
Symbol: 3 Frame: 1 3 4
Symbol: 7 Frame: 1 7 4
Symbol: 5 Frame: 1 7 5
Symbol: 4 Frame: 1 7 4
Symbol: 3 Frame: 3 7 4
Page hits: 3
Page misses: 7
11.)

/* 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:-

Enter name of directory -- 2

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice -- 1

Enter the name of the file -- 3

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice -- 2

Enter the name of the file -- 1


File 1 not found

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice -- 3

Enter the name of the file -- 1


File 1 not found
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit
Enter your choice -- 4

The Files are -- 3

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice – 2
12.)
/double level/
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
//clrscr();
dcnt=0;
while(1)
{
printf("\n\n 1. Create Directory\t 2. Create File\t 3. Delete File");
printf("\n 4. Search File \t \t 5. Display \t 6. Exit \t Enter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\n Enter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
getch();
}
Output:-

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 1

Enter name of directory -- vinay


Directory created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory -- vin


Directory vin not found

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 4

Enter name of the directory -- vinay


Enter the name of the file -- vin
File vin not found

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 3

Enter name of the directory -- vinay


Enter name of the file -- vinay
File vinay not found

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice –
13.)
#include<stdio.h>
#include<conio.h>
int main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
//clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}
Output:-
Enter no. of files:2
Enter starting block and size of file1:3
3
Enter blocks occupied by file1:1
enter blocks of file1:3
Enter starting block and size of file2:4
2
Enter blocks occupied by file2:4
enter blocks of file2:2
1
3
3

File index length


1 3 1
2 4 4

Enter file name:1


file name is:1
Index is:3Block occupied are: 3
--------------------------------
Process exited after 70.83 seconds with return value 13
Press any key to continue . . .
14.)
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}
f[10];
int main()
{
int i,j,n;
//clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}
Output:-
Enter no. of files:3
Enter file name:Vinay
Enter starting block:3
Enter no.of blocks:4
Enter block numbers:2
2
3
1
Enter file name:Vinay
Enter starting block:2
Enter no.of blocks:34
Enter block numbers:5
2
2
2
15.)
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
//clrscr();
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
scanf("%d",&x);
printf("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}
Output:-
Enter no.of files:1
Enter no. of blocks occupied by file12
Enter the starting block of file13
Filename Start block length
1 3 2
Enter file name:3
File name is:3length is:57blocks occupied: 0 048865268032762 0 0 1 0
0 0 1 0 0 0 0 0 0 048865282432762 0 033554944 0 0 033554434
0488177664327626486736 06881616 06936576 0 0 0 0 0 104
0488456762327626894576 06486873 06881280 0 128 06936592
06936576 0 128
16.)
#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;
queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
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
1
Enter the size of queue request
3
Enter the queue of disk positions to be read
1
2
3
Disk head moves from 1 to 1 with seek 0
Disk head moves from 1 to 2 with seek 1
Disk head moves from 2 to 3 with seek 1
Disk head moves from 3 to 2 with seek 1
Disk head moves from 2 to 0 with seek 2
Total seek time is 5
Average seek time is 1.666667

--------------------------------
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 . . .

You might also like