IITP · Semester ISummaries · Quizzes · Assignments

Lecture summary

FCS 10 Oct

10 Oct 20261h 46m3 parts

Source: IITP-big-files\Sem-1\ECS5102-FCS\Lectures\2026-10-10 0000 FCS 10 Oct\summary\notes.md

Overview

This class focused on the practical interface between user-space applications and the operating system kernel via system calls. It began by demonstrating common terminal commands (cat, uname, ps) to illustrate the chain from user input to kernel action. The core activity involved writing, compiling, and tracing simple C programs to observe process creation (fork, getpid) and file I/O (open, read, write) system calls using strace. The session concluded with a discussion on system log management, process visibility, and a preview of upcoming topics like virtual memory and inter-process communication, situating these practical observations within the broader course theme of the hardware/software contract.

Learning outcomes

  • Trace and explain the system calls made by a simple program using strace.
  • Distinguish between user-space library functions and kernel-space system calls.
  • Write a C program that uses fork, getpid, and file I/O system calls.
  • Explain the lifecycle of a process, including parent-child relationships and the concept of a zombie process.
  • Describe how file descriptors are used by the kernel to manage open files for a process.

Core concepts

User space vs. Kernel space

Definition: User space is the memory region where all application programs (e.g., shell, browsers, user-written C programs) run. Kernel space is the protected memory region where the core operating system (the kernel) executes. Why it matters: This separation is a fundamental security and stability mechanism. Applications in user space cannot directly access hardware or privileged kernel data; they must request services via system calls. Connection: This boundary is the central theme of the class, explored through tracing system calls that cross from user space into the kernel.

System Call

Definition: A controlled entry point into the kernel through which a user-space application requests a service, such as reading a file, creating a process, or getting the current time. Why it matters: System calls are the only way for applications to access hardware resources managed by the kernel, enforcing the hardware/software contract. Connection: The entire class session is built around observing system calls (open, read, write, fork, getpid) using the strace utility.

System Call Error Handling

Definition: A failing system call reports the failure through its return value: open() returns -1 and sets errno to an error code describing the cause. Why it matters: Using the returned descriptor without checking for -1 is undefined behaviour. This is the C-level version of "always check the return value" — the same reasoning as in FCS Assignment 4. Connection: The file I/O examples rely on open() succeeding and returning a valid descriptor such as 3.

Operating-System Interfaces

Definition: The documented interface (ABI) that a program uses to talk to the operating system is stable; the internal structures behind it change between versions. Why it matters: Programs can rely on the documented interface across kernel versions even though the implementation underneath is not fixed. Connection: This is why the same system calls (open, read, write, fork) keep working across Linux releases.

Process and Process ID (PID)

Definition: A process is a running instance of a program. The kernel assigns each process a unique Process ID (PID) to identify and manage it. Why it matters: Understanding PIDs is essential for tracking process relationships, resource ownership, and for using tools like ps and strace. Connection: The getpid() system call was demonstrated, and the fork() system call was shown to create a new child process with a different PID.

File Descriptor

Definition: A unique integer handle returned by the kernel when a process opens a file or other I/O resource (like a network socket). It is used in subsequent read, write, and close calls to refer to that resource. Why it matters: File descriptors abstract the details of the underlying resource (file on disk, pipe, device) and are a core mechanism for I/O within a process. Connection: In the file_demo.c example, the open system call returned file descriptor 3, which was then used for read and close.

Fork and Parent-Child Relationship

Definition: fork() is a system call that creates a new child process, which is an almost identical copy of the parent process. The parent's PID becomes the child's Parent Process ID (PPID). Why it matters: This is the primary mechanism for process creation in Unix-like systems. It establishes a relationship where the parent typically waits for the child to collect its exit status. Connection: A fork demo program showed the parent and child PIDs, and strace revealed the underlying clone system call and the parent's wait4 call.

Fork vs. Exec

Definition: fork() creates a new child process (a copy of the parent); exec() replaces the calling process's program image with a new program. Why it matters: The usual pattern is fork() then exec() in the child, with wait() in the parent. The two calls do different jobs and are not interchangeable. Connection: The fork demo used only fork(); the shell commands discussed earlier rely on the fork-then-exec pattern.

Failure and Termination

Definition: Ordinary system call failures return an error code (-1 plus errno); the process itself decides what to do next. Why it matters: A failed system call does not by itself terminate the process. Only signals (e.g. SIGSEGV) or an explicit exit() terminate it. Connection: This is why the parent in the fork demo calls exit_group(0) explicitly rather than relying on a failed call to end it.

