Processes and Threads

Introduction to Computer Systems

Sukrita Earn
5 min readJun 15, 2020

Have you ever suspected how does your device can run many different tasks more than one at a time without any task malfunctioning? Even if you may see just one app run in the foreground, you may have noticed that some apps also tend to run at the same time in the background. You might have seen them in form of a notification such as when an app has been updated, when you got a new email or sms, when someone tagged you on Facebook, and so on. But the question is, how does your devices manage to do a lot of things simultaneously? In this blog, we will talk about processes and threads.

http://www.rtos.be/wp-content/uploads/2013/06/process_and_thread.png
Programs, processes and threads

First of all, you probably are aware that a program is the code that is stored on your computer so that it can process a certain task. There are many types of programs, including programs that help your computer function and programs that are part of the operating system, and other are programs that fulfill a particular task. These programs that do a specific tasks are also known as applications, and can include programs such as word processing, web browsing, emailing, and so on.

Programs are typically stored in memory in a form that can be executed by your computer. The end result is a text file of code that is compiled into binary form (1 and 0) in order to run on the computer. The computer’s CPU (Central Processing Unit) understands only binary instructions, so the form of the program needs to be in binary when it runs.

Process

From program to process

The program has been loaded into the computer’s memory in binary form. So now, an executing program needs more than just the binary code that tells the computer what to do. The program needs memory and various operating system resources in order to run. A “process” is what we call a program that has been loaded into memory along with all the resources it needs to operate. The operating system is the brains behind allocating all these resources such as macOS, iOS, Microsoft Windows, Linux, and Android. The OS handles the task of managing the resources needed to turn your program into a running process.

https://cdn57.androidauthority.net/wp-content/uploads/2016/04/processes-and-threads-16x9-720p.jpg
Some resources of each process

There can be multiple instances of a single program, and each instance of that running program is a process. Each process has a ID known as PID (Process ID) which is its own separate memory address space. It owns some of memory and some state information such as running, sleeping, stopped and zombie. Therefore, that makes a process runs independently and is isolated from other processes. It cannot directly access shared data in other processes. When you want to switch from one process to another, it will require some time for saving and loading registers, memory maps, and other resources.

This independence of processes is important because when the operating system can isolate processes, a problem when one of the process corrupt or harm another process might not occur. For example, you might have occasionally run into the situation in which one application on your computer freezes or has a problem and you are able to quit that program without affecting others.

Thread

Thread lifecycle (Thread execution state)

A “thread” is the part of execution within a process. A process can contain one thread to many threads. When a process starts, it is assigned memory and resources. Each thread in the process shares that memory and resources.

Single vs. Multi

In single-threaded processes, the process contains the execution of instructions in a single sequence. The process and the thread are one and the same so only one task can be processed. In other words, one command is processes at a time.

In multi-threaded processes, the process contains more than one thread. These processes allow the execution of multiple parts of a program at the same time. That means the process is accomplishing many tasks almost at the same time.

Multi-Threaded

There are the two types of memory available to a process or a thread, the stack and the heap. It is important to distinguish between these two types of process memory because each thread will have its own stack, but all the threads in a process will share the heap.

Threads are sometimes called light weight processes because they have their own stack but can access shared data and threads share the same address space as the process and other threads within the process so the processing cost of communication between the threads is low. The weakness of the multi-threaded process is that if there is a problem with one thread in a process, it will certainly affect other threads and the executable of the process.

Multi-threaded processes implementation

Multi-threaded processes can be implemented as user-level threads or kernel-level threads.

User vs. Kernel
  • User level Threads

The user level threads are implemented by users and the OS does not recognise user level threads. It handles them as if they were single-threaded processes. User level threads are small and much faster than kernel level threads. There is no kernel involvement in synchronisation for user-level threads. If one user level thread performs blocking operation then entire process will be blocked.

  • Kernel level Threads

Kernel level threads are handled by the operating system directly and the thread management is done by the kernel. The context information for the process as well as the process threads is all managed by the kernel. This make kernel level threads are slower than user level threads. If one kernel thread performs blocking operation then another thread can still continue execution.

Process vs. Thread

--

--

Sukrita Earn

Faculty of Software and Knowledge Engineering Sophomore at Kasetsart University.