修正:这面简单问题→这么简单的问题。
进程和线程的主要区别应该是在创建的时候 线程 需要共享了(父子)进程的资源 。
在liunx系统中 没有进程和线程的区别 统称 “task” 进程标志(task_struct) 进行统一描述
严格意义上讲, 并没有进程,线程的区别
Linus Torvalds 增加在邮件说写到
Both threads and processes are really just one thing: a “context of execution”. Trying to artificially distinguish different cases is just self-limiting. A “context of execution”, hereby called COE, is just the conglomerate of all the state of that COE. That state includes things like CPU state (registers etc), MMU state (page mappings), permission state (uid, gid) and various “communication states” (open files, signal handlers etc).
COE 就是执行上下文
一个进程所包含的资源(或者称之为状态)有,
可以看到,进程和线程都是’COE’, ( stay at a high level discussion.)
线程和进程的有各自的 CPU state (栈抽象成cpu) ———不相同
线程:
MMU state,permission stat, various “communication states是共享进程的。—-相同
截止到目前 你看到进程和线程还是有区别的,线程共享资源,进程不共享资源 这不是区别吗?
Linux下内核其实本质上没有线程的概念, Linux下线程其实上是与其他进程共享某些资源的进程而已。但是我们习惯上还是称他们为线程或者轻量级进程 线程是进程的子集
The way Linux thinks about this (and the way I want things to work) is that there is no such thing as a “process” or a “thread”. There is only the totality of the COE (called “task” by Linux). Different COE’s can share parts of their context with each other, and one subset of that sharing is the traditional “thread”/“process” setup, but that should really be seen as ONLY a subset (it’s an important subset, but that importance comes not from design, but from standards: we obviusly want to run standards-conforming threads programs on top of Linux too). Linux思考这个问题的方式(以及我希望工作的方式)是这样的 没有所谓的“进程”或“线程”。只有 COE的总体(Linux称为“任务”)。不同的COE可以共享部件 它们之间的上下文,而共享的一个subset是 传统的“线程”/“进程”设置,但实际上应该只将其视为 一个子集(它是一个重要的子集,但是这个重要性不是来自它 设计,但从标准:我们显然希望运行符合标准的 线程程序也在Linux之上)。
进程的创建的 而fork()本身是由clone()实现的
clone(SIGCHLD,0);
线程创建:pthread_create→clone
clone(CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND,0);
用strace 跟踪pthread_create创建过程:
clone(child_stack=0x7fd4174b8ff0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7fd4174b99d0, tls=0x7fd4174b9700, child_tidptr=0x7fd4174b99d0) = 16477
clone参数的对应作用如下: -CLONE_VM:父子进程共享地址空间
CLONE_FS:父子进程共享文件系统信息 CLONE_FILES:父子进程共享打开的文件 CLONE_SIGHAND:父子进程共享信号处理函数及被阻断的信号
协程,又称微线程,纤程。英文名Coroutine
A coroutine is a function that can suspend its execution (yield) until the given given Yield Instruction finishes.
协程:
【待处理 有点难】
Protothreads:一个“蝇量级” C 语言协程库 libco:来自腾讯的开源协程库libco介绍,官网 coroutine:云风的一个C语言同步协程库,详细信息
目前看到大概有四种实现协程的方式:
· 第一种:利用glibc 的 ucontext组件(云风的库)
· 第二种:使用汇编代码来切换上下文(实现c协程)
· 第三种:利用C语言语法switch-case的奇淫技巧来实现(Protothreads)
· 第四种:利用了 C 语言的 setjmp 和 longjmp( 一种协程的 C/C++ 实现,要求函数里面使用 static local 的变量来保存协程内部的数据)
塔山
The Intel Core i7/Linux Memory System 为例子
塔山
目前处于第一想象 自己不知道,别人知道,该如何办呢? 求讨论
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有