Operating system knowledge summary (3)

5. Process differences and connections with the threads?

  • Process is a program with a certain individual functions on a set of data on the activities of the first run, the process is a stand-alone system unit resource allocation and scheduling.
  • Thread is a physical process, is the basic unit of CPU scheduling and dispatching, which is smaller than the process of the basic unit can operate independently.

The relationship between process and thread

(1) a thread can belong to only one process, and a process can have multiple threads, but there is at least one thread. Is the minimum thread execution units and the scheduling of the operating system recognizes.

(2) the resources allocated to the process, all threads in the same process of sharing all the resources of the process. The same process multiple threads share code segments (code and constant), the data segment (global variables and static variables), the extended segment (heap memory). But each thread has its own stack segment, stack segment called running time, used to store all local variables and temporary variables.

(3) points to the thread handling machine, i.e. the real processors running on a thread.

(4) threads in the implementation process, the need to work in sync. Between threads of different processes to make use of synchronized way messaging.

The difference between processes and threads?

(1) process has its own separate address space, there is no thread

(2) the process is the smallest unit of resource allocation, the thread is the smallest unit of CPU scheduling

Different (3) communication processes and threads of communication between the (thread more convenient. Threads share data (such as global variables, static variables) under the same process by which data communication is not only fast and convenient, of course, how to deal with these visits the synchronization and mutual exclusion is the difficulty of writing multithreaded programs. the inter-process communication only through the process of communication.)

(4) a process context switch overhead, small thread overhead

(5) a process hang up will not affect other processes, and the thread will affect the other thread hangs up

(6) general operating costs of the process the process was relatively large, the small overhead of thread 

 Why process context switch costly than the thread context switching?

Switching process in two steps:

1. Change directory to use the new page address space

2. The kernel stack and the hardware context switching

For linux, the biggest difference is that thread and process address space for a thread switch, the first step is not required to do, and the second is the process and thread switching are done.

The overhead of switching:

1, the thread context switching and a switching process down to ask the most important difference is that the thread is switched virtual memory space is still the same, but the process of switching is different. Two context switch processing is accomplished by the kernel Caozuojitong. This handover process accompanied by the kernel most significant performance loss is switched out of the register contents.

