GATE OS PYQ PCB, System Calls, Process State, Fork()

Q 31. [GATE 2026 Set-1 : 2M]
Consider the following program snippet. Assume that the program compiles and runs successfully. Further, assume that the fork() system call is always successful in creating a process.

int main() {
int i;
for(i=0; i<3; i++){
if(fork() == 0){
continue;
}
break;
}
printf(“Hello!”);
return 0;
}

The total number of times that the print statement gets executed is—-(answer in integer)

  • Answer: 4
  • Process Tree:
  • i=0: fork() -> P and C1. P -> break -> prints Hello (1). C1 -> continue -> goes to i=1
  • i=1: C1 fork() -> C1 and C2. C1 -> break -> prints Hello (2). C2 -> continue -> goes to i=2
  • i=2: C2 fork() -> C2 and C3. C2 -> break -> prints Hello (3). C3 -> continue -> i=3 loop ends -> prints Hello (4)

Trick: parent=break, child=continue pattern me Total prints = n+1 = 3+1=4.

Q 32. [GATE 2025 Set-1 : 1M]Suppose in a multiprogramming environment ,the following C-program segment is executed .A process goes to I/O queue whenever I/O performed. Assume that there will be always be a context switch whenever a process request for an I/O,and also whenever the process returns from an I/O.The number of the times the process will enter the ready queue during its lifetime(not counting the time the process enters the ready queue when it is run initially )is —-

int main()

{

int x=0,i=0;
scanf(“%d”,&x); // I/O 1
for(i=0; i<20; i++){
x=x+20;
printf(“%d\n”,x); // I/O 20 times
}

return 0;

}

Answer: 21

Har I/O pe 2 transition hote hai:
Running –(I/O request)–> Blocked (I/O Queue) –(I/O complete)–> Ready –(Dispatch)–> Running

1.scanf ke liye: Running -> I/O Queue -> Ready me 1st entry

2.Loop me 20 baar printf hai:

  • Har printf pe Running -> I/O Queue -> Ready me entry
  • 20 baar = 20 entries

Total = 1 + 20 = 21 times

Yaad karne ki Trick:

Jitne I/O operations, utni baar ready queue me wapas aayega. Initial start

ko count mat karo. Toh 1 scanf + 20 printf = 21.

Q 33. [GATE 2024 Set-2 : 1M] Consider a Process P running on a CPU which one or more of the following events will always trigger a context switch by the OS that results in process P moving to a non -running state(e.g., ready, blocked)?

(a)A Timer interrupt is raised by the hardware.
(b) P tries to access a page that is in the swap space, triggering a page fault
(c) An interrupt is raised by the disk to deliver data requested by some other process
(d) P makes a blocking system call to read a block of data from the disk.

Answer: (b) and (d) only

Option (b) Page Fault -> ALWAYS Block
P ko ek page chahiye jo RAM me nahi, swap space me hai. Disk se lane me 5-10ms lagega. CPU wait nahi karega. OS P ko Blocked state me daal dega aur dusra process chalega. Ye hamesha hoga.

Option (d) Blocking System Call -> ALWAYS Block
P ne bola read(disk). Jab tak data disk se nahi aata, P ka koi kaam nahi hai. Isliye OS usko Blocked kar deta hai. Ye bhi hamesha.

Option (a) Timer Interrupt -> NOT always non-running
Timer interrupt har time-slice ke baad aata hai. Scheduler dekhta hai ready queue me kaun hai. Agar ready queue khali hai to wahi P wapas Running me aa jata hai. P kabhi non-running hua hi nahi. Isliye always nahi.Option

(c) Disk interrupt for OTHER process -> P ka kuch lena dena nahi
Ye interrupt dusre process ke liye hai. P to mast running hai. P par koi asar nahi.

Trick: Question me P moving to a non-running state likha hai. Matlab P ko hi block hona hai. (b) aur (d) me P khud wait kar raha hai.

Q 34. [GATE 2019 Set-1 : 1M]Consider the following C-program is executed on a Unix/Linux system:

#include<unistd.h>

int main()

{

int i;
for(i=0; i<10; i++)

if (i%2==0)fork();
}
return 0;

}

