[go: up one dir, main page]

0% found this document useful (0 votes)
12 views56 pages

Aum Os

This document is a lab manual for the Operating System course (203105214) for Bachelor of Technology students in the Faculty of Engineering and Technology. It includes a certificate of completion for a student named Avani Shukla and outlines various practical exercises related to Linux commands, shell scripting, and process management. The manual details specific tasks and commands to be performed, along with examples and explanations of basic Linux commands.

Uploaded by

Mayank Chouhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views56 pages

Aum Os

This document is a lab manual for the Operating System course (203105214) for Bachelor of Technology students in the Faculty of Engineering and Technology. It includes a certificate of completion for a student named Avani Shukla and outlines various practical exercises related to Linux commands, shell scripting, and process management. The manual details specific tasks and commands to be performed, along with examples and explanations of basic Linux commands.

Uploaded by

Mayank Chouhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Laborat

FACULTY OF ENGINEERING AND TECHNOLOGY


BACHELOR OF TECHNOLOGY

OPERATING SYSTEM
(203105214)
4th SEMESTER
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Lab Manual
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

CERTIFICATE

This is to certify that Avani Shukla with


enrollment no. 2303051050149 has successfully
completed
his/her laboratory experiments in the
OPERATING SYSTEM (203105214) from the
department of COMPUTER SCIENCE AND ENGINEERING during the
academic year 2024-25.

Date of Submission:......................... Staff In charge:...........................

Head Of Department:...........................................
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
S.N Name of Page Perfor- Assess- Marks Sign
O Practical No. mance ment
date date
1. Study of Basic commands
of Linux.

2. Study the basics of shell


programming.

3. Write a Shell script to print


given numbers sum of all
digits

4. Write a shell script to


validate the entered date.
(eg. Date format is: dd-
mm-yyyy).

5. Write a shell script to


check entered string is
palindrome or not.

6. Write a Shell script to say


Good
morning/Afternoon/Evenin
g as you log in to system.

7. Write a C program to
create a child process
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
8. Finding out biggest
number from given
three numbers
supplied as command
line arguments

9. Printing the patterns


using for loop.

10. Shell script to


determine whether
given file exist or not.

11. Write a program for


process creation
using C. (Use of gcc
compiler).

12. Implementation of
FCFS &Round Robin
Algorithm.

13. Implementation of
Banker’s Algorithm.
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

List of Practicals:
1. Study of Basic commands of Linux.
2. Study the basics of shell programming.
3. Write a Shell script to print given numbers sum of all digits.
4. Write a shell script to validate the entered date. (eg. Date format is: dd-mm-yyyy).
5. Write a shell script to check entered string is palindrome or not.
6. Write a Shell script to say Good morning/Afternoon/Evening as you log in to
system.
7. Write a C program to create a child process
8. Finding out biggest number from given three numbers supplied as command line
arguments
9. Printing the patterns using for loop.
10. Shell script to determine whether given file exist or not.
11. Write a program for process creation using C. (Use of gcc compiler).
12. Implementation of FCFS & Round Robin Algorithm.
13. Implementation of Banker’s Algorithm.
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

PRACTICAL NO: 1

Definition: Study of Basic commands of Linux/UNIX.

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.

BASH (Bourne Again Shell)

 Bash is a shell written as a free replacement to the standard Bourne Shell (/bin/sh) originally
written by Steve Bourne for UNIX systems.

 It has all of the features of the original Bourne Shell, plus additions that make it easier to
program with and use from the command line.

 Since it is Free Software, it has been adopted as the default shell on most Linux systems.

BASIC LINUX COMMANDS:


1. PWD: Print Working Directory
DESCRIPTION: pwd prints the full pathname of the current working directory.

SYNTAX: pwd

EXAMPLE: $ pwd /home/directory_name

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
1
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

EXAMPLE: To change into “work directory” in “documents” need to write as follows.

Input: $ cd /documents/work

3. 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

2
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
4. 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

EXAMPLE: Create a directory named work.

