OS Manual
OS Manual
OPERATING SYSTEM
(203105204)
IV SEMESTER
Computer Science & Engineering
Department
LABORATORY MANUAL
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
CERTIFICATE
Head Of Department:...........................................
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
TABLE OF CONTENT
Page No
Sr. Date of Date of Marks
Experiment Title Sign
No Start Completion (out of 10)
From To
1. 1 Study of basic commands of 1 7
Linux
2. Study the basics of shell 8 12
programming
3. Write a Shell Script to print 13 13
given numbers sum of all digits.
Write a shell script to validate
4. the entered date.(eg, Date 14 14
format is : dd-mm-yyyy)
Write a shell script to check
5. entered string is palindrome or 15 15
not.
EXPERIMENT – 1
AIM : Study of basic commands of linux
Shell Script : Allows the user to execute commands by typing them manually at a terminal,
or automatically inprograms called shell scripts.
A shell is not an operating system. It is a way to interface with the operating system and run
commands.
DESCRIPTION:
SYNTAX:
Pwd
OUTPUT:
3) cd ..
DESCRIPTION:
Move up one directory.
Page
1
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
SYNTAX:
cd ..
OUTPUT:
5) cat
DESCRIPTION:
cat stands for "catenate". It reads data from files, and outputs their contents. It is the
simplest way to display the contents of a file at the command line.
SYNTAX:
cat filename
OUTPUT:
Page
2
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
6) head
DESCRIPTION:
head, by default, prints the first 10 lines of each FILE to standard output. With more
than one FILE, it precedes each set of output with a header identifying the file name.
If no FILE is specified, or when FILE is specified as a dash ("-"), head reads from
standard input.
SYNTAX:
head [option]...[file/directory]
OUTPUT:
7) Tail
DESCRIPTION:
tail is a command which prints the last few number of lines (10 lines by default) of a
certain file, then terminates.
SYNTAX:
tail [option]...[file/directory]
OUTPUT:
Page
3
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
Page
4
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
OUTPUT:
12) gedit
DESCRIPTION:
The gedit command is used to create and open a file.
SYNTAX:
gedit filename.txt
OUTPUT:
13) man
DESCRIPTION:
Displays on online manual page or manpage.
SYNTAX:
man command
OUTPUT:
14) echo
DESCRIPTION:
Display text on the screen.
SYNTAX:
echo yourtext
OUTPUT:
Page
5
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
15) clear
DESCRIPTION:
Used to clear the screen
SYNTAX:
Clear
OUTPUT:
16) whoami
DESCRIPTION:
whoami prints the effective user ID. This command prints the username associated
with the current effective user ID.
SYNTAX:
whoami [option]
OUTPUT:
17) wc
DESCRIPTION:
wc (word count) command, can return the number of lines, words, and characters in a
file.
SYNTAX:
wc [option]... [file]...
OUTPUT:
18) grep
DESCRIPTION:
grep command uses a search term to look through a file.
Page
6
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
SYNTAX:
grep [option]... Pattern [file]...
OUTPUT:
19) free
DESCRIPTION:
Display RAM details in Linux machine.
SYNTAX:
Free
OUTPUT:
20) pipe ( | )
DESCRIPTION:
Pipe command is used to send output of one program as a input to another. Pipes “|”
help combine 2 or more commands.
SYNTAX:
Command 1 | command 2
OUTPUT:
Page
7
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
EXPERIMENT -2
AIM : Study the basics of shell programming.
What is a Shell?
An Operating is made of many components, but its two prime components are -
• Kernel
• Shell
A Kernel is at the nucleus of a computer. It makes the communication between the hardware
and
software possible. While the Kernel is the innermost part of an operating system, a shell is the
outermost one.
A shell in a Linux operating system takes input from you in the form of commands, processes
it, and
then gives an output. It is the interface through which a user works on the programs,
commands, and
scripts. A shell is accessed by a terminal which runs it.
When you run the terminal, the Shell issues a command prompt (usually $), where you can
type
your input, which is then executed when you hit the Enter key. The output or the result is
thereafter
displayed on the terminal.
Page
8
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
The Shell wraps around the delicate interior of an Operating system protecting it from
accidental
damage. Hence the name Shell.
Types of Shell
There are two main shells in Linux:
1. The Bourne Shell: The prompt for this shell is $ and its derivatives are listed below:
2. The C shell: The prompt for this shell is %, and its subcategories are:
Shell scripting is writing a series of command for the shell to execute. It can combine lengthy
and repetitive sequences of commands into a single and simple script, which can be stored
and executed anytime. This reduces the effort required by the end user.
1. Create a file using a vi editor(or any other editor). Name script file with
extension .sh
"#!" is an operator called shebang which directs the script to the interpreter location. So, if
we use"#! /bin/sh" the script gets directed to the bourne-shell.
#!/bin/sh
ls
Page
9
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
Page
10
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
variables are used to store information and they can by the shell only.
For example, the following creates a shell variable and then prints it:
variable ="Hello"
echo $variable
Below is a small script which will use a variable.
#!/bin/sh
echo "what is your name?"
read name
echo "How do you do, $name?"
read remark
echo "I am $remark too!"
Let's understand, the steps to create and execute the script
As you see, the program picked the value of the variable 'name' as Joy and 'remark' as
excellent.
Page
11
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
This is a simple script. You can develop advanced scripts which contain conditional
statements,
loops, and functions. Shell scripting will make your life easy and Linux administration a
breeze.
Summary:
• Kernel is the nucleus of the operating systems, and it communicates between hardware and
software
• Shell is a program which interprets user commands through CLI like Terminal
• The Bourne shell and the C shell are the most used shells in Linux
• Shell scripting is writing a series of command for the shell to execute
• Shell variables store the value of a string or a number for the shell to read
• Shell scripting can help you create complex programs containing conditional statements,
loops, and functions
Page
12
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
EXPERIMENT-3
AIM: Write a Shell Script to print given numbers sum of all digits.
CODE:
# !/bin/bash
echo "Enter a number"
read n
len='expr "$n" : '.*''
for i in $len
do
a=$(( $n % 10 ))
n=$(( $n/ 10 ))
s=$(( $s + $a ))
done
echo "summation is " $s
OUTPUT:
Page
13
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
EXPERIMENT-4
AIM: Write a Shell Script to validate the entered date (eg. Date format is: dd-mm-
yyyy).
CODE :
echo "Enter the date: "
read a
echo "Enter the month: "
read b
echo "Enter the year: "
read c
if(($a>=1 && $a<=31 && $b>=1 && $b<=12 && $c>=1 && $c<=2022))
then
echo "It is a valid date"
else
echo "It's not a valid date"
fi
OUTPUT:
Page
14
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
EXPERIMENT-5
AIM: Write a shell script to check entered string is palindrome or not.
CODE :
echo "Enter a string"
read str
str1=`echo $str|rev`
if [ $str == $str1 ]
then
echo "Palindrome"
else
echo "not palindrome"
fi
OUTPUT:
Page
15
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
EXPERIMENT-6
AIM: Write a Shell script to say Good morning/Afternoon/Evening as you log in to
system.
CODE:
h=$(date +"%H")
if [ $h -gt 6 -a $h -le 12 ]
then
echo good morning
elif [ $h -gt 12 -a $h -le 16 ]
then
echo good afternoon
elif [ $h -gt 16 -a $h -le 20 ]
then
echo good evening
else
echo good night
fi
OUTPUT:
Page
16
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
EXPERIMENT-7
AIM: Write a C program to create a child process.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(void){
int pid;
int status;
printf("Hello World");
pid = fork( );
if(pid == -1){
perror("bad fork");
exit(1);
}
if(pid == 0){
printf("I am the child process= %d\n",getpid());
}
else{
printf("I am the parent process= %d\n",getppid());
}
OUTPUT:
Page
17
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
EXPERIMENT-8
AIM: Finding out biggest number from given three numbers supplied as command line
arguments.
CODE:
if [ "$#" = 0 ]
then
echo "No arguments passed."
exit 1
fi
maxEle=$1
for arg in "$@"
do
if [ "$arg" -gt "$maxEle" ]
then
maxEle=$arg
fi
done
echo "Largest value among the arguments passed is: $maxEle"
OUTPUT:
Page
18
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
EXPERIMENT-9
AIM: Printing the patterns using for loop.
CODE (for left skewed star pattern):
echo "Enter the number "
read n
i=1
j=1
for ((i=1;i<=n;i++))
do
for ((j=1;j<=i;j++))
do
echo -n "* "
done
echo
done
OUTPUT:
Page
19
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
do
for((j=1;j<=n;j++))
do
if (($j+$i >= ($n+1)))
then
echo -n "*"
else
echo -n " "
fi
done
echo
done
OUTPUT:
Page
20
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
EXPERIMENT-10
AIM: Shell script to determine whether given file exist or not.
Description : In this code name of whichever file is written in if condition should be present
in your ubuntu system otherwise it will print file doesn’t exist.
CODE:
if [ -f "loop.sh" ]
then
echo "File exist"
else
echo "File don't exist"
fi
OUTPUT:
Page
21
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
EXPERIMENT-11
AIM: Implementation of FCFS Algorithm.
CODE (FCFS):
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, s = 0, p = 0;
cout << "Enter the number of processes: ";
cin >> n;
int at[n], bt[n], ct[n], tat[n], wt[n];
for (int i = 0; i < n; i++) {
cout << "Enter the arrival time of process " << i + 1 << endl;
cin >> at[i];
}
for (int i = 0; i < n; i++){
cout << "Enter the burst time of process " << i + 1 << endl;
cin >> bt[i];
}
int temp, temp1;
for (int i = 0; i < n - 1; i++){
for (int j = 0; j < n - 1 - i; j++){
if (at[j] > at[j + 1]) {
temp = at[j];
at[j] = at[j + 1];
at[j + 1] = temp;
temp1 = bt[j];
bt[j] = bt[j + 1];
Page
22
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
bt[j + 1] = temp1;
}
}
}
for (int i = 1; i < n; i++){
ct[0] = bt[0];
ct[i]=ct[i-1] + bt[i];
}
for (int i = 0; i < n; i++){
tat[i] = ct[i] - at[i];
}
for (int i = 0; i < n; i++){
wt[i] = tat[i] - bt[i];
}
for (int i = 0; i < n; i++){
s += tat[i];
}
for (int i = 0; i < n; i++){
p += wt[i];
}
cout << "completion time" << endl;
for (int i = 0; i < n; i++){
cout << ct[i] << endl;
}
cout << "turn around time" << endl;
for (int i = 0; i < n; i++){
cout << tat[i] << endl;
}
cout << "Waiting time" << endl;
Page
23
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
Page
24
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
EXPERIMENT-12
AIM: Implementation of Round Robin Algorithm.
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, s = 0, p = 0, tq, max = 0, m, stack = 0;
cout << "Enter the number of processes: ";
cin >> n;
int at[n], bt[n], ct[n], tat[n], wt[n], tempbt[n];
for (int i = 0; i < n; i++){
cout << "Enter the arrival time of process " << i + 1 << endl;
cin >> at[i];
}
for (int i = 0; i < n; i++){
cout << "Enter the burst time of process " << i + 1 << endl;
cin >> bt[i];
tempbt[i] = bt[i];
if (bt[i] > max) {
max = bt[i];
}
}
printf("Enter the time quantum\n");
scanf("%d", &tq);
int temp, temp1;
for (int i = 0; i < n - 1; i++){
for (int j = 0; j < n - 1 - i; j++){
if (at[j] > at[j + 1]) {
Page
25
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
temp = at[j];
at[j] = at[j + 1];
at[j + 1] = temp;
temp1 = bt[j];
bt[j] = bt[j + 1];
bt[j + 1] = temp1;
}
}
}
int i = 0;
m = (max / tq) + 1;
while (m > 0) {
if (bt[i] > 0) {
if ((bt[i] - tq) <= 0) {
if (i == 0) {
ct[i] = stack + bt[i];
stack = ct[i];
bt[i] = 0;
if (i == n - 1) {
i = 0;
m--;
}
i++;
continue;
}
else{
ct[i] = stack + bt[i];
stack = ct[i];
bt[i] = 0;
Page
26
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
if (i == n – 1){
i = -1;
m--;
}
i++;
continue;
}
}
else {
ct[i] = stack + tq;
stack = ct[i];
bt[i] = bt[i] - tq;
if (i == n - 1) {
i = -1;
m--;
}
i++;
continue;
}
}
else {
if (i == n - 1)
{
i = -1;
m--;
}
i++;
}
}
Page
27
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
Page
28
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
OUTPUT:
Page
29
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
EXPERIMENT-13
AIM: Implementation of Banker’s Algorithm.
CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10];
int p, r, i, j, process, count;
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
Page
30
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
OPERATING SYSTEM (203105204) B. Tech. 2nd YEAR
ENROLLMENT NO: 200303105047
}
printf("\n\nEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
}
OUTPUT:
Page
31