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

OS Programs 2

Uploaded by

Udit Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODG, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

OS Programs 2

Uploaded by

Udit Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODG, PDF, TXT or read online on Scribd
You are on page 1/ 18

Operating System Programs

File.
INDEX
1. Implementation of CPU Scheduling using FCFS.
2. Implementation of CPU Scheduling using SJF.
3. Implementation of CPU Scheduling using Priority.
4. Implementation of CPU Scheduling using Round Robin.
5. Solution of Producer-Consumer Problem.
6. Solution of Reader-Writer Problem.
7. Implementation of Banker’s Algorithm.
8. Implementation of File Storage Allocation using continuous
allocation.
9. Implementation of File Storage Allocation using linked list.
10. Implementation of File Storage Allocation using indexing.
lOMoARcPSD|27715293

INDEX
PAGE
S.NO. EXPERMINT SIGNATURE
NO.

Implementation of CPU Scheduling


1 using FCFS. 1

Implementation of CPU Scheduling


2 using SJF. 2-3

Implementation of CPU Scheduling using


3 Priority. 4-5

Implementation of CPU Scheduling using


4 Round Robin. 6-7

Solution of Producer-Consumer
Problem. 8
5

Solution of Reader-Writer Problem.


6 9-10

Implementation of Banker’s Algorithm.


7 11-13

Implementation of File Storage Allocation


8 using continuous allocation. 14

Implementation of File Storage Allocation using


15
9 continuous

Implementation of File Storage Allocation


10 using indexing. 16
Program 1
Implementation of CPU Scheduling using FCFS .

#include <stdio.h>
int main()
{
int pid[15];
int bt[15];
int n;
printf("Enter the number of processes: ");
scanf("%d",&n);
 
printf("Enter process id of all the processes: ");
for(int i=0;i<n;i++)
{
scanf("%d",&pid[i]);
}
 
printf("Enter burst time of all the processes: ");
for(int i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
 
int i, wt[n];
wt[0]=0;
 
//for calculating waiting time of each process
for(i=1; i<n; i++)
{
wt[i]= bt[i-1]+ wt[i-1];
}
 
printf("Process ID Burst Time Waiting Time TurnAround Time\n");
float twt=0.0;
float tat= 0.0;
for(i=0; i<n; i++)
{
printf("%d\t\t", pid[i]);
printf("%d\t\t", bt[i]);
printf("%d\t\t", wt[i]);
 
//calculating and printing turnaround time of each process
printf("%d\t\t", bt[i]+wt[i]);
printf("\n");
 
//for calculating total waiting time
twt += wt[i];
 
//for calculating total turnaround time
tat += (wt[i]+bt[i]);
}
float att,awt;
 
//for calculating average waiting time
awt = twt/n;
 
//for calculating average turnaround time
att = tat/n;
printf("Avg. waiting time= %f\n",awt);
printf("Avg. turnaround time= %f",att);
}
Program 2
Implementation of CPU Scheduling using SJF.

#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,totalT=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
 
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
 
//sorting of burst times
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
 
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
 
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
 
wt[0]=0;
 
//finding the waiting time of all the processes
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
//individual WT by adding BT of all previous completed processes
wt[i]+=bt[j];
 
//total waiting time
total+=wt[i];
}
 
//average waiting time
avg_wt=(float)total/n;
 
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
//turnaround time of individual processes
tat[i]=bt[i]+wt[i];
 
//total turnaround time
totalT+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
 
//average turnaround time
avg_tat=(float)totalT/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f",avg_tat);
}
Program 3
Implementation of CPU Scheduling using Priority .

#include <stdio.h>
 
//Function to swap two variables
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int n;
printf("Enter Number of Processes: ");
scanf("%d",&n);
 
// b is array for burst time, p for priority and index for process id
int b[n],p[n],index[n];
for(int i=0;i<n;i++)
{
printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
scanf("%d %d",&b[i],&p[i]);
index[i]=i+1;
}
for(int i=0;i<n;i++)
{
int a=p[i],m=i;
 
//Finding out highest priority element and placing it at its desired
position
for(int j=i;j<n;j++)
{
if(p[j] > a)
{
a=p[j];
m=j;
}
}
 
//Swapping processes
swap(&p[i], &p[m]);
swap(&b[i], &b[m]);
swap(&index[i],&index[m]);
}
 
