Note that this question asks the difference between process switching and thread switching, not between process switching and thread switching. Threads, of course, refer to threads in the same process.

This question can well test the depth of the interviewer’s understanding of processes and threads, and has a relatively high degree of differentiation.

To answer this question correctly, interviewers need to understand virtual memory.

Virtual memory liberates productivity

For programmers, we don’t really care much about memory when programming, especially for programmers who use Java, Python, JavaScript and other types of languages. The introduction of automatic memory reclamation makes memory almost completely irrelevant to programmers who use these languages. Even for compiled languages C/C++, all the programmer needs to worry about is memory allocation and freeing.

In general, as a programmer (no matter use what kind of language) we don’t care about data and programs which position is placed in the physical memory (except design implementation of the operating system programmer), we can simply think our exclusive memory program, such as on a 32-bit system under the process of our memory space for the 4 g; Moreover, we can apply for more space than the physical memory size, for example, the programmer can apply for 1G memory space on the system of only 256MB, which greatly liberates the programmer’s productivity.

And behind this assumption is the virtual memory.

What is virtual memory

Virtual memory is an operating system for each process provides an abstract, each process has its own, private, continuous virtual memory address, of course we know the process of data and code is necessarily on the physical memory, so there must be some kind of mechanism can remember the some data in the virtual address space is on which physical memory addresses, This is called an address space mapping, a mapping between virtual memory addresses and physical memory addresses. How does the operating system remember this mapping? The page table records the mapping between virtual memory addresses and physical memory addresses. Page tables translate virtual addresses into physical memory addresses, a mechanism known as virtual memory.

Each process has its own virtual address space, which is shared by all threads within the process.

Now we can answer the interview question.

Difference between process switching and thread switching

One of the main differences between process switching and thread switching is that process switching involves switching virtual address space while thread switching does not. Since each process has its own virtual address space, and threads share the virtual address space of the process they are in, thread switching does not involve the conversion of the virtual address space.

To take an inapt example, thread switching is like walking from the master bedroom to the second bedroom. The master bedroom and the second bedroom are in the same house anyway (virtual address space), so you don’t have to change your shoes, clothes, etc. But process switching is different. Process switching is like going from your house to someone else’s house, two different houses (different virtual address Spaces), changing your clothes, shoes, etc., when you leave, changing your shoes, etc.

Therefore, we can think that threads are under the same roof, and the roof here is the virtual address space, so there is no need to switch between threads in the virtual address space; Two different processes live under different eaves, that is, processes live in different virtual address Spaces. Therefore, process switching involves switching virtual address Spaces, which is why process switching is slower than thread switching.

Some students may still not quite understand, why the virtual address space switch will be more time-consuming? This is the second question an interviewer will ask. To answer this question, follow the public account “Mynong’s Desert Island survival” and reply with the words “address switch”.

conclusion

Virtual memory is an extremely important part of modern operating systems. Of course, we can only briefly introduce it here because of limited space. For a detailed explanation of virtual memory, please refer to “Understanding Operating Systems in Depth” here.