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.

转载于:https://www.cnblogs.com/bittorrent/p/3833327.html

[原]Threads vs Processes in Linux 分析相关推荐

  1. linux 分析 ptrace

    linux 分析 ptrace() 形式 #include <sys/ptrace.h> int ptrace(int request, int pid, int addr, int da ...

  2. 嵌入式Linux——分析u-boot运行过程(3):u-boot第三阶段代码

    简介: 本文主要介绍在u-boot-1.1.6中代码的运行过程,以此来了解在u-boot中如何实现引导并启动内核.这里我们主要介绍u-boot第三阶段的代码.而第三阶段的代码主要讲解的是在u-boot ...

  3. linux 分析磁盘性能,03.分析性能瓶颈 - 3.4.磁盘瓶颈 - 《Linux性能调优指南》 - 书栈网 · BookStack...

    磁盘瓶颈磁盘瓶颈性能调优选项 磁盘子系统通常是服务器性能的最重要方面,是瓶颈问题的高发部件.但是,磁盘问题表现的有时候并不是那么直接,比如说可能是内存不足.如果CPU周期浪费在等待I/O任务完成,应用 ...

  4. 技术分享 |《原神》部分渲染效果分析

    一.光与影 游戏中的影子大致可以区分为以下几类: 1.场景的静态烘焙阴影 游戏中的场景应该主要还是基于lightmap实现的静态阴影.对于植被并没有计算lightmap,其实我感觉可以在草等植被的下方 ...

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

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

  6. linux分析目录内存,在 Linux x86-64 模式下分析内存映射流程

    前言 在上一篇中我们分析了 Linux 在 x86-32 模式下的虚拟内存映射流程,本章主要继续分析 Linux 在 x86-64 模式下的虚拟内存映射流程. 讨论的平台是 x86-64, 也可以称为 ...

  7. [并发并行]_[线程池]_[Programming With POSIX Threads的线程池实现分析1]

    场景 1.C++标准库没有提供线程池操作, 连Win32都没有集成线程池, 相比之下macOS完善多了, 至少有operations. 多线程在执行多任务时有很大优势, 比如同时管理多个设备, 多个s ...

  8. 红旗linux怎么升级,[原]使用yum更新红旗Linux

    最近找到几个新的软件包,觉得挺好用,也刚编译成rpm包,希望在所有使用到的机器上部署.后来发现,需要每一台机器去安装一遍,即使可以远程安装,这也是挺麻烦的.决定参考Redhat的升级机制,把yum也迁 ...

  9. 关于原神抽奖概率的简要分析

    序言 最近迷上了原神这款游戏,趁着保研完,肝了两个星期,也氪了一些金.先不谈这款游戏可玩性有多高,但论氪金强度算是我从小到大玩的游戏中,能排得上第一的了. 对于这种寸卡寸金的游戏,如何在无穷无尽的抽卡 ...

最新文章

  1. python【数据结构与算法】从一个例子引入动态规划❤️
  2. 温故(2):pass by value
  3. GestureDetector.OnGestureListener 详解
  4. CLS/CLR、c#(等语言)、托管、元数据、程序集 CLR学习第二课
  5. The security settings could not be applied to the database because the connection has failed安装Mysql
  6. linux数据泵导入command not found_MySQL:数据库结构优化、高可用架构设计、数据库索引优化...
  7. MCMC(MH算法)
  8. 肌电信号 聚类 Matlab
  9. AUTOSAR和OSEK关系及网络管理比较
  10. DATEDIF函数:
  11. 罗辑回归,Logistic Regression(or sigmoid function)
  12. 【LeetCode】18. 4Sum 四数之和
  13. 二叉树的层序遍历与镜像翻转js
  14. 【MySQL】创建高性能的索引
  15. 问:python-dev是什么?为什么安装了python后有时还要安装python-dev?
  16. ensp华为路由器静态配置和动态配置
  17. 一阶电路实验报告心得_电路实训心得体会
  18. java中将html特殊字符作转义和取消转义操作
  19. Windows7直接刻录ISO
  20. MT40A1G8SA-062E AAT:E内存颗粒D9XSP芯片

热门文章

  1. 用imspost制作catia后处理_新产品开发需要做原型验证,怎么样成型制作才省钱?...
  2. plsql连接mysql_安装了mysql和pl/sql,怎么配置让pl/sql能联接mysql数据库
  3. 华为怎么改输入法皮肤_微信和QQ个性键盘皮肤
  4. 超速问题的c语言编程,超速行驶问题--精选.doc
  5. linux驱动程序混合架构,嵌入式系统最小驱动框架(类似linux驱动程序架构)(示例代码)...
  6. 计算机的网络与结构,计算机结构与组成29-网络.ppt
  7. android手机生成pdf格式文件,Android根据pdf模板生成pdf文件
  8. ai人工智能在手机的应用_强化学习在人工智能中的应用
  9. C++---汉明距离
  10. 【竞赛题解】Codeforces Round #715 (Div. 2) C