Systemcalls
A guided introduction to the boundary between user processes and the kernel, process creation and replacement, child-process management, and signals.
The Boundary and System-Call Interface
The Boundary and System-Call Interface
A running process can execute computational instructions and access memory, but it cannot directly manage other processes, access files, or interact with input and output devices. These are privileged operations, reserved for the operating system’s . This boundary lets the OS decide whether an operation is allowed—for example, it can enforce file-access rules that prevent an untrusted user from reading other users’ passwords.
When a process needs a privileged service, it requests it through a . You can think of a as a special function that transfers a request from user code into space for the OS to carry out. This differs from an ordinary function call, which runs code within the process, such as code in the program or a library.
Each operating system offers user code a set of system calls. Together, they form an interface—or abstraction layer—between user code and services. Keeping this set reasonably small can make the OS abstraction easier to implement and secure. is one standardized interface, implemented by systems including Linux and macOS. The material sometimes calls this abstraction “Unix,” but is the standard’s actual name. In systems, functions in the unistd.h header wrap underlying system calls.
The output functions printf and fputc illustrate how this interface can sit beneath familiar code: both eventually use the low-level write to produce output. The point is not that every programmer must call write directly, but that higher-level functions can rely on services through the system-call interface.
Creating and Transforming Processes
Creating and Transforming Processes
: one call, two processes
Calling creates a child process that begins with nearly the same state as the parent: nearly identical register values, the same program-counter position, and the same memory contents. Both processes continue from the point where was called, so without a way to distinguish them they would initially follow the same path.
The return value provides that distinction. On success, returns 0 in the child, while the parent receives the child’s nonzero process ID. A program can check this value and choose different behavior for each process. For example, the source’s simple program checks whether the return value is 0 to print a child-specific greeting, checks whether it is positive to print a parent-specific greeting, and reports an error if the value is negative. The check matters because the two processes otherwise begin with nearly the same state. In that sense, is unusual: it returns in both the parent and the child.
: replace the program in place
The family changes what the current process is running: it replaces the program and its memory contents with those of a new executable, then starts execution in that program. It does not create or destroy a process. The existing process is transformed in place.
The source demonstrates one variation, execl. It takes the executable’s path followed by the arguments to pass to the target program. By convention, the target’s first argument, argv[0], is its executable name. Thus the source’s call execl("/bin/ls", "ls", "-l", NULL) gives execl the path to the executable, supplies ls as the target program’s first argument, passes -l as its next argument, and ends the variadic argument list with NULL. The resulting program is ls -l.
Keep the two operations distinct: makes a child process; changes the program running in the process that calls it. Their combination becomes useful when a parent needs to run a separate command without replacing its own program.
Finishing and Launching Child Processes
Finishing and Launching Child Processes
terminates the process that calls it. When a program creates a child with , the parent can use to wait for that child to finish. The parent supplies the child’s process ID; it can also provide an optional place for status information and options. The call blocks until the specified process finishes. Programs that create children will usually want to wait for all the children they .
Putting the steps together
To run a separate command while keeping the parent process, the source combines , child-side , and parent-side waiting:
Call
to create a child.In the child, call
to replace the child’s program with the command to run.In the parent, call
with the child’s process ID so the parent waits for it to finish.
The source’s example uses execl("/bin/ls", "ls", "-l", NULL) in the child to run ls -l, while the parent calls (pid, NULL, 0). Here, pid is the process ID returned to the parent by ; the NULL and 0 are the example’s arguments for status information and options. If the call fails, the example reports an error. The example also checks whether failed before treating its return value as a parent or child case.
This pattern separates responsibilities: the child becomes the command, while the parent remains its original program and can wait for the child to finish. Waiting is not the same as launching—the and child-side launch the command; coordinates the parent with the child’s completion.
Signals and Process Communication
Signals and Process Communication
System calls let a process request services from the . Signals provide a way for the to communicate with processes. Each value has a meaning: it represents something the , or another process acting through the , wants to communicate. Some signals also direct the to take an action, such as terminating a program.
A process can register a , a function to run when a particular arrives. Receiving the interrupts the process’s normal flow so the registered function can run. The source’s example registers a handler for SIGINT using (SIGINT, handle_signal). That handler prints a message and calls , terminating the process. In the example, the main program repeatedly prints that it is running and sleeps; the handler is the response set up for SIGINT.
Processes can also ask the to send signals. kill(pid, sig) requests that sig be sent to the process identified by pid; raise(sig) requests that the be sent to the current process. In this way, signals can support communication and coordination between processes, while the remains involved in delivering them.