low level programming
operating systems
thread management
process initiation
concurrency

low level programming How does the OS start a new thread/process?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction to Low-Level Programming

Low-level programming touches the foundational layer of software development. It deals with areas where abstractions end and direct communication with the hardware begins. One critical aspect of low-level programming is understanding how an operating system (OS) starts a new thread or process. This requires intimate knowledge of system calls, CPU registers, stack and heap memory management, and context switching.


1. Understanding Processes and Threads

Before diving into how operating systems initiate threads and processes, it's imperative to differentiate between them:

  • Process: A process is an instance of a program in execution. It contains its own memory space, including text, data, and heap segments.
  • Thread: A thread is the smallest sequence of programmed instructions that can be managed independently. Multiple threads can exist within a process, sharing its resources such as memory and file descriptors.

2. OS Kernel's Role

The Operating System kernel plays a pivotal role when creating new processes and threads. It manages resources and is responsible for context switching, enabling multiple processes and threads to share the CPU efficiently.

3. System Calls for Process and Thread Creation

System calls are the gateways through which applications request services from the kernel. Some critical system calls for process and thread management include:

  • `fork()`: This is the primary Unix system call used to create a new process by duplicating the calling process. The resulting process is termed as the "child", with a unique process ID.
  • `exec()`: After `fork()`, `exec()` can be called to replace the process's memory space with a new program. This is commonly used in UNIX-like systems.
  • `clone()`: Used in Linux to create threads or processes. It allows finer control over what is shared between the calling process and the child.
  • `CreateProcess()`: A Windows API function that initializes a new process, allocating resources and initiating a new program.
  • `pthread_create()`: This POSIX standard function creates a new thread within an existing process, sharing the process's resources.

4. Steps to Start a New Process

Here's a simplified breakdown of what happens when creating a new process:

  1. Clone Parent State: When `fork()` is called, the OS kernel creates a duplicate of the parent process context.
  2. Allocate Resources: The kernel allocates space in memory, initializes necessary data structures, and reserves process ID.
  3. Process Table Entry: A new entry in the system’s process table is created containing the child's execution context.
  4. Schedule Process: The scheduler places the new process in the ready queue, waiting for CPU time allocation.
  5. Execute Program: With `exec()`, a process begins executing a new program, replacing its memory image.

5. Steps to Start a New Thread

Creating a new thread involves:

  1. Allocate Thread Control Block (TCB): The kernel allocates a data structure to hold the thread's information.
  2. Shared Memory Setup: Threads share a process's memory, so there's no need to re-allocate all memory segments.
  3. Initialize Stack: A separate stack is allocated for the thread within the shared memory space.
  4. Schedule Thread: The scheduler is responsible for providing CPU time to the new thread, ensuring it gets a chance to execute.

6. Context Switching

Whether managing threads or processes, context switching is crucial. It enables multitasking by saving the state of the current active process/thread and loading the state of the next one to execute.

  • Steps involved:
    • Save CPU registers of the current process/thread to its PCB or TCB.
    • Load CPU registers for the next scheduled process/thread.
    • Update the memory management unit with the new memory map.
    • Resume execution of the new process/thread.

7. Table Summary

Key AspectProcessThread
DefinitionIndependent execution unitComponent within a process
Creationfork(), CreateProcess()pthread\_create(), clone()
Memory SpaceSeparate for each processShared across threads of a process
Control BlockProcess Control Block (PCB)Thread Control Block (TCB)
OverheadHigher due to memory and resources duplicationLower as resources & memory are shared
CommunicationInterprocess Communication (IPC) needed (signals, pipes)Easier via shared memory

8. Conclusion

Low-level understanding of how an OS creates processes and threads is foundational to systems programming, contributing to efficient resource management and optimal performance. Though modern programming often abstracts these concepts, delving into these details equips developers with the critical ability to write performant and resource-conscious code.


Course illustration
Course illustration

All Rights Reserved.