LabManual CSE325
LabManual CSE325
LabManual CSE325
II
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
III
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
PO9:: Function effectively as an individual, and as a member or leader in diverse teams, and in
multidisciplinary settings.
PO10:: Communicate effectively on complex engineering activities with the engineering community
and with society at large, such as, being able to comprehend and write effective reports and
design documentation, make effective presentations, and give and receive clear instructions.
PO11:: Demonstrate knowledge and understanding of the engineering and management principles
and apply these to one’s own work, as a member and leader in a team, to manage projects
and in multidisciplinary environments.
PO12:: Recognize the need for, and have the preparation and ability to engage in independent and
lifelong learning in the broadest context of technological change.
PEO2:: The graduates shall undertake higher education or global certifications or exhibit impactful
research accomplishment.
PEO3:: The graduate shall extend global expertise in technology development and deployment by
becoming an entrepreneur, consultant and innovator.
PEO4:: Graduates shall embrace ethics and lifelong learning to adapt to a fast-changing world and
enhance global employability in diverse work environments.
IV
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Contents
1 Experiment 1: Introduction to Linux Commands 1
1.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Reference Material[1][3][2] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2.1 Important Linux Commands for File and Directory Management . . . . . . . 1
1.2.2 Shell Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.3 Types of Shell Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.4 Linux File System Hierarchy[2] . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.5 Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.6 Types of Files in Linux or Unix Environment . . . . . . . . . . . . . . . . . . 3
1.2.7 File Permissions and the chmod Command . . . . . . . . . . . . . . . . . . . . 3
1.3 Lab Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Sample Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5 Evaluation Parameters and Rating Scale . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5.1 Student’s Self Rating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5.2 Teacher’s Rating Student Performance . . . . . . . . . . . . . . . . . . . . . . 8
V
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
VI
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
7.2.4 Mutex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
7.3 Sample Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
7.4 Lab Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
7.5 Sample Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
7.6 Evaluation Parameters and Rating Scale . . . . . . . . . . . . . . . . . . . . . . . . . 48
7.6.1 Student’s Self Rating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
7.6.2 Teacher’s Rating Student Performance . . . . . . . . . . . . . . . . . . . . . . 48
VII
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Expression of Gratitude
I extend my deepest gratitude to both of you for your invaluable support and guidance through-
out the creation of the CSE325: Operating Systems Laboratory manual. Your unwavering
encouragement, expertise, and mentorship have been pivotal in crafting a comprehensive resource
for our students.
Your commitment to academic excellence and dedication to our learning community have been
inspirational. Your visionary leadership and continuous support have significantly enriched our
educational experiences and have empowered us to produce this manual that will serve as a valuable
asset for our students’ education.
I am profoundly grateful for the opportunities you have provided us and the trust you have
placed in our abilities. Your impactful contributions continue to shape the School of Computer
Science & Engineering at Lovely Professional University and nurture an environment of growth and
learning.
Once again, thank you for your profound guidance and belief in our endeavors. Your support
has been instrumental, and I am honored to have had the privilege of working under your guidance.
VIII
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
IX
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
– ”J/E grading will be based on the student’s performance during the CAP day, marked
out of 50.”
– ”LM assessment will be out of 50, determined by the teacher’s average overall rating
from the evaluation of the previous two experiments in the lab manual. The average
overall rating will be multiplied by 5.”
• There will be total 4 continuous assessment practical (CAP) conducted during the semester
(100 marks each).
• Best 3 out of 4 will be considered in grade calculation by the end of the semester.
• During the CAPs, students must upload their solutions in DOC or PDF format both at
https://forms.gle/MmLhykcUVvVf579U9 and on the UMS platform.
X
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Installation of Linux
There are various ways in which Linux can be installed in a system:
1. Windows Subsystem for Linux (WSL)
Video reference: https://youtu.be/wjbbl0TTMeo?si=DAPNc2NevQeOaRpD
2. Virtual Machines
Using VMWare: https://youtu.be/Q0Na0f1NtpA?si=BlhtMiQvbK5JAv5u
Using Virtual Box: https://youtu.be/hYaCCpvjsEY?si=HO8PCLVF9HzkQfdi
3. Dual Boot
Video reference: https://youtu.be/MPMnizrPvHE?si=JUnIyOp0XGGfmEJY
XI
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Lab Experiments
1 Experiment 1: Introduction to Linux Commands
1.1 Objective
The objective of this lab experiment is to introduce students to fundamental Linux commands
used for navigating the file system, managing files and directories, and performing basic system
operations. By the end of this experiment, students should be familiar with commonly used com-
mands such as ls, cd, mkdir, rm, cp, and mv, gaining a foundational understanding of the Linux
command-line interface.
1
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
1.2.5 Paths
Paths refer to the location or address of a file or directory in the file system.
2
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Types of paths
(i) Absolute Path: An absolute path defines the complete location of a file or directory starting
from the root directory (/). It includes the entire directory hierarchy from the root directory
to the specific file or directory. For instance, /home/user/documents/file.txt is an absolute
path where the file.txt is located in the ’documents’ directory inside the ’user’ directory within
the ’home’ directory, starting from the root (/) directory.
(ii) Relative Path: A relative path defines the location of a file or directory with respect to the cur-
rent working directory. It doesn’t start from the root directory but refers to a location relative
to the current directory. For example, if the current directory is /home/user/, a file located
in the ’documents’ directory can be referenced using a relative path like documents/file.txt.
- r w x r - x r - x
| | | | | | |
| | | | | | +---- Others (permissions for users not covered by owner or group)
| | | | +---------- Group (File group permissions for users)
| | +------------------ Owner (permissions for the file or directory owner)
3
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
| +-------------------- Type of the file (e.g., - for a regular files, d for a directories)
+------------------------ Special permission bits (e.g., s, t, etc.)
Each group of permissions (Owner, Group, Others) consists of three characters representing
read (r), write (w), and execute (x) permissions. If a permission is allowed, the respective character
is displayed, and if it’s denied, a hyphen (-) is shown.
For example:
In this example: - The first character (-) indicates that it’s a regular file. - The next three
characters (rw-) represent the owner’s permissions (read and write, but not execute). - The following
three characters (r–) represent the group’s permissions (read-only). - The last three characters (r–)
represent permissions for others (read-only).
These permissions can be changed using commands like chmod in Linux to alter the read, write,
and execute permissions for the owner, group, and others.
File Permissions
In Unix or Linux, each file and directory has associated permissions that determine who can read,
write, or execute them. These permissions are divided into three categories:
4
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
chmod Command
The chmod command is used to change the file permissions. There are two ways to specify the
permissions: symbolic mode and numeric (octal) mode.
Symbolic Mode
In symbolic mode, you specify the permissions using characters. The syntax is:
chmod [user][operator][permissions] file
Examples:
chmod u+x file # Add execute permission for the owner
chmod g-w file # Remove write permission for the group
chmod o=r file # Set read-only permission for others
chmod a+rw file # Add read and write permissions for everyone
5
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
• 0: No permissions.
Examples:
chmod 755 file # rwxr-xr-x (owner can read, write, execute; group can read, execute; others can
chmod 644 file # rw-r--r-- (owner can read, write; group can read; others can read)
chmod 600 file # rw------- (owner can read, write; no permissions for group and others)
Video Reference:
https://youtube.com/playlist?list=PLWjmN065fOfGdAZrlP6316HVHh8jlvefD
6
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
(d) Use ls -li to display the inode numbers of original file.txt, symlink to original, and
hardlink to original. Observe and explain the differences in their inode numbers.
(e) Edit original file.txt and add a new line of content.
(f) Display the contents of symlink to original and hardlink to original to verify that
the changes made to original file.txt are reflected in both links.
(g) Delete original file.txt and verify the existence of symlink to original and hardlink to original.
Explain the outcome based on the nature of symbolic and hard links.
Exercise 4: (a) Create a new directory named grep practice in your home directory. Inside
this directory, create a text file named sample.txt with at least 50 lines of text.
Ensure the file contains a mix of sentences, some of which include the word
”Linux”.
(b) Use the grep command to find all lines in sample.txt that contain the word
”Linux”. Display the results on the terminal.
(c) Modify the grep command to be case-insensitive and find all lines containing the
word ”linux” in sample.txt.
(d) Use the grep command with the -n option to display the line numbers of all
occurrences of the word ”Linux” in sample.txt.
(e) Use the grep command with the -v option to display all lines in sample.txt
that do not contain the word ”Linux”.
(f) Use the grep command with the -c option to count the number of lines in
sample.txt that contain the word ”Linux”.
Exercise 5: (a) Create a file named permissions file.txt in your home directory.
(b) Change the file permissions of permissions file.txt to be readable, writable, and
executable by the owner, but readable only by others. Use chmod to set these
permissions.
(c) Verify the permissions using ls -l.
(d) Change the ownership of permissions file.txt to a user named newuser and a
group named newgroup using chown.
(e) Verify the ownership change using ls -l.
7
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Overall Rating 1 2 3 4 5 6 7 8 9 10
Viva Performance 1 2 3 4 5 6 7 8 9 10
Lab-manual Readiness 1 2 3 4 5 6 7 8 9 10
Overall Rating 1 2 3 4 5 6 7 8 9 10
8
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
(i) Environment Variables: These are variables that contain information about the environment
in which the shell operates, such as user settings, system paths, or configuration preferences.
(ii) User-Defined Variables: These are variables created by users to store custom data or infor-
mation required for specific tasks or scripts.
9
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
• Standard Input Redirection (<): Changes the command’s input source to a file.
• Standard Output Redirection (>): Redirects command output to a file (overwrites ex-
isting content).
command1 | command2
10
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
• expr Command: Used for integer arithmetic operations within shell scripts. It evaluates
and prints the result of expressions.
Example usage:
Supported operators in expr include addition (+), subtraction (-), multiplication (*), division
(/), modulus (
• bc Command: Stands for ’Basic Calculator’ and supports floating-point arithmetic and
advanced mathematical functions.
Example usage:
bc handles floating-point arithmetic and provides functions like sine, cosine, square root, etc.,
for more complex calculations.
These commands offer basic arithmetic functionalities within shell scripts. expr is suitable
for simple integer arithmetic, while bc provides a broader range of mathematical operations and
supports floating-point numbers.
1. Basic if Statement:
if [ condition ]; then
# Commands to execute if the condition is true
fi
2. if-else Statement:
if [ condition ]; then
# Commands to execute if the condition is true
else
# Commands to execute if the condition is false
fi
11
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
3. if-elif-else Statement:
if [ condition1 ]; then
# Commands to execute if condition1 is true
elif [ condition2 ]; then
# Commands to execute if condition2 is true
else
# Commands to execute if both condition1 and condition2 are false
fi
4. Nested if Statements:
if [ condition1 ]; then
if [ condition2 ]; then
# Commands to execute if both condition1 and condition2 are true
fi
fi
These variations enable conditional execution of commands based on different conditions within
shell scripts, offering flexibility in controlling the flow of the script.
1. Arithmetic Operators
• +, -, *, /, %: Perform addition, subtraction, multiplication, division, and modulus respectively.
2. Relational Operators
• -eq, -ne, -gt, -lt, -ge, -le: Compare numbers (equal, not equal, greater than, less than,
greater than or equal to, less than or equal to) within test or [ ] brackets.
12
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
3. String Operators
• =, !=, <, >: Compare strings (equal, not equal, less than, greater than) within test or [ ]
brackets.
4. Logical Operators
• &&, ||, !: Perform logical AND, logical OR, and logical NOT operations respectively.
5. Assignment Operators
• =, +=: Assign values to variables or concatenate strings.
6. Bitwise Operators
• &, |, ^, <<, >>, ~: Perform bitwise AND, OR, XOR, left shift, right shift, and bitwise NOT
operations respectively.
Understanding and utilizing these operators enable the creation of conditions, calculations,
string manipulations, and file property checks within shell scripts, enhancing their functionality
and flexibility.
case variable in
pattern1)
# Commands to execute if variable matches pattern1
;;
pattern2)
# Commands to execute if variable matches pattern2
;;
pattern3|pattern4)
# Commands to execute if variable matches pattern3 or pattern4
;;
*)
# Default commands to execute if no pattern matches
;;
esac
13
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Example:
fruit="apple"
case $fruit in
apple)
echo "It’s an apple."
;;
banana|orange)
echo "It’s a banana or an orange."
;;
*)
echo "It’s another fruit."
;;
esac
This example evaluates the variable $fruit against different patterns and executes respective
blocks based on the matching pattern.
The case structure simplifies code readability when multiple conditions need evaluation against
a single variable or expression.
1. for Loop
The for loop iterates through a list of items or values. It is suitable when you have a known set of
elements to loop through.
Syntax:
for variable in list
do
# Commands to execute for each iteration
done
Example:
for i in 1 2 3 4 5
do
echo "Iteration: $i"
done
2. while Loop
The while loop executes a block of code as long as a specified condition remains true.
Syntax:
14
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
while [ condition ]
do
# Commands to execute as long as the condition is true
done
Example:
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
((count++))
done
3. until Loop
The until loop executes a block of code until a specified condition becomes true.
Syntax:
until [ condition ]
do
# Commands to execute until the condition becomes true
done
Example:
count=1
until [ $count -gt 5 ]
do
echo "Count: $count"
((count++))
done
4. Nested Loops
You can nest loops within each other to create more complex control structures.
Example:
for i in {1..3}
do
echo "Outer Loop Iteration: $i"
for j in A B C
do
echo " Inner Loop Iteration: $j"
done
done
These loops in shell scripting provide various ways to iterate through data, execute code repeat-
edly based on conditions, and control the flow of a script.
15
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
• Write the desired commands and save the file with a .sh extension (e.g., script.sh).
chmod +x script.sh
– Using Bash:
bash script.sh
4. Verify Output:
• After executing the script, verify the output or actions performed by the script in the terminal
or through any generated files or changes made by the script.
16
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
echo $?
An exit code of 0 generally indicates success, while other codes signify different types of errors
or warnings.
Video Reference:
https://youtube.com/playlist?list=PLWjmN065fOfGdAZrlP6316HVHh8jlvefD
17
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Question 3: Discuss different types of operators used in shell scripting (arithmetic, comparison,
logical, etc.).
Question 4: What are variables in shell scripting? How do you declare and use them?
Question 5: What are the different types of shells commonly used in Unix/Linux?
Overall Rating 1 2 3 4 5 6 7 8 9 10
Viva Performance 1 2 3 4 5 6 7 8 9 10
Lab-manual Readiness 1 2 3 4 5 6 7 8 9 10
Overall Rating 1 2 3 4 5 6 7 8 9 10
18
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
19
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
20
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
int main ( ) {
int f i l e D e s c r i p t o r ;
c l o s e ( f i l e D e s c r i p t o r ) ; // C l o s e t h e f i l e
return 0 ;
}
int main ( ) {
char b u f f e r [ BUFFER SIZE ] ;
// 0 i s t h e f i l e d e s c r i p t o r o f s t a n d a r d i n p u t .
s s i z e t bytesRead = r e a d ( 0 , b u f f e r , BUFFER SIZE ) ;
w r i t e ( 1 , b u f f e r , bytesRead ) ; // 1 i s t h e f i l e d e s c r i p t o r o f s t a n d a r d o u t p u t .
return 0 ;
}
int main ( ) {
char data [ ] = ” This data w i l l be appended t o t h e f i l e . \ n” ;
int f i l e D e s c r i p t o r ;
21
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
close ( fileDescriptor );
return 0 ;
}
int main ( ) {
char b u f f e r [ BUFFER SIZE ] ;
int readFd , writeFd ;
s s i z e t bytesRead ;
while ( ( bytesRead = r e a d ( readFd , b u f f e r , BUFFER SIZE ) ) > 0 ) {
w r i t e ( writeFd , b u f f e r , bytesRead ) ;
}
c l o s e ( readFd ) ;
c l o s e ( writeFd ) ;
return 0 ;
}
5. A C program that reads characters from the 11th to the 20th position from a file
named ”input.txt” using the lseek system call.
int main ( ) {
int f i l e D e s c r i p t o r ;
char b u f f e r [ BUFFER SIZE ] ;
f i l e D e s c r i p t o r = open ( ” i n p u t . t x t ” , O RDONLY) ;
22
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
l s e e k ( f i l e D e s c r i p t o r , 1 0 , SEEK SET ) ;
r e a d ( f i l e D e s c r i p t o r , b u f f e r , BUFFER SIZE − 1 ) ;
b u f f e r [ BUFFER SIZE − 1 ] = ’ \0 ’ ;
p r i n t f ( ” C h a r a c t e r s from 11 th t o 20 th p o s i t i o n : %s \n” , b u f f e r ) ;
close ( fileDescriptor );
return 0 ;
}
int main ( ) {
// Name o f t h e f i l e t o be d e l e t e d
const char ∗ f i l e n a m e = ” s a m p l e f i l e . t x t ” ;
// Attempt t o d e l e t e t h e f i l e
i f ( u n l i n k ( f i l e n a m e ) == −1) {
// I f u n l i n k f a i l s , p r i n t an e r r o r message
p r i n t f ( ” Error d e l e t i n g the f i l e ” ) ;
return 1 ;
}
// Confirm s u c c e s s f u l d e l e t i o n
p r i n t f ( ” F i l e ’% s ’ s u c c e s s f u l l y d e l e t e d . \ n” , f i l e n a m e ) ;
return 0 ;
}
int main ( ) {
// Names o f t h e s o u r c e and d e s t i n a t i o n f i l e s
const char ∗ old name = ” o l d f i l e . t x t ” ;
const char ∗new name = ” n e w f i l e . t x t ” ;
23
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
// Attempt t o rename t h e f i l e
i f ( rename ( old name , new name ) != 0 ) {
// I f rename f a i l s , p r i n t an e r r o r message
p r i n t f ( ” E r r o r renaming t h e f i l e ” ) ;
return −1;
}
// Confirm s u c c e s s f u l renaming
p r i n t f ( ” F i l e renamed from ’% s ’ t o ’% s ’ . \ n” , old name , new name ) ;
return 0 ;
}
int main ( ) {
// Name o f t h e f i l e t o r e t r i e v e i n f o r m a t i o n a b o u t
const char ∗ f i l e n a m e = ” s a m p l e f i l e . t x t ” ;
// C r e a t e a s t r u c t s t a t t o h o l d f i l e i n f o r m a t i o n
struct s t a t f i l e S t a t ;
// R e t r i e v e f i l e s t a t u s i n f o r m a t i o n
i f ( s t a t ( f i l e n a m e , &f i l e S t a t ) != 0 ) {
// I f s t a t f a i l s , p r i n t an e r r o r message
p r i n t f ( ” Error r e t r i e v i n g f i l e information ” ) ;
return −1;
}
// P r i n t f i l e s t a t u s i n f o r m a t i o n
p r i n t f ( ” F i l e : %s \n” , f i l e n a m e ) ;
p r i n t f ( ” F i l e S i z e : %l d b y t e s \n” , f i l e S t a t . s t s i z e ) ;
p r i n t f ( ”Number o f L i n k s : %l d \n” , f i l e S t a t . s t n l i n k ) ;
return 0 ;
}
24
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Video Reference:
https://youtube.com/playlist?list=PLWjmN065fOfGdAZrlP6316HVHh8jlvefD
25
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Overall Rating 1 2 3 4 5 6 7 8 9 10
Viva Performance 1 2 3 4 5 6 7 8 9 10
Lab-manual Readiness 1 2 3 4 5 6 7 8 9 10
Overall Rating 1 2 3 4 5 6 7 8 9 10
26
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
• ino t d ino: This member represents the inode number of the directory entry.
• off t d off: It stores the offset of the next readdir call within the directory stream.
27
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
int main ( ) {
DIR ∗ d i r ;
struct d i r e n t ∗ e n t r y ;
dir = opendir ( ” . ” ) ;
if ( dir ) {
p r i n t f ( ” Contents o f t h e d i r e c t o r y : \ n” ) ;
while ( ( e n t r y = r e a d d i r ( d i r ) ) != NULL) {
p r i n t f ( ”%s \n” , entry −>d name ) ;
}
closedir ( dir );
}
return 0 ;
}
int main ( ) {
const char ∗ dirname = ” NewDirectory ” ;
28
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
return 0 ;
}
3. C Program to Demonstrate the Use of getcwd, chdir, and rmdir System Calls
int main ( ) {
char cwd [ 1 0 2 4 ] ; // B u f f e r t o s t o r e c u r r e n t w o r k i n g d i r e c t o r y
char ∗ o r i g i n a l d i r ;
// Get t h e c u r r e n t w o r k i n g d i r e c t o r y
i f ( getcwd ( cwd , s i z e o f ( cwd ) ) == NULL) {
p r i n t f ( ” E r r o r g e t t i n g c u r r e n t working d i r e c t o r y ” ) ;
return −1;
}
// C r e a t e a new d i r e c t o r y
i f ( mkdir (NEW DIR, 0 7 5 5 ) != 0 ) {
p r i n t f ( ” E r r o r c r e a t i n g new d i r e c t o r y ” ) ;
return −1;
}
// Change t o t h e new d i r e c t o r y
i f ( c h d i r (NEW DIR) != 0 ) {
p r i n t f ( ” E r r o r c h a n g i n g t o new d i r e c t o r y ” ) ;
return −1;
}
// P r i n t t h e new w o r k i n g d i r e c t o r y
i f ( getcwd ( cwd , s i z e o f ( cwd ) ) == NULL) {
p r i n t f ( ” E r r o r g e t t i n g new working d i r e c t o r y ” ) ;
return −1;
}
29
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
// Change b a c k t o t h e o r i g i n a l d i r e c t o r y
i f ( c h d i r ( ” . . ” ) != 0 ) {
p r i n t f ( ” E r r o r c h a n g i n g back t o o r i g i n a l d i r e c t o r y ” ) ;
return −1;
}
// Remove t h e new d i r e c t o r y
i f ( rmdir (NEW DIR) != 0 ) {
p r i n t f ( ” E r r o r removing new d i r e c t o r y ” ) ;
return −1;
}
return 0 ;
}
Video Reference:
https://youtube.com/playlist?list=PLWjmN065fOfGdAZrlP6316HVHh8jlvefD
30
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Overall Rating 1 2 3 4 5 6 7 8 9 10
Viva Performance 1 2 3 4 5 6 7 8 9 10
Lab-manual Readiness 1 2 3 4 5 6 7 8 9 10
Overall Rating 1 2 3 4 5 6 7 8 9 10
31
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
32
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
int main ( ) {
pid t child pid ;
// C r e a t e a c h i l d p r o c e s s
child pid = fork ( ) ;
i f ( c h i l d p i d == 0 ) {
// The c h i l d p r o c e s s code s e c t i o n
p r i n t f ( ” C h i l d p r o c e s s : PID = %d\n” , g e t p i d ( ) ) ;
} else i f ( c h i l d p i d > 0) {
// The p a r e n t p r o c e s s code s e c t i o n
p r i n t f ( ” Parent p r o c e s s : C h i l d PID = %d\n” , c h i l d p i d ) ;
} else {
// Fork f a i l e d
p r i n t f ( ” Fork f a i l e d \n” ) ;
return 1 ;
}
return 0 ;
}
int main ( ) {
pid t child pid = fork ( ) ;
i f ( c h i l d p i d == 0 ) {
// C h i l d p r o c e s s
p r i n t f ( ” C h i l d p r o c e s s : PID = %d\n” , g e t p i d ( ) ) ;
s l e e p ( 2 ) ; // S l e e p t o e n s u r e t h e p a r e n t p r o c e s s t e r m i n a t e s f i r s t
p r i n t f ( ” C h i l d p r o c e s s : My p a r e n t ’ s PID = %d\n” , g e t p p i d ( ) ) ;
} else i f ( c h i l d p i d > 0) {
// Parent p r o c e s s
p r i n t f ( ” Parent p r o c e s s : PID = %d\n” , g e t p i d ( ) ) ;
p r i n t f ( ” Parent p r o c e s s : Terminating . . . \ n” ) ;
33
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
} else {
p r i n t f ( ” Fork f a i l e d \n” ) ;
return 1 ;
}
return 0 ;
}
int main ( ) {
pid t child pid = fork ( ) ;
i f ( c h i l d p i d == 0 ) {
// C h i l d p r o c e s s
p r i n t f ( ” C h i l d p r o c e s s : PID = %d\n” , g e t p i d ( ) ) ;
e x i t ( 0 ) ; // C h i l d p r o c e s s e x i t s i m m e d i a t e l y
} else i f ( c h i l d p i d > 0) {
// Parent p r o c e s s
p r i n t f ( ” Parent p r o c e s s : PID = %d\n” , g e t p i d ( ) ) ;
p r i n t f ( ” Parent p r o c e s s : C h i l d PID = %d\n” , c h i l d p i d ) ;
s l e e p ( 1 0 ) ; // S l e e p t o a l l o w time f o r t h e c h i l d t o become a zombie
p r i n t f ( ” Parent p r o c e s s : Terminating . . . \ n” ) ;
} else {
p r i n t f ( ” Fork f a i l e d \n” ) ;
return 1 ;
}
return 0 ;
}
int main ( ) {
34
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
p i d t p i d ; // V a r i a b l e t o h o l d t h e p r o c e s s ID
char ∗ a r g s [ ] = { ” l s ” , ”− l ” , NULL} ; // Arguments f o r t h e command
// C r e a t e a new p r o c e s s
pid = f o r k ( ) ;
i f ( pid < 0) {
// I f f o r k ( ) f a i l s , p r i n t an e r r o r message
printf (” fork f a i l e d ” ) ;
return −1;
}
i f ( p i d == 0 ) {
// This b l o c k i s e x e c u t e d by t h e c h i l d p r o c e s s
// P r i n t a message t o i n d i c a t e t h e c h i l d p r o c e s s i s running
p r i n t f ( ” C h i l d p r o c e s s ( PID : %d ) r u n n i n g ’ l s − l ’ command . . . \ n” , g e t p i d ( ) ) ;
// E x e c u t e t h e ’ l s − l ’ command
execv ( ”/ bin / l s ” , args ) ;
// I f e x e c v ( ) f a i l s , p r i n t an e r r o r message
p r i n t f ( ” execv failed”);
e x i t ( −1); // E x i t t h e c h i l d p r o c e s s w i t h an e r r o r s t a t u s
} else {
// This b l o c k i s e x e c u t e d by t h e p a r e n t p r o c e s s
// Wait f o r t h e c h i l d p r o c e s s t o f i n i s h
w a i t (NULL ) ;
// P r i n t a message t o i n d i c a t e t h a t t h e c h i l d p r o c e s s has f i n i s h e d
p r i n t f ( ” Parent p r o c e s s ( PID : %d ) : C h i l d p r o c e s s has f i n i s h e d . \ n” , g e t p i d ( ) ) ;
}
return 0 ; // Return s u c c e s s
}
35
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
int main ( ) {
p i d t pid ; // V a r i a b l e t o h o l d t h e p r o c e s s ID
// C r e a t e a new p r o c e s s
pid = f o r k ( ) ;
i f ( pid < 0) {
// I f f o r k ( ) f a i l s , p r i n t an e r r o r message
printf (” fork f a i l e d ” ) ;
return −1;
}
i f ( p i d == 0 ) {
// This b l o c k i s e x e c u t e d by t h e c h i l d p r o c e s s
// P r i n t a message t o i n d i c a t e t h e c h i l d p r o c e s s i s running
p r i n t f ( ” C h i l d p r o c e s s ( PID : %d ) i s r u n n i n g . . . \ n” , g e t p i d ( ) ) ;
// The c h i l d p r o c e s s w i l l w a i t i n d e f i n i t e l y u n t i l i t r e c e i v e s a s i g n a l
while ( 1 ) {
pause ( ) ; // Wait f o r s i g n a l s
}
// This l i n e w i l l n e v e r be r e a c h e d i f t h e p r o c e s s i s t e r m i n a t e d by a s i g n a l
} else {
// This b l o c k i s e x e c u t e d by t h e p a r e n t p r o c e s s
// P r i n t a message t o i n d i c a t e t h a t t h e p a r e n t p r o c e s s i s s e n d i n g a s i g n a l
p r i n t f ( ” Parent p r o c e s s ( %d ) s e n d i n g s i g n a l t o c h i l d ( %d ) \ n” , g e t p i d ( ) , p i d ) ;
// Send t h e SIGTERM s i g n a l t o t h e c h i l d p r o c e s s
k i l l ( pid , SIGTERM ) ;
// Wait f o r t h e c h i l d p r o c e s s t o t e r m i n a t e
w a i t (NULL ) ;
return 0 ; // Return s u c c e s s
}
36
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Video Reference:
https://youtube.com/playlist?list=PLWjmN065fOfGdAZrlP6316HVHh8jlvefD
P1
P2 P3
P4 P6
P5
The program should create the specified process structure using the appropriate se-
quence of ‘fork()‘ system calls. Print PID and PPID of each process.
Question 2: Discuss the significance of the return values of the fork system call.
Question 3: What is the purpose of the wait system call?
Question 4: Discuss the significance of the getpid and getppid system calls in obtaining process
IDs.
37
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Question 5: How many child processes will be created if three consecutive fork statements are
used in a main function?
Overall Rating 1 2 3 4 5 6 7 8 9 10
Viva Performance 1 2 3 4 5 6 7 8 9 10
Lab-manual Readiness 1 2 3 4 5 6 7 8 9 10
Overall Rating 1 2 3 4 5 6 7 8 9 10
38
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
void ∗ t h r e a d f u n c t i o n ( void ∗ a r g ) {
p r i n t f ( ” I n s i d e t h e new t h r e a d ! \ n” ) ;
return NULL;
}
int main ( ) {
pthread t thread id ;
p t h r e a d c r e a t e (& t h r e a d i d , NULL, t h r e a d f u n c t i o n , NULL ) ;
p t h r e a d j o i n ( t h r e a d i d , NULL ) ;
return 0 ;
}
39
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
2. A C program that creates a thread and passes a message from the main function
to the thread.
int main ( ) {
pthread t thread id ;
p t h r e a d c r e a t e (& t h r e a d i d , NULL, t h r e a d f u n c t i o n , ” H e l l o from t h e main t h r e a d ! ” ) ;
p t h r e a d j o i n ( t h r e a d i d , NULL ) ;
return 0 ;
}
3. A C program where a thread returns a value to the main function using pointers.
void ∗ t h r e a d f u n c t i o n ( void ∗ a r g ) {
int ∗ r e t u r n V a l u e = m a l l o c ( s i z e o f ( int ) ) ;
∗ r e t u r n V a l u e = 1 4 3 ; // S e t t h e r e t u r n v a l u e
pthread exit ( returnValue ) ;
}
int main ( ) {
p t h r e a d t t h r e a d s [NUM THREADS ] ;
int ∗ t h r e a d r e t u r n ;
f r e e ( t h r e a d r e t u r n ) ; // Free a l l o c a t e d memory f o r r e t u r n v a l u e
return 0 ;
}
40
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Video Reference:
https://youtube.com/playlist?list=PLWjmN065fOfGdAZrlP6316HVHh8jlvefD
6.4 Lab Exercises: [Attempt any 3 within the designated lab hours]
Exercise 1: Develop a program using pthread to concatenate multiple strings passed to the thread
function.
Exercise 2: Create a pthread program to find the length of strings passed to the thread function.
Exercise 3: Implement a program that performs statistical operations (calculating average, max-
imum, and minimum) for a set of numbers. Utilize three threads, where each thread
performs its respective operation.
Exercise 4: Write a multithreaded program where a globally passed array of integers is divided
into two smaller lists and given as input to two threads. Each thread sorts their half
of the list and then passes the sorted lists to a third thread, which merges and sorts
them. The final sorted list is printed by the parent thread.
Exercise 5: Create a program using pthread create to generate multiple threads. Each thread
should display its unique ID and execution sequence.
Exercise 6: Create a threaded application that demonstrates graceful thread termination using
pthread exit for resource cleanup compared to abrupt termination via pthread cancel.
Question 4: Explain the differences between a thread and a process in terms of memory sharing
and execution context.
Question 5: Give two instances where a multi-threaded process offers benefits compared to a
single-threaded solution.
41
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Overall Rating 1 2 3 4 5 6 7 8 9 10
Viva Performance 1 2 3 4 5 6 7 8 9 10
Lab-manual Readiness 1 2 3 4 5 6 7 8 9 10
Overall Rating 1 2 3 4 5 6 7 8 9 10
42
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
7.2.3 Semaphore
Definition A semaphore is an abstract data type used for process synchronization in
concurrent programming. It controls access to shared resources among mul-
tiple processes or threads by maintaining a counter that can be incremented
or decremented.
Functionality Semaphores manage access to shared resources, prevent race conditions, and
ensure synchronization. They offer operations like initialization (sem init),
waiting (sem wait), signaling (sem post), and destruction (sem destroy).
Types Common types include Binary Semaphores (with values 0 and 1) and Count-
ing Semaphores (with values greater than 1).
43
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
7.2.4 Mutex
Definition A mutex (Mutual Exclusion) is a synchronization primitive used in multi-
threaded programming to control access to shared resources. It allows only
one thread at a time to access the resource, preventing concurrent access.
Functionality Mutexes ensure mutual exclusion, preventing race conditions and maintain-
ing data integrity. Operations include initialization (pthread mutex init),
locking (pthread mutex lock), unlocking (pthread mutex unlock), and
destruction (pthread mutex destroy).
Types Mutexes can be recursive (allows the same thread to lock it multiple times)
or non-recursive (deadlocks if the same thread tries to lock it multiple times).
int s h a r e d v a r i a b l e = 0 ;
void ∗ i n c r e m e n t v a r i a b l e ( void ∗ t h r e a d i d ) {
f o r ( int i = 0 ; i < MAX COUNT; i ++) {
s h a r e d v a r i a b l e ++;
}
p t h r e a d e x i t (NULL ) ;
}
int main ( ) {
p t h r e a d t t h r e a d s [NUM THREADS ] ;
44
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
p r i n t f ( ” Value o f s h a r e d v a r i a b l e a f t e r r a c e c o n d i t i o n : %d\n” , s h a r e d v a r i a b l e ) ;
return 0 ;
}
int s h a r e d v a r i a b l e = 0 ;
sem t semaphore ;
void ∗ i n c r e m e n t v a r i a b l e ( void ∗ t h r e a d i d ) {
f o r ( int i = 0 ; i < MAX COUNT; i ++) {
sem wait (&semaphore ) ;
s h a r e d v a r i a b l e ++;
s e m p o s t (&semaphore ) ;
}
p t h r e a d e x i t (NULL ) ;
}
int main ( ) {
p t h r e a d t t h r e a d s [NUM THREADS ] ;
s e m i n i t (&semaphore , 0 , 1 ) ; // I n i t i a l i z i n g semaphore w i t h v a l u e 1
45
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
s e m d e s t r o y (&semaphore ) ; // D e s t r o y i n g semaphore
p r i n t f ( ” Value o f s h a r e d v a r i a b l e a f t e r s y n c h r o n i z a t i o n : %d\n” , s h a r e d v a r i a b l e ) ;
return 0 ;
}
int s h a r e d v a r i a b l e = 0 ;
p t h r e a d m u t e x t mutex = PTHREAD MUTEX INITIALIZER ;
void ∗ i n c r e m e n t v a r i a b l e ( void ∗ t h r e a d i d ) {
f o r ( int i = 0 ; i < MAX COUNT; i ++) {
p t h r e a d m u t e x l o c k (&mutex ) ;
s h a r e d v a r i a b l e ++;
p t h r e a d m u t e x u n l o c k (&mutex ) ;
}
p t h r e a d e x i t (NULL ) ;
}
int main ( ) {
p t h r e a d t t h r e a d s [NUM THREADS ] ;
p t h r e a d m u t e x i n i t (&mutex , NULL ) ; // I n i t i a l i z i n g mutex
p t h r e a d m u t e x d e s t r o y (&mutex ) ; // D e s t r o y i n g mutex
p r i n t f ( ” Value o f s h a r e d v a r i a b l e a f t e r s y n c h r o n i z a t i o n : %d\n” , s h a r e d v a r i a b l e ) ;
return 0 ;
}
46
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Video Reference:
https://youtube.com/playlist?list=PLWjmN065fOfGdAZrlP6316HVHh8jlvefD
(a) A producer only produces if the buffer is empty, and the consumer only consumes
if some content is in the buffer.
(b) A producer writes an item into the buffer, and the consumer deletes the last
produced item in the buffer.
(c) A producer writes on the last consumed index of the buffer.
Exercise 2: Implement the reader-writer problem using semaphore and mutex operations to syn-
chronize n readers active in the reader section at the same time and one writer active
at a time.
Constraints:
Question 4: Explain the functions pthread mutex init, pthread mutex lock, and pthread mutex unlock
in mutex usage.
Question 5: Discuss scenarios where mutexes are preferred over other synchronization mecha-
nisms.
47
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Overall Rating 1 2 3 4 5 6 7 8 9 10
Viva Performance 1 2 3 4 5 6 7 8 9 10
Lab-manual Readiness 1 2 3 4 5 6 7 8 9 10
Overall Rating 1 2 3 4 5 6 7 8 9 10
48
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
8.2.2 Pipes
In C programming, pipes are a form of interprocess communication (IPC) that allows communi-
cation between two processes, with one process writing data into the pipe and the other process
reading from it. Pipes are one-way communication channels that can be either anonymous or
named.
49
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Sample Program
int main ( ) {
int p i p e f d [ 2 ] ;
char data [ 1 0 0 ] ;
i f ( pipe ( p i p e f d ) < 0) {
p r i n f t ( ” pipe creation f a i l e d ” ) ;
return 1 ;
}
p i d t pid = f o r k ( ) ;
i f ( pid < 0) {
p r i n t f ( ” f o r k f a i l e d \n” ) ;
return 1 ;
}
i f ( p i d > 0 ) { // Parent p r o c e s s
// Write t o p i p e
c l o s e ( p i p e f d [ 0 ] ) ; // C l o s e t h e r e a d i n g end
char message [ ] = ” H e l l o , C h i l d P r o c e s s ! ” ;
w r i t e ( p i p e f d [ 1 ] , message , s i z e o f ( message ) ) ;
c l o s e ( p i p e f d [ 1 ] ) ; // C l o s e t h e w r i t i n g end
} e l s e { // C h i l d p r o c e s s
// Read from p i p e
c l o s e ( p i p e f d [ 1 ] ) ; // C l o s e t h e w r i t i n g end
r e a d ( p i p e f d [ 0 ] , data , s i z e o f ( data ) ) ;
p r i n t f ( ” R e c e i v e d message i n c h i l d : %s \n” , data ) ;
c l o s e ( p i p e f d [ 0 ] ) ; // C l o s e t h e r e a d i n g end
}
return 0 ;
}
50
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Sample Code[3][1]
int main ( ) {
char ∗ f i f o = ” /tmp/ m y f i f o ” ; // Path t o t h e named p i p e
m k f i f o ( f i f o , 0 6 6 6 ) ; // C r e a t i n g a named p i p e
int f d ;
char data [ 1 0 0 ] ;
return 0 ;
}
1. Allocation: A process allocates a shared memory segment using the shmget() system call.
This call either creates a new shared memory segment or accesses an existing one based on a
provided key and size.
2. Attachment: After allocation, the process attaches the shared memory segment to its ad-
dress space using shmat(). This attaches the segment to a virtual address in the process’s
memory, allowing it to read from and write to the shared memory.
3. Usage: Processes that share this segment can read from and write to it, treating it like
any other memory region. Synchronization mechanisms such as semaphores or mutexes are
typically used to control access and prevent race conditions.
51
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
4. Detachment: When the process finishes using the shared memory, it detaches the segment
using shmdt(). This detaches the shared memory segment from the process’s address space.
5. Control Operations: The shmctl() function allows for control operations on the shared
memory segment, such as removing or modifying it.
8.2.6 Sample program to demonstrate shared memory segment creation and data
addition
int main ( ) {
void ∗shm ;
char buf [ 1 0 0 ] ;
int shmid ;
// C r e a t i n g a s h a r e d memory segment
shmid = shmget ( ( k e y t ) 1 2 3 , SHM SIZE , 0666 | IPC CREAT ) ;
i f ( shmid == −1) {
p r i n t f ( ” shmget E r r o r \n” ) ;
e x i t (EXIT FAILURE ) ;
}
p r i n t f ( ”The Key v a l u e o f s h a r e d memory i s %d\n” , shmid ) ;
// A t t a c h i n g t h e p r o c e s s t o t h e s h a r e d memory segment
shm = shmat ( shmid , NULL, 0 ) ;
52
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
// D e t a c h i n g s h a r e d memory
i f ( shmdt ( shm ) == −1) {
p r i n t f ( ”shmdt E r r o r \n” ) ;
e x i t (EXIT FAILURE ) ;
}
return 0 ;
}
53
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
struct m s g b u f f e r {
long msg type ;
char m s g t e x t [ MAX MSG SIZE ] ;
};
int main ( ) {
k e y t key ;
int msg id ;
struct m s g b u f f e r message ;
// Manually g e n e r a t e a key w i t h o u t u s i n g f t o k
i f ( ( key = 0 x12345678 ) == −1) {
p r i n t f ( ” key E r r o r \n” ) ;
e x i t (EXIT FAILURE ) ;
}
54
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
return 0 ;
}
Video Reference:
https://youtube.com/playlist?list=PLWjmN065fOfGdAZrlP6316HVHh8jlvefD
55
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Question 7: Explain the process of creating and using message queues for communication between
processes.
Overall Rating 1 2 3 4 5 6 7 8 9 10
56
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Viva Performance 1 2 3 4 5 6 7 8 9 10
Lab-manual Readiness 1 2 3 4 5 6 7 8 9 10
Overall Rating 1 2 3 4 5 6 7 8 9 10
• The calculation for LM marks is determined by: (Average of the teacher’s overall ratings in
Experiment 7 and Experiment 8) multiplied by 5.
References
[1] Greg Gagne Abraham Silberschatz, Peter B. Galvin. Operating System Concepts. Wiley, 10
edition, 2018.
[2] Sumitabha Das. Unix Concepts And Applications. Wiley, 4 edition, 2006.
[3] Richard Stones Neil Matthew. Beginning Linux Programming. Wiley, 4 edition, 2007.
57
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
58
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
IX. Create a new directory called ”archive” inside the ”lab exercises” directory.
X. Move ”file3.txt” to the ”archive” directory and rename it to ”file3 backup.txt”
using the mv command.
XI. Verify that ”file3 backup.txt” has been moved to the ”archive” directory and
that its contents are the same as ”file3.txt”.
XII. Create a new directory called ”temp” inside the ”lab exercises” directory.
XIII. Create a new file called ”file4.txt” in the ”temp” directory and write some text
in it.
XIV. Move ”file4.txt” to the ”lab exercises” directory using the mv command.
XV. Verify that ”file4.txt” has been moved to the ”lab exercises” directory and that
its contents are the same as before.
Exercise 2: I. Write a script that accepts a directory name as a command line argument and
displays the number of files in that directory.
II. Modify the script to display the number of files in the directory and all its
subdirectories.
Exercise 3: I. Write a script that accepts a filename as a command line argument and displays
the number of lines, words, and characters in that file.
II. Modify the script to accept multiple file names as command line arguments and
display the number of lines, words, and characters in each file.
Exercise 4: I. Declare a variable called ”name” and assign your name to it. Display the value
of the variable using the echo command.
II. Declare a variable called ”age” and assign your age to it. Display the value of
the variable using the echo command.
III. Declare a variable called ”color” and assign your favorite color to it. Display the
value of the variable using the echo command.
Exercise 5: Declare a variable called ”num1” and assign the value 10 to it. Declare a second
variable called ”num2” and assign the value 5 to it. Add the values of the two
variables and display the result using the echo command.
Exercise 6: I. Declare a variable called ”filename” and assign the value ”sample.txt” to it. Use
the variable to create a new file with that name using the touch command.
II. Declare a variable called ”directory” and assign the value ”myfolder” to it. Use
the variable to create a new directory with that name using the mkdir command.
Exercise 7: Declare a variable called ”files” and assign a list of filenames to it. Use a loop to
display the contents of each file in the list using the cat command.
59
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Exercise 8: I. Write a command that displays the contents of a file called ”file1.txt” on the
screen.
II. Use input redirection to create a new file called ”file2.txt” with the contents of
”file1.txt”.
III. Write a command that appends the contents of ”file1.txt” to the end of ”file2.txt”.
Exercise 9: I. Write a for loop that prints the numbers from 1 to 10 on the screen.
II. Modify the loop to print only the even numbers from 1 to 10.
Exercise 10: I. Write a loop that displays the names of all files in the current directory.
II. Modify the loop to display only the names of files with the extension ”.txt”.
Exercise 11: I. Write a case/esac statement that displays a message on the screen based on the
value of a variable called ”day”. If the value is ”Monday”, the message should
be ”It’s the start of the week”. If the value is ”Friday”, the message should be
”Thank goodness it’s Friday!”. For any other value, the message should be ”Just
another day”.
II. Modify the case/esac statement to use a read command to read the value of
”day” from the user.
Exercise 12: Write a case/esac statement that calculates the area of a geometric shape based on
the user’s input. If the input is ”square”, the statement should ask the user for the
length of the side and display the area. If the input is ”rectangle”, the statement
should ask the user for the length and width and display the area. If the input is
”circle”, the statement should ask the user
Exercise 13: I. Write an if statement that checks if a variable called ”x” is greater than 10. If
it is, display the message ”x is greater than 10”.
II. Modify the if statement to check if ”x” is equal to 10 as well. If it is, display the
message ”x is equal to 10”.
60
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Exercise 5: Write a C program that prints the last 10 characters of a file named as ”input.txt”
on the screen. Use open, read, write and lseek system calls.
Exercise 6: Write a C program that prints half content of a file named as ”input.txt” on the
screen. Use open, read, write and lseek system calls. If there are 100 characters
written in the file, your program should display the first 50 characters on the screen.
61
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
62
Operating Systems Lab Manual Prepared by Pushpendra Kumar Pateriya
Exercise 6: Write a C program that creates a named pipe called ”mypipe” using the mkfifo system
call. Then, use the open system call to open the pipe for writing. Write some data to
the pipe using the write system call. Finally, use the cat command to read the data
from the pipe.
Exercise 7: Write a C program that creates a named pipe called ”mypipe” using the mkfifo system
call. Then, use the open system call to open the pipe for reading. Read some data
from the pipe using the read system call. Finally, use the echo command to write the
data to the standard output.
Exercise 8: Write a C program that creates a named pipe called ”mypipe” using the mkfifo system
call. Then, fork a child process. In the parent process, use the open system call to
open the pipe for writing. In the child process, use the open system call to open the
pipe for reading. Then, write some data to the pipe in the parent process and read
the data from the pipe in the child process.
Exercise 9: Write a program in C that creates a child process using fork(). The parent process
should read a message from the user and send it to the child process using a pipe. The
child process should then read the message from the pipe and print it to the console.
Exercise 10: Implement a simple message passing system using shared memory in C. Create a
parent process that forks a child process and communicates with it using the shared
memory. Use the shmget, shmat, and shmdt functions to create a shared memory
segment, attach to it, and detach from it. The parent process should write a message
to the shared memory segment, and the child process should read and print the
message.
63