Formulas and maths

(Not covered in this session)

Worked examples

Observing Shell and Process IDs

  1. Shell PID: Ran echo $$ in the terminal, which displayed the PID of the current shell (e.g., 2006).
  2. Process List: Ran ps, which showed active processes including sudo, su, bash, and ps itself.
  3. Changing User Context: Exited root shell and re-entered with sudo su -. The new shell had a different PID (2930), demonstrating that a new process was created.

Simple C Program (identity.c)

  1. Program Purpose: Printed process ID and system information using getpid() and uname().
  2. Compilation: gcc -Wall -Wextra identity.c -o identity
  3. Execution Output:
    • Process ID: 3256
    • System: Linux
    • Node name: IIT partner 2026 autumn
    • Release: 7.0.0.34 generic
    • Machine: x86_64
  4. Targeted Tracing with strace:
    • strace -e uname ./identity: Traced only the uname system call, showing it was called by process 3283.
    • strace -e getpid,write ./identity: Traced getpid and write calls. Showed getpid returning PID 3298 and write being called from printf.

File I/O Program (file_demo.c)

  1. Program Flow:
    • Opened a file with open("demo.txt", O_CREAT | O_TRUNC | O_RDWR, 0644). Permission 0644 means: owner can read/write (6), group can read (4), others can read (4).
    • Wrote a string, then used lseek(fd, 0, SEEK_SET) to move the file offset back to the start.
    • Read data into a buffer and printed the number of bytes read.
  2. Output: The program printed the file descriptor (3) and "bytes returned is 25" (the length of "transaction id transaction 1001").
  3. Tracing: strace ./file_demo showed the sequence of openat, write, lseek, read, and close system calls, with file descriptor 3 used throughout.
  4. Error Handling: If open() had failed it would have returned -1 and set errno; using that descriptor without checking would be undefined behaviour.

Fork Program (fork_demo.c)

  1. Program Flow:
    • Printed parent PID before fork() (e.g., 3764).
    • After fork(), the parent printed its PID (3764) and the child's PID (3765), then waited.
    • The child printed its own PID (3765) and its parent's PID (3764), then exited.
  2. Tracing with strace -f: The -f flag follows child processes.
    • Command: strace -f -e clone,wait4,exit_group ./fork_demo
    • Observed: Parent process 3791 called clone to create child 3792.
    • Parent 3791 then called wait4 for child 3792.
    • Child 3792 called exit_group(0) to terminate.
    • Parent 3791 finally called exit_group(0).

Attempted Zombie Process Demo

  1. Goal: To create a child process that terminates before its parent reads its exit status, leaving it in a "zombie" state.
  2. Attempt: Lecturer ran a program where the parent sleep()s for 30 seconds after forking. The child was designed to exit immediately.
  3. Check Command: Used ps -o pid,ppid,state,cmd to look for processes in the zombie (Z) state.
  4. Outcome: The demonstration failed as the ps output did not show the expected zombie. The lecturer noted the program needed fixing and deferred the demo to the next session.

Questions asked in class

  • Q: Could you please share the notes for this lecture?
  • A: Yes, a PDF with codes and explanations will be uploaded. Assignment 2 is already uploaded.
  • Q: Do we have something similar for already running processes?
  • A: Yes, tracing is not only for initiated processes; it is also for already running processes. System logs record activity continuously. strace is a basic utility; there are other tools (some with GUI) that provide more detailed information.
  • Q: What is a system call and scheduling and context switch?
  • A: System call was covered. Scheduling and context switch will be covered in the next session if time permits, otherwise tomorrow.
  • Q (Anupam): Is there any limit to how long system logs are stored?
  • A: The limit is something we define (configurable). Not a fixed duration like 30 years.
  • Q: If multiple users log into the same system, can they see each other's processes?
  • A: It depends on privileges. Administrators can see all processes. Normal users can typically see only their own processes. Visibility can also be based on group membership.
  • Q: What is the best book for the course?
  • A: The primary recommendation is Operating System Concepts, 7th Edition by Galvin, Gagne, and Silberschatz (Wiley and Sons). The institute also suggests another book, but the lecturer advises starting with the Galvin book.

