文章目录

  • 1.线程概念
    • 1.1 什么是线程
    • 1.2 线程和进程区别
    • 1.3 线程实现原理
    • 1.4 三级映射
    • 1.5 线程共享资源
    • 1.6 线程非共享资源
    • 1.7 线程优、缺点
  • 2.线程控制原语
    • 2.1 pthread_self 函数
    • 2.2 pthread_create 函数
  • 3.线程与共享
    • 3.1 线程共享全局变量
  • 4.线程退出
    • 4.1 pthread_exit 函数

1.线程概念

1.1 什么是线程

线程:LWP(light weight process), 轻量级的进程,本质仍是进程(在 Linux 环境下)。
线程是进程的子任务,实现进程内部的并发,线程也是CPU调度和执行的最小单位。

1.2 线程和进程区别

进程拥有独立的4G内存地址空间,拥有PCB;线程有独立的 PCB,但没有独立的地址空间(共享)。
进程是最小分配资源单位;线程是最小的执行单位。
进程通信方式:管道、系统IPC(消息队列、信号量、信号、共享内存)、套接字;线程通信方式:临界区、互斥量、信号量、事件(信号)。

1.3 线程实现原理

  1. 轻量级进程(light-weight process),也有 PCB,创建线程函数(pthead_create)和创建进程(fork)的底层函数一样,都是 clone函数
  2. 从内核里看进程和线程是一样的,都有各自不同的 PCB,但是 PCB 中指向内存资源的三级页表是相同的
  3. 线程可看做寄存器和栈的集合
  4. 对于进程来说,相同的地址(同一个虚拟地址)在不同的进程中,反复使用而不冲突。原因是他们虽虚拟址一样,但页目录、页表、物理页面各不相同。相同的虚拟址,映射到不同的物理页面内存单元,最终访问不同的物理页面。
    但线程不同,两个线程具有各自独立的 PCB,但共享同一个页目录,也就共享同一个页表和物理页面。所以两个 PCB 共享一个地址空间。实际上,无论是创建进程的 fork,还是创建线程的 pthread_create,底层实现都是调用同一个内核函数 clone。
    如果复制对方的地址空间,那么就产出一个“进程”;如果共享对方的地址空间,就产生一个“线程”。
察看 LWP /线程号:ps –Lf pid 查看指定进程的 lwp 号(线程号,而非线程ID)
zhaoxr@zhaoxr-ThinkPad-E450:~$ ps -Lf 32639
UID          PID    PPID     LWP  C NLWP STIME TTY      STAT   TIME CMD
zhaoxr     32639    1815   32639  0   23 20:17 ?        SLl    0:01 /opt/google/chrome/chrome
zhaoxr     32639    1815   32648  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32656  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32657  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32661  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32662  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32663  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32664  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32665  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32666  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32667  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32668  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32671  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32672  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32673  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32674  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32675  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32676  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32678  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32680  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32695  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32716  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome
zhaoxr     32639    1815   32774  0   23 20:17 ?        SLl    0:00 /opt/google/chrome/chrome

1.4 三级映射

1.5 线程共享资源

1.文件描述符表,在同一个进程内,a线程和b线程共享文件描述符
2.每种信号的处理方式,线程和信号组合复杂,最好不要把它们搅合在一起
3.当前工作目录
4.用户 ID 和组 ID
5.内存地址空间 (.text/.data/.bss/heap/共享库),除了栈空间

1.6 线程非共享资源

1.线程 id
2.处理器现场和栈指针(内核栈)
3.独立的栈空间(用户空间栈)
4.errno 变量,很特殊的变量,是全局变量,在数据.data段,但是共享
5.信号屏蔽字
6.调度优先级

1.7 线程优、缺点

优点: 1. 提高程序并发性 2. 开销小 3. 数据通信、共享数据方便
缺点: 1. 库函数,不稳定 2. 调试、编写困难、gdb 不支持 3. 对信号支持不好
优点相对突出,缺点均不是硬伤。Linux 下由于实现方法导致进程、线程差别不是很大。

2.线程控制原语

线程所有操作函数 pthread_* 是库函数,而非系统调用,
对线程相关函数gcc编译时,需要链接第三方库-pthread

