0% found this document useful (0 votes)
15 views10 pages

OS LAB MidTerm V6 Solution

The document is a mid-term exam paper for an Operating Systems Lab course at the University of Management and Technology. It includes instructions for candidates, a series of questions related to Linux commands, process management, syntax error correction, programming tasks, and a Bash script for generating Fibonacci series. The exam is structured to assess students' understanding of operating systems concepts and practical programming skills.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

OS LAB MidTerm V6 Solution

The document is a mid-term exam paper for an Operating Systems Lab course at the University of Management and Technology. It includes instructions for candidates, a series of questions related to Linux commands, process management, syntax error correction, programming tasks, and a Bash script for generating Fibonacci series. The exam is structured to assess students' understanding of operating systems concepts and practical programming skills.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIVERSITY OF MANAGEMENT AND TECHNOLOGY

MID Term Exam

Student Name: ___________________ Student ID: _______________________

Paper: Operating Systems Lab Student Signature: _______________________

Instructor’s Name: M. Rizwan___________ Section: _V6___________________

Date: _11 – Dec - 2024____ Time Allowed: 60 minutes____________

Instructions for Candidates:


1) Mention your Name and ID.
2) Just focus on your own paper, discussion is strictly prohibited.
3) Read questions carefully before solving.
4) Solve all the questions on the provided space in your question paper.
5) Understanding of the questions is part of examination. So, none of your queries will be entertained in
the examinations hall.
6) If there is any ambiguity in the paper, benefit will be given to the students.

Question Q1 Q2 Q3 Q4 Q5 Total
Marks 5 10 5 10 10 40
Obtained

DO NOT OPEN UNTIL YOU ARE TOLD TO DO SO

1
Question No. 01: Write Linux basic commands to fulfill following operations.

[5 marks] CLO 1, CLO 2

1. List the content of the current directory or a specified directory.

ls [options] [files_or_directories]

2. Display the information about the system.

uname

3. Create a new directory ‘MID’ in ‘/home/sysadmin/Desktop’.

mkdir /home/sysadmin/Desktop/MID

4. Force to remove the directory dir which you have created with a name of
MID.

rm -rf MID

5. Move OSV2.cpp to root directory and make it executable.

mv /home/sysadmin/Desktop/OSV2.cpp
g++ OSV2.cpp -o os

2
Question No. 02: What will be the output of the following code and give its

reason.

[10 marks] CLO 2, CLO 3

1 int main ()
2 {
3 int pid1 = fork ();
4 int pid2 = fork();
5 int pid3 = fork();
6 if (pid1 < 0 | | pid2 < 0 | | pid3 < 0)
7 {
8 cout << “FORK FAILED… ” << endl;
9 }
10 cout << “Hello, From Process” << getpid() << endl;
11 return 0;
12 }

1. Write the output of the given code, and also explain the reason of the output as well.

Process IDs:

Each process has a unique PID (getpid()).

Processes execute the cout statement in line 10 independently.

Number of Outputs:

Since there are 8 processes, the output statement cout << "Hello, From Process" << getpid(); will
execute 8 times.

Output

Hello, From Process12345

Hello, From Process12346

Hello, From Process12347

Hello, From Process12348

Hello, From Process12349


3
Hello, From Process12350

Hello, From Process12351

Hello, From Process12352

Explanation of Output:

Each process prints its unique PID.

The output order may vary because processes run concurrently, and their scheduling is handled by
the OS.

2. Draw the process tree for the above code and provide the values of pid1 and pid2.

3. How many at max orphan children will be present in the system mentioned above? How
can we avoid their existence.

The parent process creates 7 child processes.

If the parent terminates without waiting for its children (wait()), all 7 child processes become
orphaned.

Maximum Orphan Processes:

At most, 7 orphan children can exist in this program if the parent process terminates
immediately after creating the children.

Avoiding Orphan Processes:

To avoid orphan processes:

Use the wait() system call in the parent process to wait for all child processes to complete.

Question No. 03: There are exactly 6 syntax errors present in the following
code. Find them and provide the correct syntax in the table below.
[5 marks] CLO 2

4
1 int main (){

5
2 char buffer [100];
3 int fd = open (O_CREAT|O_RDWR, 0777);
4 int n =read(buffer[fd]);
5 int pid = fork(-1);
6 if(fork(pid) > 0){
7 cout << "Hello, I am parent process and my id : "<<getpid()<<endl;
8 cout << "Let's show the text from the file on screen."<<endl;
9 write(buffer);
10 int cpid = wait(pid,NULL,0);
11 cout << "\nMy Child has completed his execution and his id is: "<< cpid << endl;
12 }
13 if(pid == 0){
14 execl(NULL ,"cal");
15 }
16 return 0;
17 }

Error Line no. Correction


no.

