Operating System Lab File (1)
Operating System Lab File (1)
Practical File
[BTCS404-18]
[Batch 2021-25]
0 1149
Downloaded by GULSHAN KUMAR
Index
4. Commands for files & directories: cd, ls, cp, md, rm, mkdir, rmdir.
Creating and viewing files using cat. File comparisons. Disk related
commands: checking disk free spaces. Processes in linux, connecting
processes with pipes, background processing, managing multiple
processes. Background process: changing process priority,
scheduling of processes at command, batch commands, kill, ps, who,
sleep. Printing commands, grep, fgrep, find, sort, cal, banner, touch,
file. File
related commands ws, sat, cut, grep.
5. Shell Programming: Basic of shell programming, various types of
shell, Shell Programming in bash, conditional & looping statement,
case statements, parameter passing and arguments, shell variables,
shell keywords, creating shell programs for automate system tasks,
report printing
6. Implementation of Bankers algorithm for the purpose of deadlock
avoidance
Steps:
1. Download the VIRTUAL BOX from original ORACLE VIRTUAL BOX
https://www.virtualbox.org/
Use the .iso file or ISO file that can be downloaded from the internet and start the
virtual box.
requirement.
Click on create option and then click on the START button to start the
virtual box and browse to the location of the .iso file of the OS.
install Ubuntu” in case you want to replace the existing OS otherwise select
NOTE: In case of any issue close and again start the virtual box.
choose from, most of them free to install! Linux is also the OS of choice for Server
Facebook, and Google use Linux for their Servers). It proves to be a good choice
for everyone.
10
#include <bits/stdc++.h>
using namespace std;
struct Process {
int pid;
int bt;
int art;
};
void findTurnAroundTime(Process proc[], int n, int wt[], int tat[])
{ for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}
void findWaitingTime(Process proc[], int n, int wt[]) {
int rt[n];
for (int i = 0; i < n; i++)
rt[i] = proc[i].bt;
int complete = 0, t = 0, minm = INT_MAX;
int shortest = 0, finish_time;
bool check = false;
11
12
13
14
15
16
Introduction
Virtual Box allows you to run an entire operating system inside another operating
system. Please be aware that you should have a minimum of 512 MB of RAM. 1
GB of RAM or more is recommended.
Comparison to Dual-Boot
Many websites (including the one you're reading) have tutorials on setting up dual-
boots between Windows and Ubuntu. A dual-boot allows you, at boot time, to
decide which operating system you want to use. Installing Ubuntu on a virtual
machine inside of Windows has a lot advantages over a dual-boot (but also a few
disadvantages).
In order to get any kind of decent performance, you need at least 512 MB
of RAM, because you are running an entire operating system (Ubuntu)
inside
17
Installation Process
Step 1: The first thing you have to do is to install oracle VirtualBox. Install it the
same way you would any normal Windows program. Then get Ubuntu disk image
(.iso file).
Step 2: After you launch VirtualBox from the Windows Start menu, click on New
to create a new virtual machine. When the New Virtual Machine Wizard appears,
click Next.
18
Step 4: VirtualBox will try to guess how much of your memory (or RAM) to
allocate for the virtual machine. If you have 1 GB or less of RAM, I would advise
you stick with the recommendation. If, however, you have over 1 GB, about a
quarter your RAM or less should be fine. For example, if you have 2 GB of RAM,
512 MB is fine to allocate. If you have 4 GB of RAM, 1 GB is fine to allocate. If
you have no idea what RAM is or how much of it you have, just go with the
default.
Click Next.
19
20
Step 8: Ubuntu's default installation is less than 3 GB. If you plan on adding
21
Step 9: Click Create and wait for the virtual hard drive to be created. This is
actually just a very large file that lives inside of your Windows installation.
22
23
24
25
26
AIM: Commands for files & directories: cd, ls, cp, md, rm, mkdir, rmdir. Creating
and viewing files using cat. File comparisons. Disk related commands: checking
disk free spaces. Processes in linux, connecting processes with pipes, background
processing, managing multiple processes. Background process: changing process
priority, scheduling of processes at command, batch commands, kill, ps, who,
sleep. Printing commands, grep, fgrep, find, sort, cal, banner, touch, file. File
related commands ws, sat, cut, grep.
COMMAND EXECUTION :
To begin with, the kernel displays a shell prompt after authorized login. The shell
waits for the input from the user, decodes the command line and searches for the
program.
Terminal
If the program is found, the shell retrieves and submits it to the kernel to executes
the program. The kernel delivers the output. If the command is not found, it signals
the kernel to display command not found at the terminal. The shell accepts the
kernels reply, and in both cases displays thee next prompt. This once the shell
encounters end of input, it instructs the kernel to log out the user, and displays the
login message again. The system directories where the command file programs are
stored are the /bin and /usr/bin. On receipt of the command, which is nothing but
27
CLASSIFICATION OF COMMANDS
User Access and User ID commands
Directory Commands
Editor Commands
File Manipulation commands
Security and Protection.
Inter-user and inter-machine communication commands
Information commands
Process Management commands
Program Development and Debugging commands
Text Processing commands
I/O Redirection and Piping
Shell commands and shell programming
Invoking command interpreters
System Administration commands for
Normal day-to-day administration
System monitoring
Installation and operation
Accounting
USER ACCESS AND USER ID COMMANDS
□ Login :
This command means to log in to the system. It is a process by which user
identifies himself to the system. The system prompts with a message : Login :
□ Logging off :
The new login message indicates that the session has been terminated thus making
it available for the next user. The exit command or ctrl+d means to logout the
system.
□ Password :
Set the user’s password or change the earlier login password. Password is a
sequence of letters and digits i.e. used to verify that user is allowed to use this user
id.
Syntax :- password:
DIRECTORY COMMANDS
Mkdir :
28
Find :
29
–Size +x[c] : selects the file, if size of file is greater then x blocks.
To create a file, it is similar to copy con of DOS. CAT is also used for
displaying the contents of file.Syntax: CAT filename
The problem with CAT is, it shows only 24 line (one screen full). If
your file has 50 lines, then it shows last 24 lines leaving first 16 lines. So we
can usepg/more here to see whole file.
Rm :
It sorts and merges files. It sort lines on the main files, together and
writes the result on the standard output. The sort command bases its
comparison on or more keys, which is part of the data in the file i.e. used
to make comparisons for the sorting process. It normally sorts the input
file according to the ASCII collating sequence spaces, punctuations,
numbers, uppercase and lowercase letters.
Syntax : $ sort [options] filename
E.g: $ sort a.lst
OPTIONS :
-t :overwrites the default deleter.
-d : sorts the file alphabetically.
-r : inverse order.
-n : used when sorting numeric fields.
-o : redirect the output to a new file
Chmod :
30
INFORMATION COMMANDS
Date :
31
It is use to get information about all users who are currently logged into
the system.
Syntax: Who
User name
Who am i :
It comprehends and displays the entire calendar of any year from one
A.D to 9999 A.D. Syntax: $ Cal
Cal command displays the current date as well as the calendar (month
proceeding current month itself and month following the current month)
Syntax: $ Cal <month><year>
This syntax displays that month’s calendar only.
FILTER COMMANDS
A filter is a device that reads the standard and writes to the standard
output in a particular format. The format of the output depends on the
properties of the filter.
GREP :
Grep is one of the most useful UNIX filters. It scans a file for the
occurrence of a pattern, and can display the selected pattern, the line numbers
32
It is use to comparing two files. It uses two file names as arguments and
displays the difference on the terminal. Syntax: $ Cmp file1 file2
The two files are compared byte-by-byte and the location of the first
mismatch is echoed to the string.
Switches:
–l: list option gives a detailed list of byte number and differing bytes in
octal for each character that differ in both the files.
Comm:
It is same as cmp. It displays file that consists and also tells which
time in one file have to be changed to make the two files identical. Syntax:
$ Diff file1 file2
Switches:
–e: generates a script which when applied to first file will convert to
second.
33
MISCELLANEOUS COMMANDS
Result:All Linux commands are implemented successfully.
Outcome: Students will learn Ln :
34
It is use to alter the access time, creation time and modification time.
Switches:
–a : access time
–c : creation time
–m : modification time
35
THEORY: Usually shells are interactive that mean, they accept command as input
from users and execute them. However some time we want to execute a bunch of
commands routinely, so we have type in all commands each time in terminal.
As shell can also take commands as input from file we can write these commands
in a file and can execute them in shell to avoid this repetitive work. These files are
called Shell Scripts or Shell Programs.
When you login to a Unix system, a program called a shell process is run for you.
A shell process is a command interpreter that provides you with an interface to the
operating system. A shell script is just a file of commands, normally executed by a
shell process that was spawned to run the script.
vi Editor in UNIX
The default editor that comes with the UNIX operating system is called vi (visual
editor). Using vi editor, we can edit an existing file or create a new file from
scratch. We can also use this editor to just read a text file.
Syntax:
Vi filename
Input:

Output:

Modes of Operation in vi editor There are three modes of operation in vi:

Command Mode: When vi starts up, it is in Command Mode. This mode is where
vi interprets any characters we type as commands and thus does not display them
in the window. This mode allows us to move through a file, and to delete, copy, or
paste a piece of text.
To enter into Command Mode from any other mode, it requires pressing
the [Esc] key. If we press [Esc] when we are already in Command Mode, then vi
will beep or flash the screen.
Insert mode: This mode enables you to insert text into the file. Everything that’s
typed in this mode is interpreted as input and finally, it is put in the file. The vi
always starts in command mode. To enter text, you must be in insert mode. To
36
Last Line Mode(Escape Mode): Line Mode is invoked by typing a colon [:],
while vi is in Command Mode. The cursor will jump to the last line of the screen
and vi will wait for a command. This mode enables you to perform tasks such as
saving files, executing commands.
Editing and inserting in Files(Entering and Replacing Text): To edit the file, we
need to be in the insert mode. There are many ways to enter insert mode from the
command mode.
I : Inserts text before current cursor location.
I : Inserts text at beginning of current line.
A : Inserts text after current cursor location.
A : Inserts text at end of current line.
O : Creates a new line for text entry below cursor location.
O : Creates a new line for text entry above cursor location.
R : Replace single character under the cursor with the next character typed.
R : Replaces text from the cursor to right.
S : Replaces single character under the cursor with any number of characters.
S :Replaces entire line.
Moving within a File(Navigation):
To move around within a file without affecting text must be in command mode
(press Esc twice). Here are some of the commands can be used to move around one
character at a time.
Commands and their Description
K : Moves the cursor up one line.
J : Moves the cursor down one line.
H : Moves the cursor to the left one character position.
L : Moves the cursor to the right one character position.
0 or | : Positions cursor at beginning of line.
$ : Positions cursor at end of line.
W : Positions cursor to the next word.
B : Positions cursor to previous word.
( : Positions cursor to beginning of current sentence.
) : Positions cursor to beginning of next sentence.
H : Move to top of screen.
nH : Moves to nth line from the top of the screen.
M : Move to middle of screen.
37
SHELL SCRIPTS
When a group of command have to be executed regularly, they should be stored in
a file, and the file itself executed as a shell script or shell program. Though it’s not
mandatory, we normally use the .sh extension for shell scripts. Shell scripts are
executed in a separate child shell process, and this sub-shell need not be the same
as your login shell. By default, the child and parent shells belongs to the same type,
but you can provide a special interpreter line in the first line of the script to specify
a different shell for your script. Use your vi editor to create the shell script,
script.sh.
EDITOR COMMANDS
Editors :
In an operating system, a editor is assumed authority which helps to create
and modify users files in data. Any ASCII text files such as a program or a
38
Vi - Screen editor
C Ed :
COMMAND FUNCTION
I Inserts text to left of cursors.
A Appends text to right of cursor.
I Inserts text at beginning of line.
39
COMMAND ACTION
:w Saves file and remains in editing mode.
:x Saves file and quits editing mode.
:wq As above.
:w n2w.p1 Like Save As …. In Microsoft windows.
:w! n2w.p1 As above, but overwrites existing file.
:q Quits editing mode when no changes are made to file.
:q! Quits editing mode but after abandoning changes.
:n1,n2w
Write lines n1 to n2 to file build.sql.
build.sql
:.w build.sql Write current line to file build.sql.
:$w build.sql Write last line to file build.sql.
:sh Escapes to UNIX shell.
:recover Recovers file from a crash.
NAVIGATION:
In this we consider the command mode. This is the mode
you come to when you have finished entering or changing your text. A
Command Mode command doesn’t show up on screen but simply performs a
function. We begin with navigation. Don’t forget to avoid the cursors control
keys for navigation.
40
EDITING TEXT :
The editing facilities in vi are very elaborate and involve the use of
operators.
Keys for Editing Text :
KEY FUNCTION
D Delete
Y Yank (Copy)
X Deletes a single character
dd Removes the entire line.
P/p Put the text at the new location with p or P.
yy Yanks (copy) current line
Removes the new-line character between two lines to
J
pull up line below it
Result: Basic introduction about Shell Programming and Shell script is given
Outcome: Students will learn about the basic introduction of Shell Programming
41
Objectives:To create a script to enter a string and then print its manipulation
table.
$ cat table.sh
echo "Enter a number for which the multiplication table is to be written "
read number1
read number2
count=1
do
count=`expr $count + 1`
done
OUTPUT :
42
10
1*2=2
2*2=4
3*2=6
4*2=8
5 * 2 = 10
6 * 2 = 12
7 * 2 = 14
8 * 2 = 16
9 * 2 = 18
10 * 2 = 20
$ cat factorial.sh
43
read num
temp=`expr $num - 1`
do
temp=`expr $temp - 1`
done
OUTPUT:
$ shfactorial.sh
44
$ cat prime.sh
read num
i=2
do
then
break
fi
i=`expr $i + 1`
done
if [ $i -eq $num ]
then
45
fi
OUTPUT :
$ shprime.sh
Enter no:
$ shprime.sh
Enter no:
7 is a prime number
Outcome: Students will learn the creation of script to determine whether a given
number is prime or not.
46
$ cat odd.sh
read n
x=`expr $n % 2`
if [ $x -eq 0 ]
then
echo "EVEN"
else
echo "ODD"
fi
OUTPUT :
$ shodd.sh
Enter a no.:
EVEN
47
Enter a no.:
15
ODD
Result:Script is created to find out whether the number entered is odd or even
Outcome: Students will learn the creation of script to find out whether the number
entered is odd or even.
Objectives: To Create a shell script ‘addphone’ that asks a user for a name and
then a
# phone Number. The name and phone number will be put in a file
# ‘phonebook’. You should be able to run ‘addphone’ several times without
# destroying the previous contents of ‘phonebook’.
# cat addphone.sh
c=1
while [ $c –eq 1 ]
do
read name
48
read c
done
OUTPUT:
$ shaddphone.sh
$cat phonebook
Deepak
3957417897
Rahul
49
Rohit
9872821444
Result: Script is created ‘add phone’ that asks a user for a name and then a phone
Number
Outcome: Students will learn the creation of ‘add phone’ that asks a user for a name
and then a phone Number
50
Available:
It is a 1-d array of size ‘m’ indicating the number of available resources of each
type.
Available[ j ] = k means there are ‘k’ instances of resource type Rj
Max :
It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in
a system.
Max[ I, j ] = k means process Pi may request at most ‘k’ instances of resource type
Rj.
Allocation :
It is a 2-d array of size ‘n*m’ that defines the number of resources of each type
currently allocated to each process.
Allocation[ I, j ] = k means process Pi is currently allocated ‘k’ instances of
resource type Rj
Need :
It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each
process.
Need [ I, j ] = k means process Pi currently need ‘k’ instances of resource type Rj
For its execution.
Need [ I, j ] = Max [ I, j ] – Allocation [ I, j ]
51
Algorithm:
Banker’s algorithm consists of Safety algorithm and Resource request algorithm
Safety Algorithm
The algorithm for finding out whether or not a system is in a safe state can be
described as follows:
1) Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an I such that both
a) Finish[i] = false
b) Needi <= Work
if no such I exists goto step (4)
3)Work = Work + Allocation[i]
Finish[i] = true
Goto step (2)
4) Finish [i] = true for all i
Then the system is in a safe state
Resource-Request Algorithm
Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi
wants k instances of resource type Rj. When a request for resources is made by
process Pi, the following actions are taken:
1) If Requesti <= Needi
Goto step (2) ; otherwise, raise an error condition, since the process has exceeded
its maximum claim.
2) If Requesti <= Available
Goto step (3); otherwise, Pi must wait, since the resources are not available.
3) Have the system pretend to have allocated the requested resources to process
Pi by modifying the state as
Follows:
Available = Available – Requesti
Allocationi = Allocationi + Requesti
52
Program:
#include <stdio.h>
Int main()
{
// P0, P1, P2, P3, P4 are the Process names here
Int n, m, I, j, k;
N = 5; // Number of processes
M = 3; // Number of resources
Int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4
Int max[5][3] = { { 7, 5, 3 }, // P0 // MAX Matrix
{ 3, 2, 2 }, // P1
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4
Int avail[3] = { 3, 3, 2 }; // Available Resources
Int f[n], ans[n], ind = 0;
For (k = 0; k < n; k++) {
F[k] = 0;
}
Int need[n][m];
For (I = 0; I < n; i++) {
For (j = 0; j < m; j++)
Need[i][j] = max[i][j] – alloc[i][j];
}
Int y = 0;
For (k = 0; k < 5; k++)
{ For (I = 0; I < n; i++)
{
If (f[i] == 0) {
Int flag = 0;
For (j = 0; j < m; j++) {
If (need[i][j] > avail[j])
{ Flag = 1;
Break;
53
54