Advertisement

HexaGuard: Mastering the Art of Digital Shadows

Process Management: Multitasking, Scheduling, Threads vs Processes

Process Management in OS🕒:

 Handles the execution of programs and manages the CPU.

1. Multitasking 🖥️

Multitasking refers to the ability of an OS to run multiple tasks simultaneously. It's like juggling multiple things at once! 💪

  • Preemptive Multitasking: The OS can interrupt a process and switch to another. ⏱️
  • Cooperative Multitasking: Processes must give control back to the OS to switch. 🔄

2. Processes vs Threads 🔄

  • Process:
    A process is an independent program with its own resources. Think of it as a full-blown application! 💻

  • Thread:
    A thread is a smaller unit of execution within a process. Multiple threads share the same resources, making them faster but riskier! ⚡


3. Process Scheduling 🗓️

The OS uses scheduling algorithms to decide which process gets CPU time. 🕒

  • Round Robin: Processes are given a fixed time slice.
  • FCFS: First process in gets executed first.
  • Priority Scheduling: The highest priority process runs first. 🚀

Hands-on: Process & Thread Management 💡

Linux (Bash) 🐧

Viewing Processes:

bash

ps aux # See all running processes pstree # View processes in a tree structure 🌳

Creating a Process:

bash

sleep 60 # This creates a process that waits for 60 seconds ⏳

Viewing Threads:

bash

ps -T -p 1234 # List threads of a process with PID 1234 🧵

Windows (PowerShell) 💻

Viewing Processes:

powershell

Get-Process # List all processes 📋

Creating a Process:

powershell

Start-Process "notepad.exe" # Open Notepad as a new process 📝

Creating Threads (via .NET in PowerShell) 💻:

powershell

Add-Type -TypeDefinition @" using System; using System.Threading; public class ThreadExample { public static void PrintMessage() { Console.WriteLine("Hello from Thread!"); } } "@ $thread = [System.Threading.Thread]::new([ThreadExample]::PrintMessage) $thread.Start() # Start a new thread 🧵

4. Thread vs Process Handling ⚖️

  • Memory: Threads share memory, processes don't. 🧠 vs 🧱
  • Creation Time: Threads are quicker to create since they share resources. ⚡
  • Context Switching: Switching between threads is faster than processes. 🚦

5. Advanced: Process Synchronization 🔒

When multiple threads/processes share resources, we need mutexes and semaphores to avoid chaos. 🛑

  • Mutex: Ensures only one thread can access a resource at a time. 🚷
  • Semaphore: Allows multiple threads to access a resource, but with limits. ⛔

Summary 📚:

  • Processes: Independent units of execution. 🏃‍♂️
  • Threads: Lighter units that share resources. 🧵
  • Multitasking: Managing multiple tasks at the same time. 🏃‍♀️💨
  • Scheduling: Decides which process runs when. ⏲️

Post a Comment

0 Comments