Understanding the Difference Between Processes and Threads in Computing
Explore the key distinctions between processes and threads, their roles in computing, and how context switching impacts performance. Learn more now!
File
FANG Interview Question Process vs Thread
Added on 10/02/2024
Speakers
add Add new speaker

Speaker 1: What's the difference between process and thread? This is a common interview question. Let's take a look. To better answer this question, let's first understand what a program is. A program is an executable file. It contains the code or a set of processing instructions that is stored as a file on disk. When the code in the program is loaded into memory and executed by the processor, it becomes 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 processor registers, program counters, stack pointers, memory pages assigned to the process for its heap and stack, etc. There is an important property of a process that is worth mentioning. 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. Chrome is famous for taking advantage of this process isolation by running each tab in its own process. When one tab misbehaves due to a bug or a malicious attack, other tabs are unaffected. What is a thread? A thread is the unit of execution within a process. A process has at least one thread. It is called the main thread. It is not uncommon for a process to have many threads. Each thread has its own stack. Earlier, we mentioned registers, program counters, and stack pointers as being part of a process. It is more accurate to say that those things belong to a thread. Threads within a process share a memory address space. It is possible to communicate between threads using that shared memory space. However, one misbehaving thread could bring down the entire process. How does the operating system run a thread or a process on a CPU? This is 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 of the current running process so the process can be restored and resumed 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. It involves saving and loading of registers, switching out memory pages, and updating various kernel data structures. Switching execution between threads also requires context switching. It is generally faster to switch contexts 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 more expensive operations during a context switch. Context switching is so costly, there are other mechanisms to try to minimize it, some examples are fibers and coroutines. These mechanisms trace 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 handles task scheduling. It is the responsibility of the application to make sure a long-running task is broken up by yielding periodically. This concludes our discussion of process and threads. If you would like to learn more about system design, check out our books and weekly newsletter. Please subscribe if you learned something new. Thank you so much and we will see you next time.

ai AI Insights
Summary

Generate a brief summary highlighting the main points of the transcript.

Generate
Title

Generate a concise and relevant title for the transcript based on the main themes and content discussed.

Generate
Keywords

Identify and highlight the key words or phrases most relevant to the content of the transcript.

Generate
Enter your query
Sentiments

Analyze the emotional tone of the transcript to determine whether the sentiment is positive, negative, or neutral.

Generate
Quizzes

Create interactive quizzes based on the content of the transcript to test comprehension or engage users.

Generate
{{ secondsToHumanTime(time) }}
Back
Forward
{{ Math.round(speed * 100) / 100 }}x
{{ secondsToHumanTime(duration) }}
close
New speaker
Add speaker
close
Edit speaker
Save changes
close
Share Transcript