2.1 pthread_self 函数

获取线程 ID。其作用对应进程中 getpid() 函数。
pthread_t pthread_self(void);返回值:成功:0; 失败:无!线程 ID:pthread_t 类型,本质:在 Linux 下为无符号整数(%lu),其他系统中可能是结构体实现
线程 ID 是进程内部,识别标志。(两个进程间,线程 ID 允许相同)注意:不应使用全局变量 pthread_t tid,在子线程中通过 pthread_create 传出参数来获取线程 ID,
而应使用pthread_self。

2.2 pthread_create 函数

创建一个新线程。 其作用,对应进程中 fork() 函数。
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
返回值:成功:0; 失败:错误号 -----Linux 环境下,所有线程特点,失败均直接返回错误号。
参数:
pthread_t:当前 Linux 中可理解为:typedef unsigned long int pthread_t;
参数 1:传出参数,保存系统为我们分配好的线程 ID
参数 2:通常传 NULL,表示使用线程默认属性。若想使用具体属性也可以修改该参数。
参数 3:函数指针,指向线程主函数(线程体),该函数运行结束,则线程结束。
参数 4:线程主函数执行期间所使用的参数。
在一个线程中调用 pthread_create()创建新的线程后,当前线程从 pthread_create()返回继续往下执行,
而新的线程所执行的代码由我们传给 pthread_create 的函数指针 start_routine 决定。
start_routine 函数接收一个参数,是通过pthread_create 的 arg 参数传递给它的,
该参数的类型为 void *,这个指针按什么类型解释由调用者自己定义。start_routine 的返回值类型也是 void *,这个指针的含义同样由调用者自己定义。start_routine 返回时,这个线程就退出了,
其它线程可以调用 pthread_join 得到 start_routine 的返回值,
类似于父进程调用 wait(2)得到子进程的退出状态,稍后详细介绍 pthread_join。
pthread_create 成功返回后,新创建的线程的 id 被填写到 thread 参数所指向的内存单元。
我们知道进程 id 的类型是 pid_t,每个进程的 id 在整个系统中是唯一的,
调用 getpid(2)可以获得当前进程的 id,是一个正整数值。线程id 的类型是 thread_t,它只在当前进程中保证是唯一的,在不同的系统中 thread_t 这个类型有不同的实现,
它可能是一个整数值,也可能是一个结构体,也可能是一个地址,
所以不能简单地当成整数用 printf 打印,调用 pthread_self(3)可以获得当前线程的 id。attr 参数表示线程属性,本节不深入讨论线程属性,所有代码例子都传 NULL 给 attr 参数,
表示线程属性取缺省值,感兴趣的读者可以参考 APUE。
#include<stdio.h>
#include<pthread.h>
#include <stdlib.h>
#include<unistd.h>void* print(void* arg){printf("in print:pthread id=%lu,pid=%u\n",pthread_self(),getpid());return NULL;
}
int main()
{pthread_t tid;//线程IDprintf("in main1:pthread id=%lu,pid=%u\n",pthread_self(),getpid());tid=pthread_create(&tid,NULL,print,NULL);sleep(1);if(tid!=0){printf("pthread_create error\n");exit(1);}printf("in main1:pthread id=%lu,pid=%u\n",pthread_self(),getpid());return 0;
}
zhaoxr@zhaoxr-ThinkPad-E450:~/pthread$ vim pthread_create.c
zhaoxr@zhaoxr-ThinkPad-E450:~/pthread$ gcc pthread_create.c -o pthread_create -lpthread
zhaoxr@zhaoxr-ThinkPad-E450:~/pthread$ ./pthread_create
in main1:pthread id=140607852943168,pid=33317
in print:pthread id=140607852939008,pid=33317
in main1:pthread id=140607852943168,pid=33317
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>void* func(void* arg){int i=(int)arg;//sleep(i);printf("第%d个线程,pthread id=%lu,pid=%u\n",i,pthread_self(),getpid());return NULL;
}
int main()
{pthread_t tid;int i,ret;for(int i=0;i<5;i++){ret=pthread_create(&tid,NULL,func,(void*)i);if(ret!=0){fprintf(stderr,"pthread_create error:%s\n",strerror(ret));exit(1);}}sleep(1);printf("main pthread id=%lu,pid=%u\n",pthread_self(),getpid());return 0;
}
zhaoxr@zhaoxr-ThinkPad-E450:~/pthread$ ./cycle_pthread_create
第0个线程,pthread id=139641383798528,pid=36526
第3个线程,pthread id=139641265059584,pid=36526
第4个线程,pthread id=139641358620416,pid=36526
第1个线程,pthread id=139641375405824,pid=36526
第2个线程,pthread id=139641367013120,pid=36526
main pthread id=139641383802688,pid=36526

