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. 树——populating-next-right-pointers-in-each-node(填充每个节点的next指针)

    问题: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...

  2. sort 文本排序

    1.命令功能 sort 将输入的文件内容按照指定的规则排序,然后将排序的内容输出. 2. 语法格式 sort  option  file 参数说明 参数 参数说明 -b 忽略每行开头的空格 -n 根据 ...

  3. python 按多维列表中的某一个元素位进行排序

    import os,re top = os.popen("tasklist") process_list = [] split_r = r"\s+" memor ...

  4. man LVCREATE

    LVCREATE(8)                                                        LVCREATE(8) NAME/名称       lvcreat ...

  5. PyTorch 手动提取 Layers

    Model NeuralNet( (l0): Linear(in_features=6, out_features=256, bias=True) (relu): ReLU() (bn0): Batc ...

  6. Linux必备软件安装

    若想Linux体验感更强,安装linux著名的KDE界面 (里面可以配置很多个性化的界面,特效等) # apt install plasma-desktop mpv ---很炫酷的视频播放器okula ...

  7. hdu 5212 : Code【莫比乌斯】

    题目链接 题给代码可以转化为下面的公式 然后用F[n]记录公约数为n的(a[i],a[j])对数,用f[n]记录最大公约数为n的(a[i],a[j])对数 之后枚举最大公约数d 至于求F[n],可以先 ...

  8. php中换行怎么写

    PHP中可以用PHP_EOL来替代,以提高代码的源代码级可移植性 unix系列用(推荐学习:PHP编程从入门到精通) 1 windows系列用 1 \r\n mac用 1 如: 1 2 3 4 5 6 ...

  9. 如何从Word带图粘贴到编辑器中

    这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用 后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下) ...

  10. Harbor在安装前的几个注意点

    由于Harbor有1.8后和前的配置不一样,决定先安装1.8,结果报错如下 [root@localhost harbor]# ./install.sh [Step 0]: checking insta ...