Key takeaways

  • User-space applications cannot directly access hardware; they must request services from the kernel via system calls.
  • The strace utility is a powerful tool for observing the system calls made by a program, revealing its interaction with the kernel.
  • A file descriptor is an integer handle returned by open() and used for subsequent I/O operations like read() and write() within a process.
  • open() returns -1 and sets errno on failure; using the returned descriptor without checking is undefined behaviour.
  • The fork() system call creates a new child process, which is a copy of the parent. The child gets a new PID, and the parent's PID becomes the child's PPID.
  • fork() creates a copy of the parent process, while exec() replaces the calling process's program image with a new program; the usual pattern is fork() then exec() in the child, wait() in the parent.
  • After forking, the parent should call wait() (or a variant like wait4()) to collect the child's exit status and prevent it from becoming a zombie.
  • A zombie process is a terminated child process whose exit status has not yet been read by its parent.
  • Ordinary system call failures return an error code (-1 plus errno) and the process decides what to do; only signals (e.g. SIGSEGV) or an explicit exit() terminate it.
  • The documented operating-system interface (ABI) is stable, while internal structures change between versions.
  • Process visibility in tools like ps is governed by user privileges: administrators can see all processes, while normal users typically see only their own.
  • System logs are continuously recorded by the kernel, and their retention period on local storage is configurable, often managed through log rotation.
  • The exact system call interface differs between operating systems (e.g., Linux vs. Windows), affecting the portability of programs that make direct system calls.
  • Commands like cat, ps, and echo themselves rely on a series of system calls (open, read, write, fork, exec) to perform their tasks.

Exam-style questions

  1. You run strace -e openat,read,write cat /etc/hostname. Describe the sequence of system calls you would expect to see and explain the purpose of each in the context of displaying the file's content.

    • Outline: Expect openat to open the file /etc/hostname, returning a file descriptor. Multiple read calls to read the file content into a buffer. One or more write calls to write that content to file descriptor 1 (standard output). Finally, a close call (if traced) to close the file descriptor.
  2. A C program calls fork(). Immediately after the fork, how can the child process distinguish itself from the parent process in code? What system calls could each use to print its own PID and its parent's PID?

    • Outline: fork() returns 0 to the child and the child's PID to the parent. The child can use getpid() for its PID and getppid() for the parent's PID. The parent can use getpid() for its own PID and already has the child's PID as the return value from fork().
  3. Explain what a zombie process is. What condition creates it, and what system call (or action) by the parent process removes it from the system?

    • Outline: A zombie process is a terminated child process whose exit status is still held by the kernel because the parent has not yet read it via wait() (or similar). The parent must call wait() (or waitpid, wait4) to "reap" the child, at which point the zombie entry is removed from the process table.
  4. State whether each is true or false and justify: (a) the documented operating-system interface is stable while internal structures change between versions; (b) an ordinary system call failure terminates the process.

    • Outline: (a) True — the documented ABI is stable; internal structures change between versions. (b) False — ordinary failures return an error code (-1 plus errno) and the process decides what to do; only signals (e.g. SIGSEGV) or an explicit exit() terminate it.

Glossary

  • User space - The memory area where application programs run, isolated from the kernel.
  • Kernel - The core of the operating system that manages hardware resources and system services.
  • System call - A request by a user-space program for a service from the operating system kernel.
  • errno - The error code set by a failing system call, which also returns -1.
  • Process - A running instance of a program, with its own memory space and system state.
  • PID (Process ID) - A unique numerical identifier assigned to each process by the kernel.
  • File descriptor - An integer handle returned by the kernel when opening a resource, used for subsequent I/O operations.
  • fork() - A system call that creates a new child process by duplicating the calling (parent) process.
  • exec() - A system call that replaces the calling process's program image with a new program.
  • PPID (Parent Process ID) - The PID of the process that created (forked) the current process.
  • Zombie process - A terminated child process whose exit status has not yet been read by its parent.
  • strace - A Linux command-line utility for tracing system calls and signals of a program.

Follow-ups

  • Zombie Process Demonstration: The live demo failed. The lecturer will modify the program and demonstrate it in the next session.
  • Kernel Commands/Debugging: The lecturer took an action item to cover appropriate kernel commands and debugging in a future session.
  • Upcoming Topics: The next session will cover:
    1. mmap (memory mapping) and brk (relating to virtual memory mapping to physical memory).
    2. Inter-process communication (IPC).
    3. Scheduling and context switch (if not covered tomorrow).
  • Assignment/Quiz Schedule:
    • Assignment 2 will be opened by Monday.
    • Quiz 1 will be uploaded by Tuesday/Wednesday and likely open to students by Thursday. It will cover material up to and including the next session's topics.