Fork, Exec, and Beyond: A Deep Dive into Process Management
Table of contents
This article is part of a series into learning In-depth concepts of the operating system and how it runs and manages our programs. The first introduction to this series can be found here: Understanding The Operating System - The Brain Behind Your Computer.
Basic introduction to a process
A process is a running program. yes it is as simple as that.
Essentially, when we write programs, they are saved on disk storage and remain idle until executed. Launching these programs creates a process. When we start up our computer, we run multiple programs—web browsers, code editors, music players, and background services. all of which generate different processes.
The operating system (OS) manages these processes concurrently by virtualizing the CPU. It creates the illusion of an infinite number of CPUs, making each process believe it has the entire CPU to itself.
In reality, if five processes need to run simultaneously, the OS uses a technique called Time sharing: it runs Process A for a short period, switches to Process B, and so on. This rapid switching gives the appearance that all processes are running at the same time.
Creation Of A Process
Programs reside in the disk storage as some sort of executable file. when the program is launched, The first thing the OS does is to read the program byte from storage and loads it on the memory where it creates an address space for that process to store its static data, code instructions and heap.
Before execution begins, it allocates some memory to runtime stack for local variables, function parameters, and return addresses - to track function calls.
The OS will also do some other initialization tasks, particularly as related to input/output (I/O). For example, in UNIX systems, each process by default has three open file descriptors, for standard input, output, and error; these descriptors let programs easily read input from the terminal and print output to the screen.
At this point the OS has one task left to do which is to start the process through it entry point usually main()
for c/c++. After this, the OS transfers control to the CPU after which the process begins its execution.
States Of A Process
A process can be in one of the following states at any given time
Running - The process is actively executing its instructions on the CPU.
Ready - The process is prepared to run but is waiting for the OS scheduler to assign it CPU time.
Blocked - The process started execution but has paused because it is waiting for an I/O operation to complete (e.g., reading a file, waiting for a network response). While blocked, the OS allocates the CPU to another process. Once the I/O operation finishes, an event notifies the OS, and the process moves back to the Ready state.
Process API
In this section, we would be covering two major interfaces available to a running process.
fork()
- When this interface is called inside a running process, a new process is created. this process is usually a replica of the current process but having its own separate resources like CPU virtualization and memory. This new process is referred to as a child and the process that created it is called the parent. when this API is called, we now have two processes of the same program running and now the OS has to allocate and mange each of this process.exec()
- This process interface is important when you want to create a process that is different from the current process. unlikefork()
which creates copies of the main process, theexec()
API will run a different program. it does this by taking in an argument when it is called usually this argument is a program executable. it loads it from the disk storage and overwrite the current address space of the process that created it. It is commonly used in your shell terminal when you run a program.
Beyond fork()
and exec()
, there are a lot of other interfaces for interacting with processes in systems. For example kill()
and wait()
which allows a parent process to pause until its child process terminates.
In this article we covered the basics of a process, how a process is created and we also looked at the states a process can have at any given time. we also covered some major interface provided by a process and how they function.
In the next article, we’ll put this knowledge into practice by demonstrating how to build high-performance backend applications using these core OS concepts. Stay tuned!
Don’t forget to like ❤️, comment 💬 and share🔃
Subscribe to my newsletter
Read articles from Gabriella Amaefule directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by