3.线程与共享

3.1 线程共享全局变量

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>int a=10;void* func1(void* arg){a=100;printf("pthread id=%lu,a=%d\n",pthread_self(),a);return NULL;
}void* func2(void* arg){printf("pthread id=%lu,a=%d\n",pthread_self(),a);return NULL;
}
int main()
{pthread_t tid1,tid2;int ret;ret=pthread_create(&tid1,NULL,func1,NULL);if(ret!=0){printf("pthread create error\n");exit(1);}sleep(1);ret=pthread_create(&tid2,NULL,func2,NULL);if(ret!=0){printf("pthread create error\n");exit(1);}sleep(1);return 0;
}
zhaoxr@zhaoxr-ThinkPad-E450:~/pthread$ ./pthread_value
pthread id=140622421137152,a=100
pthread id=140622412744448,a=100

线程间共享全局变量!
【牢记】:线程默认共享数据段、代码段等地址空间,常用的是全局变量。而进程不共享全局变量,只能借助 mmap。

4.线程退出

4.1 pthread_exit 函数

将单个线程退出

void pthread_exit(void *retval); 参数:retval 表示线程退出状态,通常传 NULL
思考:使用 exit 将指定线程退出,可以吗? 【pthrd_exit.c】
结论:线程中,禁止使用 exit 函数,会导致进程内所有线程全部退出。

在不添加 sleep 控制输出顺序的情况下。pthread_create 在循环中,几乎瞬间创建 5 个线程,但只有第 1 个线程有机会输出(或者第 2 个也有,也可能没有,取决于内核调度)如果第 3 个线程执行了 exit,将整个进程退出了,所以全部线程退出了。
所以,多线程环境中,应尽量少用,或者不使用 exit 函数,取而代之使用 pthread_exit 函数,将单个线程退出。
任何线程里 exit 导致进程退出,其他线程未工作结束,主控线程退出时不能 return 或 exit。

另注意,pthread_exit 或者 return 返回的指针所指向的内存单元必须是全局的或者是用 malloc 分配的,不能在线程函数的栈上分配,因为当其它线程得到这个返回指针时线程函数已经退出了。

#include<stdio.h>
#include<pthread.h>
#include <stdlib.h>
#include<unistd.h>void* print(void* arg){sleep(1);printf("in print:pthread id=%lu,pid=%u\n",pthread_self(),getpid());return NULL;
}
int main()
{pthread_t tid;//线程IDint ret;printf("in main1:pthread id=%lu,pid=%u\n",pthread_self(),getpid());ret=pthread_create(&tid,NULL,print,NULL);sleep(1);if(ret!=0){printf("pthread_create error\n");exit(1);}printf("in main1:pthread id=%lu,pid=%u\n",pthread_self(),getpid());pthread_exit(NULL);
}
zhaoxr@zhaoxr-ThinkPad-E450:~/pthread$ ./pthread_exit
in main1:pthread id=139696593151808,pid=36805
in main1:pthread id=139696593151808,pid=36805
in print:pthread id=139696593147648,pid=36805

main主函数中没有使用return,并且不能使用exit函数,且其等待其它线程结束进程才结束。

