OS Lab Manual
OS Lab Manual
---------------------------------------------------------------------------------------------------------------------
List of Experiments
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.
Implementation of various memory allocation algorithms, (First fit, Best fit and
08
Worst fit).
---------------------------------------------------------------------------------------------------------------------
Experiment No: 01
Aim: Hands on 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.
CONTENT:
gedit
---------------------------------------------------------------------------------------------------------------------
Ex. gedit
int main()
{
int A, B, sum = 0;
return 0;
}
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.
---------------------------------------------------------------------------------------------------------------------
Ex. $date
Output:
Wednesday 29 March 2023 02:02:06 PM IST
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
Syntax:$echo “text”
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
---------------------------------------------------------------------------------------------------------------------
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
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
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
---------------------------------------------------------------------------------------------------------------------
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.
goodness
goodlier
goodwills
goodwives
goodies
goodbyes
wellness
---------------------------------------------------------------------------------------------------------------------
morning
sun
1) “pattern” : To print any line from a file that contains a specific pattern of characters.
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
---------------------------------------------------------------------------------------------------------------------
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.
---------------------------------------------------------------------------------------------------------------------
● 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
---------------------------------------------------------------------------------------------------------------------
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
---------------------------------------------------------------------------------------------------------------------
$ awk '{print NR,$1}' person.txt
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
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...]
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.
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.
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'
---------------------------------------------------------------------------------------------------------------------
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
---------------------------------------------------------------------------------------------------------------------