AOS Question Paper Previous y
AOS Question Paper Previous y
Ans-a)
False.
The kernel is not a separate set of processes running in parallel to user processes. Instead, it is the
core part of the operating system that manages hardware resources and system calls. It executes in
privileged mode and is responsible for tasks like process management, memory management, and
device control. The kernel operates in the background and does not act as separate processes.
Q-b) What are the 4 different conditions for the pid argument of the kill system call?
Ans-b)
The pid argument in the kill system call can have the following conditions:
1. Positive PID: The signal is sent to the process with the given PID.
2. Zero PID: The signal is sent to all processes in the same process group as the caller.
3. Negative PID: The signal is sent to all processes in the process group specified by the
absolute value of pid.
4. -1 PID: The signal is sent to all processes for which the caller has permission, excluding
special system processes.
Ans-c)
1. wait:
o Does not provide control over which specific child process is waited on.
2. waitpid:
o Waits for a specific child process or a set of child processes to terminate based on
the PID.
Q-d) If we execute lseek(fd, 0, 2), then what will be the new file byte offset?
Ans-d)
lseek(fd, 0, 2) sets the file offset to the end of the file (SEEK_END).
The new file byte offset will be equal to the size of the file in bytes.
Q-e)
Ans-e)
A broken link (also known as a dangling symbolic link) occurs when a symbolic link points to a target
file or directory that no longer exists or has been moved. This makes the link unusable.
Q-f)
Ans-f)
Q-g)
#include<stdio.h>
#include<unistd.h>
int main() {
if (fork() || fork()) {
fork();
printf("2");
return 0;
}
Ans-g)
Let's analyze the code step by step:
1. The first fork() creates one child process, making two processes in total.
2. The parent process proceeds to check the condition (fork() && (!fork())). Depending on the
evaluation, additional processes are created.
Total Processes: The number of processes created and their order of execution depend on how the
fork() calls are evaluated. Since there are multiple nested fork() calls, a total of 10 processes will
execute the printf("2"); statement.
Output: The output will be "2222222222" (ten times "2" printed by each process).
Q2-a-i)
What is a process? Draw and explain the state transition diagram of a process.
Ans-a-i)
A process is an instance of a program in execution. It includes program code, current activity, and
associated resources like memory, files, and CPU time. Processes are the fundamental units of work
in an operating system.
States Explanation:
1. New: The process is created but has not yet been admitted into the ready queue.
2. Ready: The process is ready to run but is waiting for CPU allocation.
Transitions:
6. Running → Ready: When the CPU is preempted or a higher-priority process needs the CPU.
Q2-a-ii)
Ans-a-ii)
1. Page Table:
3. Frame Table:
o Maintains information about which process and page is using each frame.
Q2-b)
Ans-b)
1. alarm()
o Description: Schedules a SIGALRM signal to be sent to the process after the specified
time in seconds.
2. kill()
3. sbrk()
o Description: Adjusts the program’s data segment size by increment bytes. Returns a
pointer to the new program break.
4. execl()
o Syntax: int execl(const char *path, const char *arg, ..., NULL);
o Description: Replaces the current process image with a new process image specified
by path and arguments.
5. fchmod()
Q3-a-i)
Ans-a-i)
The fourth scenario for buffer allocation refers to a situation where both the process requesting the
buffer and the process holding the buffer are of the same priority. In such cases, the allocation
system resolves the deadlock by preempting the buffer from the holding process. This is done to
ensure system efficiency and fairness.
A mechanism must exist to avoid indefinite starvation of the process whose buffer was
preempted.
Q3-a-ii)
#include<fcntl.h>
char c;
if(argc != 2)
exit();
printf(“char%c\n”, c);
}
Ans-a-ii)
Behavior of the program:
1. Purpose:
The program attempts to read characters from a file and seeks forward by 1023 bytes after
every read operation.
o File Descriptor Initialization: The file descriptor fd is not initialized with a valid file
(missing fd = open(argv[1], O_RDONLY);).
o Exit Issue: The exit() call is missing a proper argument, which should ideally be
exit(1);.
3. Expected Behavior:
o If corrected, the program will read one character from the file, print it, then jump
1023 bytes forward using lseek(fd, 1023L, 1), and print the new seek position.
Output Example:
If fixed, the output will look like this (assuming a valid file):
char a
char b
...
Q3-b)
Write a C program to print the type of file for each command-line argument.
Ans-b)
Here is a C program to print the type of file for each command-line argument:
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
perror("stat");
return;
if (S_ISREG(fileStat.st_mode))
printf("Regular File\n");
else if (S_ISDIR(fileStat.st_mode))
printf("Directory\n");
else if (S_ISCHR(fileStat.st_mode))
printf("Character Device\n");
else if (S_ISBLK(fileStat.st_mode))
printf("Block Device\n");
else if (S_ISFIFO(fileStat.st_mode))
printf("FIFO/PIPE\n");
else if (S_ISLNK(fileStat.st_mode))
printf("Symbolic Link\n");
else if (S_ISSOCK(fileStat.st_mode))
printf("Socket\n");
else
printf("Unknown\n");
if (argc < 2) {
exit(1);
print_file_type(argv[i]);
return 0;
}
Explanation:
2. The file type is determined using macros like S_ISREG, S_ISDIR, etc.
Example Output:
Command:
Output:
Q4-a-i)
Ans-a-i)
Pipes are inter-process communication (IPC) mechanisms that allow data to be transferred
unidirectionally between two processes. They act like a buffer, enabling one process to write data
and another to read it.
Types of Pipes:
1. Unnamed Pipes:
o Used for communication between related processes (e.g., parent and child
processes).
o Data flows in one direction, and both processes must share the pipe file descriptor.
Example:
int pipefd[2];
o Appear as a file in the filesystem, enabling processes to open, read, and write using
standard file operations.
Example:
Q4-a-ii)
Ans-a-ii)
When fork() is executed, the kernel performs the following operations:
1. Process Duplication:
o Creates a new process (child) that is an exact copy of the parent process.
2. Copy-on-Write Memory:
o Memory pages of the parent are shared with the child initially. Separate copies are
created only when one process modifies the memory (copy-on-write mechanism).
3. Resource Duplication:
5. Execution State:
Child receives 0.
Q4-b)
Ans-b)
Here is a program that demonstrates a race condition when catching signals:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
int counter = 0;
void signal_handler(int sig) {
int main() {
signal(SIGUSR1, signal_handler);
if (pid == 0) {
// Child process
exit(0);
} else {
// Parent process
return 0;
Explanation:
1. The program sets up a signal handler for SIGUSR1 that increments a global counter.
2. Both the parent and child processes send SIGUSR1 signals, resulting in multiple updates to
the counter in the signal handler.
3. Race Condition: The counter is accessed by both processes without synchronization, leading
to unpredictable final values.
...
Solution to Avoid Race Condition: Use synchronization mechanisms such as semaphores or mutexes
to protect the critical section.
Q5-a-i)
Ans-a-i)
A process is swapped out under the following circumstances:
1. Memory Shortage:
o When the system runs out of physical memory, inactive processes are swapped out
to free up space for active processes.
2. Process Priority:
3. I/O Waiting:
o A process waiting for I/O operations (disk, network, etc.) can be swapped out to
allow other processes to utilize the CPU.
o During high system load, processes may be swapped out to maintain overall
performance and prevent overcommitment of resources.
o The operating system may decide to swap out a process as part of memory
management policies, such as paging or swapping.
Q5-a-ii)
Ans-a-ii)
The structure of a regular file in a Unix-like system consists of:
1. Inode:
o Contains metadata about the file, such as size, permissions, owner, and pointers to
data blocks.
2. Data Blocks:
3. Indirect Blocks:
o Used when the file size exceeds the capacity of direct pointers in the inode. These
include:
Single Indirect Block: Points to a block that contains pointers to data blocks.
Diagram:
Q5-b)
C program that creates a child process to read commands from the standard input and execute
them (no arguments passed).
Ans-b)
Here is a C program to create a child process that reads commands and executes them:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main() {
pid_t pid;
char command[100];
while (1) {
if (strcmp(command, "exit") == 0) {
printf("Exiting...\n");
break;
pid = fork();
if (pid < 0) {
perror("Fork failed");
exit(1);
} else if (pid == 0) {
perror("Execution failed");
exit(1);
} else {
wait(NULL);
return 0;
}
Explanation:
1. Parent Process:
2. Child Process:
3. Exit Command:
Example Output:
<output of ls>
/home/user
Exiting...
Q6-a-i)
Ans-a-i)
A process is swapped out under the following circumstances:
1. Memory Shortage:
o When physical memory is insufficient, inactive processes are swapped out to free
memory for active processes.
2. Process Priority:
4. System Overload:
o During high system load, swapping helps balance the load and ensure better
utilization of resources.
o The operating system may decide to swap out processes based on its memory
management policies.
Q6-a-ii)
Ans-a-ii)
Buffer Pool Structure:
The buffer pool is a collection of buffers maintained in memory to manage disk I/O efficiently.
1. Buffer Headers:
o Contain metadata about each buffer, such as its status, reference count, and pointers
to data blocks.
2. Free List:
o A linked list of buffers that are currently free and can be allocated.
3. Hash Queue:
o Buffers are organized into hash queues based on the block number of the data they
hold. This allows quick lookup of buffers.
Diagram:
Explanation:
Free Buffers: Buffers not currently used are part of the free list.
In-Use Buffers: Buffers holding data from disk and currently accessed are linked via hash
queues for efficient access.
When a process requests a block, the kernel searches the hash queue for the buffer or
allocates a free buffer if not found.
Q6-b)
Ans-b)
1. Hardware Layer:
o Provides the foundation for the operating system to interact with the hardware.
2. Kernel Layer:
3. User Layer:
Diagram:
Explanation:
2. Kernel:
3. User Applications:
o Users execute commands or run applications that interact with the kernel through
system calls.
Q7-a-i)
Ans-a-i)
Process context refers to the state and environment of a process at any given time, which allows it to
resume execution correctly after being interrupted or preempted. It includes all the information
needed by the operating system to manage and execute the process.
1. User Context:
o Includes the user program's state, such as program counter (PC), stack pointer (SP),
and general-purpose registers.
2. Kernel Context:
3. Address Space:
o The memory mapping of the process, including code, data, heap, and stack
segments.
4. Process-Specific Data:
The OS saves and restores the process context during context switching to ensure seamless execution
of processes.
Q7-a-ii)
Ans-a-ii)
Here is a C program demonstrating suspending and resuming a process using SIGSTOP and SIGCONT
signals:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void handle_signal(int sig) {
if (sig == SIGUSR1) {
int main() {
while (1) {
printf("Process running...\n");
sleep(2);
return 0;
Explanation:
1. The program sets up signal handlers for SIGUSR1 (to suspend the process) and SIGCONT (to
resume it).
2. The process can be suspended using the kill -SIGUSR1 <pid> command and resumed using
kill -SIGCONT <pid>.
Output Example:
Process running...
(After sending SIGUSR1)
Q7-b)
What is anonymous memory mapping? What are the advantages of allocating memory via
anonymous memory mapping?
Ans-b)
Anonymous Memory Mapping:
A mechanism where memory is allocated directly to a process without being backed by a file.
The mmap() system call is used with the MAP_ANONYMOUS flag to allocate memory.
The memory is initialized to zero and is only accessible to the process unless shared explicitly.
2. Dynamic Allocation:
3. Memory Isolation:
o The memory is isolated to the process, enhancing security and reducing interference.
o Provides greater flexibility for applications that require specific memory alignment or
allocation patterns.
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
if (ptr == MAP_FAILED) {
perror("mmap");
exit(1);
return 0;