Computer Basics - processes and threads

When you started going to the workplace, whether you are using C ++ or Java or even applying only for the operation and maintenance jobs, I believe you will encounter this problem.

This is a very basic question, but a very human level test questions.

He said the foundation is because everyone should understand computer science, process threads is based on the concept of the computer, is something every programmer should always contact.

But this is a trap full of unlimited expansion and depth of a problem. Such knowledge can be extended from the operating system to computer composition principle, it can be extended from single-threaded to concurrent programming, concurrent programming thread synchronization from deep into the thread-safe, interprocess communication, and so on.

 

So how do we answer this question it?

Now to talk about the difference, you first need to understand what processes and threads.

Before, I read an article material, found a good analogy, they can explain the clear and understandable.

1. The computer is the core CPU, it assumed all computing tasks. It's like a factory, always running.

2. Suppose limited power plant, can only supply a workshop. In other words, a workshop started when the plant must shut down the other. Meaning behind is that a single CPU can run only one task.

3. The process is like the factory floor, it represents a single CPU can handle the task. At any one time, CPU always run a process, other processes is not running.

4. a workshop, there are many workers. They complete a collaborative task.

5. threads like workers than in the workshop. A process can include multiple threads.

6. The workshop space is shared by workers, such as many of the rooms is that each worker can be in and out of. This symbolizes a process memory space is shared, each thread can use these shared memory.

7. However, different size of each room, and some rooms can only accommodate a maximum of one person, such as a toilet. Someone inside when others can not go. This represents a thread when using certain shared memory, other threads must wait for it to end, in order to use this piece of memory.

8. An easy way to prevent others from entering, plus a lock that door. First man locked the door, and then to see people locked, you line up at the door, and so on and then go to open the lock. This is called "mutual exclusion lock" (Mutual exclusion, abbreviated Mutex), to prevent multiple threads simultaneously read and write a block of memory.

9. some room, n can accommodate individual, such as a kitchen. That is, if the number is greater than n, the extra man to wait outside. This is like certain memory areas, can only provide a fixed number of threads.

10. At this solution, n is the door of the key. Go in they took a key, when the key hang out and then back into place. After people have found the key to the overhead, you must know that waiting in line at the door. This practice is called " semaphore" (Semaphore), used to ensure that multiple threads do not conflict with each other.

Easy to see, mutex semaphore is a special case of (n = 1 hours). In other words, you can replace the latter with the former. However, because the mutex is simple and efficient, it must ensure the availability of resources in the exclusive, or the use of this design.

11. The operating system is designed, and therefore can be summed up in three points:

(1) to form a multi-process, allowing multiple tasks to run at the same time;

(2) in the form of a multi-threaded, allowing a single operation tasks into different parts;

(3) provide coordination mechanism, on the one hand and between the threads to prevent conflict between processes, on the other hand allow the sharing of resources between threads and between processes.

This analogy may explain the very image of the difference between processes and threads.

Of course, the interview when you can not say this bunch, the interviewer is estimated tell you worry, time is precious, we need to tell the difference between the two concepts and the most streamlined language.

 

You can answer such points (logically structured interview requires a clear, concise language, the straight key):

(1) Process

Process is a process of executing the program, is a dynamic concept, is the basic unit of program management and allocation of resources in the implementation process, each process has its own address space, there are at least five basic state, they are: the initial state execution state, wait state, ready state, end state.

(2) Thread

 A thread is the basic unit of CPU scheduling and dispatching, which can be shared with other threads process belong to a process that is owned by all of the resources.

(3) Contact

 A thread is part of the process, a thread can belong to only one process, and a process can have multiple threads, but there is at least one thread.

(4) differences: Understanding the difference between them, I start from the perspective of resource use. (So-called resource is the computer's central processor, memory, files, networks, etc.)

The fundamental difference: the process is the basic unit of the operating system resource allocation, and the thread is the basic unit of task scheduling and execution of

In terms of overhead: each process has its own code and data space (application context), switching between programs have larger overhead; thread can be seen as lightweight processes, threads share the same type of code and data space each thread has its own independent runtime stack and program counter (PC), small switches between threads overhead.

The environment: run multiple processes (programs) in the operating system at the same time; and there are multiple threads in the same process (program) simultaneously perform (by CPU scheduling, the film has only one execution thread at each time)

Memory allocation: When the system is running a different memory space will be allocated for each process; and thread, in addition to the CPU, the system will not (from that resource belongs to the process of resource thread used) to allocate memory for the thread can share resources, thread group.

Containment relationship: no thread can be seen as a single-threaded process, if there are multiple threads within a process, perform the procedure is not a line, but a plurality of lines (threads) together to complete; the thread is part of the process, so the thread is also known as light weight process or LWP.

 

Further reading

1, Why the need to design a thread?

In the traditional process model, the connotation of the process can be divided into the following two aspects:

  • Scheduling, the basic unit of execution: each process has its own operational status, priority, registers, etc., is the basic unit of the OS scheduler.
  • Ownership of resources: including programs, data, files and other resources. A process has ownership of these resources, OS provides protection against a resource conflict between the different processes.

 

Since it is two separate functions, is it possible to separate them from it? This presented a concept of threads (thread) of:

  • The basic unit of execution and scheduling: thread
  • Resource Ownership: process

 

So the basic unit of execution and scheduling is the thread, so what good set?

Computer operating system there are two important concepts: concurrent and isolation.
Concurrency is to try to make the high hardware utilization, in order to achieve concurrent threads at the system level. Thread context switching efficiency will be much higher than the switching process context, this can increase the efficiency of concurrency.

Isolation also after concurrent to solve important issues of general computer resources are shared, to be able to protect the isolation collapse of these resources can be recovered, it does not affect other code. So the thread is not only an operating system process is also possible, but such a system will often crash it, when the operating system and the beginning of the development of this situation much like.


So: threads and concurrency are related, processes, and isolated a relationship. The basic thread is to introduce the concept of concurrent execution of code, because you want to assign cpu time slice, pause and then resume to be able to continue without pause and continue the same; the process is equivalent to a bunch of threads together with the application process thread execution resources, once hung , and these resources can be recovered, without affecting other programs.

Guess you like

Origin www.cnblogs.com/chglog/p/11965816.html