关于线程和进程的一个问题

发布于 2024-11-04 06:29:15 字数 132 浏览 0 评论 0原文

我读了一些关于线程和进程的教程,据说进程是由操作系统内核调度的,而线程可以在用户模式下管理和调度。

我不明白“可以在用户模式下管理和调度线程”这句话, 例如:生产者和消费者问题?这是“在用户模式下安排”的示例吗?或者有人可以解释我吗?

I read some tutorial about threads and processes, it is said that the processes be scheduled by operating system kernel, and the threads can be managed and scheduled in a user mode.

I do not understand the saying "threads can be managed and scheduled in a user mode",
for example: the producer and consumer problem? is this a example for "scheduled in a user mode"? or can anyone explain me?

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

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

发布评论

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

评论(3

囚我心虐我身 2024-11-11 06:29:17

获得更好的教程?官方的 Java 教程非常好,并且包含关于并发的课程,这也定义进程线程的意思。

PS:是否在用户模式下管理/调度线程是Java虚拟机的实现细节,通常不需要应用程序员关心。

Get a better tutorial? The official Java tutorials are quite good, and contain a lesson on concurrency, that also defines what process and thread mean.

PS: Whether threads are managed/scheduled in user mode is an implementation detail of the Java Virtual Machine that generally need not concern the application programmer.

风渺 2024-11-11 06:29:17

在用户模式下调度意味着您可以控制软件的线程,但它们由操作系统内核管理。所以,是的,生产者消费者问题是您通常通过拥有两个线程(一个生产者线程和一个消费者线程)自己处理的一个示例(但它与用户模式调度没有直接关系)。两个线程访问相同的共享资源。该资源必须是线程安全的,这意味着您必须确保共享资源不会被损坏,因为两个线程同时访问它。线程安全可以通过使用线程安全数据类型或手动锁定或同步资源来保证。

然而,即使您对线程有一定的控制权,例如启动线程、停止线程、使线程休眠等,您也没有完全的控制权。操作系统仍在管理允许哪些线程使用 cpu 时间等。

Scheduled in user mode means you have control over the threads of your software but they are managed by the operating system kernel. So yes, the producer consumer problem is an example you normally handle yourself (but it is not directly related to user mode scheduling) by having two threads, a producer thread and a consumer thread. Both threads access the same shared recource. This resource has to be thread-safe, this means you have to make sure the shared resource does not get corrupted becouse both threads access it at the same time. Thread safety can either be guaranteed by using thread-safe data types or by manually locking or synchronizing your resource.

However, even if you have some control over your threads e.g. starting threads, stopping threads, make threads sleep etc. you do not have full control. The operating system is still managing which threads are allowed cpu time etc.

魔法少女 2024-11-11 06:29:16

不确定您正在看什么教程,但有两种方法可以安排线程。

第一个是用户模式调度,这基本上意味着一个进程,使用 绿色线程 或< a href="http://en.wikipedia.org/wiki/Fiber_%28computer_science%29" rel="nofollow">Fibers,安排不同的线程运行,而不需要操作系统参与其决策。这可以更方便地跨操作系统移植,但通常不允许您利用多个处理器。

第二个是内核调度,这意味着各种线程对内核可见并由内核调度,可能同时在不同的处理器上进行调度。然而,这可能会使线程创建和调度更加昂贵。

因此,这并不真正取决于您要解决的问题。 用户模式只是意味着线程的调度发生在不涉及操作系统的情况下。一些早期的 Java 版本使用绿色/用户模式线程,但我相信现在大多数版本都使用本机/内核线程。

编辑:
《Coding Horror》很好地概述了用户模式和内核模式的区别。

Not sure what tutorial you're looking at, but there are two ways that threads can be scheduled.

The first is user-mode scheduling, which basically mean that one process, using Green threads or perhaps fibers, schedules different threads to run without involving the operating system in its decision. This can be more portable across operating systems, but usually doesn't allow you to take advantage of multiple processors.

The second is kernel scheduling, which means that the various threads are visible to the kernel and are scheduled by it, possibly simultaneously on different processors. This can make thread creation and scheduling more expensive, however.

So it doesn't really depend on the problem that you are trying to solve. User-mode just means that the scheduling of threads happens without involving the operating system. Some early Java versions used Green/user-mode threads, but I believe most now use native/kernel threads.

EDIT:
Coding Horror has a nice overview of the difference between user and kernel mode.

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