linux——线程(1)相关推荐

  1. [转载]Linux 线程实现机制分析

    自从多线程编程的概念出现在 Linux 中以来,Linux 多线应用的发展总是与两个问题脱不开干系:兼容性.效率.本文从线程模型入手,通过分析目前 Linux 平台上最流行的 LinuxThreads ...

  2. Linux 线程的创建与同步

    Linux 线程的创建与同步 1.线程的定义 2.线程的创建和使用 3.理解线程的并发运行 3.线程同步 3.线程的实现 1.线程的定义 线程:进程内部的一条执行路径.是资源调度和执行的基本单位. 进 ...

  3. linux 线程操作问题undefined reference to ‘pthread_create‘的解决办法(cmake)

    linux 线程操作问题undefined reference to 'pthread_create'的解决办法(cmake) 参考文章: (1)linux 线程操作问题undefined refer ...

  4. linux线程的实现【转】

    转自:http://www.cnblogs.com/zhaoyl/p/3620204.html 首先从OS设计原理上阐明三种线程:内核线程.轻量级进程.用户线程 内核线程 内核线程就是内核的分身,一个 ...

  5. linux 线程 进程经典文章

    进程是程 序在计算机上的一次执行活动.当你运行一个程序,你就启动了一个进程.显然,程序是 死的(静态的),进程是活的(动态的).进程可以分为系统进程和用户进程.凡是用于完成操作系统的各种功能的进程就是 ...

  6. linux 线程--内核线程、用户线程实现方法

    Linux上进程分3种,内核线程(或者叫核心进程).用户进程.用户线程 内核线程拥有 进程描述符.PID.进程正文段.核心堆栈 当和用户进程拥有相同的static_prio 时,内核线程有机会得到更多 ...

  7. Linux 线程与进程,以及通信

    http://blog.chinaunix.net/uid-25324849-id-3110075.html 部分转自:http://blog.chinaunix.net/uid-20620288-i ...

  8. Linux线程-互斥锁pthread_mutex_t

    Linux线程-互斥锁pthread_mutex_t 在线程实际运行过程中,我们经常需要多个线程保持同步.这时可以用互斥锁来完成任务:互斥锁的使用过程中,主要有pthread_mutex_init, ...

  9. 【Linux开发】彻底释放Linux线程的资源

    Linux系统中程序的线程资源是有限的,表现为对于一个程序其能同时运行的线程数是有限的.而默认的条件下,一个线程结束后,其对应的资源不会被释放,于是,如果在一个程序中,反复建立线程,而线程又默认的退出 ...

  10. c++ linux 线程等待与唤醒_Linux线程同步(互斥量、信号量、条件变量、生产消费者模型)...

    为什么要线程同步? 线程间有很多共享资源,都对一个共享数据读写操作,线程操作共享资源的先后顺序不确定,可能会造成数据的冲突 看一个例子 两个线程屏行对全局变量count++ (采用一个val值作为中间 ...

最新文章

  1. 对于来自范兵提供光电检测带模块解析
  2. Python安装hmmlearn
  3. 用NanoPi neo制作网络音箱了解一下?
  4. 分区和分片的区别_MySQL分区与分片的差异
  5. [渝粤教育] 西北工业大学 博弈论基础 参考 资料
  6. (82)Vivado系统同步接口约束
  7. pthread_mutex_lock的作用
  8. Cinesamples CineBrass Descant Horn Mac(号角音色库)
  9. OpenCV Python 直方图
  10. Linux安装GIMP
  11. 吴声年度演讲全文:场景品牌,新商业的此时此刻
  12. Android混淆介绍
  13. 以太网通信协议UDP
  14. 人工智能的基础--知识分类
  15. Spring系列第20篇:@Conditional通过条件来控制bean的注册
  16. 如何实现给PDF文件添加图片
  17. AES-GCM加密算法的简单介绍
  18. 编程思想图书推荐,新手入门应该看些啥
  19. php 追溯系统,php 无限分类父子追溯方法
  20. HTML+CSS自定义树状图

热门文章

  1. LeetCode 246. 中心对称数(哈希)
  2. oem718d 基准站设置_RTK电台、网络模式作业设置流程
  3. python算法入门_GitHub标星2.6万!Python算法新手入门大全
  4. JavaScript 书写方式与注释
  5. php增加mysql用户_mysql 增加用户
  6. 细数一行代码改变结局的炼丹骚操作
  7. 消息中间件系列(九):详解RocketMQ的架构设计、关键特性、与应用场景
  8. import 和from... import的作用,导入模块or导入函数
  9. 注意力机制-深度学习中的注意力机制+注意力机制在自然语言处理中的应用
  10. 基于中文主观性知识库的句子主观性计算项目