Operating System GATE Question Answers with Explanation.
Q1 – Which one or more of the following needs to be saved on a context switch from one thread (T1) of a process to another thread (T2) of the same process? (GATE 2023)
Â
Page table base register
Stack pointer
Program counter
General purpose registers
Ans – (2, 3, 4)
Explanation – During a context switch from one thread (T1) to another thread (T2) within the same process, the following information needs to be saved:
Program Counter (PC): This indicates the address of the next instruction to be executed.
Stack Pointer (SP): Points to the top of the stack where local variables and function call information are stored.
Register Values: The values stored in the processor registers need to be saved so that the state of the CPU is preserved. These registers include general-purpose registers, floating-point registers, and status registers.
Saving this information ensures that when the operating system switches between threads, the state of the executing thread is preserved, and execution can resume seamlessly from where it left off when the thread is scheduled to run again.
Q2 – Which one or more of the following options guarantees that a computer system will transition from user mode to kernel mode? (GATE 2023)
Â
Function Call
malloc Call
Page Fault
System Call
Ans – (3, 4)
Explanation –
3. Page Fault: When a process attempts to access a memory page that is not currently loaded into physical memory, a page fault occurs. Handling page faults requires kernel intervention to load the necessary page from secondary storage into physical memory, which necessitates a transition from user mode to kernel mode.
4. System Call: System calls are requests made by a user-level process to the operating system kernel for performing privileged operations such as I/O operations, process control, and file management. When a process invokes a system call, it triggers a transition from user mode to kernel mode to execute the requested operation within the protected kernel space.
Q3 – Which one or more of the following CPU scheduling algorithms can potentially cause starvation? (GATE 2023)
Â
First-in First-Out
Round Robin
Priority Scheduling
Shortest Job First
Ans – (1, 3, 4)
Explanation –
The CPU scheduling algorithms that can potentially cause starvation are:
1. First-in First-Out (FIFO) – In FIFO scheduling, processes are executed in the order they arrive. If long-running processes continuously arrive, short processes may be delayed indefinitely, leading to starvation.
2. Priority Scheduling – Priority scheduling assigns priority levels to processes, and the scheduler selects the process with the highest priority for execution. If there are processes with high priority continuously arriving or if the scheduler always selects high-priority processes, lower-priority processes may starve, as they may never get a chance to execute.
3. Shortest Job First (SJF) – SJF scheduling selects the process with the smallest burst time next for execution. If there are constantly arriving long jobs, short jobs may starve as they continually get preempted by the arrival of longer jobs.
4. Round Robin (RR) – Round Robin scheduling, by design, does not cause starvation, as it allocates a fixed time quantum to each process. However, it may lead to poor performance for short processes if the time quantum is too large.
Q4 – Consider the two functions incr and decr shown below.
There are 5 threads each invoking incr once, and 3 threads each invoking decr once, on the same shared variable X. The initial value of X is 10.
Â
Suppose there are two implementations of the semaphores, as follows:
I-1: s is a binary semaphore initialized to 1.
I-2: s is a counting semaphore initialized to 2.
Let V1, V2 be the values of X at the end of execution of all the threads with implementations I-1, I-2, respectively.
Â
Which one of the following choices corresponds to the minimum possible values of V1, V2, respectively? (GATE 2023)
15, 7
7, 7
12, 7
12, 8
Ans – (3)
Explanation –
Let’s analyze the scenario using each semaphore implementation:
Implementation I-1: Binary Semaphore Initialized to 1
– With a binary semaphore initialized to 1, only one thread can access the critical section at a time.
– Since there are 5 threads invoking incr and 3 threads invoking decr, and incr and decr operations do not interfere with each other, they can execute concurrently.
– Hence, the final value of X, V1, will be 10 (initial value) + 5 (incremented) – 3 (decremented) = 12.
Implementation I-2: Counting Semaphore Initialized to 2
– With a counting semaphore initialized to 2, up to two threads can access the critical section simultaneously.
– Again, since incr and decr operations do not interfere with each other, they can execute concurrently.
– Similar to the previous scenario, 5 threads invoking incr will increase X by 5, and 3 threads invoking decr will decrease X by 3.
– The maximum value of X after all incr
threads run will be 10 + 5 = 15.
– The minimum value of X after all decr threads run will be 10 – 3 = 7.
So, the minimum possible values of V1 and V2 are 12 and 7, respectively, as you’ve provided. The correct answer is indeed 12, 7.