You are on page 1of 18

Shiksha Mandal’s

Bajaj Institute of Technology,


Wardha

Session 2020-21 Even Term


Operating System Lab Manual
BTCOL410

Name: Husain Saify


Roll no:406

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-

1 Using awk with printf


$ awk '{ printf "%10s\n", $1 }' employee.txt
2 awk with grep
$ cat employee.txt
$ grep '1002' employee.txt | awk -F '\t' '{ print $2 " will get $" ($3*5)/100 "
bonus"}' 3 awk with sed
$ cat employee.txt
$ sed -n '/J/p' employee.txt | awk -F '\t' '{ printf "%s(%s)\n", $2, $1 }'
Output screenshots-
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha

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?

1. Completion Time: Time at which process completes its execution.


2. Turn Around Time: Time Difference between completion time and arrival
time. Turn Around Time = Completion Time – Arrival Time
3. Waiting Time(W.T): Time Difference between turn around time and burst
time. Waiting Time = Turn Around Time – Burst Time
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha

1- Input the processes along with their burst time


(bt). 2- Find waiting time (wt) for all processes.
3- As first process that comes need not to wait
so waiting time for process 1 will be 0 i.e.
wt[0] = 0.
4- Find waiting time for all other processes i.e.
for process i ->
wt[i] = bt[i-1] + wt[i-1] .
5- Find turnaround time = waiting_time +
burst_time for all processes.
6- Find average waiting time =
total_waiting_time / no_of_processes.
7- Similarly, find average turnaround time =
total_turn_around_time /
no_of_processes.

Program

// C program for implementation of FCFS


// scheduling
#include<stdio.h>
// Function to find the waiting time for all
// processes
void findWaitingTime(int processes[], int n,
int bt[], int wt[])
{
// waiting time for first process is 0
wt[0] = 0;

// calculating waiting time


for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}

// Function to calculate turn around time


void findTurnAroundTime( int processes[],
int n, int bt[], int wt[], int tat[])
{
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha
// calculating turnaround time by adding
// bt[i] + wt[i]

for (int i = 0; i < n ; i++)

tat[i] = bt[i] + wt[i];


}

//Function to calculate average time


void findavgTime( int processes[], int n, int bt[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

//Function to find waiting time of all processes


findWaitingTime(processes, n, bt, wt);

//Function to find turn around time for all processes


findTurnAroundTime(processes, n, bt, wt, tat);

//Display processes along with all details


printf("Processes Burst time Waiting time Turn around time\n");

// Calculate total waiting time and total turn


// around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat +
tat[i]; printf(" %d
",(i+1));
printf(" %d ", bt[i] );
printf(" %d",wt[i] );
printf(" %d\n",tat[i] );
}
int s=(float)total_wt /
(float)n; int t=(float)total_tat /
(float)n;
printf("Average waiting time =
%d",s); printf("\n");
printf("Average turn around time = %d ",t);
}
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha
// Driver code
int main()
{
//process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];

//Burst time of all processes


int burst_time[] = {10, 5, 8};

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

1.Aim - Implementation of various page replacement algorithms(FIFO, Optimal, LRU).

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.

So total page faults = 5.


Example -1. Consider the following reference string: 0, 2, 1, 6, 4, 0, 1, 0,
3, 1, 2, 1. Using FIFO page replacement algorithm –

So, total number of page faults = 9.


Given memory capacity (as number of pages it can hold) and a string representing
pages to be referred, write a function to find number of page faults.
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha

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;

// Function to find page faults using FIFO


int pageFaults(int pages[], int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or
not unordered_set<int> s;

// To store the pages in FIFO


manner queue<int> indexes;
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha
// Start from initial page
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha
int page_faults = 0;
for (int i=0; i<n; i++)
{
// Check if the set can hold more
pages if (s.size() < capacity)
{
// Insert it into set if not present
// already which represents page
fault if (s.find(pages[i])==s.end())
{
// Insert the current page into the
set s.insert(pages[i]);

// increment page
fault page_faults++;

// Push the current page into the


queue indexes.push(pages[i]);
}
}

// If the set is full then need to perform FIFO


// i.e. remove the first page of the queue from
// set and queue both and insert the current
page else
{
// Check if current page is not already
// present in the set
if (s.find(pages[i]) == s.end())
{
// Store the first page in the
// queue to be used to find and
// erase the page from the
set int val = indexes.front();

// Pop the first page from the


queue indexes.pop();

// Remove the indexes page from the


set s.erase(val);
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha

// insert the current page in the


set s.insert(pages[i]);

// push the current page into


// the queue
indexes.push(pages[i
]);

// 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

process. shashi@linuxtechi ~}$ man

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

below, pid_t fork(void);

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.

shashi@linuxtechi ~}$ man vfork

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()

shashi@linuxtechi ~}$ vim


1.vfork.c #include<stdio.h>
#include<unistd.h>
Int main(void)
{
printf("Before
fork\n"); vfork();
printf("after fork\n");
}
Shiksha Mandal’s
Bajaj Institute of Technology,
Wardha
shashi@linuxtechi ~}$ vim
1.vfork.c shashi@linuxtechi ~}$
cc 1.vfork.c shashi@linuxtechi
~}$ ./a.out Before vfork
after
vfork
after
vfork
a.out: cxa_atexit.c:100: new_exitfn: Assertion `l != NULL'
failed. Aborted

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.

You might also like