零线程进程?

发布于 2024-12-07 08:13:47 字数 45 浏览 1 评论 0原文

一个进程中是否必须至少有一个线程?进程是否可能没有任何线程,或者这没有意义?

Does a process have to have at least one thread in it? Is it possible for a process to be void of any threads, or does this not make sense?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦途 2024-12-14 08:13:47

一个进程通常至少有一个线程。 维基百科有这样的定义:

执行线程是操作系统可以调度的最小处理单元。线程和进程的实现因操作系统而异,但在大多数情况下,线程包含在进程内。

MSDN 支持这一点:

处理器执行线程,而不是进程,因此每个应用程序都至少有一个进程,并且进程始终至少有一个执行线程,称为主线程。

尽管它接着说:

一个进程可以有零个或多个单线程单元和零个或一个多线程单元。

这意味着如果单线程单元和多线程单元的数量都可以为零。然而,这个过程不会做太多:)

A process usually has at least one thread. Wikipedia has the definition:

a thread of execution is the smallest unit of processing that can be scheduled by an operating system. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process.

The MSDN backs this up:

A processor executes threads, not processes, so each application has at least one process, and a process always has at least one thread of execution, known as the primary thread.

Though it does go on to say:

A process can have zero or more single-threaded apartments and zero or one multithreaded apartment.

Which implies that if both the number of single-threaded apartments and multithreaded apartments could be zero. However, the process wouldn't do much :)

ゃ懵逼小萝莉 2024-12-14 08:13:47

在类 Unix 操作系统中,可能存在 僵尸进程,其中的条目仍然存在于进程表,即使没有(不再)任何线程。

In Unix-like operating systems, it's possible to have a zombie process, where an entry still exists in the process table even though there are no (longer) any threads.

旧人 2024-12-14 08:13:47

您可以选择不使用显式线程库,或者没有线程概念的操作系统(因此不称其为线程),但对于大多数现代编程来说,所有程序都至少有一个执行线程(通常称为作为主线程或 UI 线程或类似)。如果存在,则该过程也存在。

思想实验:零执行线程的进程会做什么?

You can choose not to use an explicit threading library, or an operating system that has no concept of threads (and so doesn't call it a thread), but for most modern programming all programs have at least one thread of execution (generally referred to as a main thread or UI thread or similar). If that exits, so does the process.

Thought experiment: what would a process with zero threads of execution do?

执着的年纪 2024-12-14 08:13:47

从理论上讲,我不明白为什么不。但这对于流行的操作系统来说是不可能的。

进程通常由几个不同的部分组成:

  • 线程
  • 内存空间
  • 文件描述符
  • 环境(根目录、当前目录等)
  • 特权(UID 等)
  • 等等

理论上,进程可以在没有线程的情况下作为 RPC 服务器存在。其他进程会进行 RPC 调用,从而在服务器进程中生成线程,然后当函数返回时这些线程就会消失。我不知道有任何操作系统可以这样工作。

在大多数操作系统上,进程会在最后一个线程退出时或主线程退出时退出。

注意:这会忽略“无用”的情况,例如僵尸进程,它们没有线程,但不执行任何操作。

In theory, I don't see why not. But it would be impossible with the popular operating systems.

A process typically consists of a few different parts:

  • Threads
  • Memory space
  • File discriptors
  • Environment (root directory, current directory, etc.)
  • Privileges (UID, etc.)
  • Et cetera

In theory, a process could exist with no threads as an RPC server. Other processes would make RPC calls which spawn threads in the server process, and then the threads disappear when the function returns. I don't know of any operating systems that work this way.

On most OSs, the process exits either when the last thread exits, or when the main thread exits.

Note: This ignores the "useless" cases such as zombie processes, which have no threads but don't do anything.

氛圍 2024-12-14 08:13:47

“main”本身就是线程。它是一个被执行的线程。因此,每个进程至少在一个线程上运行。

"main" itself is thread. Its a thread that gets executed. So, every process runs on at least one thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文