zoukankan      html  css  js  c++  java
  • 线程与进程(我的理解)

    更新,日期20161218。下面的内容是之前写的,有些错误的理解,也更新过一些内容,这里就不再修改了,作为自己技术成长的记录吧,以后对这个概念有比较深刻的理解后会在更新一篇的。

    看书的时候有这样几段话,希望大家能加深理解:

    深入理解linux内核:进程是程序执行的实例,或者一个运行程序的上下文。在Linux源码中,常把进程称为任务(task),或者线程(thread)。一般来说,能被独立调度的每个执行上下文都必须有他们自己的进程描述符,轻量级进程也有自己的task_struct结构。

    ---

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

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

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

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

    以后有时间会看一看深入理解linux内核一书,估计看完后对这些概念的理解会好很多。(这里先补充一下,在深入理解linux内核中,进程的定义是程序的实例,也叫程序运行的上下文。在linux源码中,常把进程成为任务或线程。上面的理解应该有误区,以后有更深入的linux在更新吧。)

    下面是摘自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.

    这里再补充一下LWP(light weight process轻量级进程),这个LWP在不同的操作系统上概念是不一样的。详见

    https://en.wikipedia.org/wiki/Light-weight_process

    Wikipedia里面讲的都比较细而且准确,以后有不明白的概念可以从这里面搜。

  • 相关阅读:
    iOS NSProgress的使用
    GIT的 .gitignore 配置
    MagicalRecord入门教程
    CoreData的数据存储
    NSLog打印信息的从新设置
    大石头得博客
    exc_bad_access(code=1, address=0x789870)野指针错误
    oc 获取当前设备系统的版本号
    免证书真机调试脚本iphoneentitlements
    支持非arc
  • 原文地址:https://www.cnblogs.com/WisWang/p/5294281.html
Copyright © 2011-2022 走看看