Operating Systems Lecture 3: System Calls for Process Management
Understanding Operating System APIs and Process Management
Introduction to API and System Calls
- The lecture focuses on the Application Programming Interface (API) provided by operating systems for user programs to create and manage processes.
- An API consists of system calls, which are function calls that allow user programs to interact with the operating system at a higher privilege level.
Characteristics of System Calls
- System calls enable access to sensitive operations like reading from disk or allocating memory, which regular functions cannot perform.
- Some system calls are blocking; for instance, if a process requests a file read, it may be temporarily removed from CPU execution until the operation completes.
Portability Across Operating Systems
- Different operating systems have unique system calls, but the POSIX API standardizes common system calls across major OS platforms.
- Programs written using POSIX-compliant APIs can run on any compatible operating system without needing rewrites.
User Interaction with System Calls
- Most user programs do not directly invoke system calls; instead, they use high-level functions provided by libraries (e.g., C library).
- For example, rather than calling a write system call directly, programmers typically use
printf, which internally invokes the appropriate system call.
Process Creation and Management in Unix-like Systems
Key System Calls for Process Management
- The primary system call for creating processes is
fork, which generates a child process from a parent process.
- Processes cannot be created independently; every new process originates from an existing one. The init process is the first created by the OS.
Understanding Fork Operation
- When
forkis called, it duplicates the parent’s memory image into a new child process. Both processes then execute independently.
Understanding Parent-Child Process Relationships
Process Creation and Memory Management
- When a child process is created, it initially has a copy of the parent's memory. However, both processes can modify their memory independently after creation.
- The
forksystem call creates an exact duplicate of the parent process, resulting in two processes running the same code but with different execution paths based on their return values.
- The return value from
forkis crucial: it is zero in the child process and non-zero (the child's PID) in the parent process, allowing differentiation between them.
Execution Flow After Forking
- Upon executing a program that uses
fork, both parent and child processes print output to standard output, demonstrating they are running concurrently yet independently.
- Processes can terminate normally by calling
exit, or abnormally due to illegal operations. This leads to different cleanup scenarios for terminated processes.
Zombie Processes and Cleanup
- Terminated processes do not immediately disappear; they become "zombie" processes until their parent calls
wait, which cleans up these zombie states.
- If a parent does not call
wait, its terminated child remains as a zombie, consuming resources until cleaned up by another process.
Handling Orphaned Processes
- If a parent exits before its child terminates, the init process adopts the orphaned child to ensure proper cleanup.
- The init process acts as a caretaker for orphaned children, ensuring no resources are wasted on uncleaned zombie processes.
Importance of Wait System Call
- It’s essential for any terminating process to be reaped via a wait call; otherwise, it will remain as a zombie indefinitely.
- A simple example illustrates how parents use
waitto block execution until their children finish processing.
Using exec System Call
Transitioning Between Executables
- After using
fork, if the parent wants the child to execute different code rather than duplicating its own, it employs theexecsystem call.
Understanding Process Management in Operating Systems
The Role of Fork and Exec System Calls
- The process management begins with the
forksystem call, which allows a parent process to spawn a child process. This is crucial for creating new processes within an operating system.
- The child process can execute a different program using the
execfamily of functions, such asexecvp, allowing it to run any specified executable instead of just duplicating the parent's code.
- After spawning, the parent process typically waits for the child to complete its execution using the
waitsystem call. Understanding these calls is essential for grasping how shells operate.
What is a Shell?
- A shell acts as an interface between users and the operating system, created by the init process during system initialization. It facilitates user command execution.
- A simple shell provides a command prompt where users can input commands. Upon receiving input, it forks a child process and executes commands via
exec.
Command Execution Flow
- When a user types a command like
ls, the shell forks a child process that runs this command. The output from this command is then displayed back to the user after completion.
- Once the child finishes executing (e.g., running
ls), it terminates, and control returns to the shell for further user input.
Advanced Shell Functionality
- Beyond basic execution, shells can manipulate child processes in various ways, such as redirecting output from commands to files instead of displaying them on screen.
- For example, when redirecting output from
lsto a file (output.txt), the shell changes file descriptors so that standard output points to this file rather than displaying on-screen.
Example Code Explanation
- In practical terms, when implementing redirection in code:
- The parent spawns a child.
- The child's standard output is closed and redirected to another file.