"In-depth understanding of computer systems," read the notes - the difference between process and thread?

Threads and processes Summary:


1. Processes and Threads

1.1 Overview:

Process is a program with a certain individual functions on a set of data on the activities of the first run , the system process is an independent unit of resource allocation and scheduling.

A 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. Basically thread does not own its own system resources, has only a little in the operation of essential resources (such as a program counter, a set of registers and stack), but it can be shared with other threads process belong to a process that is owned by all of the resources.

A thread can be created and destroyed another thread ; can execute concurrently across multiple threads in the same process.

For relatively processes, threads are closer to the concept of a body to perform, it can share data with other threads in the same process, but has its own stack space, an independent execution sequence.

The introduction of the serial program threads and processes on the basis of the program is to improve concurrency, thereby improving process efficiency and response time.

1.2 difference:

The main difference between processes and threads is that they are different operating system resource management. Processes have separate address space, after a process crashes, will not have an impact on other processes in protected mode, and the thread is just a process of different execution paths. Thread has its own stack and local variables, but there is no separate address spaces between the threads, a thread to die is tantamount to the whole process dies, so the program than multi-process multi-threaded programs robust, but in the process of switching, consuming greater resource efficiency to be worse. But for some of the requirements at the same time and have to share the concurrent operation of certain variables, only with thread, the process can not be used.

1) In short, there is at least one program a process, a process has at least one thread.

Scale division 2) thread is less than the process, so that multithreaded programs concurrency high.

3) In addition, the process has a separate memory unit in the implementation process, and multiple threads of shared memory, thereby greatly enhancing the efficiency of the program.

4)  threads in the implementation process and the process is different. Each entry has a separate thread running, sequences and procedures performed in the order outlet. But the thread is not able to execute independently, it must exist according to the application, providing multiple execution threads controlled by the application.

5)  From a logical standpoint, meaning that a multi-threaded application program execution section may perform a plurality of simultaneously. But the operating system will not be seen as multiple threads multiple independent applications to achieve scheduling and management and resource allocation processes. This is an important difference between processes and threads.

1.3 Advantages and disadvantages:

Threads and processes have advantages and disadvantages in the use: small thread execution overhead, but not conducive to the management and protection of resources; and the process contrary. At the same time, the thread suitable for running on SMP machines, and the process can migrate across machines.

2. The multi-process, multi-threaded

2.1 Overview:

When a process is running is running out of CPU abstract, abstract after a program is a process, but the thread is split from the inside out a process, because the process of CPU processing time is the use of round-robin fashion, so to put a big process is divided into a plurality of threads, for example: FlashGet file portion 100 into threads 10 file is divided into 10 parts at the same time to download a thread occupy 1-10 11-20 occupy a thread, and so on, the more threads, the file is divided more, but of course the faster download

Process is program implementation activities on the computer. When you run a program, you start a process. Obviously, the program is just an ordered set of instructions, meaning it does not have any running, just a static entity. The process is different, it is executed on a data set, is a dynamic entity. It is due to create and produce, due to scheduling and running, waiting for resources or events are in a wait state, because the task is revoked, all reflecting the dynamic process of a program running on a given data set. Process is the unit operating system to allocate resources. In Windows, the process has been refined as a thread, that is, there are a number of smaller units can operate independently of the next process. Thread (Thread) is a physical process, is the basic unit of CPU scheduling and dispatch. Thread can not be executed independently, be present depending on the application, a plurality of threads by the application program execution control.

The relationship between threads and processes are: the thread is part of the process, thread runs in the process space, threads of the same process generated share the same memory space, when the process exits the process generated by the thread will be forced to quit and clear. All threads share the process resources with other threads belonging to the same process have, but basically do not have their own system resources, has only a little information is essential in the operation (such as a program counter, a set of registers and stack).

在同一个时间里,同一个计算机系统中如果允许两个或两个以上的进程处于运行状态,这便是多任务。现代的操作系统几乎都是多任务操作系统,能够同时管理多个进程的运行。 多任务带来的好处是明显的,比如你可以边听mp3边上网,与此同时甚至可以将下载的文档打印出来,而这些任务之间丝毫不会相互干扰。那么这里就涉及到并行的问题,俗话说,一心不能二用,这对计算机也一样,原则上一个CPU只能分配给一个进程,以便运行这个进程。我们通常使用的计算机中只有一个CPU,也就是说只有一颗心,要让它一心多用,同时运行多个进程,就必须使用并发技术。实现并发技术相当复杂,最容易理解的是“时间片轮转进程调度算法”,它的思想简单介绍如下:在操作系统的管理下,所有正在运行的进程轮流使用CPU,每个进程允许占用CPU的时间非常短(比如10毫秒),这样用户根本感觉不出来CPU是在轮流为多个进程服务,就好象所有的进程都在不间断地运行一样。但实际上在任何一个时间内有且仅有一个进程占有CPU。

如果一台计算机有多个CPU,情况就不同了,如果进程数小于CPU数,则不同的进程可以分配给不同的CPU来运行,这样,多个进程就是真正同时运行的,这便是并行。但如果进程数大于CPU数,则仍然需要使用并发技术。

Windows中,进行CPU分配是以线程为单位的,一个进程可能由多个线程组成,这时情况更加复杂,但简单地说,有如下关系:

总线程数<= CPU数量:并行运行

总线程数> CPU数量:并发运行

并行运行的效率显然高于并发运行,所以在多CPU的计算机中,多任务的效率比较高。但是,如果在多CPU计算机中只运行一个进程(线程),就不能发挥多CPU的优势。

 多任务操作系统(如Windows)的基本原理是:操作系统将CPU的时间片分配给多个线程,每个线程在操作系统指定的时间片内完成(注意,这里的多个线程是分属于不同进程的).操作系统不断的从一个线程的执行切换到另一个线程的执行,如此往复,宏观上看来,就好像是多个线程在一起执行.由于这多个线程分属于不同的进程,因此在我们看来,就好像是多个进程在同时执行,这样就实现了多任务.

 2.2 分类

根据进程与线程的设置,操作系统大致分为如下类型:

(1) 单进程、单线程,MS-DOS大致是这种操作系统;

(2) 多进程、单线程,多数UNIX(及类UNIX的Linux)是这种操作系统;

(3) 多进程、多线程,Win32(Windows NT/2000/XP等)、Solaris 2.x和OS/2都是这种操作系统;

(4) 单进程、多线程,VxWorks是这种操作系统。

2.3 引入线程带来的主要好处:

(1) 在进程内创建、终止线程比创建、终止进程要快;

(2) 同一进程内的线程间切换比进程间的切换要快,尤其是用户级线程间的切换。


详情又可见:http://www.cnblogs.com/lmule/archive/2010/08/18/1802774.html

发布了18 篇原创文章 · 获赞 86 · 访问量 16万+

Guess you like

Origin blog.csdn.net/u013178472/article/details/64440404