When you’re starting your career, whether you’re using C++ or Java or just applying for an operations position, you’ll probably encounter this problem.
This is a very basic question, but it is also a question that tests people’s level.
I say basic because everyone who studies computer should understand that process thread is the basic concept of computer, is every programmer all the time to contact something.
But this is another problem full of pitfalls that can be extended and deepened indefinitely. For example, it can be extended from operating system knowledge to computer composition principles, from single thread to concurrent programming, from concurrent programming to thread synchronization, thread safety, interprocess communication and so on.
So how do we answer this question?
Now that we’re talking about differences, we need to understand what processes and threads are.
I was reading a paper a while ago and found a good analogy to explain them clearly.
1. The core of the computer is the CPU, which undertakes all the computing tasks. It’s like a factory, always running.
2. Assume that the plant has limited power to supply only one workshop at a time. That is to say, when one workshop starts, all the others must stop. The implication is that a single CPU can only run one task at a time.
3. Processes are like factory floors, representing individual tasks that the CPU can handle. At any one time, the CPU is always running one process and the other processes are not running.
There can be many workers in a workshop. They work together on a task.
5. Threads are like workshop workers. A process can contain multiple threads.
6. The space of the workshop is shared by workers, for example, many rooms are accessible to each worker. This indicates that the memory space of a process is shared, and each thread can use the shared memory.
However, each room is of a different size. Some rooms, such as toilets, can only hold one person at most. When there are people inside, no one else can get in. This means that when a thread uses some shared memory, other threads must wait for it to finish before they can use it.
8. An easy way to keep people out is to add a lock to the door. Those who arrive first lock the door, and those who arrive later, when they see the lock, line up at the door and wait for the lock to open before they enter. Known as “Mutual exclusion,” or Mutex, this prevents multiple threads from simultaneously reading and writing an area of memory.
9. There are also rooms that can accommodate more than one person, such as the kitchen. In other words, if the number of people is greater than n, the extra people have to wait outside. This is like some memory area that can only be used by a fixed number of threads.
10. The solution is to hang a bunch of keys at the door. He who goes in takes a key, and hangs it back on his way out. When the last person to arrive finds his keys on the rack, he knows he must wait in line at the door. This practice, called Semaphore, is used to ensure that multiple threads do not conflict with each other.
It is not hard to see that mutex is a special case of Semaphore (when n=1). In other words, you can replace the former with the latter. However, because of mutex’s simplicity and efficiency, this design is used in cases where resource exclusivity must be guaranteed.
11. Operating system design, therefore, can be boiled down to three points:
-
In the form of multi-process, allowing multiple tasks to run at the same time;
-
In the form of multi-threading, allowing a single task to run in different parts;
-
Provides a coordination mechanism that prevents conflicts between processes and threads on the one hand, and allows resources to be shared between processes and threads on the other.
This analogy vividly illustrates the difference between a process and a thread.
Of course, you can’t talk about this during the interview, the interviewer is probably in a hurry with you, time is precious, we need to use the most concise language to explain the concept and the difference between the two.
Here’s how you can point your answer (you need to be logical, succinct and to the point) :
(1) Process
Process is an execution process of the program, is a dynamic concept, is the basic unit of program allocation and management of resources in the execution process, each process has its own address space, at least five basic states, they are: initial state, execution state, waiting state, ready state, termination state.
(2) Threads
The thread is the basic unit of CPU scheduling and dispatching. It can share all the resources owned by the process with other threads belonging to the same process.
(3) Contact
Threads are part of a process. A thread can only belong to one process, and a process can have multiple threads, but at least one thread.
(4) Differences: To understand their differences, I start from the perspective of resource use. (Resources are the CPU, memory, files, networks, etc.)
Fundamental difference: Processes are the basic unit of operating system resource allocation, while threads are the basic unit of task scheduling and execution
In terms of overhead: each process has independent code and data space (program context), switching between programs will have a large overhead; Threads can be regarded as lightweight processes. The same type of threads share code and data space, and each thread has its own independent running stack and program counter (PC). Switching between threads has little overhead.
Environment: Can run multiple processes (programs) at the same time in the operating system; Multiple threads execute simultaneously in the same process (with CPU scheduling, only one thread executes in each slice)
Memory allocation: The system will allocate different memory space for each process during operation; In the case of threads, no memory is allocated to them except for the CPU (they use resources from the process they belong to), and only resources are shared between thread groups.
Inclusion: a process without threads can be regarded as single-threaded. If there are multiple threads in a process, the execution process is not a single line, but multiple lines (threads). Threads are part of a process, so they are also called lightweight or lightweight processes.
Further reading
Why design threads?
In the traditional process model, process can be divided into the following two aspects:
The basic unit of scheduling and execution: Each process has its own running state, priority, register, etc., which is the basic unit of OS scheduling.
Resource ownership: includes programs, data, files, and other resources. A process has ownership of these resources, and the OS provides protection from resource conflicts between different processes.
Since they are two separate functions, can they be separated? This leads to the concept of threads:
The basic unit of execution and scheduling: Thread
Resource ownership: Process
So the basic unit of execution and scheduling is thread, what good is that?
There are two important concepts in computer operating systems: concurrency and isolation.
Concurrency is about maximizing hardware utilization, and threading is about concurrency at the system level. Thread context switching is much more efficient than process context switching, which improves concurrency efficiency.
Isolation is also an important problem to solve after concurrency. Resources on a computer are generally shared, and isolation ensures that if a crash occurs these resources can be reclaimed without affecting the use of other code. So it’s ok to have an operating system with only threads and no processes, but the system will crash a lot, which is a lot like when the operating system first developed.
So: threads are related to concurrency, processes are related to isolation. Threads are basically a concept introduced for concurrent code execution, because to allocate CPU time slices, the ability to resume after a pause must be able to continue execution as if it were not paused; A process is equivalent to a bunch of threads plus the resources applied for during the execution of the thread. Once suspended, these resources must be recoverable without affecting other programs.
Write at the end:
Welcome everyone to pay attention to my newly opened public account [calm as code], massive Java related articles, learning materials will be updated in it, sorting out the data will be placed in it.
If you think it’s written well, click a “like” and add a follow! Point attention, do not get lost, continue to update!!