操作系统——Threads 线程

本文探讨了操作系统中的线程概念,从并发与并行的区别、多核编程的挑战到多线程模型的详细分析,包括用户级线程和内核级线程的优缺点。此外,还讨论了线程库、隐式线程管理和多线程编程中的问题,如信号处理和线程取消。
摘要由CSDN通过智能技术生成

目录​​​​​​​

1. Overview 

2. Multicore Programming 多核编程

2.1 Concurrency vs. Parallelism 并发vs.并行

2.2 Programming challenges 编程挑战

3. Multithreading Models 多线程模型

 3.1 Threading support 线程支持

3.2 User-level threads (ULT) 用户线程

3.3 Kernel-level threads (KLT) 内核线程

3.4 Multithreading Models 多线程模型

 4. Thread Libraries 线程库

5. Implicit threading 

5.1 Managing threads 显示线程

5.2 Implicit threading 隐式线程

6. Threading issues / Designing multithreaded programs

6.1 Threading issues 多线程问题

6.1 fork() system call 

 6.2 exec() system call

6.3 Semantics of fork() and exec()

6.4 Signal Handling 信号处理

6.5 Thread Cancellation 线程关闭


1. Overview 

OS view: A thread is an independent stream of instructions that can be scheduled to run by the OS. 一个线程是一个独立的指令流,可以由操作系统安排运行。

Software developer view: A thread can be considered as a “procedure” that runs independently from the main program. 一个线程可以被视为一个独立于主程序运行的 "程序"。

  • Sequential program: a single stream of instructions in a program.
  • Multi-threaded program: a program with multiple streams. 使用多内核/CPUs需要多线程。

 Benefits of threads

  1. Takes less time to create a new thread than a process.
  2. Less time to terminate a thread than a process.
  3. Switching between two threads takes less time than switching between processes.
  4. Threads enhance efficiency in communication between programs.

Examples

用户在word输入文本:打开一个文件并输入文本(一个线程),文本自动格式化(另一个线程),文本自动识别拼写错误(另一个线程),文件自动保存到磁盘(另一个线程)。

Typing in a file is a process, formatting, spells checking, saving these are threads.

Threads are scheduled on a processor, and each thread can execute a set of instructions independent of other processes and threads.  线程被安排在处理器上,每个线程可以独立于其他进程和线程执行一组指令。

Thread Control Block (TCB) stores the information about a thread

它与属于同一进程的其他线程共享其代码区、数据区和其他操作系统资源,如开放文件和信号。

 A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack.  线程是CPU利用率的基本单位。

线程状态:

  1. Undefined: 未创建的线程处于undefined状态;
  2. Ready: 当线程就绪时,处于ready状态;
  3. Running: 同一时刻只能有一个线程处于running状态;
  4. Suspended: 当线程被中断运行(suspend),处于suspended状态,随后可调用resume()回归就绪状态;
  5. Terminated & Destroyed: 处于中断terminated状态的进程会被释放,随后处于销毁destroyed状态;

2. Multicore Programming 多核编程

Whether the cores appear across CPU chips or within CPU chips, we call these systems multicore or multiprocessor systems. Multithreaded programming provides a mechanism for more effificient use of these multiple computing cores and improved concurrency.

2.1 Concurrency vs. Parallelism 并发vs.并行

Concurrent execution on single-core system: Concurrency means multiple tasks which start, run, and complete in overlapping time periods, in no specific order. 并发意味着多个任务在重叠的时间段内启动、运行和完成,没有特定的顺序。

 Parallelism on a multi-core system: A system is parallel if it can perform more than one task simultaneously.  如果一个系统可以同时执行一个以上的任务,那么它就是并行的。

并发(Concurrency)和并行 (Parallelism) 的区别:

并发是指一个处理器同时处理多个任务。 
并行是指多个处理器或者是多核的处理器同时处理多个不同的任务。 
并发是逻辑上的同时发生,而并行是物理上的同时发生。 
来个比喻:并发是一个人同时吃三个馒头,而并行是三个人同时吃三个馒头。 

2.2 Programming challenges 编程挑战

In general, five areas present challenges in programming for multicore systems:

  1. Identifying tasks. 这涉及到检查应用程序,以找到可以划分为独立的、并发的任务的区
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高纯度Coder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值