CS2201 OS Solution
CS2201 OS Solution
Enrolment No:
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 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
int main() {
int parent_to_child_pipe[2];
int child_to_parent_pipe[2];
if (pid < 0) {
perror("Fork failed");
exit(EXIT_FAILURE);
}
int num;
read(parent_to_child_pipe[READ_END], &num, sizeof(int));
close(parent_to_child_pipe[READ_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);
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);
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
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)