Assignment 01: System Calls and OS Structures

Self-Assessment

Instructions: Answer the following questions concisely. For code analysis and architectural design questions, ensure you justify your reasoning based on operating system principles.

Part A: Core Concepts and Workflows

  1. System Call Definition: In your own words, define what a system call is and explain why user programs cannot bypass this interface to interact directly with hardware.
  2. The Execution Flow: Outline the exact lifecycle of a system call, starting from the user application and ending at the kernel service routine. List the intermediate steps in order.
  3. The Kernel Boundary: Why do system calls require a dedicated CPU instruction (such as a trap to kernel mode) rather than functioning like standard C function calls?
  1. Definition: A system call is a controlled interface through which user programs request services from the operating system kernel, such as file access, process creation, or device control. User programs cannot bypass this interface because system calls enforce privilege separation, ensuring secure interaction between user and kernel space.

  2. Execution Flow: The correct sequence is:

  • User Application.

  • Function Call to Library Function (e.g., libc).

  • Syscall Instruction triggering the System Call Interface.

  • Kernel Executes the request.

  • Kernel Service Routine.

  1. Kernel Boundary: System calls require a switch to kernel mode because user programs must invoke a lower-level syscall instruction that triggers a CPU trap. This ensures privileged operations (like I/O and memory management) remain strictly isolated from unprivileged user space.

Part B: System Call Categorization

Match the following specific system calls to their correct functional category. Provide a brief explanation of what the system call does.

System Calls:

  1. fork()
  2. ioctl()
  3. pipe()
  4. gettimeofday()
  5. open()

Categories:

  1. fork(): Process Control. It is used to create processes and is security-critical as it affects process isolation.

  2. ioctl(): Device Management. It is security-sensitive because it interacts directly with hardware.

  3. pipe(): Communication. It is performance-sensitive and is used in Inter-Process Communication (IPC).

  4. gettimeofday(): Information Maintenance. It is a lightweight call used for system bookkeeping.

  5. open(): File Management. It is performance-sensitive due to frequent I/O operations.

Part C: Architectural Design and Trade-offs

  1. Monolithic vs. Microkernel: Compare and contrast the Monolithic kernel architecture with the Microkernel architecture. In your comparison, address the primary trade-off between execution speed and system reliability.
  2. Architectural Application: Assume you are the lead architect for the following computing systems. Which classical OS architecture would you choose for each, and why?
  1. Monolithic vs. Microkernel:
  • Monolithic: All OS services (file system, memory, device drivers) run in kernel space as a single large binary. It is fast due to direct function calls, but suffers from poor modularity, meaning bugs can crash the entire system.

  • Microkernel: Only essential services (IPC, scheduling) run in kernel space, while others run in user space. This provides high modularity and better fault isolation, but introduces a performance overhead due to Inter-Process Communication (IPC).

  1. Architectural Application:
  • Autonomous Drone: A microkernel architecture is favored here, as microkernels are preferred in embedded and safety-critical systems for their reliability and modularity.

  • Desktop OS: A hybrid kernel (or monolithic kernel) is appropriate, as modern desktop OSes like macOS and Windows use hybrid kernels to balance performance and reliability. Linux itself uses a hybrid monolithic model to remain fast but extensible.

Part D: Theoretical Code Analysis

Review the following conceptual C code snippet and answer the related questions.

#include <unistd.h>
#include <stdio.h>
int main() {
    pid_t pid = fork();
    if (pid == 0) {
        printf("Child Process\n");
    } else {
        printf("Parent Process\n");
    }
    return 0;
}
  1. Process Duplication: When the fork() system call is executed, what lower-level syscall does the Linux operating system use internally to implement it?
  2. Memory State: Immediately after the fork() execution succeeds, describe the state of the child process regarding its Process ID (PID) and its memory space relative to the parent.
  1. Process Duplication: Internally, Linux uses the clone() syscall to implement fork().

  2. Memory State: The child process is assigned a new PID. Furthermore, the child receives a complete copy of the parent’s memory space.

Part E: Advanced Synthesis & Application (Exam Preparation)

Instructions: These exercises require you to synthesize multiple concepts from the lecture. Provide detailed, well-reasoned answers.

Exercise 1: The strace Investigation (System Boundary Analysis) In the laboratory, you are tasked with executing the command strace ls on a standard Linux terminal to observe how processes interact with the kernel in real time.

  • Grading Criteria: The student must correctly identify categories and match them to logical syscalls.
  • Expected Answer:
  1. Process Control: The shell must use fork() and execvp() to spawn the ls process.

  2. File Management: The process must use open() to access the directory, and read() or write() to process the contents.

  3. Information Maintenance: The process may query metadata using bookkeeping calls.

Exercise 2: Architectural Case Study Defense You have been hired as the lead systems architect for a medical technology company developing the operating system for a next-generation, life-support ventilator.

  • Grading Criteria: The student must select Microkernel and justify it using fault isolation.
  • Expected Answer: A Microkernel is the correct choice, as they are favored in embedded and safety-critical systems for their reliability and modularity. In a Monolithic kernel, a bug in a single module (like a device driver) can crash the entire system. For a life-support ventilator, this lack of fault isolation is a catastrophic risk. While a microkernel introduces performance overhead due to IPC message passing, the superior fault isolation guarantees that a non-essential service failure will not halt the core life-support scheduling.

Exercise 3: Inter-Process Communication (IPC) Code Tracing Analyze the following theoretical C snippet designed to send data from a parent process to a child process using a pipe.

#include <unistd.h>
int main() {
    int fd[2];
    pipe(fd);
    if (fork() == 0) {
        close(fd[1]);
        dup2(fd[0], STDIN_FILENO);
        execlp("wc", "wc", "-w", NULL);
    } else {
        close(fd[0]);
        write(fd[1], "hello world\n", 12);
        close(fd[1]);
    }
    return 0;
}
  • Grading Criteria: Correct categorization of syscalls and understanding of pipe file descriptors.

  • Expected Answer:

  • Process Control: fork() and execlp().

  • Communication: pipe() and write().

  • Challenge Explanation: The pipe() system call creates a unidirectional data channel. fd[0] is the read end, and fd[1] is the write end. The child must close the write end (fd[1]) because it is only reading from the parent. The parent must close the read end (fd[0]) because it is only writing. If these are not closed, the kernel will not send an End-Of-File (EOF) signal to the reader when the writer finishes, causing the wc program to hang indefinitely waiting for more input.

Exercise 4: The Kernel Module Challenge (Exploratory) Modern operating systems allow for dynamic extensibility.

  • Grading Criteria: Understanding of the system call interface and privilege separation.
  • Expected Answer: The logging module must intercept data at the System Call Interface or within the Kernel Service Routine. It cannot be implemented purely in user space (e.g., by modifying libc) because user applications can easily bypass libc and invoke the syscall instruction directly. To guarantee all system calls are logged, the interception must happen after the CPU switches to kernel mode, enforcing strict privilege separation.