5.touch :command in Unix/Linux is used to create an empty file or update the timestamp
of an existing file.
SYNTAX: touch [options] file
EXAMPLE : touch newfile.txt

6. vi (Text Editor)

Description:
vi is a text editor used to create or modify text files in a terminal.

Syntax:vi [filename]

Example:vi myfile.txt This opens myfile.txt in the vi editor.

7. id (User Identity)

Description:
The id command is used to print user and group information for the current user or a specified user.

Syntax: id [username]

3
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

8.rmdir (Remove Files or Directories)


Discription :

The rm command is used to remove files or directories in Linux/Unix systems.

Syntax:
rm [OPTION] FILE...
Example:

 Remove a single fifille: rmdir

9. 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

10.head
DESCRIPTION: head, by default, prints the first 10 lines of each FILE to standard output.
4
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
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]

EXAMPLE:
Display the first ten lines of myfile.txt.

head myfile.txt

11.tail

DESCRIPTION: tail is a command which prints the last few number of lines (10 lines by
default) of a certain file, then terminates.
SYNTAX: tail [option]…[file/directory]

EXAMPLE: Output the last 100 lines of the file myfile.txt.

tail myfile.txt -n 100

12.chmodThe chmod command in Unix/Linux is used to change the permissions of files or directories.
SYNTAX: chmod [options] mode file
Example : chmod +X file.txt

5
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

13. 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: mv file.txt source directory
 Moves the file myfile.txt to the directory destination-directory. mv myfile.txt
destination_directory  Move the file myfile.txt into the parent directory.
mv myfile.txt ../

14. man

DESCRIPTION: Displays on online manual page or manpage.


SYNTAX: man command
EXAMPLE: To learn about listing files

15. wc
DESCRIPTION: wc (word count) command, can return the number of lines, words, and characters in a file.

SYNTAX: wc
EXAMPLE:

 Print the byte counts of file myfile.txt

wc -c myfile.txt
 Print the line counts of file myfile.txt

wc -l myfile.txt

6
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
 Print the word counts of file myfile.txt

wc -w myfile.txt

16. whoami

DESCRIPTION: whoami prints the effective user ID.

This command prints the username associated with the current effective user ID.

SYNTAX: whoami [option]

EXAMPLE: Display the name of the user who runs the command.
Whoami

17.history

Description : shows last runned commands

Syntax: history

Example: Display the entire command history:historyThis shows a list of previously executed
commands

7
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

18. hostname

The hostname command in Unix/Linux is used to display or set the system's hostname.

Syntax: hostname [options] [name]

Example 1: Displaying the current hostname

19. more (View File Contents Page by Page)

Description: The more command is used to view the contents of a file one page at a time.

Syntax: more [filename]

Example: more file.txt

20 Nl :

The nl command in Unix/Linux is used to number the lines of a file or input stream. It displays the content of
a file with line numbers.

Syntax: nl [options] file

Example: nl filename.txt

8
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
21 . diff (Repeat of Previous Command)

This is the same as the previous one, so it follows the same explanation.

Description:
diff compares two files and outputs their differences.

Syntax:diff [options] file1 file2

Example:diff file1.txt file2.txt

22. ECHO
● Description: Display text on the screen
● Syntax: Display text on the screen
● Example : echo “text” >filename

23. FREE
● Description: To display the RAM details in Linux machine need to write following
command.
● Syntax: free

24. ifconfig
* Description : to display ip address and mac address
* syntax : ifconfig

9
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

25. Clear

Description : to clear the history


syntax :clear

26.
nmap:
Description: for scanning the network connected device using ip address.
Syntax: nmap ipaddress

27.Ping : Host to Check Connectivity


Description: Sends ICMP echo requests to a network host to check its availability
Syntax: ping <hostname-or-IP>

10
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

28. ps aux : View Running Processes


Description: Displays a list of currently running processes on the system.
Syntax: . ps aux

29. DF: Check Disk Usage


Description: Shows the amount of disk space used and available on your system.
Syntax: df -h