1 3
int fd = open ("IOS.txt",O_CREAT|O_RDWR,0777);
2 4
int n = read(fd,buffer,50);
3 5
int pid = fork();
4 6
if(pid > 0){
5 9
write(1,buffer,n);
6 10 int cpid = wait(NULL);

7 14 execl("/usr/bin/cal", "cal", NULL);

Question 04: Write the following programs [5 + 5 marks] CLO 2, CLO 3

A. Write a program to demonstrate how a child process become orphaned. Display the
process Id’s of both parent and child.

6
B. Write a program to create three child processes, where the first child terminates early
(demonstrating a zombie process), the second child runs independently, and the third
child waits for a specific event. Display the process IDs of all processes, and identify
which process becomes a zombie.

A: B:
int main() { int main() {

pid_t pid1, pid2, pid3;


pid_t pid = fork(); // Create a new process
// Create the first child process
if (pid < 0) {
pid1 = fork();
// Fork failed
if (pid1 < 0) {
cerr << "Fork failed!" << endl;
cerr << "Fork failed for first child!" << endl;
return 1;
return 1;
} else if (pid > 0) {
}

// Parent process if (pid1 == 0) {

cout << "Parent process. PID: " << getpid() << endl; // First child process

cout << "Child process PID: " << pid << endl; cout << "First child process. PID: " << getpid() << endl;

// Parent exits immediately cout << "First child is terminating early..." << endl;

exit(0); // First child exits immediately


cout << "Parent is terminating..." << endl;
}
exit(0);
// Create the second child process
} else {
pid2 = fork();
// Child process
if (pid2 < 0) {
sleep(5); // Child process sleeps to ensure parent terminates
first cerr << "Fork failed for second child!" << endl;

return 1;
cout << "Child process. PID: " << getpid() << endl;
}
cout << "Parent PID as seen by child: " << getppid() << endl;
if (pid2 == 0) {
// Child continues execution after parent has terminated
// Second child process
cout << "Child process is now orphaned and adopted by
init/systemd." << endl; cout << "Second child process. PID: " << getpid() << endl;

} for (int i = 0; i < 5; ++i) {

cout << "Second child is running independently... (" << i + 1 << "/5)" << endl;
return 0;
sleep(1); // Simulate work
}

7
}

cout << "Second child completed its task." << endl;

exit(0); // Second child exits

// Create the third child process

pid3 = fork();

if (pid3 < 0) {

cerr << "Fork failed for third child!" << endl;

return 1;

if (pid3 == 0) {

// Third child process

cout << "Third child process. PID: " << getpid() << endl;

cout << "Third child is waiting for an event (e.g., user input)." << endl;

cout << "Press Enter to continue...";

cin.get(); // Wait for user input

cout << "Third child received event and is now exiting." << endl;

exit(0); // Third child exits

// Parent process

cout << "Parent process. PID: " << getpid() << endl;

cout << "Created first child (PID: " << pid1 << "), second child (PID: " << pid2 <<
"), and third child (PID: " << pid3 << ")." << endl;

// Wait to demonstrate the zombie process

sleep(5);

cout << "Parent checking processes..." << endl;

// First child becomes a zombie temporarily (if parent does not call wait())

int status;

pid_t zombie_pid = waitpid(pid1, &status, WNOHANG); // Non-blocking wait

if (zombie_pid == 0) {

8
cout << "First child is a zombie process (not yet reaped)." << endl;

} else {

cout << "First child (PID: " << zombie_pid << ") has been reaped by the parent."
<< endl;

// Wait for the second and third children

waitpid(pid2, &status, 0); // Block until second child terminates

cout << "Second child has terminated." << endl;

waitpid(pid3, &status, 0); // Block until third child terminates

cout << "Third child has terminated." << endl;

cout << "Parent process is exiting." << endl;

return 0;

Question 05: Write a Bash Script: [10 marks] CLO 1, CLO 2


Write a Bash script to generate the Fibonacci series up to a given number. The script should
take input from the user (the number of terms to generate) and display the Fibonacci series.

#!/bin/bash

# Function to print Fibonacci series up to n terms

fibonacci() {

local n=$1

local a=0

local b=1

local c

echo "Fibonacci series up to $n terms:"

# Special case for 1 term

if [ $n -ge 1 ]; then

9
echo -n "$a "

fi

# Special case for 2 terms

if [ $n -ge 2 ]; then

echo -n "$b "

fi

# Generate Fibonacci series for more than 2 terms

for (( i=3; i<=n; i++ ))

do

c=$((a + b)) # Next Fibonacci number

echo -n "$c "

a=$b

b=$c

done

echo # To print a newline at the end

# Read user input for the number of terms

read -p "Enter the number of terms to generate in the Fibonacci series: " terms

# Call the fibonacci function

fibonacci $terms

10

You might also like