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
- Shell PID: Ran
echo $$in the terminal, which displayed the PID of the current shell (e.g.,2006). - Process List: Ran
ps, which showed active processes includingsudo,su,bash, andpsitself. - 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)
- Program Purpose: Printed process ID and system information using
getpid()anduname(). - Compilation:
gcc -Wall -Wextra identity.c -o identity - Execution Output:
- Process ID:
3256 - System:
Linux - Node name:
IIT partner 2026 autumn - Release:
7.0.0.34 generic - Machine:
x86_64
- Process ID:
- Targeted Tracing with
strace:strace -e uname ./identity: Traced only theunamesystem call, showing it was called by process3283.strace -e getpid,write ./identity: Tracedgetpidandwritecalls. Showedgetpidreturning PID3298andwritebeing called fromprintf.
File I/O Program (file_demo.c)
- Program Flow:
- Opened a file with
open("demo.txt", O_CREAT | O_TRUNC | O_RDWR, 0644). Permission0644means: 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.
- Opened a file with
- Output: The program printed the file descriptor (
3) and "bytes returned is 25" (the length of "transaction id transaction 1001"). - Tracing:
strace ./file_demoshowed the sequence ofopenat,write,lseek,read, andclosesystem calls, with file descriptor3used throughout. - Error Handling: If
open()had failed it would have returned-1and seterrno; using that descriptor without checking would be undefined behaviour.
Fork Program (fork_demo.c)
- 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.
- Printed parent PID before
- Tracing with
strace -f: The-fflag follows child processes.- Command:
strace -f -e clone,wait4,exit_group ./fork_demo - Observed: Parent process
3791calledcloneto create child3792. - Parent
3791then calledwait4for child3792. - Child
3792calledexit_group(0)to terminate. - Parent
3791finally calledexit_group(0).
- Command:
Attempted Zombie Process Demo
- Goal: To create a child process that terminates before its parent reads its exit status, leaving it in a "zombie" state.
- Attempt: Lecturer ran a program where the parent
sleep()s for 30 seconds after forking. The child was designed to exit immediately. - Check Command: Used
ps -o pid,ppid,state,cmdto look for processes in the zombie (Z) state. - Outcome: The demonstration failed as the
psoutput 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.
straceis 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
straceutility 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 likeread()andwrite()within a process. open()returns-1and setserrnoon 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, whileexec()replaces the calling process's program image with a new program; the usual pattern isfork()thenexec()in the child,wait()in the parent.- After forking, the parent should call
wait()(or a variant likewait4()) 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 (
-1pluserrno) and the process decides what to do; only signals (e.g.SIGSEGV) or an explicitexit()terminate it. - The documented operating-system interface (ABI) is stable, while internal structures change between versions.
- Process visibility in tools like
psis 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, andechothemselves rely on a series of system calls (open,read,write,fork,exec) to perform their tasks.
Exam-style questions
-
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
openatto open the file/etc/hostname, returning a file descriptor. Multiplereadcalls to read the file content into a buffer. One or morewritecalls to write that content to file descriptor 1 (standard output). Finally, aclosecall (if traced) to close the file descriptor.
- Outline: Expect
-
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()returns0to the child and the child's PID to the parent. The child can usegetpid()for its PID andgetppid()for the parent's PID. The parent can usegetpid()for its own PID and already has the child's PID as the return value fromfork().
- Outline:
-
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 callwait()(orwaitpid,wait4) to "reap" the child, at which point the zombie entry is removed from the process table.
- 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
-
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 (
-1pluserrno) and the process decides what to do; only signals (e.g.SIGSEGV) or an explicitexit()terminate it.
- Outline: (a) True — the documented ABI is stable; internal structures change between versions. (b) False — ordinary failures return an error code (
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:
mmap(memory mapping) andbrk(relating to virtual memory mapping to physical memory).- Inter-process communication (IPC).
- 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.