30 . top : display real-time system information, primarily about processes running on your
system
Description: show cpu and processes running in your system
Syntax: top

11
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

PRACTICAL NO: 2

I ) take manual input than print :

#!/bin/bash

num1=8

num2=6

let sum=num1+num2

let difference=num1-num2

let product=num1*num2

let quotient=num1/num2

echo "Sum of $num1 and $num2 is: $sum"

echo "Difference of $num1 and $num2 is: $difference"

echo "Product of $num1 and $num2 is: $product"


12
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
echo "Quotient of $num1 and $num2 is: $quotient"

output :

13
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

II ) USER INPUT THAN PRINT :

#!/bin/bash

echo "Enter the first number:"

read num1

echo "Enter the second number:"

read num2

let sum=num1+num2

let difference=num1-num2

let product=num1*num2

let quotient=num1/num2

echo "Sum of $num1 and $num2 is: $sum"

echo "Difference of $num1 and $num2 is: $difference"

echo "Product of $num1 and $num2 is: $product"

echo "Quotient of $num1 and $num2 is: $quotient"

output :

14
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149Avani Shukla
PRACTICAL NO: 3

I) If- else program :

#!/bin/bash

number=7

if (( number % 2 == 0 )); then

echo "$number is even."

else

echo "$number is odd."

Fi

Output :

2. If – elif : #!/bin/bash

echo "Enter a number between 1 and 5:"

read num

if [ $num -eq 1 ]; then

echo "You entered One."

elif [ $num -eq 2 ]; then

echo "You entered Two."

elif [ $num -eq 3 ]; then

15
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149Avani Shukla
echo "You entered Three."

elif [ $num -eq 4 ]; then

echo "You entered Four."

elif [ $num -eq 5 ]; then

echo "You entered Five."

else

echo "Invalid number! Please enter a number between 1 and 5."

Fi

Output:

III) for loop :


#!/bin/bash
number=12345
sum=0
for (( i=0; i<${#number}; i++ ));
do
digit=${number:$i:1}
sum=$((sum + digit))
done
echo "The sum of all digits is: $sum"
output:

16
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149Avani Shukla
IV) While loop :

#!/bin/bash
echo "Enter a number:"
read num
i=1
while [ $i -le $num ]
do
echo "Number $i"
((i++))
Done

Output :

IV) Until loop :


#!/bin/bash
echo "Enter a number:"
read num
i=1
until [ $i -gt $num ]
do
echo "Number $i"
((i++))
Done

17
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149Avani Shukla
Output :

VI ) switch case :
echo "Enter your choice:"
echo "a : Enter your date"
echo "b : Check ink office"
echo "c : Check scheduling directions"

read choice

case $choice in
a)
date
;;
b)
ls
;;
c)
pwd
;;
*)
echo "Invalid choice"
;;
esac

Output :

18
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149Avani Shukla

vii) write a program to print the given number is odd or even

input:

#!/bin/bash

number=7

if (( number % 2 == 0 )); then

echo "$number is even."

else

echo "$number is odd."

Fi

Output :

19
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149Avani Shukla
viii) write a program to calculate the sum of all digits

input :

#!/bin/bash

number=12345

sum=0

for (( i=0; i<${#number}; i++ ));

do

digit=${number:$i:1}

sum=$((sum + digit))

done

echo "The sum of all digits is: $sum"

output :

20
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149Avani Shukla

PRACTICAL NO: 4

Definition: 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 it is a leap year then the no. days will be 29. Bychecking
these conditions for days, month and year using various control statements of Linux can validate the
date entered by the user. (eg. Date format is: dd-mm-yyyy)

Flowchart:

Set 1: Read a string from the user and if it is hello then print “Hello yourself”, if it is bye then print
“See you again” otherwise print “Sorry, I don't understand" using case statement.

Syntax: bash case statement.

21
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
case expression
in
pattern1 )
statements ;;
pattern2 )
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:
dd=0
mm=0
yy=0

days=0
echo -n "Enter day (dd) : "
read dd

echo -n "Enter month (mm) : "


read mm

echo -n "Enter year (yyyy) : "


read yy

if [ $mm -le 0 -o $mm -gt 12 ];


then
echo "$mm is invalid month."
exit 1
fi
case $mm in
1) days=31;;
2) days=28 ;;
3) days=31 ;;
4) days=30 ;;
5) days=31 ;;
6) days=30 ;;
7) days=31 ;;
8) days=31 ;;
9) days=30 ;;
10) days=31 ;;
11) days=30 ;;
12) days=31 ;;

