Professional Documents
Culture Documents
PRACTICAL NO: 3
.Aim- Shell Script programming using the commands grep, awk, and sed.
Theory-
grep stands for global regular expression print. It is a family of programs that is used
to search the input file for all lines that match a specified regular expression and
write them to the standard output file (monitor).
Operation
To write scripts that operate correctly, we must understand how the greputilities
work. For each line in the standard input (input file or keyboard), grep performs the
following operations:
1. Copies the next input line into the pattern space. The pattern space is a buffer
that can hold only one text line.
2. Applies the regular expression to the pattern space.
3. If there is a match, the line is copied from the pattern space to the standard
output. The grep utilities repeat these three operations on each line in the
input.
Grep Command
The grep filter searches a file for a particular pattern of characters, and
displays all lines that contain that pattern. The pattern that is searched in the
file is referred to as the regular expression (grep stands for globally search for
regular expression and print out).
Syntax:
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha
grep [options] pattern [files]
Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-A n : Prints searched line and nlines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.
Program-
Conclusion
in this article, we started off with a basic introduction to grep, sed, and awk. Then,
we showed the usage of grep on simple text scanning and matching. Next, we saw
how sed is more useful than grep when we want to transform our text.
Finally, we’ve demonstrated how awk is capable of replicating grep and sed
functionality while additionally providing more features for advanced text
processing.
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha
Practical no - 4
Aim- Implementation of various CPU scheduling algorithms (FCFS, SJF, Priority).
Theory- Given n processes with their burst times, the task is to find average
waiting time and average turn around time using FCFS scheduling algorithm.
First in, first out (FIFO), also known as first come, first served (FCFS), is the
simplest scheduling algorithm. FIFO simply queues processes in the order that they
arrive in the ready queue.
In this, the process that comes first will be executed first and next process starts
only after the previous gets fully executed.
Here we are considering that arrival time for all processes is
0. How to compute below times in Round Robin using a
program?
Program
findavgTime(processes, n,
burst_time); return 0;
}
Output screenshots-
Conclusion-
1. Non-preemptive
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha
2. Average Waiting Time is not optimal
3. Cannot utilize resources in parallel : Results in Convoy effect (Consider a
situation when many IO bound processes are there and one CPU bound
process. The IO bound processes have to wait for CPU bound process when
CPU bound process acquires CPU. The IO bound process could have better
taken CPU for some time, then used IO devices).
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha
Practical. 5
2. Theory –
In operating systems that use paging for memory management, page replacement
algorithm are needed to decide which page needed to be replaced when new
page comes in. Whenever a new page is referred and not present in memory,
page fault occurs and Operating System replaces one of the existing pages with
newly needed page. Different page replacement algorithms suggest different ways
to decide which page to replace. The target for all algorithms is to reduce number
of page faults.
First In First Out (FIFO) page replacement algorithm –
This is the simplest page replacement algorithm. In this algorithm, operating
system keeps track of all pages in the memory in a queue, oldest page is in the
front of the queue. When a page needs to be replaced page in the front of the
queue is selected for removal.
Example -1. Consider page reference string 1, 3, 0, 3, 5, 6 and 3 page slots.
Initially all slots are empty, so when 1, 3, 0 came they are allocated to the empty
slots —> 3 Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults.
Then 5 comes, it is not available in memory so it replaces the oldest page slot i.e 1. —
>1 Page Fault.
Finally 6 comes, it is also not available in memory so it replaces the oldest page slot i.e 3
—
>1 Page Fault.
Implementation – Let capacity be the number of pages that memory can hold. Let
set be the current set of pages in memory.
1- Start traversing the pages.
i) If set holds less pages than capacity.
a) Insert page into the set one by one
until the size of set reaches capacity
or all page requests are processed.
b) Simultaneously maintain the pages in
the queue to perform FIFO.
c) Increment page fault
ii) Else
If current page is present in set, do nothing.
Else
a) Remove the first page from the
queue as it was the first to be
entered in
the memory
b) Replace the first page in the queue
with the current page in the string.
c) Store current page in the queue.
d) Increment page faults.
3. Program –
// C++ implementation of FIFO page replacement
// in Operating
Systems.
#include<bits/stdc++.
h> using namespace
std;
// increment page
fault page_faults++;
// Increment page
faults page_faults++;
}
}
}
return page_faults;
}
// Driver
code int
main()
{
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4,
2, 3, 0, 3, 2};
int n =
sizeof(pages)/sizeof(pages[0]); int
capacity = 4;
cout << pageFaults(pages, n,
capacity); return 0;
}
. Output screenshots -
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha
. Conclusion- We can also find the number of page hits. Just have to maintain a
separate count. If the current page is already in the memory then that must be
count as Page-hit.
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha
Practical no -6
.Aim - Concurrent programming use of threads and processes, system calls (fork
and v- fork).
Theory –
1. fork() :
Fork() is system call which is used to create new process. New process created
by fork() system call is called child process and process that invoked fork()
system call is called parent process. Code of child process is same as code of
its parent process. Once child process is created, both parent and child
processes start their execution from next statement after fork() and both
processes get executed simultaneously.
2. vfork() :
Vfork() is also system call which is used to create new process. New process
created by vfork() system call is called child process and process that invoked
vfork() system call is called parent process. Code of child process is same as code
of its parent process. Child process suspends execution of parent process until child
process completes its execution as both processes share the same address space.
fork()
fork(): System call to create a child
fork
This will yield output mentioning what is fork used for, syntax and along with all the
required details.
The syntax used for the fork system call is as
Fork system call creates a child that differs from its parent process only in
pid(process ID) and ppid(parent process ID). Resource utilization is set to zero.
File locks and pending signals are not inherited. (In Linux “fork” is implemented as
“copy-on-write()“).
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha
vfork()
vfork –> create a child process and block parent
process. Note:- In vfork, signal handlers are inherited
but not shared.
This will yield output mentioning what is vfork used for, syntax and along with
all the required details.
Program
fork()
shashi@linuxtechi ~}$ vim
1_fork.c #include<stdio.h>
#include<unistd.h>
Int main(void)
{
printf("Before
fork\n"); fork();
printf("after fork\n");
}
shashi@linuxtechi ~}$
shashi@linuxtechi ~}$ cc
1_fork.c shashi@linuxtechi
~}$ ./a.out Before fork
After fork
shashi@linuxtechi
~}$
vfork()
Output screenshots
Conclusion - Again if you observe the outcome of vfork is not defined. Value of “n”
has been printed first time as 10, which is expected. But the next time in the
parent process it has printed some garbage value.