线程与进程(我的理解)

Posted 圣诞使者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程与进程(我的理解)相关的知识,希望对你有一定的参考价值。

最近面试有人问到线程与进程的区别,我就说进程是系统资源分配的最小单位,线程是CPU调度的最小单位。但是人家有细问了一下,我就没回答出来,

后来我查查书,没有查到我满意的解释。感觉应该是这样的(以后有新的理解会补充进来,如果有错也会改正O(∩_∩)O~)

最开始没有线程,只有进程,比如写个程序,程序运行起来以后,进程就是程序运行的实例,pid就是进程的标识。

后来有了多处理器啥的,一些程序为了用上这多个CPU,让这些程序共享系统资源等,但每个程序又有自己的一些属性,这些程序就称之为线程。如果这些程序,系统资源啥的都是不共享的,那么这些程序就是进程。

下面是摘自unix高级环境编程的一段话:

1.5.1 程序
程序(p r o g r a m)是存放在磁盘文件中的可执行文件。使用6个e x e c函数中的一个由内核将
程序读入存储器,并使其执行。8 . 9节将说明这些e x e c函数。
1.5.2 进程和进程I D
程序的执行实例被称为进程( p r o c e s s)。本书的每一页几乎都会使用这一术语。某些操作
系统用任务表示正被执行的程序。
每个U N I X进程都一定有一个唯一的数字标识符,称为进程I D(process ID)。进程I D总是
一非负整数。

下面是摘自IBM的官方红皮书:

General Programming Concepts:
Writing and Debugging Programs   这个是(AIX 7.1)
这个也有中文的书籍:AIX V6.1 通用编程概念:编写并调试程序(这段是写的一样的)

Understanding Threads and Processes
A thread is an independent flow of control that operates within the same address space as other
independent flows of controls within a process. Traditionally, thread and process characteristics are
grouped into a single entity called a process. In other operating systems, threads are sometimes called
lightweight processes, or the meaning of the word thread is sometimes slightly different.
The following sections discuss the differences between a thread and a process.
In traditional single-threaded process systems, a process has a set of properties. In multi-threaded
systems, these properties are divided between processes and threads.
Threads have some limitations and cannot be used for some special purposes that require multi-processed
programs. For more information, see “Limitations” on page 366.
Process Properties
A process in a multi-threaded system is the changeable entity. It must be considered as an execution
frame. It has traditional process attributes, such as:
v Process ID, process group ID, user ID, and group ID
v Environment

v Working directory
A process also provides a common address space and common system resources, as follows:
v File descriptors
v Signal actions
v Shared libraries
v Inter-process communication tools (such as message queues, pipes, semaphores, or shared memory)
Thread Properties
A thread is the schedulable entity. It has only those properties that are required to ensure its independent
control of flow. These include the following properties:
v Stack
v Scheduling properties (such as policy or priority)
v Set of pending and blocked signals
v Some thread-specific data
An example of thread-specific data is the errno error indicator. In multi-threaded systems, errno is no
longer a global variable, but usually a subroutine returning a thread-specific errno value. Some other
systems may provide other implementations of errno.
Threads within a process must not be considered as a group of processes. All threads share the same
address space. This means that two pointers having the same value in two threads refer to the same data.
Also, if any thread changes one of the shared system resources, all threads within the process are
affected. For example, if a thread closes a file, the file is closed for all threads.

以上是关于线程与进程(我的理解)的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发 - 线程与进程的认识与理解

进程与线程

线程与进程的区别以及对多线程并发的理解

谈谈对线程与进程的理解

进程与线程并行与并发的理解

多线程与多进程