Solution: 31

Concept for Students:

fork() ka matlab process double.

  • i=0 (even): 1 process -> 2 process
  • i=1 (odd): No fork -> 2 process
  • i=2 (even): 2 -> 4 process
  • i=3 (odd): 4 process
  • i=4 (even): 4 -> 8 process
  • i=5 (odd): 8 process
  • i=6 (even): 8 -> 16 process
  • i=7 (odd): 16 process
  • i=8 (even): 16 -> 32 process
  • i=9 (odd): 32 process

Total processes = 32
Child processes = Total – 1 (original parent) = 31Formula: Number of even forks = 5 (0,2,4,6,8) => 2^5 = 32

Q 35. [GATE 2024 Set-1 : 1M] Which transition is NOT possible?
(a) Waiting to Running
(b) Ready to Waiting
(c) Running to Terminated
(d) Running to Ready

Answer: (a) and (b)

Full State Diagram Trick –

New –A–> Ready –B–> Running –D–> Terminated
^ | | |
| C | F
E v v v
Ready <—- Blocked/Waiting

Reason:

a) Waiting to Running = NOT POSSIBLE – Koi bhi process direct Waiting se Running me nahi ja sakta. Pehle uska I/O complete hoga, wo Waiting -> Ready (E) jayega, fir scheduler Ready -> Running

(B) karega.(b) Ready to Waiting = NOT POSSIBLE – Ready state ka matlab hai “CPU milne ka wait”. Uske paas CPU hi nahi hai to I/O request kaise karega? Pehle Ready -> Running hoga, fir Running -> Waiting (F) hoga.

(c) Running to Terminated = POSSIBLE – Process khatam hone par exit() se direct.

(d) Running to Ready = POSSIBLE – Ye hi preemption hai. Time slice khatam hua toh OS usko Ready me bhej deta hai.

Yaad karne ki trick for students:

Waiting aur Ready kabhi direct bhai-bhai nahi ban sakte. Dono ko milne ke liye Running ka bridge chahiye.

Question:36 [GATE 2023 : 1M] – Threads TRUE/FALSE

Which of the following statements about threads is/are TRUE?(a) Threads can only be implemented in Kernel space
(b) Each thread has its own file descriptor table for open files
(c) Threads belonging to a process are by default not protected from each other
(d) All the threads belonging to a process share a common stack

Answer: Only (c) – Threads belonging to a process are by default not protected from each other

Table –

THREADS me kya SHARED hota hai (Process level)THREADS me kya PRIVATE hota hai (Har thread ka alag)
Code, Data, HeapStack
File Descriptor Table / Open FilesProgram Counter (PC), Registers, SP
Process ID, Signal HandlersThread ID (TID)

Option-wise GATE Logic:

(a) FALSE: Threads sirf kernel me nahi, User space me bhi implement hote hai. Ex: pthreads user-level library. Do types hote hai: ULT and KLT.

(b) FALSE: File table share hoti hai. Ek thread ne open() kiya to dusra thread usi fd se read() kar sakta hai.

(c) TRUE: Yehi main point hai. Process me memory protection hota hai (ek process dusre ka memory corrupt nahi kar sakta), lekin threads me by default koi protection nahi. Ek thread dusre thread ka stack/data kharab kar dega to OS rokega nahi. Isi wajah se race condition hota hai.

(d) FALSE: Agar common stack hota to function call return kaise pata chalta? Har thread ka apna alag stack hota hai. Common stack hota to sab ulta-pulta ho jata.

Question:37 [GATE 2023 : 1M]-Which one or more of the following options guarantee that a computer system will transition from user mode to Kernel mode?

a) Function call

(b) Malloc call

(c) Page fault

(d) System call –

Solution -(c)and (d)

a) Function call – FALSE: Normal function call user mode me hi rehta hai. No trap.

(b) Malloc call – FALSE: malloc() ek library call hai. Kabhi-kabhi internally brk()/mmap() system call kar deta hai, but guarantee nahi hai. Agar heap me jagah hai toh kernel me jayega hi nahi.

(c) Page fault – TRUE: Page fault is a Hardware Trap / Exception. CPU forcefully kernel mode me chala jata hai page laane ke liye

