System Call · Operating System
16 questions · asked in 11 of 27 years (2000–2026)
MCQ 11 · MSQ 2 · NAT 31-mark 12 · 2-mark 4
2000Questions per year2026
Questions 1–5 of 16
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 printf statement gets executed is ________. (answer in integer)Answer: 4
Trace the loop. Remember that after , the child gets the return value and runs , so it goes to the next loop round. The parent gets a nonzero value and runs , so it leaves the loop at once.
- : The original process forks. The parent breaks at once, so 1 process reaches printf with . The child continues the loop.
- (in that child): It forks again. This process, now the parent of its own fork, breaks, so the 2nd process reaches printf. Its child continues.
- (in that grandchild): It forks again. This one breaks, so the 3rd process reaches printf. Its child continues.
- (in the great-grandchild): The loop condition fails, so the loop ends by itself with no more forks. This is the 4th process to reach printf.
So exactly different processes each run the statement once.
Answer: 4
- : The original process forks. The parent breaks at once, so 1 process reaches printf with . The child continues the loop.
- (in that child): It forks again. This process, now the parent of its own fork, breaks, so the 2nd process reaches printf. Its child continues.
- (in that grandchild): It forks again. This one breaks, so the 3rd process reaches printf. Its child continues.
- (in the great-grandchild): The loop condition fails, so the loop ends by itself with no more forks. This is the 4th process to reach printf.
So exactly different processes each run the statement once.
Answer: 4
Consider the following code snippet using the fork() and wait() system calls. Assume that the code compiles and runs correctly, and that the system calls run successfully without any errors.
int x = 3;
while(x > 0) {
fork();
printf("hello");
wait(NULL);
x--;
} The total number of times the printf statement is executed is ___Answer: 14
In every iteration, each process that exists calls fork(). So the number of processes becomes double, and every one of them prints once. (wait() changes only the order of printing, not the count. Each child carries on with the loop using the current x.)
- x = 3: 1 2 processes, so 2 prints
- x = 2: 2 4 processes, so 4 prints
- x = 1: 4 8 processes, so 8 prints
Total prints:
Answer: 14
- x = 3: 1 2 processes, so 2 prints
- x = 2: 2 4 processes, so 4 prints
- x = 1: 4 8 processes, so 8 prints
Total prints:
Answer: 14
Which one or more of the following options guarantee that a computer system will transition from user mode to kernel mode?
One or more options are correct.
Answer: (C), (D)
See which events always take the system into the kernel:
- Function call: it runs fully in user mode so there is no mode switch.
- malloc: it is a library function. Most of the time it takes memory from the heap that already exists, and only sometimes does it make a system call so it is not guaranteed.
- Page fault: it is an exception that the OS handles so it is always kernel mode.
- System call: it is a trap into the kernel so it is always kernel mode.
Answer: Page Fault, System Call
- Function call: it runs fully in user mode so there is no mode switch.
- malloc: it is a library function. Most of the time it takes memory from the heap that already exists, and only sometimes does it make a system call so it is not guaranteed.
- Page fault: it is an exception that the OS handles so it is always kernel mode.
- System call: it is a trap into the kernel so it is always kernel mode.
Answer: Page Fault, System Call
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?
One or more options are correct.
Answer: (A), (C)
Let us look at each function:
- exit: it has to ask the kernel to end the process so it is always a system call.
- sleep: it has to ask the kernel to suspend the process so it is always a system call.
- malloc: it is often served from heap memory that is already allocated. It calls brk/mmap only some of the time.
- strlen: it is a pure user-space computation.
Answer: exit, sleep
- exit: it has to ask the kernel to end the process so it is always a system call.
- sleep: it has to ask the kernel to suspend the process so it is always a system call.
- malloc: it is often served from heap memory that is already allocated. It calls brk/mmap only some of the time.
- strlen: it is a pure user-space computation.
Answer: exit, sleep
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;
} The total number of child process created is __________ .Answer: 31
fork() runs only when i is even, that is for i = 0, 2, 4, 6, 8 so 5 times. Each time, every process that exists at that point forks.
Total processes . One of them is the original process, so the number of children .
Answer: 31
Total processes . One of them is the original process, so the number of children .
Answer: 31
Keep practising