Notes
Notes - notes.io |
Practical: - 2
AIM: -STUDY OF UNIX COMMANDS
IU2041050116
BHAVYA SHAH
PAGE NO 14
OPERATING SYSTEM(CE0418)
PROGRAM:
2006 cd ~
2007 ls
2008 ls -l
2009 ls -a
2010 ls -r
2011 man ls
2012 echo "An Amazing Day" 2013 echo "An nAmazing Day" 2014 echo -e "An nAmazing Day"
2015 echo -e "An tAmazing Day" 2016 echo -e "An bAmazing Day" 2017 cal -1
2018 cal -3
2019 cal -s
2020 cal -m
2021 cal
2022 cal -j
2023 cal -y
2024 date +%a
2025 date +%A
2026 date +%b
2027 date +%B
2028 date +%Y
IU2041050116
BHAVYA SHAH
PAGE NO 15
OPERATING SYSTEM(CE0418)
2029 date +%w
2030 date +%d
2031 date +%m
2032 date +%y
2033 date +%T
2034 date +%H
2035 date +%M
2036 date +%S
2037 date +%V
2038 date +%P
2039 cat > file1.txt
2040 cat file1.txt
2041 cat file1.txt > file2.txt 2042 cat file2.txt
2043 cat file1.txt file2.txt
2044 cat file1.txt file2.txt > file3.txt 2045 cat file3.txt
2046 passwd
2047 who
2048 who -b
2049 who -H
2050 who -q
IU2041050116
BHAVYA SHAH
PAGE NO 16
OPERATING SYSTEM(CE0418)
2051 who -a
2052 whoami
2053 whoami --version
2054 uname -s
2055 uname -n
2056 uname -v
2057 uname -m
2058 uname -o
2059 ls
2060 mkdir temp 2061 ls
2062 mkdir -v temp2 2063 ls
2064 ls -l
2065 mv file1.txt ../temp 2066 mv file1.txt ../temp2 2067 mv file1.txt ../temp 2068 ls
2069 cat file1.txt
2070 cut -c 3 file1.txt
2071 cut -c 3-4 file1.txt
2072 cmp -b file1.txt file2.txt 2073 cmp -s file1.txt file2.txt 2074 cat file3.txt
2075 chmod 764 file3.txt 2076 ls -l
2077 file -i file1.txt
2078 finger root
IU2041050116
BHAVYA SHAH
PAGE NO 17
OPERATING SYSTEM(CE0418)
2079 sudo apt install finger 2080 finger root
2081 finger -m
2082 finger -l
2083 sleep 5
2084 sleep 6
2085 ps -e
2086 ps -x
2087 ps -f
2088 ps
2089 cat file2.txt
2090 wc file2.txt
2091 nl file1.txt
2092 sort file1.txt
2093 ls
2094 find file1.txt
2095 find file*
2096 find -newer file1.txt 2097 ls
2098 cat file1.txt
2099 grep hi file1.txt
2100 grep hello file1.txt
IU2041050116
BHAVYA SHAH
PAGE NO 18
OPERATING SYSTEM(CE0418)
2101 history
2102 history -40
2103 history -4
2104 history 4
2105 history 40
2106 history 50
2107 history 70
2108 history 100
2109 history 120
2110 history 110
2111 clear
IU2041050116
BHAVYA SHAH
PAGE NO 19
OPERATING SYSTEM(CE0418)
Practical: - 3
AIM: -IMPLEMENTATION OF SHELL SCRIPT
Implement following with shell script:
1. Write a shell script to input two numbers from the user and perform addition,
subtraction, multiplication, and division.
2.
The distance between two cities (in km.) is input through the keyboard.
Write a shell script to convert and print distance in meters, feet,
inches and centimeters.
3. Any integer is input through the keyboard. Write a shell script to find out whether it
is an odd number or even number.
4.
Write a shell script which receives any year form the keyboard and
determines whether the year is a leap year or not. If no argument is
supplied the current year should be assumed.
5. Write a shell script to find the factorial of any no entered through keyboard
IU2041050116
BHAVYA SHAH
PAGE NO 20
OPERATING SYSTEM(CE0418)
1.Write a shell script to input two numbers from the user and perform
addition, subtraction, multiplication, and division.
Code
#!bin/bash
echo "Enter Two
numbers: " read a
read b
echo "Enter
Choice: " echo
"1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4.
Division" read
ch
case $ch in
· ans=`echo $a + $b | bc`
;;
· ans=`echo $a - $b | bc`
;;
· ans=`echo $a * $b | bc`
;;
· ans=`echo "scale=2; $a / $b" | bc`
IU2041050116
BHAVYA SHAH
PAGE NO 21
OPERATING SYSTEM(CE0418)
;;
esac
echo "Result: $ans"
Output
IU2041050116
BHAVYA SHAH
PAGE NO 22
OPERATING SYSTEM(CE0418)
1. The distance between two cities (in km.) is input through the
keyboard.Write a shell script to convert and print distance in meters,
feet,inches and centimeters.
Code
#!bin/bash
echo "Enter distance between two cities
(in km.) : " read km
meter=`echo $km * 1000 |
bc` feet=`echo $meter *
3.2808 | bc` inches=`echo
$feet * 12 | bc` cm=`echo
$feet * 30.48 | bc`
IU2041050116
BHAVYA SHAH
PAGE NO 23
OPERATING SYSTEM(CE0418)
echo "Total meter is :
$meter " echo "Total feet
is : $feet" echo "Total
inches is : $inches" echo
"Total centimeters : $cm"
Output
IU2041050116
BHAVYA SHAH
PAGE NO 24
OPERATING SYSTEM(CE0418)
2. Any integer is input through the keyboard. Write a shell script to find out
whether it is an odd number or even number.
Code
Output
IU2041050116
BHAVYA SHAH
PAGE NO 25
OPERATING SYSTEM(CE0418)
3. Write a shell script which receives any year form the keyboard
anddetermines whether the year is a leap year or not. If no argument issupplied
the current year should be assumed.
echo "Enter a
year:" read year
if [ -z "$year" ]; then
year=$(date +"%Y")
fi
if [ $(( year % 4 ))
-eq 0 ] then
if [ $(( year % 100 ))
-eq 0 ] then
if [ $(( year % 400))
-eq 0 ] then
echo "$year is a leap year"
else
echo "$year is not a leap year"
fi
else
echo "$year is a leap year"
fi
else
echo "$year is not a leap year"
fi
IU2041050116
BHAVYA SHAH
PAGE NO 26
OPERATING SYSTEM(CE0418)
Output
4. Write a shell script to find the factorial of any no entered through
keyboard
Code
IU2041050116
BHAVYA SHAH
PAGE NO 27
OPERATING SYSTEM(CE0418)
Output
IU2041050116
BHAVYA SHAH
PAGE NO 28
OPERATING SYSTEM(CE0418)
Practical: - 4
AIM: -IMPLEMENTATION OF SHELL SCRIPT
1 Write a shell script which will accept a number band display first n prime numbers as
output.
2 Write a shell script which will generate first n Fibonacci numbers like: 1, 1, 2, 3, 5,
13,...
3 Write a shell script to read n numbers as command arguments and sort them in
descending order. Knowledge of
4 Write a shell script to display all executable files, directories and zero sized files.
5 Write a shell script to fetch data from a file and display data into another file in
reverse order.
1. Write a shell script which will accept a number band display first
n prime numbers as output.
Code
echo "Enter a
limit" read
limit
echo "prime numbers upto
$limit are :" echo "1"
i=2
while [ $i -le
$limit ] do
flag=1
j=2
IU2041050116
BHAVYA SHAH
PAGE NO 29
OPERATING SYSTEM(CE0418)
while [ $j -lt
$i ] do
rem=$(( $i %
$j )) if [ $rem
-eq 0 ] then
flag=0
break
fi
j=$(( $j+1
)) done
if [ $flag -eq
1 ] then
echo "$i"
fi
i=$(( $i+1
)) done
Output
IU2041050116
BHAVYA SHAH
PAGE NO 30
OPERATING SYSTEM(CE0418)
IU2041050116
BHAVYA SHAH
PAGE NO 31
OPERATING SYSTEM(CE0418)
2. Write a shell script which will generate first n Fibonacci numbers like: 1,
1, 2, 3, 5, 13,... Knowledge of
Code
IU2041050116
BHAVYA SHAH
PAGE NO 32
OPERATING SYSTEM(CE0418)
Output
3. Write a shell script to read n numbers as command arguments and sort
them in descending order.
Code
if [ $# -eq 0
] then
echo "Enter valid
Arguments" exit
fi
> temp
for i in $*
IU2041050116
BHAVYA SHAH
PAGE NO 33
OPERATING SYSTEM(CE0418)
do
echo "$i" >>
temp done
echo "Your sorted
data:" sort -nr temp
echo "End of Script"
IU2041050116
BHAVYA SHAH
PAGE NO 34
OPERATING SYSTEM(CE0418)
Output
4. Write a shell script to display all executable files, directories and zero
sized files.
Code
IU2041050116
BHAVYA SHAH
PAGE NO 35
OPERATING SYSTEM(CE0418)
Output
IU2041050116
BHAVYA SHAH
PAGE NO 36
OPERATING SYSTEM(CE0418)
5.Write a shell script to fetch data from a file and display data into another
file in reverse order.
Code
echo "Enter
filename: " read
file
cat $file | tac >>
reversed.txt echo "$file
content: "
cat $file
echo "nreversed.txt (after reversed)
content:" cat reversed.txt
Output
IU2041050116
BHAVYA SHAH
PAGE NO 37
OPERATING SYSTEM(CE0418)
IU2041050116
BHAVYA SHAH
PAGE NO 38
OPERATING SYSTEM(CE0418)
Practical: - 5
AIM: -IMPLEMENT FCFS ALGORITHM AND IT'S PREEMPTIVE VERSION
ROUND ROBIN,TAKE NECESSARY ASSUMPTIONS IF NEEDED
FCFS ALGORITHM
package fcfs2;
import java.util.Scanner;
public class Fcfs2
{
int burstTime[];
int arrivalTime[];
String[] processId;
int numberOfProcess;
void getProcessData(Scanner input)
{
System.out.print("Enter the number of Process for Scheduling : ");
int inputNumberOfProcess = input.nextInt();
numberOfProcess = inputNumberOfProcess;
burstTime = new int[numberOfProcess];
arrivalTime = new int[numberOfProcess];
IU2041050116
BHAVYA SHAH
PAGE NO 39
OPERATING SYSTEM(CE0418)
processId = new String[numberOfProcess];
String st = "P";
for (int i = 0; i < numberOfProcess; i++)
{
processId[i] = st.concat(Integer.toString(i));
System.out.print("Enter the burst time for Process - " + (i) + " : ");
burstTime[i] = input.nextInt();
System.out.print("Enter the arrival time for Process - " + (i) + " : ");
arrivalTime[i] = input.nextInt();
}
}
void sortAccordingArrivalTime(int[] at, int[] bt, String[] pid)
{
boolean swapped;
int temp;
String stemp;
for (int i = 0; i < numberOfProcess; i++)
{
swapped = false;
for (int j = 0; j < numberOfProcess - i - 1; j++)
{
if (at[j] > at[j + 1])
{
IU2041050116
BHAVYA SHAH
PAGE NO 40
OPERATING SYSTEM(CE0418)
//swapping arrival time
temp = at[j];
at[j] = at[j + 1];
at[j + 1] = temp;
//swapping burst time
temp = bt[j];
bt[j] = bt[j + 1];bt[j + 1] = temp;
//swapping process id
stemp = pid[j];
pid[j] = pid[j + 1];
pid[j + 1] = stemp;
//enhanced bubble sort
swapped = true;
}
}
if (swapped == false)
{
break;
}
}
}
IU2041050116
BHAVYA SHAH
PAGE NO 41
OPERATING SYSTEM(CE0418)
void Fcfs2()
{
int finishTime[] = new int[numberOfProcess];
int bt[] = burstTime.clone();
int at[] = arrivalTime.clone();
String pid[] = processId.clone();
int waitingTime[] = new int[numberOfProcess];
int turnAroundTime[] = new int[numberOfProcess];
sortAccordingArrivalTime(at, bt, pid);
//calculating waiting & turn-around time for each process
finishTime[0] = at[0] + bt[0];
turnAroundTime[0] = finishTime[0] - at[0];
waitingTime[0] = turnAroundTime[0] - bt[0];
for (int i = 1; i < numberOfProcess; i++)
{
finishTime[i] = bt[i] + finishTime[i - 1];
turnAroundTime[i] = finishTime[i] - at[i];
waitingTime[i] = turnAroundTime[i] - bt[i];
}
float sum = 0;
for (int n : waitingTime)
{
sum += n;
IU2041050116
BHAVYA SHAH
PAGE NO 42
OPERATING SYSTEM(CE0418)
}
float averageWaitingTime = sum / numberOfProcess;
sum = 0;
for (int n : turnAroundTime)
{
sum += n;
}
float averageTurnAroundTime = sum / numberOfProcess;
//print on console the order of processes scheduled using FirstComeFirstServer Algorithm
System.out.println("FCFS Scheduling Algorithm : ");
System.out.format("%20s%20s%20s%20s%20s%20sn", "ProcessId", "BurstTime",
"ArrivalTime", "FinishTime", "WaitingTime", "TurnAroundTime");
for (int i = 0; i < numberOfProcess; i++)
{
System.out.format("%20s%20d%20d%20d%20d%20dn", pid[i], bt[i], at[i], finishTime[i],
waitingTime[i], turnAroundTime[i]);
}
System.out.format("%80s%20f%20fn", "Average", averageWaitingTime,
averageTurnAroundTime);
}
public static void main(String[] args)
IU2041050116
BHAVYA SHAH
PAGE NO 43
OPERATING SYSTEM(CE0418)
{
Scanner input = new Scanner(System.in);
Fcfs2 obj = new Fcfs2();
obj.getProcessData(input);
obj.Fcfs2();
}
}
OPERATING SYSTEM IU2141051151
IU2041050116
BHAVYA SHAH
PAGE NO 44
OPERATING SYSTEM(CE0418)
IU2041050116
BHAVYA SHAH
PAGE NO 45
OPERATING SYSTEM(CE0418)
ROUND ROBIN ALGORITHM
IU2041050116
BHAVYA SHAH
PAGE NO 46
OPERATING SYSTEM(CE0418)
public class Main
{
// Method to find the waiting time for all
// processes
static void findWaitingTime(int processes[], int n,
int bt[], int wt[], int quantum)
{
// Make a copy of burst times bt[] to store remaining
// burst times.
int rem_bt[] = new int[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
int t = 0; // Current time
// Keep traversing processes in round robin manner
// until all of them are not done.
while(true)
{
boolean done = true;
// Traverse all processes one by one repeatedly
for (int i = 0 ; i < n; i++)
{
IU2041050116
BHAVYA SHAH
PAGE NO 47
OPERATING SYSTEM(CE0418)
// If burst time of a process is greater than 0
// then only need to process further
if (rem_bt[i] > 0)
{
done = false; // There is a pending process
if (rem_bt[i] > quantum)
{
// Increase the value of t i.e. shows
// how much time a process has been processed
t += quantum;
// Decrease the burst_time of current process
// by quantum
rem_bt[i] -= quantum;
}
// If burst time is smaller than or equal to
// quantum. Last cycle for this process
else
{
// Increase the value of t i.e. shows
// how much time a process has been processed
t = t + rem_bt[i];
IU2041050116
BHAVYA SHAH
PAGE NO 48
OPERATING SYSTEM(CE0418)
// Waiting time is current time minus time
// used by this process
wt[i] = t - bt[i];
// As the process gets fully executed
// make its remaining burst time = 0
rem_bt[i] = 0;
}
}
}
// If all processes are done
if (done == true)
break;
}
}
// Method to calculate turn around time
static void findTurnAroundTime(int processes[], int n,
int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
IU2041050116
BHAVYA SHAH
PAGE NO 49
OPERATING SYSTEM(CE0418)
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
// Method to calculate average time
static void findavgTime(int processes[], int n, int bt[],
int quantum)
{
int wt[] = new int[n], tat[] = new int[n];
int total_wt = 0, total_tat = 0;
// Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt, quantum);
// Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
// Display processes along with all details
System.out.println("Processes " + " Burst time " +
" Waiting time " + " Turn around time");
// Calculate total waiting time and total turn
IU2041050116
BHAVYA SHAH
PAGE NO 50
OPERATING SYSTEM(CE0418)
// around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
System.out.println(" " + (i+1) + "tt" + bt[i] +"t " +
wt[i] +"tt " + tat[i]);
}
System.out.println("Average waiting time = " +
(float)total_wt / (float)n);
System.out.println("Average turn around time = " +
(float)total_tat / (float)n);
}
// Driver Method
public static void main(String[] args)
{
// process id's
int processes[] = { 1, 2, 3};
int n = processes.length;
// Burst time of all processes
int burst_time[] = {10, 5, 8};
IU2041050116
BHAVYA SHAH
PAGE NO 51
OPERATING SYSTEM(CE0418)
// Time quantum
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
}
}
IU2041050116
BHAVYA SHAH
PAGE NO 52
OPERATING SYSTEM(CE0418)
IU2041050116
BHAVYA SHAH
PAGE NO 53
OPERATING SYSTEM(CE0418)
IU2041050116
BHAVYA SHAH
PAGE NO 54
OPERATING SYSTEM(CE0418)
OUTPUTProcesses Burst time Waiting time Turn around time
1 10 13 23
2 5 10 15
3 8 13 21
Average waiting time = 12.0
Average turn around time = 19.666666
IU2041050116
BHAVYA SHAH
PAGE NO 55
OPERATING SYSTEM(CE0418)
IU2041050116
BHAVYA SHAH
PAGE NO 56
OPERATING SYSTEM(CE0418)
Practical: - 6
AIM: -implement SJF and SRTN algorithm with necessary assumptions
SJF
package sjf;
import java.util.*;
public class SJF {
public static void main(String args[])
{
try (Scanner sc = new Scanner(System.in)) {
System.out.println ("enter no of process:");
int n = sc.nextInt();
int pid[] = new int[n];
int at[] = new int[n]; // at means arrival time
int bt[] = new int[n]; // bt means burst time
int ct[] = new int[n]; // ct means complete time
int ta[] = new int[n]; // ta means turn around time
int wt[] = new int[n]; //wt means waiting time
int f[] = new int[n]; // f means it is flag it checks process is completed or not
IU2041050116
BHAVYA SHAH
PAGE NO 57
OPERATING SYSTEM(CE0418)
int st=0, tot=0;
float avgwt=0, avgta=0;
for(int i=0;i<n;i++)
{
System.out.println ("enter process " + (i+1) + " arrival time:");
at[i] = sc.nextInt();
System.out.println ("enter process " + (i+1) + " brust time:");
bt[i] = sc.nextInt();
pid[i] = i+1;
f[i] = 0;
} boolean a = true;
while(true)
{
int c=n, min=999;
if (tot == n) // total no of process = completed process loop will be terminated
break;
for (int i=0; i<n; i++)
{
/*
* If i'th process arrival time <= system time and its flag=0 and burst<min
* That process will be executed first
*/
if ((at[i] <= st) && (f[i] == 0) && (bt[i]<min))
{
min=bt[i];
IU2041050116
BHAVYA SHAH
PAGE NO 58
OPERATING SYSTEM(CE0418)
c=i;
}
}
/* If c==n means c value can not updated because no process arrival time< system time so
we increase the system time */
if (c==n)
st++;
else
{
ct[c]=st+bt[c];
st+=bt[c];
ta[c]=ct[c]-at[c];
wt[c]=ta[c]-bt[c];
f[c]=1;
tot++;
}
} System.out.println("npid arrival brust complete turn waiting");
for(int i=0;i<n;i++)
{
avgwt+= wt[i];
avgta+= ta[i];
System.out.println(pid[i]+"t"+at[i]+"t"+bt[i]+"t"+ct[i]+"t"+ta[i]+"t"+wt[i]);
} System.out.println ("naverage tat is "+ (float)(avgta/n));
IU2041050116
BHAVYA SHAH
PAGE NO 59
OPERATING SYSTEM(CE0418)
System.out.println ("average wt is "+ (float)(avgwt/n));
}
}
}
IU2041050116
BHAVYA SHAH
PAGE NO 60
OPERATING SYSTEM(CE0418)
1
IU2041050116
BHAVYA SHAH
PAGE NO 61
OPERATING SYSTEM(CE0418)
OUTPUTpid arrival brust complete turn waiting
1 2 2 4 2 0
2 4 4 8 4 0
3 5 6 14 9 3
average tat is 5.0
average wt is 1.0
IU2041050116
BHAVYA SHAH
PAGE NO 62
OPERATING SYSTEM(CE0418)
SRTF
import java.io.*;
public class SRTF {
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int n;
System.out.println("Please enter the number of Processes:
");
n = Integer.parseInt(br.readLine());
int proc[][] = new int[n + 1][4];//proc[][0] is the AT
array,[][1] - RT,[][2] - WT,[][3] - TT
for(int i = 1; i <= n; i++)
{
System.out.println("Please enter the Arrival Time for
Process " + i + ": ");
proc[i][0] = Integer.parseInt(br.readLine());
System.out.println("Please enter the Burst Time for Process
" + i + ": ");
proc[i][1] = Integer.parseInt(br.readLine());
}
System.out.println();
//Calculation of Total Time and Initialization of Time
Chart array
int total_time = 0;
for(int i = 1; i <= n; i++)
OPERATING SYSTEM IU2141051151
{
total_time += proc[i][1];
}
int time_chart[] = new int[total_time];
for(int i = 0; i < total_time; i++)
{
//Selection of shortest process which has arrived
int sel_proc = 0;
int min = 99999;
IU2041050116
BHAVYA SHAH
PAGE NO 63
OPERATING SYSTEM(CE0418)
for(int j = 1; j <= n; j++)
{
if(proc[j][0] <= i)//Condition to check if Process has
arrived
{
if(proc[j][1] < min && proc[j][1] != 0)
{
min = proc[j][1];
sel_proc = j;
}
}
}
//Assign selected process to current time in the Chart
time_chart[i] = sel_proc;
//Decrement Remaining Time of selected process by 1 since it
has been assigned the CPU for 1 unit of time
proc[sel_proc][1]--;
//WT and TT Calculation
for(int j = 1; j <= n; j++)
{
if(proc[j][0] <= i)
{
if(proc[j][1] != 0)
{
proc[j][3]++;//If process has arrived and it has not
already completed execution its TT is incremented by 1
if(j != sel_proc)//If the process has not been
currently assigned the CPU and has arrived its WT is incremented
by 1
proc[j][2]++;
}
else if(j == sel_proc)//This is a special case in which
the process has been assigned CPU and has completed its execution
proc[j][3]++;
}
}
//Printing the Time Chart
if(i != 0)
{
if(sel_proc != time_chart[i - 1])
IU2041050116
BHAVYA SHAH
PAGE NO 64
OPERATING SYSTEM(CE0418)
OPERATING SYSTEM IU2141051151
//If the CPU has been assigned to a different Process we need to
print the current value of time and the name of
//the new Process
{
System.out.print("--" + i + "--P" + sel_proc);
}
}
else//If the current time is 0 i.e the printing has just
started we need to print the name of the First selected Process
System.out.print(i + "--P" + sel_proc);
if(i == total_time - 1)//All the process names have been
printed now we have to print the time at which execution ends
System.out.print("--" + (i + 1));
}
System.out.println();
System.out.println();
//Printing the WT and TT for each Process
System.out.println("Pt WT t TT ");
for(int i = 1; i <= n; i++)
{
System.out.printf("%dt%2dmst%2dms",i,proc[i][2],proc[i][3]);
System.out.println();
}
System.out.println();
//Printing the average WT & TT
float WT = 0,TT = 0;
for(int i = 1; i <= n; i++)
{
WT += proc[i][2];
TT += proc[i][3];
}
WT /= n;
IU2041050116
BHAVYA SHAH
PAGE NO 65
OPERATING SYSTEM(CE0418)
TT /= n;
System.out.println("The Average WT is: " + WT + "ms");
System.out.println("The Average TT is: " + TT + "ms");
}
}
IU2041050116
BHAVYA SHAH
PAGE NO 66
OPERATING SYSTEM(CE0418)
IU2041050116
BHAVYA SHAH
PAGE NO 67
OPERATING SYSTEM(CE0418)
IU2041050116
BHAVYA SHAH
PAGE NO 68
OPERATING SYSTEM(CE0418)
OUTPUT—
IU2041050116
BHAVYA SHAH
PAGE NO 69
OPERATING SYSTEM(CE0418)
Practical: - 7
AIM: -IMPLEMENT PREMPTIVE AND NON PREEMPTIVE PRIORITY
SCHEDULING
PRIORITY PREMPTIVE
CODEimport java.util.Scanner;
public class priorityp{
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int x,n,p[],pp[],bt[],w[],t[],awt,atat,i;
p = new int[10];
pp = new int[10];
bt = new int[10];
w = new int[10];
t = new int[10];
//n is number of process
//p is process
//pp is process priority
IU2041050116
BHAVYA SHAH
PAGE NO 73
OPERATING SYSTEM(CE0418)
//bt is process burst time
//w is wait time
// t is turnaround time
//awt is average waiting time
//atat is average turnaround time
System.out.print("Enter the number of process : ");
n = s.nextInt();
System.out.print("nt Enter burst time : time priorities n");
for(i=0;i<n;i++)
{
System.out.print("nProcess["+(i+1)+"]:");
bt[i] = s.nextInt();
pp[i] = s.nextInt();
p[i]=i+1;
}
//sorting on the basis of priority
for(i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(pp[i]>pp[j])
IU2041050116
BHAVYA SHAH
PAGE NO 74
OPERATING SYSTEM(CE0418)
{
x=pp[i];
pp[i]=pp[j];
pp[j]=x;
x=bt[i];
bt[i]=bt[j];
bt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
}
}
}
w[0]=0;
awt=0;
t[0]=bt[0];
atat=t[0];
for(i=1;i<n;i++)
{
w[i]=t[i-1];
awt+=w[i];
t[i]=w[i]+bt[i];
atat+=t[i];
}
IU2041050116
BHAVYA SHAH
PAGE NO 75
OPERATING SYSTEM(CE0418)
//Displaying the process
System.out.print("nnProcess t Burst Time t Wait Time t Turn Around Time Priority n");
for(i=0;i<n;i++)
System.out.print("n "+p[i]+"tt "+bt[i]+"tt "+w[i]+"tt "+t[i]+"tt "+pp[i]+"n");
awt/=n;
atat/=n;
System.out.print("n Average Wait Time : "+awt);
System.out.print("n Average Turn Around Time : "+atat);
}
}
OUTPUTPlease enter the number of Processes:
3
Please enter the Arrival Time for Process 1:
4
Please enter the Burst Time for Process 1:
5
Please enter the Arrival Time for Process 2:
6
IU2041050116
BHAVYA SHAH
PAGE NO 76
OPERATING SYSTEM(CE0418)
Please enter the Burst Time for Process 2:
7
Please enter the Arrival Time for Process 3:
8
Please enter the Burst Time for Process 3:
9
0--P0--4--P1--9--P2--16--P3--21
P WT TT
1 0ms 5ms
2 3ms 10ms
3 8ms 13ms
The Average WT is: 3.6666667ms
The Average TT is: 9.333333ms
IU2041050116
BHAVYA SHAH
PAGE NO 77
OPERATING SYSTEM(CE0418)
PRIORITY NON – PREMPTIVE
import java.util.*;
/// Data Structure
class Process {
int at, bt, pri, pno;
Process(int pno, int at, int bt, int pri)
{
I =
this.pno = pno;
this.pri = pri;
this.at = at;
this.bt = bt;
}
}
IU2041050116
BHAVYA SHAH
PAGE NO 78
OPERATING SYSTEM(CE0418)
/// Gantt chart structure
class GChart {
// process number, start time, complete time,
// turn around time, waiting time
int pno, stime, ctime, wtime, ttime;
}
// user define comparative method (first arrival first serve,
// if arrival time same then heigh priority first)
class MyComparator implements Comparator {
public int compare(Object o1, Object o2)
{
Process p1 = (Process)o1;
Process p2 = (Process)o2;
if (p1.at < p2.at)
return (-1);
else if (p1.at == p2.at && p1.pri > p2.pri)
return (-1);
else
return (1);
}
}
// class to find Gantt chart
class FindGantChart {
void findGc(LinkedList queue)
{
// initial time = 0
int time = 0;
// priority Queue sort data according
// to arrival time or priority (ready queue)
IU2041050116
BHAVYA SHAH
PAGE NO 79
OPERATING SYSTEM(CE0418)
TreeSet prique = new TreeSet(new MyComparator());
// link list for store processes data
LinkedList result = new LinkedList();
// process in ready queue from new state queue
while (queue.size() > 0)
prique.add((Process)queue.removeFirst());
Iterator it = prique.iterator();
// time set to according to first process
time = ((Process)prique.first()).at;
// scheduling process
while (it.hasNext()) {
// dispatcher dispatch the
// process ready to running state
Process obj = (Process)it.next();
GChart gc1 = new GChart();
gc1.pno = obj.pno;
gc1.stime = time;
time += obj.bt;
gc1.ctime = time;
gc1.ttime = gc1.ctime - obj.at;
gc1.wtime = gc1.ttime - obj.bt;
/// store the exxtreted process
result.add(gc1);
}
// create object of output class and call method
new ResultOutput(result);
}
}
IU2041050116
BHAVYA SHAH
PAGE NO 80
OPERATING SYSTEM(CE0418)
OUTPUTPlease enter the number of Processes:
4
Please enter the Arrival Time for Process 1:
3
Please enter the Burst Time for Process 1:
4
Please enter the Arrival Time for Process 2:
5
Please enter the Burst Time for Process 2:
6
Please enter the Arrival Time for Process 3:
7
Please enter the Burst Time for Process 3:
8
Please enter the Arrival Time for Process 4:
9
Please enter the Burst Time for Process 4:
10
0--P0--3--P1--7--P2--13--P3--21--P4--28
P WT TT
1 0ms 4ms
IU2041050116
BHAVYA SHAH
PAGE NO 81
OPERATING SYSTEM(CE0418)
2 2ms 8ms
3 6ms 14ms
4 12ms 19ms
The Average WT is: 5.0ms
The Average TT is: 11.25ms
I
IU2041050116
BHAVYA SHAH
PAGE NO 82
OPERATING SYSTEM(CE0418)
Practical: - 8
AIM: -Implement and Present the output of following Disk scheduling
Algorithms.
a. FIFO
b. SCAN
c. LOOK
a. FIFO
class GFG
{
static int size = 8;
static void FCFS(int arr[], int head)
{
int seek_count = 0;
int distance, cur_track;
for (int i = 0; i < size; i++)
{
cur_track = arr[i];
IU2041050116
BHAVYA SHAH
PAGE NO 83
OPERATING SYSTEM(CE0418)
// calculate absolute distance
distance = Math.abs(cur_track - head);
// increase the total count
seek_count += distance;
// accessed track is now new head
head = cur_track;
}
System.out.println("Total number of " +
"seek operations = " +
seek_count);
// Seek sequence would be the same
// as request array sequence
System.out.println("Seek Sequence is");
for (int i = 0; i < size; i++)
{
System.out.println(arr[i]);
}
}
IU2041050116
BHAVYA SHAH
PAGE NO 84
OPERATING SYSTEM(CE0418)
// Driver code
public static void main(String[] args)
{
// request array
int arr[] = { 176, 79, 34, 60,
92, 11, 41, 114 };
int head = 50;
FCFS(arr, head);
}
}
Output
IU2041050116
BHAVYA SHAH
PAGE NO 85
OPERATING SYSTEM(CE0418)
b. SCAN
// Java program to demonstrate
// SCAN Disk Scheduling algorithm
import java.util.*;
class GFG
{
static int size = 8;
static int disk_size = 200;
static void SCAN(int arr[], int head, String direction)
IU2041050116
BHAVYA SHAH
PAGE NO 86
OPERATING SYSTEM(CE0418)
{
int seek_count = 0;
int distance, cur_track;
Vector<Integer> left = new Vector<Integer>(),
right = new Vector<Integer>();
Vector<Integer> seek_sequence = new Vector<Integer>();
// appending end values
// which has to be visited
// before reversing the direction
if (direction == "left")
left.add(0);
else if (direction == "right")
right.add(disk_size - 1);
for (int i = 0; i < size; i++)
{
if (arr[i] < head)
left.add(arr[i]);
if (arr[i] > head)
right.add(arr[i]);
}
// sorting left and right vectors
IU2041050116
BHAVYA SHAH
PAGE NO 87
OPERATING SYSTEM(CE0418)
Collections.sort(left);
Collections.sort(right);
// run the while loop two times.
// one by one scanning right
// and left of the head
int run = 2;
while (run-- >0)
{
if (direction == "left")
{
for (int i = left.size() - 1; i >= 0; i--)
{
cur_track = left.get(i);
// appending current track to seek sequence
seek_sequence.add(cur_track);
// calculate absolute distance
distance = Math.abs(cur_track - head);
// increase the total count
seek_count += distance;
IU2041050116
BHAVYA SHAH
PAGE NO 88
OPERATING SYSTEM(CE0418)
// accessed track is now the new head
head = cur_track;
}
direction = "right";
}
else if (direction == "right")
{
for (int i = 0; i < right.size(); i++)
{
cur_track = right.get(i);
// appending current track to seek sequence
seek_sequence.add(cur_track);
// calculate absolute distance
distance = Math.abs(cur_track - head);
// increase the total count
seek_count += distance;
// accessed track is now new head
head = cur_track;
}
direction = "left";
IU2041050116
BHAVYA SHAH
PAGE NO 89
OPERATING SYSTEM(CE0418)
}
}
System.out.print("Total number of seek operations = "
+ seek_count + "n");
System.out.print("Seek Sequence is" + "n");
for (int i = 0; i < seek_sequence.size(); i++)
{
System.out.print(seek_sequence.get(i) + "n");
}
}
// Driver code
public static void main(String[] args)
{
// request array
int arr[] = { 176, 79, 34, 60,
92, 11, 41, 114 };
int head = 50;
String direction = "left";
IU2041050116
BHAVYA SHAH
PAGE NO 90
OPERATING SYSTEM(CE0418)
SCAN(arr, head, direction);
}
}
OUTPUT
c. LOOK
// Java program to demonstrate
// LOOK Disk Scheduling algorithm
import java.util.*;
class GFG{
static int size = 8;
static int disk_size = 200;
IU2041050116
BHAVYA SHAH
PAGE NO 91
OPERATING SYSTEM(CE0418)
public static void LOOK(int arr[], int head,
String direction)
{
int seek_count = 0;
int distance, cur_track;
Vector<Integer> left = new Vector<Integer>();
Vector<Integer> right = new Vector<Integer>();
Vector<Integer> seek_sequence = new Vector<Integer>();
// Appending values which are
// currently at left and right
// direction from the head.
for(int i = 0; i < size; i++)
{
if (arr[i] < head)
left.add(arr[i]);
if (arr[i] > head)
right.add(arr[i]);
}
// Sorting left and right vectors
// for servicing tracks in the
IU2041050116
BHAVYA SHAH
PAGE NO 92
OPERATING SYSTEM(CE0418)
// correct sequence.
Collections.sort(left);
Collections.sort(right);
// Run the while loop two times.
// one by one scanning right
// and left side of the head
int run = 2;
while (run-- > 0)
{
if (direction == "left")
{
for(int i = left.size() - 1;
i >= 0; i--)
{
cur_track = left.get(i);
// Appending current track to
// seek sequence
seek_sequence.add(cur_track);
// Calculate absolute distance
distance = Math.abs(cur_track - head);
IU2041050116
BHAVYA SHAH
PAGE NO 93
OPERATING SYSTEM(CE0418)
// Increase the total count
seek_count += distance;
// Accessed track is now the new head
head = cur_track;
}
// Reversing the direction
direction = "right";
}
else if (direction == "right")
{
for(int i = 0; i < right.size(); i++)
{
cur_track = right.get(i);
// Appending current track to
// seek sequence
seek_sequence.add(cur_track);
// Calculate absolute distance
distance = Math.abs(cur_track - head);
// Increase the total count
IU2041050116
BHAVYA SHAH
PAGE NO 94
OPERATING SYSTEM(CE0418)
seek_count += distance;
// Accessed track is now new head
head = cur_track;
}
// Reversing the direction
direction = "left";
}
}
System.out.println("Total number of seek " +
"operations = " + seek_count);
System.out.println("Seek Sequence is");
for(int i = 0; i < seek_sequence.size(); i++)
{
System.out.println(seek_sequence.get(i));
}
}
// Driver code
public static void main(String[] args) throws Exception
IU2041050116
BHAVYA SHAH
PAGE NO 95
OPERATING SYSTEM(CE0418)
{
// Request array
int arr[] = { 176, 79, 34, 60,
92, 11, 41, 114 };
int head = 50;
String direction = "right";
System.out.println("Initial position of head: " +
head);
LOOK(arr, head, direction);
}
}
Output
IU2041050116
BHAVYA SHAH
PAGE NO 96
OPERATING SYSTEM(CE0418)
IU2041050116
BHAVYA SHAH
PAGE NO 97
OPERATING SYSTEM(CE0418)
Practical: - 9
AIM: -Implement and Present the output of following Page Replacement
Algorithm.
A. FIFO
B. OPTIMAL
A. FIFO
// Java implementation of FIFO page replacement
// in Operating Systems.
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
class Test
{
// Method to find page faults using FIFO
static int pageFaults(int pages[], int n, int capacity)
{
// To represent set of current pages. We use
IU2041050116
BHAVYA SHAH
PAGE NO 98
OPERATING SYSTEM(CE0418)
// an unordered_set so that we quickly check
// if a page is present in set or not
HashSet<Integer> s = new HashSet<>(capacity);
// To store the pages in FIFO manner
Queue<Integer> indexes = new LinkedList<>() ;
// Start from initial page
int page_faults = 0;
for (int i=0; i<n; i++)
{
// Check if the set can hold more pages
if (s.size() < capacity)
{
// Insert it into set if not present
// already which represents page fault
if (!s.contains(pages[i]))
{
s.add(pages[i]);
IU2041050116
BHAVYA SHAH
PAGE NO 99
OPERATING SYSTEM(CE0418)
// increment page fault
page_faults++;
// Push the current page into the queue
indexes.add(pages[i]);
}
}
// If the set is full then need to perform FIFO
// i.e. remove the first page of the queue from
// set and queue both and insert the current page
else
{
// Check if current page is not already
// present in the set
if (!s.contains(pages[i]))
{
//Pop the first page from the queue
int val = indexes.peek();
IU2041050116
BHAVYA SHAH
PAGE NO 100
OPERATING SYSTEM(CE0418)
indexes.poll();
// Remove the indexes page
s.remove(val);
// insert the current page
s.add(pages[i]);
// push the current page into
// the queue
indexes.add(pages[i]);
// Increment page faults
page_faults++;
}
}
}
return page_faults;
}
IU2041050116
BHAVYA SHAH
PAGE NO 101
OPERATING SYSTEM(CE0418)
// Driver method
public static void main(String args[])
{
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4,
2, 3, 0, 3, 2};
int capacity = 4;
System.out.println(pageFaults(pages, pages.length, capacity));
}
}
Output
B. OPTIMAL
// CPP program to demonstrate optimal page
IU2041050116
BHAVYA SHAH
PAGE NO 102
OPERATING SYSTEM(CE0418)
// replacement algorithm.
#include <bits/stdc++.h>
using namespace std;
// Function to check whether a page exists
// in a frame or not
bool search(int key, vector<int>& fr)
{
for (int i = 0; i < fr.size(); i++)
if (fr[i] == key)
return true;
return false;
}
// Function to find the frame that will not be used
// recently in future after given index in pg[0..pn-1]
int predict(int pg[], vector<int>& fr, int pn, int index)
{
// Store the index of pages which are going
// to be used recently in future
IU2041050116
BHAVYA SHAH
PAGE NO 103
OPERATING SYSTEM(CE0418)
int res = -1, farthest = index;
for (int i = 0; i < fr.size(); i++) {
int j;
for (j = index; j < pn; j++) {
if (fr[i] == pg[j]) {
if (j > farthest) {
farthest = j;
res = i;
}
break;
}
}
// If a page is never referenced in future,
// return it.
if (j == pn)
return i;
}
// If all of the frames were not in future,
IU2041050116
BHAVYA SHAH
PAGE NO 104
OPERATING SYSTEM(CE0418)
// return any of them, we return 0. Otherwise
// we return res.
return (res == -1) ? 0 : res;
}
void optimalPage(int pg[], int pn, int fn)
{
// Create an array for given number of
// frames and initialize it as empty.
vector<int> fr;
// Traverse through page reference array
// and check for miss and hit.
int hit = 0;
for (int i = 0; i < pn; i++) {
// Page found in a frame : HIT
if (search(pg[i], fr)) {
hit++;
continue;
IU2041050116
BHAVYA SHAH
PAGE NO 105
OPERATING SYSTEM(CE0418)
}
// Page not found in a frame : MISS
// If there is space available in frames.
if (fr.size() < fn)
fr.push_back(pg[i]);
// Find the page to be replaced.
else {
int j = predict(pg, fr, pn, i + 1);
fr[j] = pg[i];
}
}
cout << "No. of hits = " << hit << endl;
cout << "No. of misses = " << pn - hit << endl;
}
// Driver Function
int main()
IU2041050116
BHAVYA SHAH
PAGE NO 106
OPERATING SYSTEM(CE0418)
{
int pg[] = { 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 };
int pn = sizeof(pg) / sizeof(pg[0]);
int fn = 4;
optimalPage(pg, pn, fn);
return 0;
}
OUtPUT
IU2041050116
BHAVYA SHAH
PAGE NO 107
OPERATING SYSTEM(CE0418)
Practical: - 10
AIM: -Implement and Present the output of following Page Replacement
Algorithm.
A. LRU
CODEimport java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
class Test
{
// Method to find page faults using indexes
static int pageFaults(int pages[], int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
HashSet<Integer> s = new HashSet<>(capacity);
IU2041050116
BHAVYA SHAH
PAGE NO 108
OPERATING SYSTEM(CE0418)
// To store least recently used indexes
// of pages.
HashMap<Integer, Integer> indexes = new HashMap<>();
// Start from initial page
int page_faults = 0;
for (int i=0; i<n; i++)
{
// Check if the set can hold more pages
if (s.size() < capacity)
{
// Insert it into set if not present
// already which represents page fault
if (!s.contains(pages[i]))
{
s.add(pages[i]);
// increment page fault
page_faults++;
}
// Store the recently used index of
// each page
indexes.put(pages[i], i);
IU2041050116
BHAVYA SHAH
PAGE NO 109
OPERATING SYSTEM(CE0418)
}
// If the set is full then need to perform lru
// i.e. remove the least recently used page
// and insert the current page
else
{
// Check if current page is not already
// present in the set
if (!s.contains(pages[i]))
{
// Find the least recently used pages
// that is present in the set
int lru = Integer.MAX_VALUE, val=Integer.MIN_VALUE;
Iterator<Integer> itr = s.iterator();
while (itr.hasNext()) {
int temp = itr.next();
if (indexes.get(temp) < lru)
{
IU2041050116
BHAVYA SHAH
PAGE NO 110
OPERATING SYSTEM(CE0418)
lru = indexes.get(temp);
val = temp;
}
}
// Remove the indexes page
s.remove(val);
//remove lru from hashmap
indexes.remove(val);
// insert the current page
s.add(pages[i]);
// Increment page faults
page_faults++;
}
// Update the current page index
indexes.put(pages[i], i);
}
}
return page_faults;
IU2041050116
BHAVYA SHAH
PAGE NO 111
OPERATING SYSTEM(CE0418)
}
// Driver method
public static void main(String args[])
{
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
int capacity = 4;
System.out.println(pageFaults(pages, pages.length, capacity));
}
}
OUTPUTPlease enter the number of Processes:
4
Please enter the Arrival Time for Process 1:
2
Please enter the Burst Time for Process 1:
3
Please enter the Arrival Time for Process 2:
4
IU2041050116
BHAVYA SHAH
PAGE NO 112
OPERATING SYSTEM(CE0418)
Please enter the Burst Time for Process 2:
5
Please enter the Arrival Time for Process 3:
6
Please enter the Burst Time for Process 3:
7
Please enter the Arrival Time for Process 4:
8
Please enter the Burst Time for Process 4:
9
0--P0--2--P1--5--P2--10--P3--17--P4--24
P WT TT
1 0ms 3ms
2 1ms 6ms
3 4ms 11ms
4 9ms 16ms
The Average WT is: 3.5ms
The Average TT is: 9.0ms
IU2041050116
BHAVYA SHAH
PAGE NO 113
OPERATING SYSTEM(CE0418)
IU2041050116
BHAVYA SHAH
PAGE NO 114
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
