Priyankaos Lab Manual
Priyankaos Lab Manual
PRACTICAL-1
Aim: To study of basic commands of Linux/Unix.
BASIC LINUX COMMANDS:
1. Pwd: -
Pwd-Present working directory
Syntax: - pwd
Description: - The pwd Linux command prints the current
working directory path starting from the root. Use the pwd
command to find your way in the Linux file system
Structure maze or the pass the working directory in the
bash script. In this tutorial, you will learn to use the pwd
command.
Output: -
2. Cd: -
Cd: - Change directory
Syntax: -cd
Description: - cd command in Linux known as change
directory command. It is used to change current
working directory.
Output: -
3. Cd .. :-
Cd .. : - To change the parent directory to current directory.
Description: -This command is used to move to the parent
directory of current directory, or the directory one level up
from the current directory. “..” represents parent directory
Syntax: cd ..
Output: -
LS: -
Ls: - This command is used to list the files present in that directory.
Description: - we use ls command to list files and directories. This command will print a
Syntax: ls
Output: -
5. CAT: -
Cat: - A utility command in Linux.
Description: - The cat command is a utility command in Linux.
One of its most common usages is to print the content of a file
onto the standard output stream. Other than that, the cat
command also allows us to write some texts into a file.
Syntax: - cat [file-name]
Output:
MKDIR: -
Mkdir:- Mkdir command in Linux/Unix allows users to create.
Description: - The mkdir command in Linux/Unix allows users to create or make new d
Syntax: - mkdir [directory name]
Output: -
7. MV: -
Mv: -Move.
Description: - The mv command termed as “Move”, which is a
command-line utility to move files or directories from source
Output:
-
echo: -
Description: - Display text on the screen.
Syntax: - echo your text
Output:
9 .CP: -
cp: -Copies the files.
Description: - cp command copies files (or, optionally,
directories). The copy is completely independent of the
original. You can either copy one file to another, or copy
arbitrarily many files to a destination directory. In the first
format, when two file names are given, cp command copies
SOURCE file to DEST file.
10 .RMDIR: -
rmdir: -to remove or empty the diroctaries.
Description: - mdir command is used remove empty
directories from the filesystem in Linux. The rmdir
command removes each and every directory specified in the
command line only if these directories are empty. So, if the
specified directory has some directories or files in it then this
cannot be removed by rmdir command.
Syntax: - rmdir [directory name]
Output: -
PRACTICAL NO:2
What is a Shell?
An Operating is made of many components, but its two prime components are -
Kernel
Shell
A shell in a Linux operating system takes input from you in the form of commands,
processes it, and then gives an output. It is the interface through which a user works
on the programs, commands, and scripts. A shell is accessed by a terminal which
runs it.
When you run the terminal, the Shell issues a command prompt (usually $),
where you can type your input, which is then executed when you hit the Enter key.
The output or the result is thereafter displayed on the terminal.
The Shell wraps around the delicate interior of an Operating system protecting it
from accidental damage. Hence the name Shell.
Types of Shell
There are two main shells in Linux:
1. The Bourne Shell: The prompt for this shell is $ and its derivatives are
listed below:
2. The C shell: The prompt for this shell is %, and its subcategories are:
Create a file using a vi editor(or any other editor). Name script file with
extension .sh
Start the script with #! /bin/sh
3. Write some code.
4. Save the script file as filename.sh
5. For executing the script type bash filename.sh
"#!" is an operator called shebang which directs the script to the interpreter location.
So, if we use"#!
/bin/sh" the script gets directed to
small script –
vi
:wq
vi
Commenting is important in any program. In Shell programming, the syntax to add a comment is
#comment
For example, the following creates a shell variable and then prints it:
Variable = “hello”
echo $variable
Below is a small script which will use a variable. echo “what is your name?”
read $name
echo “how do you do $name?” read remark
echo “I am $remark too! ”
As you see, the program picked the value of the variable 'name' as Joy and 'remark'
as excellent.
This is a simple script. You can develop advanced scripts which contain conditional
statements, loops, and functions. Shell scripting will make your life easy and Linux
administration a breeze.
Summary:
Kernel is the nucleus of the operating systems, and it communicates between hardware and software
Shell is a program which interprets user commands through CLI like Terminal
The Bourne shell and the C shell are the most used shells in Linux
Shell scripting is writing a series of command for the shell to execute
Shell variables store the value of a string or a number for the shell to read
Shell scripting can help you create complex programs containing conditional statements, loops, and functions
PRACTICAL NO:3
AIM: Take any number from the user. Get each and every digit one by one and make
addition of that to find the sum of digits of a given number. E.g if user has entered 342
then the sum of digits is 3+4+2 = 9.
Flowchart:
Code:
Output:
Code:
Output:
Code:
Output:
PRACTICAL NO:4
Description: -
In a year there are 12 months and in each month the no. of days
are 30 or 31.For February month the no. of days is 28 and if the leap year
then the no. of days is 29. By checking these conditions for days , months
and year using various control statements of Linux can validate the data
entered by the user (eg. Date format is : dd-mm-yyyy )
Flow-chart: -
Syntax
Bash case statement.
Case expression in
Pattern 1 )
Statements ;;
Pattern 2 )
Statements ;; … esac
Following are the key points of bash case statements
Case statement first expands the expression and tries to match it against each pattern
When a match is found all of the associated statements until the double semicolon (;; ) are executed
After the first match, case terminates with the exit status of the last command that was executed
If there is no match, exit status of case is zero.
Code:
Hi
Output:
Code:
Input:
Enter the year 2004 Enter the year 2001 Output :
SET 3: Write a shell script to validate the entered date. (eg. Date format is : dd-mm-yyyy )
PRACTICAL NO:5
Definition: A string is said to be palindrome if reverse of the string is same as string. For example,
“abba” is palindrome, but “abbc” is not palindrome. We can use Linux command to find the length
of the string and then by comparing first character and last character of the string, second
character and second last character of the string and so on., we can identify that whether the string
is palindrome or not.
Flow chart:
SAMPLE CODE
OUTPUT
PRACTICAL NO:6
Definition: The date command is used to print out the system's time and date
information. By extracting hours from the date using Linux ‘cut’ command and by using if
else ladder can wish user an appropriate message like Good morning/Afternoon/Evening.
SET 2:Write a Shell script to say Good morning/Afternoon/Evening as you log in to system.
CODE :
Output :
PRACTICAL NO:7
Definition: Create child process using fork( ), which is a system call to create a child process.
Use getpid( ) for getting the id of the process, getppid( ) for getting the parent process id.
Flowchart:
Note: fork() is threading based function, to get the correct output run
the program on a local system. Please note that the above program do
INPUT:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h> int main()
{
// make two process which run same
// program after this instruction fork();
printf("Hello world!\n");
return 0;
}
OUTPUT:
Set 2.
INPUT:
#include <stdio.h>
#include <sys/types.h>
int main()
fork();
fork();
Output:
Set 3
INPUT:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void forkexample()
zero if (fork() == 0)
else
int main()
forkexample();
return 0;
Output:
PRACTICAL NO:8
Definition: Finding out biggest number from given three numbers supplied as command line
argument. Command argument means at run time, the user will enter the input. First input will be
considered as number 1, second input will be consider as number 2 and so on. It depends on the no
Flowchart:
Show the use of command line argument for finding the biggest number among three
INPUT:
OUTPUT:
PRACTICAL NO:9
Definition: Print the pattern using for loop which is used to do the same thing again and
again until
For loop is used to do the same thing until some condition is there.
Flowchart:
Input:
Output:
PRACTICAL NO:10
Definition: Various commands are available in Linux to check whether the given
directory or fileare exist or not in the system. Several options are also available to check
special conditions like file.is empty or not. We can use these commands in shell script as
well.
Flowchart:
Code:
OUT PUT:
PRACTICAL NO:11
Definition: First come, first served (FCFS) is an operating system process scheduling
algorithm and a network routing management mechanism that automatically executes
queued requests and processesby the order of their arrival.
Flowchart:
Set 1: How to take Arrival Time and Burst Time of the processes.
Sample Code:
#include<stdio.h>
wt[0] = 0;
void findTurnAroundTime( int processes[], int n,int bt[], int wt[], int tat[])
total_tat = total_tat +
printf(" %d",wt[i] );
printf(" %d\n",tat[i] );
printf("\n");
int main()
PRACTICAL NO:12
Definition: Round robin (RR) scheduling is a job-scheduling algorithm that is considered
to be very fair, as it uses time slices that are assigned to each process in the queue or line.
Flowchart:
Eac h process is
then allowed to use the CPU for a given amount of time, and if it does not finish within the
allotted time,it is preempted and then moved at the back of the line so that the next
process in line is able to use the CPU for the same amount of time
Set 1: How to take Arrival Time, Burst Time and Time Quantum of the processes.
Sample Code:
#include<stdio.h>
void main()
scanf("%d", &NOP);
// Use for loop to enter the details of the process like Arrival time and the Burst Time for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1); printf(" Arrival time is: \t"); // Acc
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // Accept the Burst time scanf("%d", &bt[i]);
scanf("%d", &quant);
// Display the process No, burst time, Turn Around Time and the waiting
time printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
sum = sum +
temp[i]; temp[i] =
0; count=1;
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]- bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
if(i==NOP-1)
i=0;
else if(at[i+1]<=sum)
else
i=0;
Output:
PRACTICAL NO:13
Definition: The Banker's algorithm, sometimes referred to as the detection
algorithm, is a resource allocation and deadlock avoidance algorithm. It tests for safety by
simulating the allocation of predetermined maximum possible amounts of all resources.
When a new process enters a system, it must declare the maximum number of instances of
each resource type that it may ever claim; clearly, that number may not exceed the total
number of resources in the system. Also, when a process gets all its requested resources it
must return them in a finite amount of time.
Flow Chart:
Resource-Request Algorithm
Set 1: How to take no. of processes, no. of resources, maximum resource matrix, allocated
resource matrix and available resources for each process.
#include <stdio.h>
int main()
int n, m, i, j, k;
n = 5;
m = 3;
int alloc[5][3] = { { 0, 1, 0 },
{ 2, 0, 0 },
{ 3, 0, 2 },
{ 2, 1, 1 },
{ 0, 0, 2 } };
int max[5][3] = { { 7, 5, 3 },
{ 3, 2, 2 },
{ 9, 0, 2 },
{ 2, 2, 2 },
{ 4, 3, 3 } };
int avail[3] = { 3, 3, 2 };
f[k] = 0;
int need[n][m];
int y = 0;
if (f[i] == 0) {
int flag = 0;
if (flag == 0) {
ans[ind++] = i;
avail[y] += alloc[i][y];
f[i] = 1;
for(int i=0;i<n;i++)
if(f[i]==0)
flag=0;
safe"); break;
if(flag==1)
return (0);
Output:
Set 3: Perform the Banker’s Algorithm and find out that whether the System is Safe or not
and also
Safety Algorithm:
Work = Available
This means, initially, no process has finished and the number of available resources is
It means, we need to find an unfinished process whose need can be satisfied by the available resources. If no
3. Perform the following: Work = Work + Allocation; Finish[i] = true;
Go to step 2.
When an unfinished process is found, then the resources are allocated and the process is
marked finished. And then, the loop is repeated to check the same for all other processes.
That means if all processes are finished, then the system is in safe state.