Open In App

Formation of Process from Program

Last Updated : 14 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A process is a program in execution. It is an active entity that requires system resources such as CPU time, memory, and I/O devices to execute. A program resides in secondary memory (like a hard disk or SSD). For a program to become a process it must go through various actions such as loading the program into memory, allocating memory for the process (code, data, stack, heap), creating a Process Control Block (PCB) to track the process, initializing the CPU context (registers, stack pointer, program counter, etc.), and finally putting the new process into the scheduler’s queue (transitioning it to the “ready” state, and eventually to “running”).

The detailed stages involved in transforming a program into a process are as follows:

1. Program Loading (Loading the Executable into Memory)

  • The first step is loading the program from disk into memory. The operating system’s loader is responsible for this. It locates the executable file on storage and reads its contents (the program’s machine code and static data) into memory.
  • In this stage, the OS typically creates a new address space for the process and begins populating it with the program’s code (text segment) and initialized data.
  • During this loading phase, the OS also prepares basic memory structures for the process’s runtime, such as setting up an initial stack and heap region for the process.

2. Memory Allocation

  • Once the executable’s contents are identified, the OS allocates memory for the new process. The memory manager reserves a block of virtual memory for all the components of the process which includes space for the program’s code (text segment), data segments (initialized and uninitialized data), the runtime heap, and the stack.
  • During this stage, the OS may also load any necessary shared libraries into the process’s address space (e.g., dynamic link libraries) and set up other memory-mapped resources required by the program. 
  • The OS also designates space for the heap. By the end of this stage, the new process has a defined virtual address space with all the regions it will need: code, data, heap, and stack are reserved and ready for use.
Memory-Layout-of-C-Program

3. Creating the Process Control Block (PCB)

  • In parallel with establishing the memory, the OS creates a Process Control Block (PCB) for the new process. The PCB (sometimes called a process descriptor or task control block) is a data structure in the kernel that holds all the metadata about the process.
  • When the process is created, the OS assigns it a unique Process Identifier (PID) and adds an entry for the process in the system’s process table (an index of all active processes). Memory for the PCB itself is allocated (often as part of the kernel’s process table or a separate kernel memory area).

Read more about PCB

PCB-in-OS
PCB

4. Setting Up the Execution Context 

After loading the program and creating the PCB, the operating system must set up the process’s execution context so that it can actually run. This involves preparing the initial stack contents, the heap pointer, and CPU registers:

  • Stack Initialization: The process’s stack region was allocated earlier, now the OS arranges the initial stack frame. This often includes placing the program’s command-line arguments and environment variables onto the stack or in a specific memory region, and setting the stack pointer accordingly.
  • Heap Setup: The heap segment is typically empty at start (no allocations done yet), but the OS establishes a pointer (or break value) that indicates the current end of the heap.  The OS ensures the process has the right to expand its heap up to some limit. In practice, nothing specific is placed in the heap at creation except setting its boundaries. 
  • CPU Registers and Execution Start: The OS initializes the CPU register context for the new process so that it can start execution. This includes setting the instruction pointer (program counter) to the program’s entry point. It also sets the stack pointer register to the top of the stack segment that was set up, and may set other registers . Essentially, the OS prepares a context such that if the CPU were to switch to this process, it would start executing the new program from its beginning with a clean stack.
  • Kernel/Mode Context: The OS also sets up any needed kernel-mode context for the process. For example, it may allocate a kernel stack for the process/thread (used when the process transitions to kernel mode for system calls or interrupts).

5. Transition to Ready State and Scheduling

  • With the new process’s memory and context prepared, the OS can make the process eligible to run. At this point, the process was in a “new” or “created” state (not yet scheduled). The operating system now transitions the process to the Ready state.
  • This means the process is added to the system’s ready queue (or an equivalent scheduler data structure), indicating it is now ready to execute whenever the scheduler chooses it.
  • Once in the ready state, the new process must wait its turn to use the CPU. The scheduler (part of the OS) will decide when to actually start executing this process, based on scheduling algorithms and policies.
  • The new process will remain in the ready queue until the scheduler picks it and dispatches it to the CPU. At that moment, the process state changes to Running, and the CPU begins executing its instructions from the program’s entry point using the context that was set up.
  • The OS ensures that if the process is preempted (e.g., its time slice expires or a higher-priority process needs the CPU), the current CPU context is saved (in the PCB or related structures) so that the process can resume correctly later – this is part of normal scheduling and context switching, not specific to creation but crucial for multi-tasking.
  • Eventually, the process may finish execution or be terminated, at which point the OS will free its resources and remove its PCB from the process table.

Read more about Process Management

Process-Management
State Transition Diagram in process management

Next Article

Similar Reads