31

I want to know if a program can run two threads at the same time (that is basically what it is used for correct?). But if I were to do a system call in one function where it runs on thread A, and have some other tasks running in another function where it runs on thread B, would they both be able to run at the same time or would my second function wait until the system call finishes?

Add-on to my original question: Now would this process still be an uninterruptable process while the system call is going on? I am talking about using any system call on UNIX/LINUX.

4

7 回答 7

63

多线程和并行处理是两个完全不同的话题,各有千秋,但为了介绍...

线程
当你启动一个可执行文件时,它在一个进程中的一个线程中运行。当您启动另一个线程时,将其称为线程 2,您现在在同一进程中有 2 个单独运行的执行链(线程)。在单核微处理器 (uP)上,可以运行多个线程,但不能并行运行。尽管从概念上讲,线程通常被认为是同时运行的,它们实际上是在操作系统分配和控制的时间片中连续运行的。这些切片相互交错。因此,线程 1 的执行步骤实际上并不与线程 2 的执行步骤同时发生。这些行为通常会扩展到您创建的线程数,即执行链的数据包都在同一个进程中工作并共享时间由操作系统分配的切片。

因此,在您的系统调用示例中,它实际上取决于系统调用是否会在允许其他线程的执行步骤继续之前完成。有几个因素会影响会发生什么:它是阻塞调用吗?一个线程是否比另一个线程具有更高的优先级。时间片的持续时间是多少?

与 C 中的线程相关的链接:
SO Example
POSIX
ANSI C

并行处理
当多核系统(多个 uP 或多个多核 uP)上发生多线程程序执行时,线程可以同时运行,或者并行运行,因为不同的线程可能被拆分到不同的内核以共享工作负载。这是并行处理的一个示例。

同样,从概念上讲,并行处理和线程被认为是相似的,因为它们允许同时完成一些事情。但这只是概念,它们在目标应用和技术方面确实非常不同。线程作为一种在进程中识别和拆分整个任务的方法很有用(例如,当请求新连接时,TCP/IP 服务器可能会启动工作线程,然后连接并保持该连接,只要它仍然存在) ),并行处理通常用于发送同一任务的较小组件(例如,可以在不同位置独立执行的一组复杂计算)到要同时完成的单独资源(内核或 uP)。这就是多核处理器真正发挥作用的地方。但并行处理也利用了多个系统,这些系统在遗传学MMORPG游戏等领域很流行。

与 C 中的并行处理相关的链接
OpenMP
更多 OpenMP(示例)
Gribble Labs -
NVIDIA 的 OpenMP CUDA Tookit简介

关于线程和架构的一般主题的附加阅读:

这个线程和架构的总结几乎没有触及表面。这个话题有很多部分。解决这些问题的书籍将填满一个小图书馆,并且有数千个链接。毫不奇怪,在更广泛的主题中,一些概念似乎不符合理性。例如,并不是说简单地拥有更多内核就会导致更快的多线程程序

于 2013-10-11T18:16:36.413 回答
7

是的,它们至少可能会“同时”运行,这正是线程的用途;当然还有很多细节,例如:

  • 如果两个线程都运行系统调用,例如写入相同的文件描述符,它们可能会暂时相互阻塞。

  • 如果使用互斥锁等线程同步原语,则并行执行将被阻止。

  • 您需要一个至少具有两个内核的处理器才能让两个线程真正同时运行。

这是一个非常庞大且非常复杂的主题。

于 2013-10-11T17:55:12.793 回答
3
  • 如果您的计算机只有一个 CPU,您应该知道它如何同时执行多个线程。

  • 在单处理器系统中,在给定时刻仅发生单个执行线程。因为单处理器系统支持逻辑并发,而不是物理并发。

  • 在多处理器系统上,实际上是多个线程同时执行,并且实现了物理并发。

  • 多线程程序的重要特点是支持逻辑并发,而不是实际是否实现物理并发。

于 2015-06-10T15:04:07.660 回答
2

The basics are simple, but the details get complex real quickly.

You can break a program into multiple threads (if it makes sense to do so), and each thread will run "at its own pace", such that if one must wait for, eg, some file I/O that doesn't slow down the others.

On a single processor multiple threads are accommodated by "time slicing" the processor somehow -- either on a simple clock basis or by letting one thread run until it must wait (eg, for I/O) and then "switching" to the next thread. There is a whole art/science to doing this for maximum efficiency.

On a multi-processor (such as most modern PCs which have from 2 to 8 "cores") each thread is assigned to a separate processor, and if there are not enough processors then they are shared as in the single processor case.

The whole area of assuring "atomicity" of operations by a single thread, and assuring that threads don't somehow interfere with each other is incredibly complex. In general a there is a "kernel" or "nucleus" category of system call that will not be interrupted by another thread, but thats only a small subset of all system calls, and you have to consult the OS documentation to know which category a particular system call falls into.

于 2013-10-11T19:21:55.517 回答
1

它们将同时运行,因为一个线程独立于另一个线程,即使您执行系统调用也是如此。

不过测试它很容易,您可以创建一个线程,将某些内容打印到控制台输出并在另一个线程上执行系统调用,您知道这将花费一些合理的时间。您会注意到消息将继续由另一个线程打印。

于 2013-10-11T17:53:20.800 回答
1

是的,一个程序可以同时运行两个threads

它被称为 多线程

他们都能够同时运行还是我的第二个函数会等到系统调用完成?

他们都能够同时运行。


如果你愿意,你可以让线程 B 等到线程 A 完成或反转

于 2013-10-11T17:56:11.133 回答
0

两个线程只有在多核处理器系统上才能同时运行,但如果它只有一个核处理器,则两个线程不能同时运行。所以一次只有一个线程运行,如果它完成了它的工作,那么队列中的下一个线程需要时间。

于 2014-07-15T10:23:17.747 回答