Process and Thread

chuuuing
3 min readAug 19, 2021

--

You see processes and threads in multitasking operating system. To understand this 2 concepts, we first have a look on the how CPU works.

CPU Working Mechanism

A program consists of several steps:

Flow of Program execution

Thread

A program consists of several steps, each step is an instruction. Some of the steps may together achieve some final goal. So we have:

some steps in the Program
= unit of execution
= set of instructions which together form some specific task
= Thread

What if there are several threads aiming for several purposes respectively?

Here we need a Software Component called scheduler in our PC, basically controls how the Threads go into the CPU, it works like a switch, decide which Thread can progress next. In the diagram we see 2 threads: CLION and SPOTIFY.

The switch process happens on a regular basis — a time slice. If time_slice = 1 millisecond, it means the CPU handles instructions from CLION-Thread for 1 ms, then handles instructions from SPOTIFY-Thread for another 1 ms. This “switch” happens back and forth, but its so quick, we human don’t really realise, this 2 threads works simultaneously for us.

This type of multitasking is called Preemptive Multitasking. Because it doesn’t rely on any of these applications to give up control in order to make the Scheduler work.

Threads are also associated with priority — which is simply a number, it might differs in different system. Some system use 1~32 to indicate the priority, some system use 1~99. But those priority is dynamic, it changes according to the time the thread has been waiting, whether it has been executed in a recent time slice.

Eventually all the threads reach a same priority and the scheduler has to treat them equally, and thats the goal we want to archieve — so that it looks simultaneous for the stupid human being.

Comparison

Process

Each process provides the resources needed to execute a program. A process contains the following:

  • a virtual address space
  • executable code
  • open handles to system objects
  • a security context
  • a unique process identifier
  • environment variables
  • a priority class
  • minimum and maximum working set sizes
  • at least one thread of execution.

Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.

--

--