22
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
*) days=-1;;
esac
if [ $mm -eq 2 ]; # if it is feb month then only check of leap year
then
if [ $((yy % 4)) -ne 0 ] ; then
: # not a leap year : means do nothing and use old value of days
elif [ $((yy % 400)) -eq 0 ] ; then
# yes, it's a leap year
days=29
elif [ $((yy % 100)) -eq 0 ] ; then
: # not a leap year do nothing and use old value of days
else
# it is a leap year
days=29
fi
fi

if [ $dd -le 0 -o $dd -gt $days ];


then
echo "$dd day is invalid"
exit 3

echo "$dd/$mm/$yy is a vaild date"

OUTPUT

23
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

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.

Flowchart:

Set 1: Extract the second character of the entered string.

Sample Code:

24
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

echo “Enter a String”

read str
k=`echo $str | cut -c 2` echo “$k”

Input:

#!/bin/bash
echo "Enter a String"
read input
reverse=""
len=${#input}
for (( i=$len-1; i>=0; i-- ))
do
reverse="$reverse${input:$i:1}"
done
if [ $input == $reverse ]
then
echo "$input is palindrome"
else
echo "$input is not palindrome"
fi

INPUT: ligil
OUTPUT

Set 2:Write a program to check given num is palindrome or not.

#!/bin/bash
echo "Enter a number:"
read num
temp=$num
rev_num=0

while [ $num -gt 0 ]


do
digit=$((num % 10))
rev_num=$((rev_num * 10 + digit))
num=$((num / 10))

25
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
done

if [ $temp -eq $rev_num ]; then


echo "$temp is a palindrome number."
else
echo "$temp is not a palindrome number."
Fi
INPUT: 565

Output:

26
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

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.

Flowchart:

Set 1: Read the minutes from system.

Sample Code:

minutes=`date + M%`
echo $minutes

27
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

Set 2: Read the hours from system using cut command of unix.

Sample Code:

clear
hours=`date | cut -c12-13`
echo "the value of hour is $hours"

Input: -

Output:

the value of hour is 4

Set 3: Write a Shell script to say Good morning/Afternoon/Evening as you log in to system.

Syntax
h=$(date +"%H")
if [ $h -gt 6 -a $h -le 12 ]
then
echo good morning
elif [ $h -gt 12 -a $h -le 16 ]
then
echo good afternoon
elif [ $h -gt 16 -a $h -le 20 ]
then
echo good evening
else
echo good night
fi

28
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
2) Read the minutes hours using date command.

#!/bin/bash
# Get the current hour (24-hour format), minute, and second
hour=$(date +"%H") # Gets hours (00-23)
minute=$(date +"%M") # Gets minutes (00-59)
second=$(date +"%S") # Gets seconds (00-59)

# Modulo operations
hour_mod=$((hour % 12)) # Convert 24-hour format to 12-hour format
minute_mod=$((minute % 10)) # Example: Get last digit of minutes
second_mod=$((second % 10)) # Example: Get last digit of seconds

# Display results
echo "Current Time: $hour:$minute:$second"
echo "Hour Modulo 12: $hour_mod"
echo "Minute Modulo 10: $minute_mod"
echo "Second Modulo 10: $second_mod"

Output :

29
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

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.

Fork system call is used for creating a new process, which is called child process, which runs
concurrently with the process that makes the fork() call (parent process). After a new child
process is created, both processes will execute the next instruction following the fork() system
call. A child process uses the same pc(program counter), same CPU registers, same open files
which use in the parent process.