2, a further loss is hidden context switching processor disturb caching mechanism. Simply put, once to switch context, all processors have cache memory addresses moment unmade. Another notable difference is that when you change the virtual memory space when processing the page table buffer (processor's Translation Lookaside Buffer (TLB)) or equivalent Shenma things will be all refreshed, which will lead to memory access at a time quite inefficient. But in switching threads, the problem does not occur.

 

Turn almost knew: the difference between process and thread

Link: https: //www.zhihu.com/question/25532384/answer/81152571

Subjects first to a general: processes and threads are described in a period of time, it is to describe the work of CPU time period.

The following elaborate background :
CPU + RAM + various resources (such as graphics cards, optical drives, keyboard, GPS, etc. peripherals) constitute our computers, but the operation of the computer, is actually a matter between the CPU and associated registers and RAM.

One of the most basic facts : CPU too fast, too fast, too fast, the register can only catch on in his footsteps, RAM, and other hanging on each bus device is completely offer. That when multiple tasks to be performed how to do it? Take turns to? Who or high priority Who? Whatever the strategy, word is that the CPU seems to take turns.

A need to know the facts : during the execution of a program code to implement a function of the introduction, when the CPU get relevant resources must be in place, is the graphics card ah, GPS ah, what must be in place, then the CPU begins executing. In addition to all of this CPU constitute the execution environment of this program, which is defined in the context of our program. When the program execution is over, or assigned to his execution CPU time runs out, then it will be handed out once graced the CPU wait. In the final step is to save the program context is switched out, because this is the next time he was Lin Xing CPU operating environment, it must be saved.

The fact concatenated : Recall the CPU view, the specific method turns all tasks are executed one by one in turn is: first load the context of the program A, then A begins execution, saving the context of the program A, transferred B next program to be executed in the context of the program, and begins execution B, program B stored context . . . .

========= ======== important things appeared
processes and threads is such a background out , but the two terms are the corresponding period of the CPU description, the term is one such function .
  • Replacement process is that the program execution context switch total time  =  the CPU load context + CPU performs context save + CPU

What thread is it?
Process particle size is too large, every time there is transferred up and down, save, recall. If we put a process likened to a software running on the computer, then execute software can not be executed by a logic, there must be multiple branches and multiple program segments, like to implement the program A, actually divided into a, b , c, etc. a combination of a plurality of blocks. This specific implementation may become:

A program to give CPU = "CPU load context, subparagraph starts executing a program A, and then perform the A subparagraph b, then c subparagraph A is performed, and finally to save the context A the CPU.

Performing a, b, c are here of shared context A, CPU context switching is not performed during execution. This is where a, b, c is the thread, that thread is a shared process context, more small CPU time.

The full text of this end, and then a summary:

process and thread is a description of the time period, is to describe the work of CPU time, but different particle sizes.
 
  • The biggest difference process (process) Thread (thread) is the process has its own address space, threads within a process is not visible to other processes, i.e. a process A can not pass by way of direct write address storage area B of the process . Communication between processes requires interprocess communication (Inter-process communication, IPC) by. By contrast, the way among among threads of the same process can be passed directly through the address or global variables pass information .

  • Process as a basic unit of the operating system has the resources and independence scheduling, you can have multiple threads. A program running on the system operating normally correspond to a process. In the same process, thread switching does not cause the process of switching. When a thread switch in different processes, such as the switching from the thread in one process to another threads, will cause the switching process. Compared process switch, thread switching overhead is much smaller. Thread in the process can be combined with each other to improve the efficiency of the system.

Thread can be divided into two categories:

  • User-level thread (the User Level the Thread) : For these threads, thread management of all work-related by the application is complete, the existence of the thread's kernel awareness. After the application starts, the operating system assigns a process ID to the program, and its corresponding memory space and other resources. Applications typically run first in a thread, the thread is to become the main thread. At some point it runs, you can create a new thread running in the same process by calling the thread library functions. The benefits of user-level threads is very efficient and does not need to enter the kernel space, but complicated by inefficient.

  • Kernel-level threads (Kernel Level the Thread) : For these threads, thread management is all about the work completed by the kernel, the application does not perform thread management code, the interface can only be called a kernel thread. Each thread kernel maintains process and its internal scheduling also completed by the kernel thread-based architecture. The benefits of kernel-level threads is that the kernel can be better allocated to different threads of different CPU, for true parallel computing.

In fact, in modern operating systems, often using a combination of ways to achieve multi-threaded, a thread that is created entirely done in user space, and an application of multiple user-level threads are mapped to the number of kernel-level threads, the equivalent of a kind of compromise.

 

To be a simple analogy: Process = trains , thread = compartment

  • Thread traveled under process (a simple cabin does not work)
  • A process can contain multiple threads (a train can have multiple compartments)
  • Difficult to share data among different processes (a passenger on the train is difficult to change to another train, such as transfer station)
  • Under the same process between different threads it is very easy to share data (A compartment B compartment is easy to change)
  • Process than thread consumes more computer resources (multi-train carriages more compared to more consumption of resources)
  • Interprocess not affect each other, hang a thread will cause the whole process to hang (one train to another will not affect a train, but if on a train in the middle of a car on fire, will affect all cabins)
  • The process can be extended to multi-machine, multi-core processes most suitable for (different trains can be opened on multiple tracks, the same train carriage can not travel on different tracks)
  • When the memory address used by the process can be locked, that is a thread to use certain shared memory, other threads must wait for it to end, in order to use this piece of memory. (Such as the toilet on the train) - "mutex"
  • Process memory addresses can limit usage (such as a restaurant on the train, a maximum number of people allowed to enter, if need full at the door, and so someone can go out) - "semaphore"

 

Guess you like

Origin www.cnblogs.com/JesseP/p/11647518.html