Linux中thread (light-weighted process) 跟process在實作上幾乎一樣。

最大的差異來自於,thread 會分享 virtual memory address space.

a. 從kernel角度看兩者沒差別,在user看來process是least shared而thread是most sharing.

b. 從實作角度來比較:

創建user process的方法是有三個API: fork, vfork, clone
創建user thread的方法主要是使用thread library: pthread_create
創建kernel thread的方法有兩種API: kernel_thread, kthread_create

而以上這些API還有library function最後都會call到do_fork
而do_fork所做的事就是填task_struct,這邊有一個我覺得很酷的觀念要講一下
          *** 只要填好task_struct就是把該task給創建出來了 !!! *** 
可以知道其實user process, user thread就只是在task_struct的填法不一樣而已。

ref.

[1]http://www.mulix.org/lectures/kernel_workshop_mar_2004/things.pdf

[2]http://stackoverflow.com/questions/807506/threads-vs-processes-in-linux

Linux uses a 1-1 threading model, with (to the kernel) no distinction between processes and threads
-- everything is simply a runnable task. * On Linux, the system call clone clones a task, with a configurable level of sharing, among which are: CLONE_FILES: share the same file descriptor table (instead of creating a copy)
CLONE_PARENT: don't set up a parent-child relationship between the new task and the old
(otherwise, child's getppid() = parent's getpid())
CLONE_VM: share the same memory space (instead of creating a COW copy)
fork() calls clone(least sharing) and pthread_create() calls clone(most sharing). ** forking costs a tiny bit more than pthread_createing because of copying tables and creating COW mappings for memory,
but the Linux kernel developers have tried (and succeeded) at minimizing those costs. Switching between tasks, if they share the same memory space and various tables, will be a tiny bit cheaper
than if they aren't shared, because the data may already be loaded in cache. However,
switching tasks is still very fast even if nothing is shared -- this is something else that Linux kernel developers
try to ensure (and succeed at ensuring). In fact, if you are on a multi-processor system, not sharing may actually be a performance boon:
if each task is running on a different processor, synchronizing shared memory is expensive.

[原]Threads vs Processes in Linux 分析的更多相关文章

  1. linux分析、诊断及调优的必备“杀器”之一

    下面分别列出linux分析.诊断及调优时用到的工具,并分别进行说明,以方便自己和其他同学参考学习,禁止转载. 1.top top - 02:06:59 up 4 days, 17:14, 2 user ...

  2. linux分析利刃之sar命令详解

    一.sar的概述 在我使用的众多linux分析工具中,sar是一个非常全面的一个分析工具,可以比较瑞士军刀,对文件的读写,系统调用的使用情况,磁盘IO,CPU相关使用情况,内存使用情况,进程活动等都可 ...

  3. Show All Running Processes in Linux

    ps由于历史的原因,所以很奇特,有些命令必须加"-",比如: ps A 上面的写法是错误的 ********* simple selection ********* ******* ...

  4. Show tree of processes in linux

    pstree(1): tree of processes - Linux man pagehttps://linux.die.net/man/1/pstree How to view process ...

  5. Linux分析第六周——进程的描述和进程的创建

    Linux分析第六周--进程的描述和进程的创建 李雪琦+原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/US ...

  6. 基于Xenomai的实时Linux分析与研究

    转自:http://blog.csdn.net/cyberlabs/article/details/6967192 引 言 随着嵌入式设备的快速发展,嵌入式设备的功能和灵活性要求越来越高,很多嵌入式设 ...

  7. [原]那些年整理的Linux常用命令,简单明了

    查询相关 find 按规则查找某个文件或文件夹,包括子目录 find . -name '*.sh' -- 以.sh结尾的文件 find . -name '*channel*' -- 包含channel ...

  8. linux分析、诊断及调优必备的“杀器”之二

    先说明下,之所以同类内容分成多篇文章,不是为了凑篇数,而是为了便于自己和大家阅读,下面继续: 7.sar The sar command is used to collect, report, and ...

  9. PHP获取网卡的MAC地址原码;目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址

    声明转换于其它博客当中的. <?php /** 获取网卡的MAC地址原码:目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址 **/ class GetMacAddr{ var $ ...

随机推荐

  1. thinkphp 系统变量

    一.查看可用变量 dump($_SERVER); 可以直接在html输出系统变量的值 <p>{$Think.server.HTTP_HOST}</p>. 二.环境变量 1.查看 ...

  2. JVM加载class文件原理

    装载的概念 所谓装载就是寻找一个类或是一个接口的二进制形式并用该二进制形式来构造代表这个类或是这个接口的class对象的过程. Java中类装载器装载类到虚拟机 在Java中,类装载器把一个类装入Ja ...

  3. Python---基础---dict_tuple_set

    2019-05-21 ------------------------ help(tuple) ------------------------- Help on class tuple in mod ...

  4. 【leetcode】516. Longest Palindromic Subsequence

    题目如下: 解题思路:很经典的动态规划题目,但是用python会超时,只好用C++了. 代码如下: class Solution { public: int longestPalindromeSubs ...

  5. @ApiModelProperty

    @ApiModelProperty用法   @ApiModelProperty()用于方法,字段: 表示对model属性的说明或者数据操作更改 value–字段说明 name–重写属性名字 dataT ...

  6. python学习笔记(十)常用模块

    import os print(os.getcwd())#取当前工作目录,绝对路径 print(os.chdir("../"))#更改当前目录,.代表当前目录,..代表上一级目录 ...

  7. redis学习一 String数据类型

    一:概述 字符串类型是Redis中最为基础的数据存储类型,它在Redis中是二进制安全的,这便意味着该类型可以接受任何格式的数据,如JPEG图像数据或Json对象描述信息等.在Redis中字符串类型的 ...

  8. php上传文件的原理

    文件上传原理 将客户端的文件上传到服务器,再将服务器的临时文件上传到指定目录 客户端配置 提交表单 表单的发送方式为post 添加enctype="multipart/form-data&q ...

  9. Python_002(基础知识)

    1.格式化输出 %s:字符串的占位符,可以放置任何内容(数字) 代码展示:%去除法 admin_input1 = input("请输入用户名:") pass_input = inp ...

  10. 【进阶技术】一篇文章搞掂:RibbitMQ

    一.简介 一开始,消息队列源自于一个激进的工程师的思想,他希望有一种通用软件“总线”能解决程序间繁重的信息通信工作 后来出现了很多消息队列产品,但是他们互不兼容,价格昂贵 后来出现了AMQP,高级消息 ...