OS LAB MidTerm V6 Solution
OS LAB MidTerm V6 Solution
Question Q1 Q2 Q3 Q4 Q5 Total
Marks 5 10 5 10 10 40
Obtained
1
Question No. 01: Write Linux basic commands to fulfill following operations.
ls [options] [files_or_directories]
uname
mkdir /home/sysadmin/Desktop/MID
4. Force to remove the directory dir which you have created with a name of
MID.
rm -rf MID
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.
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:
Number of Outputs:
Since there are 8 processes, the output statement cout << "Hello, From Process" << getpid(); will
execute 8 times.
Output
Explanation of Output:
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.
If the parent terminates without waiting for its children (wait()), all 7 child processes become
orphaned.
At most, 7 orphan children can exist in this program if the parent process terminates
immediately after creating the children.
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 }
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);
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() {
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;
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;
cout << "Second child is running independently... (" << i + 1 << "/5)" << endl;
return 0;
sleep(1); // Simulate work
}
7
}
pid3 = fork();
if (pid3 < 0) {
return 1;
if (pid3 == 0) {
cout << "Third child process. PID: " << getpid() << endl;
cout << "Third child is waiting for an event (e.g., user input)." << endl;
cout << "Third child received event and is now exiting." << endl;
// Parent process
cout << "Parent process. PID: " << getpid() << endl;
cout << "Created first child (PID: " << pid1 << "), second child (PID: " << pid2 <<
"), and third child (PID: " << pid3 << ")." << endl;
sleep(5);
// First child becomes a zombie temporarily (if parent does not call wait())
int status;
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;
return 0;
#!/bin/bash
fibonacci() {
local n=$1
local a=0
local b=1
local c
if [ $n -ge 1 ]; then
9
echo -n "$a "
fi
if [ $n -ge 2 ]; then
fi
do
a=$b
b=$c
done
read -p "Enter the number of terms to generate in the Fibonacci series: " terms
fibonacci $terms
10