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

OS Lab Manual

Uploaded by

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

OS Lab Manual

Uploaded by

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

NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S

NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------

ACAD-DI-86 Academic Year: 2023-24


Rev : 01 Lab Manual
Semester: II
Date: 02.04.2021

Subject: Operating Systems Lab (BTCOL406)

List of Experiments

Exp. No. Name of Experiments

01 Hands on Unix Commands.


02 Shell Script programming using the commands grep, awk, and sed.
03 Implementation of various CPU scheduling algorithms (FCFS, SJF).

04 Concurrent programming; use of threads and processes, system calls (fork and v-
fork).
Study Pthreads and implement the following: write a program which shows a
05
performance.

06 Implementation of Producer-Consumer problem.

07 Implementation of various page replacement algorithms (FIFO, LRU).

Implementation of various memory allocation algorithms, (First fit, Best fit and
08
Worst fit).

09 Implementation of Bankers algorithm.

10 Scheduling algorithms (FCFS, SCAN, SSTF, C-SCAN).

Operating Systems Lab (BTCL406) 1


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------

Experiment No: 01
Aim: Hands on Unix Commands.

Objectives: To study and implement various UNIX Commands.

Theory:

UNIX:
It is a multi-user operating system. Developed at AT & T Bell Industries, USA in 1969.
Ken Thomson along with Dennis Ritchie developed it from MULTICS (Multiplexed Information
and Computing Service) OS. By1980, UNIX had been completely rewritten using C language.

LINUX:
It is similar to UNIX, which is created by Linus Torualds. All UNIX commands works in
Linux. Linux is open source software. The main feature of Linux is coexisting with other OS
such as windows and UNIX.

STRUCTURE OF A LINUX SYSTEM:


It consists of three parts.
a) UNIX kernel
b) Shells
c) Tools and Applications
UNIX KERNEL:
Kernel is the core of the UNIX OS. It controls all tasks, schedule all Processes and carries
out all the functions of OS. Decides when one programs tops and another starts.
SHELL:
Shell is the command interpreter in the UNIX OS. It accepts command from the user and
analyses and interprets them.

CONTENT:

1) gedit: Use to open a text editor to create the C source code.

gedit

Operating Systems Lab (BTCL406) 2


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------

Ex. gedit

Enter the C source code below:

int main()
{
int A, B, sum = 0;

printf("Enter two numbers A and B: \n");

scanf("%d%d", &A, &B);

// Calculate the addition of A and B


// using '+' operator
sum = A + B;

// Print the sum


printf("Sum of A and B is: %d", sum);

return 0;
}

Save the program and close the editor window.

1) gcc -o filename filename.c : Use to compile a C program.

Ex.
gcc -o sum sum.c

This command will invoke the GNU C compiler to compile the file sum.c and output (-o)
the result to an executable called sum.

2) ./filename : Use to run a C program:


Ex. ./sum

This should result in the output

Enter two numbers A and B:


45

Sum of A and B is: 9

Operating Systems Lab (BTCL406) 3


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------

3) date: used to check the date and time


Syntax: $date [+format]

Ex. $date
Output:
Wednesday 29 March 2023 02:02:06 PM IST

List of Format specifiers used with date command:

%D: Display date as mm/dd/yy.


%d: Display the day of the month (01 to 31).
%a: Displays the abbreviated name for weekdays (Sun to Sat).
%A: Displays full weekdays (Sunday to Saturday).
%h: Displays abbreviated month name (Jan to Dec).
%b: Displays abbreviated month name (Jan to Dec).
%B: Displays full month name(January to December).
%m: Displays the month of year (01 to 12).
%y: Displays last two digits of the year(00 to 99).
%Y: Display four-digit year.
%T: Display the time in 24 hour format as HH:MM:SS.
%H: Display the hour.
%M: Display the minute.
%S: Display the seconds.

Operating Systems Lab (BTCL406) 4


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------

5) cal: used to display the calendar.


Syntax:$cal

6) echo : used to print the message on the screen.


Operating Systems Lab (BTCL406) 5
NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------
Syntax:$echo “text”

7) ls : used to list the files.


Syntax:$ls

8) who & whoami –it displays data about all users who have logged into the system currently.
The next command displays about current user only.
Syntax:$who
student tty2 2023-03-29 13:54 (tty2)

