Thursday, December 12, 2019

Threads Vs Processes


You might come across situations where you need to decide whether to use a thread or a process to achieve a certain task. Here I am sharing my experience on selecting what's best depending on your requirements

First, let's what is a process?
  • A process is an executing instance of an application/program
  • Process control block holds information about the process.
  • Process priority, process id, process state, CPU register, program counter, stack pointer, the status of opened files, scheduling algorithms, etc. 
  • A process can create other processes which are known as Child Processes.
  • The process takes more time to terminate and it is isolated means it does not share the memory with any other process.

Then what is a thread?
  • A thread is a path of execution within a process. 
  • A process can contain multiple threads
  • A thread takes less time to terminate as compared to a process
Let's see how does a process work
  • Process creation using fork system call
    • fork() creates a replica of the parent process 
    • Page Table and Kernal Stack are duplicated, new PCB is created
    • Child process state is set to NEW -> READY
    • Process is moved to the ready queue of the Kernal
    • Copy On Write (COW) is used by the child process
  • The first process of Unix system is known as the Super parent.
    • Location: /sbin/init
    • Created by the kernel during boot
    • Typically starts several scripts present in /etc/init.d
  • Process termination
    • Voluntary termination: exit(0)
    • Involuntary termination: kill(pid, signal) 
    • Pid: process Id
    • Signal: asynchronous message send by the OS/another process (eg: SIGTERM, SIGQUIT)
  • Process communication(IPC)
    • Shared memory: shmget, shmat, shmdt 
    • Message passing:
    • Shared memory is created in kernel
    • Slow
    • Pipes
    • Signals
How does a thread work?
  • Threads are very inexpensive to create and destroy, and they are inexpensive to represent
  • When a new thread is created it shares its code section, data section and operating system resources like open files with other threads.
  • But it allocates its own stack, register set and a program counter.
  • With so little context, it is much faster to switch between threads
  • Creating a thread, switching between threads and synchronization between threads can all be done without the intervention of the kernel

Processes are good when you have a task, where the task

  • Has sufficient work to do for a long period of time continuously
  • Does not require much communication between tasks
  • Has less context switching
  • Can independently work from other tasks
  • Does not require to be controlled

Threads are good when you have a task, where the task

  • Has less heavy work to do
  • Has a small life span
  • Requires communication between other tasks
  • Requires context switching
  • Depends on other tasks
  • Requires synchronization
  • Depends on shared data/resources
  • Requires control and coordination

You may consider the following factors when selecting between threads and processes

  • Type of work (IO-bound, computation, etc.)
  • Lifetime (Short vs Long)
  • Scheduling
  • Resource Sharing
  • Data Sharing
  • Communication
  • Thread/Process security and safety
  • Control and Synchronization
  • Environment and technology stack

No comments:

Post a Comment