Process v.s. Thread & Background Processing with Worker & Context Switching in O.S.

Hanwen Zhang
5 min readDec 18, 2023

--

Process v.s. Thread

What is a Program

It’s an executable file that contains the code or a set of processor instructions, that is stored as a file on disk.

When a code is loaded into memory and executed by the processor, it becomes a process.

What is a Process

An active process also includes the resources the program needs to run, these resources are managed by the operating system. Some examples are processors, registers, program counters, stack pointers, and memory pages assigned to the process for its heap and stack.

Each process has its own memory address space. One process cannot corrupt the memory space of another process. This means that when one process malfunctions, other processes keep running. Like Chrome, running each tab independently.

What is a Thread

A thread is the unit of execution within a process. A process has at least one thread which is called the main thread. Commonly, a process has many threads.

Each thread has its stack. Stack, registers, and program counters of a process belong to a thread. Threads within a process share a memory address space, it’s possible to communicate between threads using that shared memory space. However, one misbehaving thread could bring down the entire process.

Single-threaded

Execute a sequence of instructions in a single stream, meaning one operation is performed at a time, writing and debugging code can be simpler; e.g. Redis key/value in-memory data storage with synchronous I/O operations in a sequential manner.

Multi-threaded

Execute multiple threads concurrently, with each thread performing a different set of instructions that improves performance for tasks that can be parallelized; e.g. a web server with multiple threaded servers.

Recap

  • When program/code executes, it becomes a progress
  • Thread is a part of process
  • Threads run in processes, one process can have many threads in it, and as they are in the same process, they share a memory space
  • Events refer to asynchronous occurrences that are triggered by some action or response in a system
  • Events are pushed to the main thread, then worker threads process the request

Background Processing

Long-running synchronous web requests in production applications can lead to timeouts and errors by queuing up all the requests nobody gets a response from the server, offloading long-running requests to background processes can solve this issue.

Background processing can be achieved by using a queue and a worker process. Queue processes in the order of first in first out. Worker performs tasks independently/asynchronously offloading time-consuming or non-urgent tasks.

The worker is a separate request from the web, reads the job from the queue, and generates a result. The worker process is not restricted by the timeout, once it is done then we act with the information.

Asynchronous response/request cycle: instead of taking a set time like 30 seconds, we can add a request to a to-do list, and return to the user that we add a request to the to-do list and we will get back to you later.

Context Switching

Thread or Process on a CPU

How Does the Operating System Run a Thread or Process on a CPU? It’s handled by context switching. During a context switch, one process is switched out of the CPU so another process can run.

The operating system stores the states or the currently running process so the process can be stored and resume execution at a later point. It then restores the previously saved states of a different process and resumes execution for that process.

Context switching is expensive, and involves saving and loading registers, switching out memory pages, and updating various kernel data structures.

Switching Threads

Switching execution between threads also requires context switching. It’s generally faster to switch context between threads than between processes. There are fewer states to track and more importantly, since threads share the same memory address space, there is no need to switch out virtual memory pages, which is one of the most expensive operations during a context switch.

Alternatives

Context switching is costly, but there are other mechanisms to try to minimize it. Examples: fibers and coroutines. These mechanisms trade complexity for even lower context-switching costs. In general, they are cooperatively scheduled, that is, they must yield control for others to run. In other words, the application itself task scheduling, which is the responsibility of an application to make sure a long-running task is broken up by yield periodically.

Recap

The ability of context switching to switch rapidly between tasks is a core capability of a multitasking operating system (OS). Context switching enables multiple processes to share a single CPU while making it appear as though the CPU is executing multiple processes simultaneously.

Web Worker

Perform heavy tasks on the background of a web page that offloads heavy work to a worker. Web content to run scripts in an isolated thread in the browser in parallel, a completely separate thread from the thread that is running the main JavaScript program, to prevent the UI from freezing up

JavaScript is a single-threaded language, but web worker allows JavaScript to run in the background threads, which are separate from the main execution thread, without affecting the performance of the page.

Web Workers is a web platform extension if(window.Worker){}, const worker = new Worker('worker.js'), worker.terminate(). PostMessage API raises an event from one script that another script can catch and listen to worker.postMessage("hello, worker") .

  const myWorker = new Worker("worker.js");
myWorker.postMessage() // sending data
onmessage=(e)=>{e.data} // listenig to message event and capture data

--

--

Hanwen Zhang
Hanwen Zhang

Written by Hanwen Zhang

Full-Stack Software Engineer at a Healthcare Tech Company | Document My Coding Journey | Improve My Knowledge | Share Coding Concepts in a Simple Way

No responses yet