$whoami
Student

9) uptime: tells you how long the computer has been running since its last reboot or power-off.
Syntax:$uptime
14:36:39 up 43 min, 1 user, load average: 0.71, 0.66, 0.63

10) uname: it displays the system information such as hardware platform, system name and
processor, OS type.
Syntax:$uname
Linux

11) hostname –displays and set system host name


Syn:$ hostname

Operating Systems Lab (BTCL406) 6


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------

12) bc –stands for “best calculator”


Syntax:$bc
10/2*3
15
Quit

13) $cat>filename : This command is used to create a file.


Syntax: $cat>filename

14) $cat filename : This command is used to view a file


Syntax:$cat filename

15) $cat>>filename : this command is used to add text to an existing file:


Syntax:$cat>>filename

16) Concatenate:
Operating Systems Lab (BTCL406) 7
NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------
Syn:$cat file1 file2>file3

17) rm–deletes a file from the file system


Syn:$rm filename

18) cp–copies the files or directories


Syn:$cp source file destination file

Operating Systems Lab (BTCL406) 8


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------

19) mv–to rename the file or directory


syn:$mv old file new file
Eg:$mv abc xyz

20) head–displays10 lines from the head(top) of a given file


Syn: $head filename
Eg: $head concat

To display the top two lines:


Syn: $head -2 concat

21) tail–displays last 10 lines of the file


Syn: $tail filename
Operating Systems Lab (BTCL406) 9
NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------
Eg: $tail -2 concat

22) wc–it counts the number of lines, words, character in a specified file(s)
$ wc state.txt
5 7 58 state.txt

Operating Systems Lab (BTCL406) 10


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------
Experiment No: 02
Aim: Shell Script programming using the commands grep, awk, and sed.

Objectives: To study and implement various Shell Script programming UNIX Commands like
grep, awk, sed.

Theory:

Grep, sed, and AWK are all standard Linux tools that are able to process text. Each of these tools
can read text files line-by-line and use regular expressions to perform operations on specific parts
of the file. Grep is used for finding text patterns in a file and is the simplest of the three. Sed can
find and modify data, however, its syntax is a bit more complex than grep. AWK is a full-fledged
programming language that can process text and perform comparison and arithmetic operations
on the extracted text.

grep:
grep is a Linux utility used to find lines of text in files or input streams using regular expressions.
It’s name is short for Global Regular Expression Pattern

Beginning at the first line in the file, grep copies a line into a buffer, compares it against the
search string, and if the comparison passes, prints the line to the screen. Grep will repeat this
process until the Example data file.

Here is the basic syntax of the grep command:

grep [options] pattern [file...]

Ex. Consider a text file ‘good’

goodness

goodlier

goodwills

goodwives

goodies

goodbyes

wellness

Operating Systems Lab (BTCL406) 11


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------
morning

sun

1) “pattern” : To print any line from a file that contains a specific pattern of characters.

$grep “good” good

2) –n : When grep prints results with many matches, it comes handy to see the line numbers.
$grep –n “good” good

3) –vn : To print any line from a file that doesn’t contain a specific pattern of characters.
$grep –vn “good” good

4) –i : ignores the case of the text string.


$grep –i “good” good

5) –c : To print a count of matching lines to standard output.


$grep –c “good” good

Operating Systems Lab (BTCL406) 12


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------

awk:
Awk is a scripting language used for manipulating data and generating reports. The awk
command programming language requires no compiling and allows the user to use variables,
numeric functions, string functions, and logical operators.
Awk is a utility that enables a programmer to write tiny but effective programs in the form of
statements that define text patterns that are to be searched for in each line of a document and the
action that is to be taken when a match is found within a line. Awk is mostly used for pattern
scanning and processing. It searches one or more files to see if they contain lines that matches
with the specified patterns and then perform the associated actions.
Awk is abbreviated from the names of the developers – Aho, Weinberger, and Kernighan.
awk 'pattern { action }' file
The awk command in Ubuntu (and other Unix-like operating systems) is a versatile text-
processing tool used for pattern scanning and processing. It operates on a per-line basis, allowing
users to specify patterns and actions to perform on lines that match those patterns. awk derives
its name from the initials of its designers: Alfred Aho, Peter Weinberger, and Brian Kernighan.
Here is the basic syntax of the awk command:
awk 'pattern { action }' file

