0% found this document useful (0 votes)
30 views

CS2201 OS Solution

The document outlines the mid-term examination for the Operating Systems course for B. Tech - CSE students, detailing the structure and content of the exam. It includes various questions on topics such as caches, thread creation, kernel and user modes, semaphores, process creation, inter-process communication, and resource allocation using the Banker's Algorithm. Additionally, it features programming tasks and scheduling algorithms with Gantt chart illustrations.

Uploaded by

megav369
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)
30 views

CS2201 OS Solution

The document outlines the mid-term examination for the Operating Systems course for B. Tech - CSE students, detailing the structure and content of the exam. It includes various questions on topics such as caches, thread creation, kernel and user modes, semaphores, process creation, inter-process communication, and resource allocation using the Banker's Algorithm. Additionally, it features programming tasks and scheduling algorithms with Gantt chart illustrations.

Uploaded by

megav369
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/ 8

Name:

Enrolment No:

Even Semester Mid Term Examination, March 2024


Faculty of Engineering, School of Computer Science and Engineering
Department of Computer Science and Engineering
B. Tech - CSE
Course Code: CS2201 Course: Operating Systems- Solution Semester: IV
Time: 1.5 hrs. Max. Marks: 30
Instructions: All questions are compulsory.
Missing data, if any, may be assumed suitably.
Calculator is allowed.
SECTION A
S.No. Marks CO
Q A1 Give a reason why caches are useful. What problems do they solve? What problems do 1+.5+.5 CO1
they cause?
Ans: Caches are useful when two or more components need to exchange data, and
the components perform transfers at differing speeds. Caches solve the transfer
problem by providing a buffer of intermediate speed between the components. If
the fast device finds the data it needs in the cache, it need not wait for the slower
device. The data in the cache must be kept consistent with the data in the
components. If a component has
a data value change, and the datum is also in the cache, the cache must also be
updated. This is especially a problem on multiprocessor systems where more than
one process may be accessing a datum.
Q A2 What resources are used when a thread is created? How do they differ from those used 1+1 CO2
when a process is created?
Ans: Because a thread is smaller than a process, thread creation typically uses
fewer resources than process creation. Creating a process requires allocating a
process control block (PCB), a rather large data structure. The PCB includes a
memory map, list of open files, and environment variables. Allocating and
managing the memory map is typically the most time-consuming activity. Creating
either a user or kernel thread involves allocating a small data structure to hold a
register set, stack, and priority.
Q A3 How does the distinction between kernel mode and user mode function as a 2 CO1
rudimentary form of protection (security) system?
Ans: The distinction between kernel mode and user mode provides a rudimentary
form of protection in the following manner. Certain instructions could be executed
only when the CPU is in kernel mode. Similarly, hardware devices could be
accessed only when the program is executing in kernel mode. Control over when
interrupts could be enabled or disabled is also possible only when the CPU is in
kernel mode. Consequently, the CPU has very limited capability when executing in
user mode, thereby enforcing protection of critical resources.
SECTION B
Q B1 Show how semaphore provides the solution to the reader writer problem. 2+2 CO4
Ans: The structure of a writer process

do {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
} while (true);

As seen above in the code for the writer, the writer just waits on the rw_mutex
semaphore until it gets a chance to write to the resource.
After performing the write operation, it increments rw_mutex so that the next
writer can access the resource.
The structure of a reader process
do {
wait(mutex); //acquire lock
read_count++;
if (read_count == 1)
wait(rw_mutex);
signal(mutex); //release lock
...
/* reading is performed */
...
wait(mutex); //acquire lock
read count--;
if (read_count == 0)
signal(rw_mutex);
signal(mutex); //release lock
}while(true);
On the other hand, in the code for the reader, the lock is acquired whenever the
read_count is updated by a process. When a reader wants to access the resource,
first it increments the read_count value, then accesses the resource and then
decrements the read_count value. The semaphore rw_mutex is used by the first
reader which enters the critical section and the last reader which exits the critical
section. The reason for this is, when the first readers enters the critical section, the
writer is blocked from the resource. Only new readers can access the resource now.
Similarly, when the last reader exits the critical section, it signals the writer using
the rw_mutex semaphore because there are zero readers now and a writer can have
the chance to access the resource.

