Skip to content

Latest commit

 

History

History

01_fundamentals

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

📘 Processes, Threads, and Operating System Concepts

1. 💻 What is a Process?

A process is an instance of a program that is currently running. It provides the environment where a program executes, including memory, CPU time, and resources.

📝 Key Points:

  • Execution Environment: A process contains the program's code, data, and resources needed to execute.
  • Memory Allocation: Each process has its own memory space, which includes areas for the stack, heap, data, and code.

🛠️ Components of a Process:

  • Stack: Used for storing function calls, local variables, and control flow. It follows a LIFO (Last In, First Out) structure.
  • Heap: Used for dynamic memory allocation (e.g., malloc in C or new in Go).
  • Data: Stores global and static variables that persist during the program’s lifetime.
  • Code: Contains the machine code (instructions) that the CPU executes.

📦 Process Example:

In an operating system, each process runs independently, and the OS allocates CPU time and memory resources to it. If you launch a web browser, the browser’s process runs as a separate entity from other applications, with its own memory and execution environment.

2. 🧵 What is a Thread?

A thread is the smallest unit of execution that the CPU can manage. It is a sequence of instructions within a process, and every process has at least one thread—the main thread.

📝 Key Points:

  • Unit of Execution: A thread is the smallest sequence of programmed instructions that can be managed independently by a scheduler.
  • Multiple Threads: A process can have multiple threads that share the same memory space but can execute independently.
  • Shared Memory: Threads within the same process share the same address space (e.g., heap, data, and code sections).

🔄 Example:

A web server process might have multiple threads: one to handle each incoming client request. All threads share the same memory space, but they run independently.

⚠️ Thread Militation:

  • C10K Problem: Threads can encounter limitations when handling a large number of concurrent connections (e.g., 10,000 connections). Too many threads can lead to inefficiency and resource exhaustion.

🏁 Thread States:

Threads can be in various states:

  • Running: Actively executing instructions.
  • Blocked: Waiting for some event (like I/O or synchronization).
  • Ready: Waiting to be assigned to a CPU.

3. 🔄 Context Switching

Context switching occurs when the CPU switches from executing one thread (or process) to another. This is expensive because the operating system must save and restore the state of threads, including their registers, stack, and program counter.

📝 Key Points:

  • Process Context: Includes the entire state of the process (e.g., memory, open files).
  • Thread Context: Refers to the state of a single thread (e.g., its stack, register values).

Context switches are more expensive when dealing with processes, as each process has its own memory space and requires saving/restoring more state information.

4. ⚙️ Operating System and Resources

The Operating System (OS) is responsible for managing processes, threads, and the allocation of hardware resources (like CPU, memory, and I/O devices).

📝 Key Functions:

  • Resource Allocation: The OS allocates CPU time and memory resources to processes and threads.
  • Scheduling: The OS decides which process or thread to execute at any given moment.
  • Concurrency Management: The OS handles concurrency between processes and threads, ensuring that they don’t interfere with each other.

🕹️ CPU Scheduling:

The OS uses scheduling algorithms (like round-robin or priority-based scheduling) to determine which thread or process gets to run next.

5. 💾 Memory Layout in Processes

Processes have a specific memory layout that consists of several key sections:

  • Stack: Stores local variables and function calls. Each thread in the process has its own stack.
  • Heap: Used for dynamically allocated memory during program execution.
  • Data: Stores global variables and static data that persist throughout the program's execution.
  • Code: Contains the machine code instructions that are executed by the CPU.

📝 Example:

In Go, the stack can store the local variables of a function, while the heap stores dynamically allocated objects throughout the execution of the program. For example, when you create a goroutine, each goroutine will have its own separate stack and share other parts of memory, such as the heap, with other goroutines in the same process.

6. 📝 Conclusion

Understanding processes and threads is crucial for designing efficient applications and optimizing performance. A process provides an isolated environment for execution, while threads allow concurrent execution within that environment. Efficiently managing these resources is essential for building scalable and high-performance systems.

However, to build more optimized and scalable execution systems, concepts like Concurrency, Parallelism, and Asynchronous Programming are extremely important. You can explore how these concepts are applied and compared in the Comparison section of the project, where different techniques and programming approaches are discussed, helping you better understand how they affect concurrent processing and performance optimization in software systems.