Flowchart:

Set 1: Create a child process using one fork( ), getpid( ), getppid( ).

INPUT:

30
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
// make two process which run same
// program after this instruction
fork();
printf("Aum Patel!\n");
return 0;
}

OUTPUT:

Set 2.
INPUT:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
fork();
fork();
fork(); printf("Avani Shukla\n");
return 0;
}
OUTPUT:

31
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
**The number of times ‘hello’ is printed is equal to number of process
created. Total Number of Processes = 2n, where n is number of fork
system calls. So here n = 3, 23 = 8.

Set 3
INPUT:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void forkexample() {
// child process because return value zero
if (fork() == 0)
printf("Hello from Child!\n");

// parent process because return value non-zero.


else
printf("Hello from Parent!\n"); }
int main()
{ forkexample();
return 0; }

OUTPUT:

Set 1: Create a child process using one fork( ), getpid( ), getppid( ). Sample Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
main() {
intpid ;
printf("I'am the original process with PID %d and PPID %d.\n", getpid(), getppid()) ;
32
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

pid = fork ( ) ; /* Duplicate. Child and parent continue from here */


if ( pid != 0 ) /* pid is non-zero,so I must be the parent*/ {
printf("I'am the parent with PID %d and PPID %d.\n", getpid(), getppid()) ;
printf("My child's PID is %d\n", pid ) ; }
else /* pid is zero, so I must be the child */ {
sleep(4); /* make sure that the parent terminates first */
printf("I'm the child with PID %d and PPID %d.\n",getpid(), getppid()) ; }
printf ("PID %d terminates.\n", getpid()) ; }

INPUT :
I'am the original process with PID 455 and PPID 448.
I'am the parent process with PID 455 and PPID 448.
My child's PID is 456

OUTPUT :

33
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

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 of
command line argument.

Flowchart:

Show the use of command line argument for finding the biggest number among three
numbers using if else ladder.

34
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
INPUT:
echo "Enter Num1"
read num1
echo "Enter Num2"
read num2
echo "Enter Num3"
read num3
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]
then
echo $num1
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]
then
echo $num2
else
echo $num3
fi
OUTPUT:

35
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
2) Biggest num in given two number.

#!/bin/bash
# Biggest number from two numbers
echo "Enter first number:"
read num1
echo "Enter second number:"
read num2

if [ $num1 -gt $num2 ]; then


echo "$num1 is the biggest number."
else
echo "$num2 is the biggest number."
fi

INPUT:10
20

Output :

36
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

PRACTICAL NO: 9

Definition: Print the pattern using for loop which is used to do the same thing again and again until
some condition is satisfied.

For loop is used to do the same thing until some condition is there.

Flowchart:

Code:

#Here $1 is the parameter you passed. It specifies the no. of rows i,e “ / ”
# Static input for N
N=5
i=0
j=0
while [ $i -le `expr $N - 1` ]
do

37
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
j=0
while [
$j -le `expr $N - 1` ]
do
if [ `expr $N - 1` -le `expr $i + $j` ]
then
# Print the pattern
echo -ne "/"
else
# Print the spaces required
echo -ne " "
fi
j=`expr $j + 1`
done

# For next line


echo
i=`expr $i + 1`
done

Input:
/

38
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

PRACTICAL NO: 10

Definition: Various commands are available in Linux to check whether the given directory or file are
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:

39
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

Code:

Shell script to determine whether given directory, exist or not.

#!/bin/bash
File=read_file.txt
if [ -f "$File" ]; then
echo "$File exist"
else
echo "$File does not exist"
fi

Output:

40
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

PRACTICAL :11

Write a program for process creation using C. (Use of gcc compiler).


Let us consider a simple program.

File name: basicfork.c

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main() {

fork();

printf("Called fork() system call\n");

return 0;

}
Execution Steps

Compilation

gcc basicfork.c -o basicfork

Execution/Output

Called fork() system call

Called fork() system call

