Os - Arif 12
Os - Arif 12
IV SEMESTER
Laboratory Manual
Session 2024-25
Page 1
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
CERTIFICATE
This is to Certify that
Mr./Ms. Sk Arif Muhammad With enrolment no.2303051050904
TABLE OF CONTENT
Page No
Sr. Dateof Date of Marks(out
Experiment Title Sign
No Start Completion of 10)
From To
Command shell:
A program that interprets commands is Command shell.
Shell Script: Allows a user to execute commands by typing them manually at a terminal, or automatically in
programs called shell scripts. A shell is not an operating system. It is a way to interface with the operating
system and run Commands.
DESCRIPTION:
pwd prints the full pathname of the current working directory.
SYNTAX:
pwd
EXAMPLE:
$ pwd
OUTPUT:
2. cd:
Change Directory
DESCRIPTION:
It allows you to change your working directory. You use it to move around within the hierarchy of your
file system.
SYNTAX:
cd directory_name
EXAMPLE: To change into “work directory” in “KALI”
Page 4
Enrolment no : 2303051050904
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
OUTPUT:
3. cd ..
DESCRIPTION:
Move up one directory.
SYNTAX:
cd ..
EXAMPLE:
If you are in work directory and want to go to documents then write
OUTPUT:
4. ls :
list all the files and directories
DESCRIPTION:
List all files and folders in the current directory in the column format.
SYNTAX:
ls [options]
EXAMPLE:
Using various options
• Lists the total files in the directory and subdirectories, the names of the files in the current directory, their
permissions, the number of subdirectories in directories listed, the size of the file, and the date of last
modification. ls -l
• List all files including hidden files ls -a
OUTPUT:
Page 5
Enrolment no : 2303051050904
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
5. cat
DESCRIPTION:
cat stands for "catenate".
It reads data from files, and outputs their contents.
It is the simplest way to display the contents of a file at the command line.
SYNTAX:
cat filename
EXAMPLES:
• Print the contents of files mytext.txt and yourtext.txt cat mytext.txt yourtext.txt
• Print the cpu information using cat command cat /proc/cpuinfo
• Print the memory information using cat command cat /proc/meminfo
OUTPUT:
6. head
DESCRIPTION:
head, by default, prints the first 10 lines of each FILE to standard output. With more than one FILE, it precedes
each set of output with a header identifying the file name. If no FILE is specified, or when FILE is specified as a
dash ("-"), head reads from standard input.
SYNTAX:
head [option]…[file/directory]
Page 6
Enrolment no : 2303051050904
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
EXAMPLE:
Display the first ten lines of myfile.txt. head myfile.txt
OUTPUT:
Page 7
Enrolment no : 2303051050904
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
7. tail
DESCRIPTION:
tail is a command which prints the last few number of lines (10 lines by default) of a certain file, then
terminates.
VSYNTAX:
tail [option]…[file/directory]
EXAMPLE:
Output the last 100 lines of the file myfile.txt. tail myfile.txt -n 100
OUTPUT:
8. mv :
Moving (and Renaming) Files
DESCRIPTION:
The mv command lets you move a file from one directory location to another. It also lets you rename a file
(there is no separate rename command).
SYNTAX: mv
[option] source directory
EXAMPLE:
• Moves the file myfile.txt to the directory destination-directory. mv myfile.txt destination_directory
OUTPUT:
9. mkdir :
Make Directory
DESCRIPTION:
If the specified directory does not already exist, mkdir creates it. More than one directory may be specified
when calling mkdir.
SYNTAX:
mkdir [option] directory
Page 8
Enrolment no : 2303051050904
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
EXAMPLE:
Create a directory named 210304124456. mkdir work
OUTPUT:
10. cp :
Copy Files
DESCRIPTION:
The cp command is used to make copy of files and directories.
SYNTAX:
cp [option] source directory
EXAMPLE:
Creates a copy of the file in the currently working directory named origfile. The copy will be named newfile, and
will be located in the working directory. cp origfile newfile
OUTPUT:
11. rmdir :
Romove Directory
DESCRIPTION:
The rmdir command is used to remove a directory that contains other files or directories.
SYNTAX:
rm directory_name
EXAMPLE:
Delete mydir directory along with all files and directories within that directory. Here, -r is for recursive and –f is
for forcefully. rmdir -rf mydir
OUTPUT:
12. echo
DESCRIPTION:
Display text on the screen.
Page 9
Enrolment no : 2303051050904
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
OUTPUT:
13. clear
DESCRIPTION:
Used to clear the screen
SYNTAX: clear
OUTPUT:
14. date :
DESCRIPTION:
display current date and time.
OUTPUT:
15. cal:
DESCRIPTION:
display current month calendar.
OUTPUT:
Page 10
Enrolment no : 2303051050904
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
16. touch:
DESCRIPTION:
create a new file
OUTPUT:
Page 11
Enrolment no : 2303051050904
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 2
What is a Shell?
An Operating is made of many components, but its two prime components are -
• Kernel
• Shell
A Kernel is at the nucleus of a computer. It makes the communication between the hardware and
software possible. While the Kernel is the innermost part of an operating system, a shell is the
outermost one.
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.
Shell scripting is writing a series of command for the shell to execute. It can combine lengthy and
repetitive sequences of commands into a single and simple script, which can be stored and
executed anytime. This reduces the effort required by the end user.
1. Create a file using a vi editor(or any other editor). Name script file with extension .sh
2. Start the script with #! /bin/sh
Page 12
Enrolment no : 2303051050904
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
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 the bourne-shell.
#!/bin/sh
x=10
y=11
if [ $x-ne $y ]
then
fi
OUTPUT:
Page 13
Enrolment no : 2303051050904
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 3
AIM: Write a shell script to print given numbers sum of all digits
Flow chart :
CODE:
#!/bin/bash
Page 14
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 3
OUTPUT:
Page 15
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 4
AIM: Write a shell script to validate the entered date. (eg. Date format is: dd-mm-yyyy).
Flow chart :
CODE:
Page 16
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 4
OUTPUT:
Page 17
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 5
Flow chart :
CODE:
else
fi
Page 18
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 5
OUTPUT:
Page 19
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 6
AIM: Write a Shell script to say Good morning/Afternoon/Evening as you log in to system.
Flow chart :
CODE:
#!/bin/bash
hour=`date +%H`
if [ $hour -lt 12 ] # if hour is less than 12
then
echo "GOOD MORNING WORLD"
elif [ $hour -le 16 ] # if hour is less than equal to 16
then
echo "GOOD AFTERNOON WORLD"
elif [ $hour -le 20 ] # if hour is less than equal to 20
then
echo "GOOD EVENING WORLD"
else
echo "GOOD NIGHT WORLD"
fi
Page 20
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 6
OUTPUT:
Page 21
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 7
Flow chart :
CODE:
#include <stdio.h>
#include <sys/wait.h> /* contains prototype for wait */
int main(void)
{
int pid;
int status;
printf("Hello World!\n");
pid = fork( );
if(pid == -1) /* check for error in fork */
{
perror("bad fork");
exit(1);
}
if (pid == 0)
printf("I am the child process.\n");
else
{
wait(&status); /* parent waits for child to finish */
printf("I am the parent process.\n");
}
}
Page 22
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 7
OUTPUT:
Page 23
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 8
AIM: Find out the biggest number from given three numbers supplied as command line
arguments.
Flow chart :
CODE:
#!/bin/bash
echo "enter number 1"
read n1
echo "enter number 2"
read n2
echo "enter number 3"
read n3
if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]
then
echo "Number 1 is biggest: $n1"
elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ]
then
echo "Number 2 is biggest: $n2"
else
echo "Number 3 is biggest: $n3"
fi
Page 24
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 8
OUTPUT:
Page 25
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 9
Flow chart :
CODE:
#! /bin/sh
for ((i=1;i<=5;i++)); # Loop to create 5 lines of text
do
for((k=1;k<=(5-i);k++)); # Loop to padd prepending spaces
do
printf "%s" " ";
done;
for ((j=1;j<=i;j++)); # Loop to create asterix
do
printf "%s" "*";
done;
printf "\n"; # Print the carriage return
done
Page 26
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 9
OUTPUT:
Page 27
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 10
AIM: Shell script to determine whether given file exist or not.
CODE:
#!/bin/bash
if [ -f "File.txt" ];
then
OUTPUT:
Page 28
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 11
AIM: Write a program for process creation using C. (Use of gcc compiler)..
CODE:
#include <stdio.h>
#include <sys/wait.h> /* contains prototype for wait */
int main(void)
{
int pid;
int status;
printf("PROCESS CREATION\n");
pid = fork( );
if(pid == -1) /* check for error in fork */
{
perror("bad fork");
exit(1);
}
if (pid == 0)
printf("I am the child process.\n");
else
{
wait(&status); /* parent waits for child to finish */
printf("I am the parent process.\n");
}
}
OUTPUT:
Page 29
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 12
CODE:
#include<stdio.h>
int main(){
int bt[10]={0},at[10]={0},tat[10]={0},wt[10]={0},ct[10]={0};
int n,sum=0;
float totalTAT=0,totalWT=0;
for(int i=0;i<n;i++)
{
printf("\n");
}
for(int j=0;j<n;j++)
{
sum+=bt[j];
ct[j]+=sum;
}
for(int k=0;k<n;k++)
{
tat[k]=ct[k]-at[k];
totalTAT+=tat[k];
}
Page 30
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
for(int k=0;k<n;k++)
{
wt[k]=tat[k]-bt[k];
totalWT+=wt[k];
}
printf("Solution: \n\n");
printf("P#\t AT\t BT\t CT\t TAT\t WT\t\n\n");
for(int i=0;i<n;i++)
{
printf("P%d\t %d\t %d\t %d\t %d\t %d\n",i+1,at[i],bt[i],ct[i],tat[i],wt[i]);
}
return 0;
}
OUTPUT:
#include<stdio.h>
int main()
{
Page 31
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
Page 32
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
}
OUTPUT:
Page 33
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
PRACTICAL – 13
AIM: Implementation of Banker’s Algorithm.
CODE:
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
int p, r, i, j, process, count;
count = 0;
Page 34
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
Page 35
Faculty of Engineering & Technology
Subject Name : OS Laboratory
Subject Code : 303105252
B.Tech CSE Year 2nd Semester 4th
}while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}
OUTPUT:
Page 36