// T stores the starting time of process
int t=0;
 
//Printing scheduled process
printf("Order of process Execution is\n");
for(int i=0;i<n;i++)
{
printf("P%d is executed from %d to %d\n",index[i],t,t+b[i]);
t+=b[i];
}
printf("\n");
printf("Process Id Burst Time Wait Time TurnAround Time\n");
int wait_time=0;
for(int i=0;i<n;i++)
{
printf("P%d %d %d %d\
n",index[i],b[i],wait_time,wait_time + b[i]);
wait_time += b[i];
}
return 0;
}
Program 4
Implementation of CPU Scheduling using Round Robin.
#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;
}
Program 5
Implementation of the solution for Bounded
Buffer.
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{ int n; void
producer(); void
consumer(); int
wait(int); int
signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit"); while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n); switch(n)
{
case 1: if((mutex==1)&&(empty!=0)) producer();
else
printf("Buffer is full!!"); break;
case 2: if((mutex==1)&&(full!=0))
consumer(); else
printf("Buffer is empty!!");
break;
case 3: exit(0);
break;
}} return
0;
}
int wait(int s)
{ return (--
s);
}
int signal(int s)
{ return(++s);
}
void producer()
{ mutex=wait(mutex);
full=signal(full);
empty=wait(empty); x++;
printf("\nProducer produces the item
mutex=signal(mutex);
}
void consumer()
{ mutex=wait(mutex); full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--; mutex=signal(mutex);
}
%d",x);
Program 6
Implementation of the solution for
Reader-Writer Problem
#include
< pthread.h>
#include <semaphore.h>
#include <stdio.h>
/*
This program provides a possible solution for first readers writers
problem using mutex and semaphore.
I have used 10 readers and 5 producers to demonstrate the solution. You
can always play with these values.
*/
sem_t wrt;
pthread_mutex_t mutex;
int cnt = 1; int numreader =
0;
void *writer(void *wno)
{
sem_wait(&wrt);
cnt =
cnt*2;
printf("Writer %d modified cnt to %d\n",(*((int *)wno)),cnt);
sem_post(&wrt);
}
void *reader(void *rno)
{
// Reader acquire the lock before modifying numreader
pthread_mutex_lock(&mutex);
numreader++;
if(numreader == 1) {
sem_wait(&wrt); // If this id the first reader, then it will block
the writer
}
pthread_mutex_unlock(&mutex);
//
Reading Section
printf("Reader %d: read cnt as %d\n",*((int *)rno),cnt);
// Reader acquire the lock before modifying numreader
pthread_mutex_lock(&mutex);
numreader--;
if(numreader == 0) {
sem_post(&wrt); // If this is the last reader, it will wake up the
writer.
}
pthread_mutex_unlock(&mutex);
}
int main()
{
pthread_t read[10],write[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt,0,1);
int a[10] = {1,2,3,4,5,6,7,8,9,10}; //Just used for numbering the
producer and consumer
for(int i = 0; i < 10; i++) {
(void *)reader, (void *)&a[i]);
}pthread_create(&read[i], NULL,
for(int i = 0; i < 5; i++) {
(void *)writer, (void *)&a[i]);
}pthread_create(&write[i], NULL,
for(int i = 0; i < 10; i++) {
pthread_join(read[i], NULL);
}
for(int i = 0; i < 5; i++) {
pthread_join(write[i], NULL);
}
pthread_mutex_destroy(&mutex);
return 0;
}
Program 7
Implementation of Banker’s Algorithm.
#include <stdio.h>
int current[5][5], maximum_claim[5][5], available[5];
int allocation[5] = {0, 0, 0, 0, 0}; int maxres[5],
running[5], safe = 0; int counter = 0, i, j, exec,
resources, processes, k = 1;
int main()
{ printf("\nEnter number of processes:
");
scanf("%d", &processes);
for (i = 0; i < processes; i++)
{
running[i] = 1;
counter++;
}
printf("\nEnter
number
scanf("%d", &resources);
of
resources:
");
sem_destroy(&wrt);printf("\nEnter Claim Vector:");
for (i = 0; i < resources; i++)
{
scanf("%d", &maxres[i]);
}
printf("\nEnter Allocated Resource Table:\n");
for (i = 0; i < processes; i++)
{
for(j = 0; j < resources; j++)
{
scanf("%d", &current[i][j]);
}
}
printf("\nEnter Maximum Claim Table:\n");
for (i = 0; i < processes; i++)
{
for(j = 0; j < resources; j++)
{
scanf("%d", &maximum_claim[i][j]);
}
}
printf("\nThe Claim Vector is: ");
for (i = 0; i < resources; i++)
{
printf("\t%d", maxres[i]);
}
printf("\nThe Allocated Resource Table:\n");
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
printf("\t%d", current[i][j]);
}
printf("\n");
}
printf("\nThe Maximum Claim Table:\n");
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
printf("\t%d", maximum_claim[i][j]);
}
printf("\n");
}
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
allocation[j] += current[i][j];
}
}printf("\nAllocated
resources:");
for (i = 0; i < resources; i++)
{
printf("\t%d", allocation[i]);
}
for (i = 0; i < resources; i++)
{
available[i] = maxres[i] - allocation[i];
}
printf("\nAvailable
resources:");
for (i = 0; i < resources; i++)
{
printf("\t%d", available[i]);
}
printf("\n");
while (counter != 0)
{
safe = 0;
for (i = 0; i <
processes; i++)
{
if (running[i])
{
exec = 1;
for (j =
0; j < resources; j++)
{
if (maximum_claim[i][j] - current[i][j] >
available[j])
{
exec = 0;
break;
}
}
if
(exec)
{
printf("\nProcess%d is executing\n", i + 1);
running[i] = 0;
counter--;
safe
= 1;
for (j = 0; j < resources; j++)
{
available[j] += current[i][j];
}
break;
}
}
}
if
(!safe)
{
printf("\nThe processes are in unsafe state.\n");
break;
}
else
{
printf("\nThe process is in safe state");
printf("\nAvailable vector:");
for (i = 0; i < resources; i++)
Program 8
Implement File Storage Allocation Technique.

#include < stdio.h>


#include<conio.h>
void main()
{
int f[50], i, st, len, j, c, k, count = 0;
clrscr(); for(i=0;i<50;i++) f[i]=0;
printf("Files Allocated are : \n"); x:
count=0;
printf(“Enter starting block and length of files: ”);
scanf("%d%d", &st,&len); for(k=st;k<(st+len);k++)
if(f[k]==0) count++;
if(len==count)
{
for(j=st;j<(st+len);j++) if(f[j]==0)
{ f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf(” The file is allocated to disk\n");
} else
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)"); scanf("%d",
&c);
if(c==1)
goto x;
else exit();
getch();
}
Program 9
Implement File Storage Allocation Technique using Linked list.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
int f[50], p,i, st, len, j, c, k, a;
clrscr(); for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: "); scanf("%d",&p);
printf("Enter blocks already allocated: "); for(i=0;i<p;i++)
{
scanf("%d",&a); f[a]=1;
}
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len); k=len; if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{ f[j]=1; printf("%d--------
>%d\n",j,f[j]);
} else
{
printf("%d Block is already allocated \n",j); k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c); if(c==1) goto x;
else
exit(0);
getch();
}
Program 10
Implement File Storage Allocation Technique using Indexing.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr(); for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind); if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \
n", ind);
scanf("%d",&n);
} else
{
printf("%d index is already allocated \n",ind); goto
x;
}
y: count=0;
for(i=0;i<n;i++)
{scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++) f[index[j]]=1; printf("Allocated\n");
printf("File Indexed\n"); for(k=0;k<n;k++) printf("%d-
------->%d : %d\n",ind,index[k],f[index[k]]);
} else
{ printf("File in the index is already allocated
\n"); printf("Enter another file indexed"); goto
y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c); if(c==1) goto x;
else exit(0);
getch();
}

You might also like