Professional Documents
Culture Documents
Implementation:
Implementation of code:
17
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
// Driver code
int main()
{
//process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
findavgTime(processes, n, burst_time);
return 0;
}
18
Output:
Processes Burst time Waiting time Turn around time
1 10 0 10
2 5 10 15
3 8 15 23
19
5. Implementation of SJF Scheduling algorithm
#include <iostream>
using namespace std;
int main() {
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
// Calculation of Waiting Times
20
for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
cout << "P BT WT TAT" << endl;
avg_tat = (float)total / n;
cout << "Average Waiting Time= " << avg_wt << endl;
cout << "Average Turnaround Time= " << avg_tat << endl;
}
Output:
Enter number of process: 4
P BT WT TAT
P2 3 0 3
P3 4 3 7
P1 5 7 12
P4 6 12 18
21
6. Implementation of Round-Robin Scheduling
algorithm
// C++ program for implementation of RR scheduling
#include<iostream>
using namespace std;
22
// Increase the value of t i.e. shows
// how much time a process has been processed
t = t + rem_bt[i];
23
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
<< wt[i] <<"\t\t " << tat[i] <<endl;
}
// Driver code
int main()
{
// process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
// Time quantum
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}
Output:
PN BT WT TAT
1 10 13 23
2 5 10 15
3 8 13 21
24
7. What is vi editor and its commands?
The vi editor is elaborated as visual editor. It is installed in every Unix system. In other words,
it is available in all Linux distros. It is user-friendly and works same on different distros and
platforms. It is a very powerful application. An improved version of vi editor is vim.
o Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key
will take you to the command mode from insert mode.
By default, the vi editor starts in command mode. To enter text, you have to be in insert
mode, just type 'i' and you'll be in insert mode. Although, after typing i nothing will appear
on the screen but you'll be in insert mode. Now you can type anything.
Using vi
The vi editor tool is an interactive tool as it displays changes made in the file on the screen
while you edit the file.
In vi editor you can insert, edit or remove a word as cursor moves throughout the file.
Commands are specified for each function like to delete it's x or dd.
The vi editor is case-sensitive. For example, p allows you to paste after the current line
while P allows you to paste before the current line.
Command mode
This is what you'll see when you'll press enter after the above command. If you'll start typing,
nothing will appear as you are in command mode. By default vi opens in command mode.
25
Look at the above snapshot, it is blank as it is a new file. To start typing, you have to move to
the insert mode. At the end of the terminal window, directory name and file name are
displayed.
Insert mode
To move to the insert mode press i. Although, there are other commands also to move to
insert mode which we'll study in next page.
Look at the above snapshot, after pressing i we have entered into insert mode. Now we can
write anything. To move to the next line press enter.
Once you have done with your typing, press esc key to return to the command mode.
Commands Action
:w Save
:q Quit
26
Look at the above snapshot, command :wq will save and quit the vi editor. When you'll type
it in command mode, it will automatically come at bottom left corner.
If you want to quit without saving the file, use :q. This command will only work when you
have not made any changes in the file.
Vi Commands
Linux vi editor is different from other editors. You have to use different keys to use different
functions. Although, it's quite easy and interesting to use vi editor.
The vi editor commands are case sensitive.
Have a look at the vi commands in the following table.
Command Action
j To move down
k To move up
h To move left
l To move right
To jump lines:
Commands Action
To delete:
Commands Action
D Delete the current line from current character to the end of the line
28
8. Shell Commands
4) Create a file
7) Cat command can append the contents of one file to the end of another file.
29
9. Shell Scripting- Using variables
Variable Names-:
The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the
underscore character ( _).
Defining Variables
Variables are defined as follows −
-> variable_name=variable_value
For example −
SYNTAX-:
NAME="RAJAT"
echo $NAME
VAR2=100
echo $VAR2
OUTPUT-:
RAJAT
100
30
10. Shell Scripting- - Input & Output
You can use the "read" command to prompt the user to enter input. The input can be
stored in a variable, which can then be used in the script. For example, the following
code prompts the user to enter their name and then displays a greeting message:
You can use the "echo" command to display output on the screen. For example, the
following code displays a message:
Output to a file:
You can use the ">" symbol to redirect output to a file. For example, the following
code writes the output of the "ls" command to a file called "files.txt":
ls > files.txt
31
11. Shell Scripting- Data types
Integers: Integers are whole numbers without decimal points, positive or negative.
They are used for performing arithmetic operations.
Floats: Floats are numbers with decimal points. They are also used for performing
arithmetic operations, but they have limited precision compared to other
programming languages.
!/bin/bash
# String variable
name="John"
# Integer variable
age=30
# Float variable
height=1.75
echo "My name is $name, I am $age years old, and my height is $height
meters."
32
12. Shell Scripting- Use of arithmetic operators
Example: sum=$((10+20))
Example: difference=$((30-20))
Example: product=$((5*6))
Example: quotient=$((30/5))
Example: remainder=$((25%3))
Example: count=0
count++
33
13. Shell Scripting- if control statement
programs
```
#!/bin/bash
if [ -e "/path/to/file" ]
then
echo "File exists"
else
echo "File does not exist"
fi
```
```
#!/bin/bash
34
14. Shell Scripting- while control statement
The `while` loop is a control structure used in shell scripting to execute a set of
commands repeatedly until a certain condition is met. The syntax for the `while` loop
in shell scripting is as follows:
```
while [ condition ]
do
# commands to be executed
done
```
Here, the `condition` is a logical expression that must evaluate to true (i.e., non-zero) or false
(i.e., zero) value. The `do` keyword starts the block of commands to be executed, and the
`done` keyword marks the end of the loop.
The `while` loop continues to execute the commands within the loop as long as the
`condition` remains true. When the `condition` becomes false, the loop terminates, and the
control transfers to the statement following the `done` keyword.
Here's an example of a shell script that uses a `while` loop to print numbers from 1 to 10:
#!/bin/bash
count=1
35
15. Shell Scripting- for control statement
In shell scripting, the `for` statement is used to iterate through a sequence of values
and perform a set of commands for each value. The syntax of the `for` statement is
as follows:
```
for variable in sequence
do
commands
done
```
Here, `variable` is a placeholder for the current value in the sequence being
processed, and `sequence` is a list of values or expressions that can be expanded
into a list of values.
For example, consider the following shell script that uses a `for` statement to iterate
through a list of filenames and perform a set of commands for each file:
```
#!/bin/bash
36