Q B2 How many processes will be 4 CO2


spawned after executing the code
segment given below? Also, draw
the corresponding process tree. How
can you verify that you have drawn
the correct process tree?
main ()
{
if (fork () || fork ())
if (fork () && fork ())
fork ();
}
Ans:

Q B3 Write a program in ‘C’ (Linux OS) in which the parent process sends an integer value 4 CO2
to the child process and the child process computes whether the value is prime or not
and returns the result to the parent process using a different pipe. The parent process
prints the received result on the terminal. The communication between the parent and
the child is through the pipe.
Ans:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

#define READ_END 0
#define WRITE_END 1

int is_prime(int n) {
if (n <= 1) return 0; // 0 and 1 are not prime
if (n <= 3) return 1; // 2 and 3 are prime
if (n % 2 == 0 || n % 3 == 0) return 0; // multiples of 2 and 3 are not prime

for (int i = 5; i * i <= n; i += 6) {


if (n % i == 0 || n % (i + 2) == 0) return 0;
}
return 1;
}

int main() {
int parent_to_child_pipe[2];
int child_to_parent_pipe[2];

if (pipe(parent_to_child_pipe) == -1 || pipe(child_to_parent_pipe) == -1) {


perror("Pipe creation failed");
exit(EXIT_FAILURE);
}

pid_t pid = fork();

if (pid < 0) {
perror("Fork failed");
exit(EXIT_FAILURE);
}

if (pid == 0) { // Child process


close(parent_to_child_pipe[WRITE_END]);
close(child_to_parent_pipe[READ_END]);

int num;
read(parent_to_child_pipe[READ_END], &num, sizeof(int));
close(parent_to_child_pipe[READ_END]);

int result = is_prime(num);

write(child_to_parent_pipe[WRITE_END], &result, sizeof(int));


close(child_to_parent_pipe[WRITE_END]);

exit(EXIT_SUCCESS);
} else { // Parent process
close(parent_to_child_pipe[READ_END]);
close(child_to_parent_pipe[WRITE_END]);

int num;
printf("Enter an integer: ");
scanf("%d", &num);

write(parent_to_child_pipe[WRITE_END], &num, sizeof(int));


close(parent_to_child_pipe[WRITE_END]);

int result;
read(child_to_parent_pipe[READ_END], &result, sizeof(int));
close(child_to_parent_pipe[READ_END]);

if (result)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);

wait(NULL); // Wait for the child process to finish


}

return 0;
}

Q B4 Assume that there are 5 processes, P0 through P4, and 4 types of resources. At T 0 we have 4 CO4
the following system state:
Max Instances of Resource Type A = 3 (2 allocated + 1 Available)
Max Instances of Resource Type B = 17 (12 allocated + 5 Available)
Max Instances of Resource Type C = 16 (14 allocated + 2 Available)
Max Instances of Resource Type D = 12 (12 allocated + 0 Available)

Apply Banker’s Algorithm whether the given system is in a safe state or not. If yes find the
safe states sequences.
Solution :

Need Matrix
A B C D
P0 0 1 0 0
P1 0 4 2 1
P2 1 0 0 1
P3 0 0 2 0
P4 0 6 4 2

Available Matrix
A B C D
Initially Available 1 5 2 0
Available resources when P0 completes its task 1 6 3 0
Available resources when P3 completes its task 1 12 6 2
Available resources when P4 completes its task 1 12 7 6
Available resources when P1 completes its task 2 14 10 7
Available resources when P2 completes its task 3 17 16 12

Safe Sequence P0 → P3 → P4 → P1 → P2

Multiple Sequence can be possible.

SECTION-C
Consider the following set of processes, with the length of the CPU-burst time given in 8 CO3
milliseconds:
Process Arrival Time Burst Time
A 0 3
B 1 6
C 4 4
D 6 2

Q C1 E 2 5
F 3 9

Illustrate the execution of these processes with a Gantt chart using Pre-emptive SJF and
Round Robin (RR-3ms time quantum) scheduling algorithm. Also, find the average
waiting time and turnaround time.
Ans: Pre-emptive SJF
Round Robin Scheduling (Time Quantum 3 ms)

You might also like