Note − Usually after fork() call, the child process and the parent process would perform different
tasks. If the same task needs to be run, then for each fork() call it would run 2 power n times, where
n is the number of times fork() is invoked.

41
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

In the above case, fork() is called once, hence the output is printed twice (2 power 1). If fork() is
called, say 3 times, then the output would be printed 8 times (2 power 3). If it is called 5 times,
then it prints 32 times and so on and so forth.

Having seen fork() create the child process, it is time to see the details of the parent and the child
processes.

File name: pids_after_fork.c

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main() {

pid_t pid, mypid, myppid;

pid = getpid();

printf("Before fork: Process id is %d\n", pid);

pid = fork();

if (pid < 0) {

perror("fork() failure\n");

return 1;

// Child process

if (pid == 0) {

printf("This is child process\n");

mypid = getpid();

myppid = getppid();

printf("Process id is %d and PPID is %d\n", mypid, myppid);

} else { // Parent process


sleep(2);

printf("This is pa rent process\n");


42
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
mypid = getpid();

myppid = getppid();

printf("Process id is %d and PPID is %d\n", mypid, myppid);

printf("Newly created process id or child pid is %d\n", pid);

return 0;

INPUT : 2520

OUTPUT:

A process can terminate in either of the two ways −

 Abnormally, occurs on delivery of certain signals, say terminate signal.

 Normally, using _exit() system call (or _Exit() system call) or exit() library function.

The difference between _exit() and exit() is mainly the cleanup activity. The exit() does some
cleanup before returning the control back to the kernel, while the _exit() (or _Exit()) would return
the control back to the kernel immediately.

43
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
PRACTICAL NO: 12
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 processes
by the order of their arrival.

Flowchart:

Set 1: How to take Arrival Time and Burst Time of the processes.

Sample Code:

#include<stdio.h>
int main()
{
int n,count;
int at[10],bt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
for(count=0;count<n;count++)

44
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
printf("Enter Arrival Time and Burst Time for Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
}
return 0;
}

Input:

Enter Total Process: 4

OUTPUT:

Set 2: How to sort processes based on their Arrival Time for 5 processes.

Sample Code:

#include <stdio.h>
int main() {
int i, temp, j, f, array[5];
printf("Enter 5 Processes: ");
for (i = 0; i < 5; i++) {
scanf("%d", &array[i]);
}
for (i = 0; i < 5; i++) {
f = 1;
for (j = 0; j < 4 - i; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
f = 0;
}
}
if (f == 1)
break;
}
printf("After sorting: ");
for (i = 0; i < 5; i++) {
printf("%d ", array[i]);
}

45
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
return 0;
}

INPUT: 9

OUTPUT:

46
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

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. Each 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.

Flowchart:

Set 1: How to take Arrival Time, Burst Time and Time Quantum of the processes.

Sample Code:
#include <stdio.h>

47
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
int main()
{

int n,count, tq;


int at[10],bt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
}
printf("Enter Time Quantum:\t ");
scanf("%d",&tq);
return 0;
}

Input:

Enter Total Process: 3

Output:

48
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

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.
49
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla

Code:

#include <stdio.h>
#include <stdlib.h>

int main()
{ int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10]; int p, r, i, j, process, count;
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]); }
printf("\n\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]); }
printf("\n\nEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
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");
}}

Set 3: Perform the Banker’s Algorithm and find out that whether the System is Safe or not and also print the Safe
Sequence.
Safety Algorithm:
1. Let Work and Finish be vectors of length m and n, respectively. Initially,

Work = Available
Finish[i] =false for i = 0, 1, ... , n - 1.
This means, initially, no process has finished and the number of available resources is
represented by the Available array.

2. Find an index i such that both


50
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
2303051050149 Avani Shukla
Finish[i] ==false
Needi <= Work
If there is no such i present, then proceed to step 4.
It means, we need to find an unfinished process whose need can be satisfied by the available
resources. If no such process exists, just go to step 4.

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.

4. If Finish[i] == true for all i, then the system is in a safe state.

That means if all processes are finished, then the system is in safe state.

51

You might also like