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

查看:100
本文介绍了多处理器机器中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.

内核调度程序会将Process(及其所有线程)视为一个用于调度的实体.依次是线程库选择要运行的线程.它可以在可运行线程之间划分内核给定的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实现来回答该问题.如果您谈论的是绿色"线程,它们是在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库一起工作以管理线程.内核执行上下文切换,调度,内存管理,高速缓存内存管理等.当然,还有其他管理是在用户级别完成的,但是如果没有内核,pthread的许多功能将丢失.

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)内核调度程序会将Process(及其所有线程)视为一个进行调度的实体.依次是线程库选择要运行的线程.它可以将内核给定的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天全站免登陆