多处理器机器中posix线程的并发 [英] Concurrency of posix threads in multiprocessor machine

查看:16
本文介绍了多处理器机器中posix线程的并发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对多处理器机器中 posix 线程的并发性有一些疑问.我在 SO 中发现了类似的问题,但没有找到结论性的答案.

I have some doubts regarding concurrency of posix threads in multiprocessor machine. I have found similar questions in SO regarding it but didnt find conclusive answer.

以下是我的理解.我想知道我是否正确.

Below is my understanding. I want to know if i am correct.

  1. Posix 线程是用户级线程,内核不知道这一点.

  1. Posix threads are user level threads and kernel is not aware of it.

内核调度程序会将进程(及其所有线程)视为一个实体进行调度.线程库反过来选择运行哪个线程.它可以在可运行线程之间分割内核给出的 CPU 时间.

Kernel scheduler will treat Process( with all its threads) as one entity for scheduling. It is the thread library that in turn chooses which thread to run. It can slice the cpu time given by the kernel among the run-able threads.

用户线程可以在不同的 CPU 内核上运行.即让线程 T1 &T2 由 Process(T) 创建,然后 T1 可以在 Cpu1 中运行,T2 可以在 Cpu2 中运行但它们不能同时运行.

User threads can run on different cpu cores. ie Let threads T1 & T2 be created by a Process(T), then T1 can run in Cpu1 and T2 can run in Cpu2 BUT they cant run concurrently.

如果我的理解正确,请告诉我.

Please let me know if my understanding in correct.

谢谢...

推荐答案

既然你用Linux"标记了你的问题,我将根据 linux 下的标准 pthreads 实现来回答它.如果您在谈论 "green" 线程,它们在 VM/语言级别而不是操作系统,那么您的答案大多是正确的.但我下面的评论是关于 Linux pthreads.

Since you marked your question with "Linux" tag I'm going to answer it according to standard pthreads implementation under linux. If you are talking about "green" threads, which are scheduled at the VM/language level instead of the OS, then your answers are mostly correct. But my comments below are on Linux pthreads.

1) Posix 线程是用户级线程,内核不知道这一点.

1) Posix threads are user level threads and kernel is not aware of it.

不,这肯定不正确.Linux 内核和 pthreads 库协同工作来管理线程.内核执行上下文切换、调度、内存管理、缓存内存管理等.当然还有其他的管理在用户级别完成,但如果没有内核,pthreads 的大部分功能就会丢失.

No this is certainly not correct. The Linux kernel and the pthreads libraries work together to administer the threads. The kernel does the context switching, scheduling, memory management, cache memory management, etc.. There is other administration done at the user level of course but without he kernel, much of the power of pthreads would be lost.

2) 内核调度器会将进程(及其所有线程)视为一个实体进行调度.线程库反过来选择运行哪个线程.它可以将内核给定的 CPU 时间分片到可运行线程之间.

2) Kernel scheduler will treat Process( with all its threads) as one entity for scheduling. It is the thread library that in turn chooses which thread to run. It can slice the cpu time given by the kernel among the run-able threads.

不,内核将每个进程线程视为一个实体.它有自己的时间切片规则,将进程(和进程优先级)考虑在内,但每个子进程线程都是一个可调度的实体.

No, the kernel treats each process-thread as one entity. It has it's own rules about time slicing that take processes (and process priorities) into consideration but each sub-process thread is a schedulable entity.

3) 用户线程可以运行在不同的 CPU 内核上.即让线程 T1 &T2由Process(T)创建,那么T1可以在Cpu1中运行,T2可以在Cpu2中运行,但是它们不能同时运行.

3) User threads can run on different cpu cores. ie Let threads T1 & T2 be created by a Process(T), then T1 can run in Cpu1 and T2 can run in Cpu2 BUT they cant run concurrently.

没有.多线程程序需要并发执行.这就是为什么同步和互斥如此重要,也是为什么程序员要忍受多线程编程的复杂性.

No. Concurrent executing is expected for multi-threaded programs. That's why synchronization and mutexes are so important and why programmers put up with the complexity of multithreaded programming.

向您证明这一点的一种方法是查看带有 -L 选项的 ps 的输出以显示关联的线程.ps 通常将多个线程进程包装成一行,但是使用 -L 可以看到内核为每个线程都有一个单独的虚拟进程 ID:

One way to prove this to you is to look at the output of ps with -L option to show the associated threads. ps usually wraps multiple threaded processes into one line but with -L you can see that the kernel has a separate virtual process-id for each thread:

ps -ef | grep 20587
foo    20587     1  1 Apr09 ?        00:16:39 java -server -Xmx1536m ...

对比

ps -eLf | grep 20587
foo    20587     1 20587  0  641 Apr09 ?    00:00:00 java -server -Xmx1536m ...
foo    20587     1 20588  0  641 Apr09 ?    00:00:30 java -server -Xmx1536m ...
foo    20587     1 20589  0  641 Apr09 ?    00:00:03 java -server -Xmx1536m ...
...

我不确定 Linux 线程是否仍然这样做,但历史上 pthreads 使用 clone(2) 系统调用来创建自己的另一个线程副本:

I'm not sure if Linux threads still do this but historically pthreads used the clone(2) system call to create another thread copy of itself:

与 fork(2) 不同,这些调用允许子进程与调用进程共享部分执行上下文,例如内存空间、文件描述符表和信号处理程序表.

Unlike fork(2), these calls allow the child process to share parts of its execution context with the calling process, such as the memory space, the table of file descriptors, and the table of signal handlers.

这与创建另一个完整进程时使用的 fork(2) 不同.

This is different from fork(2) which is used when another full process is created.

这篇关于多处理器机器中posix线程的并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