.(d) System call -ye bhi true hai .

Question:38 [GATE 2023 : 1M]-Which one or more of the following need to be saved on a context switch from one thread (T1) to another thread (T2) of the same process?

(a)Page table base register

(b)Stack pointer

(C)Program counter

(d)General purpose registers

Answer: (b), (c), (d)

RegisterSave hoga?Kyun?
Page Table Base Register (PTBR)NOSame process hai toh Page Table same hai. Address space change nahi hua. Isiliye TLB flush bhi nahi hota.
Stack Pointer (SP)YESHar thread ka apna alag stack hota hai.
Program Counter (PC)YESHar thread alag jagah execute ho raha hai.
General Purpose RegistersYESHar thread ka apna computation state.

Trick: Process to Process hota to 4 ke 4 save hote. Thread to Thread (same process) me sirf PTBR bachta hai.

Question:39 [GATE 2021 Set-1 : 1M]-Which of the following standard C library functions will always invoke a system call when executed from a single-threaded process in a UNIX/Linux operating system?

(a) exit (b) strlen (c) sleep (d) malloc

Answer: (a) exit and (c) sleep [MSQ]

This question checks concept of User Mode vs Kernel Mode. Library function != System call.

1. strlen() – NEVER system call
Pure user-space loop hai. String ke end tak \0 dhundhta hai. Memory already process ke paas hai. Koi kernel help nahi chahiye. So NOT correct.

2. malloc() – SOMETIMES system call
malloc() heap manage karta hai user space me. Jab process start hota hai, OS pehle se hi kuch heap de deta hai. Agar malloc(100) maango aur free space hai to bina syscall ke de dega.
Tabhi syscall brk()/sbrk()/mmap() call karta hai jab heap khatam ho jaye. Isliye always nahi hai. NOT correct.

3. exit() – ALWAYS system call
Process ko terminate karna privileged kaam hai. File close, memory free, PCB delete — ye sab sirf kernel kar sakta hai. Isliye exit() andar exit_group() / _exit() system call ko hamesha call karta hai.

4. sleep() – ALWAYS system call
CPU ko khali chodna aur timer lagana. Process ko blocked state me daalna padega. Ye scheduler ka kaam hai, jo kernel me hai. sleep() hamesha nanosleep() system call karta hai.

Conclusion: exit and sleep hamesha kernel me jayenge.

Key Point for Students: Yaad rakhne ki trick: Jo kaam user space me ho sakta hai (string length, heap check) wo syscall nahi hai. Jo kaam OS ke bina impossible hai (process khatam karna, CPU chodna) wo hamesha syscall hai.

Question:40 [GATE 2020 : 1M]Consider the following statement about process state transitions for a system using Preemptive scheduling .
I.A Running process can move to Ready state
II. A Ready process can move to Running state
III. A Blocked process can move to Running state
IV. A Blocked process can move to Ready state

Which of the above statements are TRUE?

Answer: (d) I, II and IV only

Ye 5-state model ka sabse favourite question hai.

I. Running -> Ready : TRUE
Preemptive ka matlab hi ye hai. Jaise time quantum khatam ho gaya (Round Robin me) ya koi high priority process aa gaya (SRTF me) toh running process ko wapas ready queue me bhej diya jata hai. Isko Preemption bolte hain.

II. Ready -> Running : TRUE
Ye toh sabse common hai. Scheduler jab kisi ready process ko CPU deta hai, use Dispatch bolte hain.

III. Blocked -> Running : FALSE – Ye sabse badi galti hai
Koi bhi process I/O wait se direct CPU pe nahi jaa sakta. Pehle usko ready queue me aana padega. OS ko chance dena padega ki wo decide kare kisko chalana hai. Direct Blocked se Running impossible hai.

IV. Blocked -> Ready : TRUE
Jab I/O complete ho jaye (jaise file read ho gayi), toh blocked process ready ho jata hai.

Diagram:
Running –(preempt)–> Ready –(dispatch)–> Running
Running –(I/O request)–> Blocked –(I/O complete)–> Ready

Isliye sahi option I, II, IV hai.

Leave a Comment

Your email address will not be published. Required fields are marked *