● pattern: This is a condition or a regular expression that is tested against each line of
input. If the pattern matches, the associated action is executed.

Operating Systems Lab (BTCL406) 13


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------
● action: This is the action to be performed when the pattern matches. It can be any valid
AWK script.
● file: This is the input file that awk will process. If not provided, awk reads from standard
input.
Consider the following text file as the input file for all cases below:
$cat > person.txt
ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000
1. Default behavior of Awk: By default Awk prints every line of data from the specified file.
$ awk '{print}' person.txt

2. Print the lines which match the given pattern.


$ awk '/account/ {print}' person.txt

Operating Systems Lab (BTCL406) 14


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------

3. Splitting a Line Into Fields : For each record i.e line, the awk command splits the record
delimited by whitespace character by default and stores it in the $n variables. If the line has 4
words, it will be stored in $1, $2, $3 and $4 respectively. Also, $0 represents the whole line.
$ awk '{print $1,$4}' person.txt

4. Use of NR built-in variables (Display Line Number)


$ awk '{print NR,$0}' person.txt

Operating Systems Lab (BTCL406) 15


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------
$ awk '{print NR,$1}' person.txt

5. Another use of NR built-in variables (Display Line From 3 to 6)

6 . Use of NF built-in variables (Display Last Field)


$ awk '{print $1,$NF}' person.txt

Operating Systems Lab (BTCL406) 16


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------

7. To find the length of the longest line present in the file:


$ awk '{ if (length($0) > max) max = length($0) } END { print max }' person.txt

8. Print the number of lines in a file:


$ awk 'END {print NR}' person.txt

9. To print the squares of first numbers from 1 to n say 6:


$ awk 'BEGIN { for(i=1;i<=6;i++) print "square of", i, "is",i*i; }'

Operating Systems Lab (BTCL406) 17


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------

sed:
SED command in UNIX stands for stream editor and it can perform lots of functions on file like
searching, find and replace, insertion or deletion. Though most common use of SED command in
UNIX is for substitution or for find and replace. By using SED you can edit files even without
opening them, which is much quicker way to find and replace something in file, than first
opening that file in VI Editor and then changing it.

● SED is a powerful text stream editor. Can do insertion, deletion, search and
replace(substitution).
● SED command in unix supports regular expression which allows it perform complex pattern
matching.
Here is the basic syntax of the sed command:
sed options... [script] [inputfile...]

Consider the below text file as an input.


$cat > geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

1. Replacing or substituting string : Sed command is mostly used to replace the text in a file.
The below simple sed command replaces the word “unix” with “linux” in the file.
$sed 's/unix/linux/' unix

By default, the sed command replaces the first occurrence of the pattern in each line and it won’t
replace the second, third…occurrence in the line.
Operating Systems Lab (BTCL406) 18
NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------
2. Replacing the nth occurrence of a pattern in a line : Use the /1, /2 etc flags to replace the
first, second occurrence of a pattern in a line. The below command replaces the second
occurrence of the word “unix” with “linux” in a line.

$sed 's/unix/linux/2' unix

3. Replacing all the occurrence of the pattern in a line : The substitute flag /g (global
replacement) specifies the sed command to replace all the occurrences of the string in the line.

$sed 's/unix/linux/g' unix

4. Parenthesize first character of each word : This sed example prints the first character of
every word in parenthesis.
$ echo "Welcome to Unix Operating System" | sed 's/\(\b[A-Z]\)/\(\1\)/g'

Operating Systems Lab (BTCL406) 19


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------

5. Duplicating the replaced line with /p flag : The /p print flag prints the replaced line twice on
the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that
line only once.
$sed 's/unix/linux/p' unix

6. Deleting lines from a particular file : SED command can also be used for deleting lines from
a particular file. SED command is used for performing deletion operation without even opening
the file
Examples:
To Delete a particular line say n in this example
Syntax:
$ sed 'nd' filename.txt
$ sed '2d' filename.txt

Operating Systems Lab (BTCL406) 20


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING - ARTIFICIAL INTELLIGENCE

---------------------------------------------------------------------------------------------------------------------

Operating Systems Lab (BTCL406) 21

You might also like