Operating Systemsmanual
Operating Systemsmanual
LABORATORY MANUAL
BCA(R22)
(2023‐2026)
DEPARTMENT OF IT&CA
Experiment-1
This section contains some basic commands of Ubuntu, so let us start and
discuss them one by one. Firstly, you must open the terminal in Ubuntu; press “ctrl+alt+t”
from your keyboard to open the terminal.
Command 1: pwd
This command refers to the present working directory in which you are operating; in
simpler words, in which your terminal is open. To check PWD, execute the pwd keyword in
your terminal and hit enter; the command of PWD is written below along with the result of
that command.
Command 2: dir
The dir command is used to print (on the terminal) all the available directories in the
present working directory:
Command 3: ls
This command is used to list down all the directories and files inside the present
working directory (or you can give the path of a specific directory);
Command 4: cd
One of the most used commands of Ubuntu; you can change the directories in the
terminal using the “cd” command.
Command 5: touch
This Ubuntu command can be used to create a new file as well one can use it to
change the timestamp of any file; the command given below will create a new text time in
pwd:
Command 6: cat
This command is used to show the content of any file: you can use this command to
save the content of multiples files to one file:
Command 7: mkdir
The above-mentioned command will make a directory in your pwd; for example, the
following command will make the directory “new” in pwd.
Command 8: rm
This remove command is used to remove the specific file from a directory. you can
remove the empty directory,
Command 9: cp
The cp command will help you to copy any file or folder to any directory. If you want
to copy the complete folder, then.
This command helps you to get the first ten lines of a text file; for instance,
The tail command is used to get the last ten lines of the text file.
You can use the command to get the release number, version of Linux, and much
more. The “-a” flag is used to get detailed information.
You can use the wget command to download the content from the internet.
The history command shows the list of commands (with numeric numbers) executed:
To convert your files to zip archive; you can get help by using the “gzip” command;
moreover, a zipped file can be unzipped using the “gunzip” command
Command 16:
Experiment-2
// scheduling
#include
// processes
wt[0] = 0;
void findTurnAroundTime( int processes[], int n, int bt[], int wt[], int tat[])
//Function to find turn around time for all processes findTurnAroundTime(processes, n, bt,
wt, tat);
// around time
printf(" %d ",(i+1));
printf(" %d",wt[i] );
printf(" %d\n",tat[i] );
printf("\n");
printf("Average turn around time = %f ",t);
// Driver code
int main() {
//process id's
findavgTime(processes, n, burst_time);
return 0;
Output:
Processes Burst time Waiting time Turn around time //The Output is Wrong please correct it
1 10 0 10
2 5 10 15
3 8 15 23
SOURCE CODE :
#include<stdio.h>
#include<conio.h>
main()
tatavg;
clrscr();
scanf("%d", &n);
for(i=0;i<n;i++)
p[i]=i;
scanf("%d", &bt[i]);
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
wt[0] = wtavg = 0;
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
INPUT
OUTPUT
PROCESS BURST
TIME
WAITING
TIME
TURNARO
UND TIME
P3 3 0 3
P0 6 3 9
P2 7 9 16
P1 8 16 24
SOURCE CODE
#include<stdio.h>
main()
int
i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&bu[i]);
ct[i]=bu[i];
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t) {
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
else {
bu[i]=bu[i]-t;
temp=temp+t;
for(i=0;i<n;i++){
wa[i]=tat[i]
ct[i]; att+=tat[i];
awt+=wa[i];}
for(i=0;i<n;i++)
getch();}
INPUT:
OUTPUT:
PROCESS
BURST TIME
24
30
D). PRIORITY:
SOURCE CODE:
#include<stdio.h>
main()
tatavg;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++){
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- ",i); scanf("%d
%d",&bt[i], &pri[i]);
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
wtavg = wt[0] = 0;
for(i=1;i<n;i++)
for(i=0;i<n;i++)
getch();}
INPUT
OUTPUT
G TIME
TURNARO
UND TIME
11101
42516
0 3 10 6 16
2 4 2 16 18
3 5 1 18 19
#!/bin/bash
#!/bin/bash
read a
read b
sum=$((a + b))
#!/bin/bash
read a
read b
temp=$a
a=$b
b=$temp
#!/bin/bash
read num
temp=$num
sum=0
do
digit=$((temp % 10))
temp=$((temp / 10))
done
else
fi
#!/bin/bash
read n
a=0
b=1
do
next=$((a + b))
a=$b
b=$next
done
echo
#!/bin/bash
read n
fact=1
do
fact=$((fact * i))
done
#!/bin/bash
read num
temp=$num
rev=0
do
digit=$((temp % 10))
rev=$((rev * 10 + digit))
temp=$((temp / 10))
done
else
fi
#!/bin/bash
read num
sum=0
do
sum=$((sum + i))
fi
done
else
Fi
Experiment-4
#!/bin/bash
read n
declare -a processId
declare -a burstTime
declare -a waitingTime
declare -a turnaroundTime
read burstTime[$i]
processId[$i]=$((i + 1))
done
waitingTime[0]=0
turnaroundTime[$i]=$((waitingTime[i] + burstTime[i]))
done
totalWaitingTime=0
totalTurnaroundTime=0
echo -e "${processId[$i]}\t\t${burstTime[$i]}\t\t${waitingTime[$i]}\t\t$
{turnaroundTime[$i]}"
totalWaitingTime=$((totalWaitingTime + waitingTime[i]))
totalTurnaroundTime=$((totalTurnaroundTime + turnaroundTime[i]))
done
SJF
#!/bin/bash
read n
# Initialize arrays for storing process data
declare -a processId
declare -a burstTime
declare -a waitingTime
declare -a turnaroundTime
read burstTime[$i]
processId[$i]=$((i + 1))
done
temp=${burstTime[$i]}
burstTime[$i]=${burstTime[$j]}
burstTime[$j]=$temp
# Swap process ID
temp=${processId[$i]}
processId[$i]=${processId[$j]}
processId[$j]=$temp
fi
done
done
waitingTime[0]=0
done
turnaroundTime[$i]=$((waitingTime[i] + burstTime[i]))
done
totalWaitingTime=0
totalTurnaroundTime=0
echo -e "${processId[$i]}\t\t${burstTime[$i]}\t\t${waitingTime[$i]}\t\t$
{turnaroundTime[$i]}"
totalWaitingTime=$((totalWaitingTime + waitingTime[i]))
totalTurnaroundTime=$((totalTurnaroundTime + turnaroundTime[i]))
done
Experiment-5
PRIORITY
#!/bin/bash
read n
declare -a processId
declare -a burstTime
declare -a priority
declare -a waitingTime
declare -a turnaroundTime
read burstTime[$i]
echo "Enter priority for process $((i + 1)) (Lower number means higher priority):"
read priority[$i]
processId[$i]=$((i + 1))
done
temp=${burstTime[$i]}
burstTime[$i]=${burstTime[$j]}
burstTime[$j]=$temp
# Swap priority
temp=${priority[$i]}
priority[$i]=${priority[$j]}
priority[$j]=$temp
# Swap process ID
temp=${processId[$i]}
processId[$i]=${processId[$j]}
processId[$j]=$temp
fi
done
done
waitingTime[0]=0
done
turnaroundTime[$i]=$((waitingTime[i] + burstTime[i]))
done
totalWaitingTime=0
totalTurnaroundTime=0
echo -e "${processId[$i]}\t\t${burstTime[$i]}\t\t${priority[$i]}\t\t${waitingTime[$i]}\t\t$
{turnaroundTime[$i]}"
totalWaitingTime=$((totalWaitingTime + waitingTime[i]))
totalTurnaroundTime=$((totalTurnaroundTime + turnaroundTime[i]))
done
ROUND ROBIN
#!/bin/bash
read n
declare -a processId
declare -a burstTime
declare -a remainingTime
declare -a waitingTime
declare -a turnaroundTime
read timeQuantum
read burstTime[$i]
remainingTime[$i]=${burstTime[$i]}
processId[$i]=$((i + 1))
waitingTime[$i]=0
turnaroundTime[$i]=0
done
currentTime=0
completedProcesses=0
currentTime=$((currentTime + timeQuantum))
remainingTime[$i]=$((remainingTime[$i] - timeQuantum))
else
currentTime=$((currentTime + remainingTime[$i]))
waitingTime[$i]=$((currentTime - burstTime[$i]))
remainingTime[$i]=0
turnaroundTime[$i]=$((waitingTime[$i] + burstTime[$i]))
completedProcesses=$((completedProcesses + 1))
fi
fi
done
done
totalWaitingTime=0
totalTurnaroundTime=0
echo -e "${processId[$i]}\t\t${burstTime[$i]}\t\t${waitingTime[$i]}\t\t$
{turnaroundTime[$i]}"
totalWaitingTime=$((totalWaitingTime + waitingTime[i]))
totalTurnaroundTime=$((totalTurnaroundTime + turnaroundTime[i]))
done
Experiment-6
DESCRIPTION
One of the simplest methods for memory allocation is to divide memory into several fixed-
sized partitions. Each partition may contain exactly one process. In this multiple-partition
method, when a partition is free, a process is selected from the input queue and is loaded into
the free partition. When the process terminates, the partition becomes available for another
process. The operating system keeps a table indicating which parts of memory are available
and which are occupied. Finally, when a process arrives and needs memory, a memory
section large enough for this process is provided. When it is time to load or swap a process
into main memory, and if there is more than one free block of memory of sufficient size, then
the operating system must decide which free block to allocate. Best-fit strategy chooses the
block that is closest in size to the request. First-fit chooses the first available block that is
large enough. Worst-fit chooses the largest available block.
PROGRAM
FIRST-FIT
#include<stdio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
Page 25
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
Enter the size of the blocks:-
Block 1: 5
Block 2: 2
Block 3: 7
Enter the size of the files:-
File 1: 1
File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment
1 1 1 5 4
2 4 3 7 3
BEST-FIT
#include<stdio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}}
frag[i]=lowest; bf[ff[i]]=1; lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock
Size\tFragment"); for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
Enter the size of the blocks:-
Block 1: 5
Block 2: 2
Block 3: 7
Enter the size of the files:-
File 1: 1
File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment
1 1 2 2 1
2 4 1 5 1
WORST-FIT
#include<stdio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
}
}
frag[i]=highest; bf[ff[i]]=1; highest=0;
}
ff[i]=j; highest=temp;
}
printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
Enter the size of the blocks:-
Block 1: 5
Block 2: 2
Block 3: 7
Enter the size of the files:-
File 1: 1
File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment
1 1 3 7 6
2 4 1 5 1
Experiment-7
Bankers algorithm
#include <stdio.h>
int checkSafety(int processes[], int avail[], int max[][10], int allocation[][10], int n, int m) {
int need[10][10], finish[10], safeSeq[10], work[10];
calculateNeed(need, max, allocation, n, m);
int main() {
int n, m, i, j;
return 0;
}
Output:
Enter the number of processes: 5
Enter the number of resource types: 3
Enter the available resources for each type: 3 3 2
Enter the maximum matrix:
For process 0: 7 5 3
For process 1: 3 2 2
For process 2: 9 0 2
For process 3: 2 2 2
For process 4: 4 3 3
Enter the allocation matrix:
For process 0: 0 1 0
For process 1: 2 0 0
For process 2: 3 0 2
For process 3: 2 1 1
For process 4: 0 0 2
Experiment-8
Dining-Philosophers Problem and Producer Consumer Problem
Dining-philosphers:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
int main() {
pthread_t thread_id[N]; // Thread IDs for philosophers
return 0;
}
Output:
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 5 is thinking
Philosopher 1 is Hungry
Philosopher 1 takes fork 5 and 1
Philosopher 1 is Eating
Philosopher 3 is Hungry
Philosopher 3 takes fork 2 and 3
Philosopher 3 is Eating
Philosopher 1 putting fork 5 and 1 down
Philosopher 1 is Thinking
Philosopher 4 is Hungry
Philosopher 4 takes fork 3 and 4
Philosopher 4 is Eating
...
Producer-consumer :
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
int main() {
pthread_t prod, cons;
// Join threads
pthread_join(prod, NULL);
pthread_join(cons, NULL);
return 0;
}
Output:
Producer produced: 15
Consumer consumed: 15
Producer produced: 28
Consumer consumed: 28
Producer produced: 42
Consumer consumed: 42
Producer produced: 99
Consumer consumed: 99
Producer produced: 37
Consumer consumed: 37
Producer produced: 74
Consumer consumed: 74
Experiment-9
DEAD LOCK AVOIDANCE:
AIM: To Simulate bankers algorithm for Dead Lock Avoidance (Banker‘s Algorithm)
ALGORITHM:
8. If the new request comes then check that the system is in safety.
11. End
SOURCE CODE :
#include <stdio.h>
#include <stdbool.h>
void calculateNeed(int need[10][10], int max[10][10], int allocation[10][10], int n, int
m) {
bool isSafe(int processes[], int avail[], int max[][10], int allocation[][10], int n, int m) {
int need[10][10];
bool finish[10] = {0}; // Array to keep track of processes that are completed
int count = 0; // Count of processes that are included in the safe sequence
int j;
if (j == m) {
work[k] += allocation[p][k];
safeSeq[count++] = p;
finish[p] = 1;
found = true;
if (!found) {
return false;
}
printf("\n");
return true;
int main() {
int n, m;
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &avail[i]);
scanf("%d", &max[i][j]);
}
printf("Enter the allocation matrix:\n");
scanf("%d", &allocation[i][j]);
return 0;
input:
For process 0: 7 5 3
For process 1: 3 2 2
For process 2: 9 0 2
For process 3: 2 2 2
For process 4: 4 3 3
For process 0: 0 1 0
For process 1: 2 0 0
For process 2: 3 0 2
For process 3: 2 1 1
For process 4: 0 0 2
Output:
Experiment-10
Banker‘s Algorithm: When a new process enters a system, it must declare the
maximum number of instances of each resource type it needed. This number may
exceed the total number of resources in the system. When the user request a set of
resources, the system must determine whether the allocation of each resources will
leave the system in safe state. If it will the resources are allocation; otherwise the
process must wait until some other process release the resources
Program:
#include <stdio.h>
#include <stdbool.h>
printf("\n");
}
printf("\n");
printf("\nAvailable Resources:\n");
printf("\n\n");
return false; // Cannot allocate if the request is greater than available resources
return true;
}
void resourceAllocation(int allocation[NUM_PROCESSES][NUM_RESOURCES], int
request[NUM_PROCESSES][NUM_RESOURCES], int avail[NUM_RESOURCES])
{
// Allocate resources
} else {
int main() {
int allocation[NUM_PROCESSES][NUM_RESOURCES] = {
};
int request[NUM_PROCESSES][NUM_RESOURCES] = {
{0, 0, 0}, // Request for Process 0
};
return 0;
Output:
010
200
302
211
002
000
000
000
000
000
Available Resources:
332
010
200
302
211
002
202
000
002
000
Available Resources:
332
030
402
302
211
002
000
000
000
002
000
Available Resources:
100
Experiment - 11
ALGORITHM:
6. Replace the page with circular queue, while re-placing check page availability in the
frame Place avail[i]=1 if page is placed in theframe Count page faults
7. Print the results.
A) FIFO
#include <stdio.h>
if (frames[i] == -1)
printf(" [ ]");
else
printf("\n");
int main() {
scanf("%d", &num_pages);
printf("Enter the number of frames: ");
scanf("%d", &num_frames);
int pages[num_pages];
int frames[num_frames];
int page_faults = 0;
frames[i] = -1;
scanf("%d", &pages[i]);
if (frames[j] == page) {
break;
if (!flag) {
frames[index] = page; // Replace the oldest frame with the current page
page_faults++;
} else {
return 0;
Input:
Page 1: 1
Page 2: 3
Page 3: 0
Page 4: 3
Page 5: 5
Page 6: 6
Page 7: 3
Page 8: 5
Page 9: 6
Output:
B) LRU:
#include <stdio.h>
if (frames[i] == -1)
printf(" [ ]");
else
printf("\n");
int lru_index = 0;
lru_index = i;
return lru_index;
int main() {
scanf("%d", &num_frames);
int pages[num_pages];
int frames[num_frames];
int last_used[num_frames];
int page_faults = 0;
frames[i] = -1;
last_used[i] = -1;
scanf("%d", &pages[i]);
int found = 0;
break;
if (!found) {
page_faults++;
} else {
return 0;
Page 1: 7
Page 2: 0
Page 3: 1
Page 4: 2
Page 5: 0
Page 6: 3
Page 7: 0
Page 8: 4
Page 9: 2
C) OPTIMAL
#include <stdio.h>
#include <limits.h>
if (frames[i] == -1)
printf(" [ ]");
else
printf("\n");
int findOptimal(int frames[], int num_frames, int pages[], int current_index, int
num_pages) {
int j;
if (frames[i] == pages[j]) {
if (j > furthest_index) {
furthest_index = j;
break;
if (j == num_pages) {
// If all pages are found, replace the one that will be used furthest in the future
return page_to_replace;
int main() {
scanf("%d", &num_pages);
scanf("%d", &num_frames);
int pages[num_pages];
int frames[num_frames];
int page_faults = 0;
frames[i] = -1;
scanf("%d", &pages[i]);
int found = 0;
if (frames[j] == page) {
break;
if (!found) {
int replace_index = findOptimal(frames, num_frames, pages, i + 1,
num_pages);
page_faults++;
} else {
return 0;
Page 1: 7
Page 2: 0
Page 3: 1
Page 4: 2
Page 5: 0
Page 6: 3
Page 7: 0
Page 8: 4
Page 9: 2