DEV Community

bojack
bojack

Posted on

Processes and Threads in Operating Systems

Process

In an Operating System, Any program that is currently in execution is called a Process.

Every process has some unique attributes that allows the OS to control it. These attributes are stored inside a structure called the Process Control Block (PCB).

Some of the attributes are:

  • Process Id (PID): Used to identify a process
  • Process State: Displays the current state of a process (waiting, running, terminated).
  • File Descriptors: Information about open files and networks
  • Memory Management Information: Details about the allocated memory space to the process and memory structure of the process (stack, heap, etc)
Thread

A thread is the smallest unit of execution. Many threads work inside a single process to run multiple tasks concurrently.

Each thread has it's unique register, counter and stack, while all threads inside a process share code, data and operating system resources.

A single thread process can only accomplish one task at a time. Example- most CLI tools

A multithread process can accomplish multiple tasks at a time. Example- Browsers (Can display web page and download a file at the same time)

Benefits of multithreading:

  • Improves Performance: Running different tasks along multiple threads allow for faster execution, better performance and CPU utilization.
  • Improves Responsiveness: If one thread is busy executing a heavy action, other threads can still process user input or other tasks. This results in responsiveness and better user experience.
  • Efficient Resource Sharing: Threads share the same memory and resources within a process which makes data sharing and communication faster.
Difference Between Process and Thread:
Process Thread
A program in execution A unit of execution inside a process
Independent. Has it's own address space and resources Shares the memory space and resources of it's parent process with other threads
Takes more time to create and terminate faster to create and terminate
Heavyweight Lightweight
Requires explicit protocol to communicate with other processes Can communicate directly with other threads using shared memory

Top comments (0)