线程 &进程 vs 多线程 &多核/多处理器:它们是如何映射的? [英] Threads & Processes Vs MultiThreading & Multi-Core/MultiProcessor : How they are mapped?

查看:20
本文介绍了线程 &进程 vs 多线程 &多核/多处理器:它们是如何映射的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑,但以下线程消除了我的疑虑:

多处理、多线程、超线程、多核

但它从硬件的角度解决了查询.我想知道这些硬件功能是如何映射到软件上的?

显而易见的一件事是 MultiProcessor(=Mutlicpu) 和 MultiCore 之间没有区别,除了在多核中所有 cpus 都驻留在一个芯片(die)上,而在 Multiprocessor 中,所有 cpus 都在自己的芯片上连接在一起.

因此,多核/多处理器系统能够同时"执行多个进程(firefox、mediaplayer、googletalk)(与在单个处理器系统上切换这些进程的上下文不同)对吗?

如果正确.到目前为止我很清楚.但是当多线程出现时就会出现混乱.

  1. 多线程用于"并行处理.对吗?

  2. cpu内部涉及多线程的元素有哪些?图表?对于我要利用并行处理两个独立任务的能力,CPU 的要求应该是什么?

  3. 当人们说线程的上下文切换时.我真的不明白.因为如果它的线程上下文切换那么它不是并行处理.线程必须严格同时"执行.对吗?

    我的多线程概念是:考虑一个带有单 CPU 的系统.当进程上下文切换到 Firefox 时.(假设)firefox 的每个选项卡都是一个线程,所有线程都严格同时执行.不像一个线程执行了一段时间然后另一个线程又执行了直到上下文切换时间到来.

  4. 如果我在无法处理线程的处理器上运行多线程软件会怎样?我的意思是 cpu 如何处理此类软件?

  5. 如果到目前为止一切都很好,那么现在的问题是有多少线程?我猜它必须受硬件限制吗?如果硬件只能支持 2 个线程并且我在我的进程中启动了 10 个线程.cpu 将如何处理它?优点缺点?从软件工程的角度来看,在开发可供用户在各种系统中使用的软件时,我该如何决定是否应该使用多线程?如果是这样,有多少个线程?

解决方案

首先,尝试理解进程"和线程"的概念.线程是执行的基本单位:线程由操作系统调度,由CPU执行.进程是一种容纳多个线程的容器.

  1. 是的,多处理或多线程用于并行处理.更准确地说,是为了利用线程级并行性.

  2. 好的,多线程可能意味着硬件多线程(一个例子是超线程).但是,我假设您只是说软件中的多线程.从这个意义上说,CPU 应该支持上下文切换.

  3. 需要上下文切换来实现多任务,即使是在物理单核中按时分.

  4. 假设有两个物理内核和四个非常繁忙的线程.在这种情况下,两个线程只是在等待,直到他们有机会使用 CPU.阅读一些与抢占式操作系统调度相关的文章.

  5. 可以物理并发运行的线程数与逻辑处理器的数量相同.您正在询问 OS 文献中的一般线程调度问题,例如循环..

强烈建议您先学习操作系统的基础知识.然后继续处理多线程问题.您似乎还不清楚上下文切换和调度等关键概念.这将需要几个月的时间,但是如果您真的想成为计算机软件专家,那么您应该了解这些非常基本的概念.请拿走任何操作系统书籍和讲座幻灯片.

I was very confused but the following thread cleared my doubts:

Multiprocessing, Multithreading,HyperThreading, Multi-core

But it addresses the queries from the hardware point of view. I want to know how these hardware features are mapped to software?

One thing that is obvious is that there is no difference between MultiProcessor(=Mutlicpu) and MultiCore other than that in multicore all cpus reside on one chip(die) where as in Multiprocessor all cpus are on their own chips & connected together.

So, mutlicore/multiprocessor systems are capable of executing multiple processes (firefox,mediaplayer,googletalk) at the "sametime" (unlike context switching these processes on a single processor system) Right?

If it correct. I'm clear so far. But the confusion arises when multithreading comes into picture.

  1. MultiThreading "is for" parallel processing. right?

  2. What are elements that are involved in multithreading inside cpu? diagram? For me to exploit the power of parallel processing of two independent tasks, what should be the requriements of CPU?

  3. When people say context switching of threads. I don't really get it. because if its context switching of threads then its not parallel processing. the threads must be executed "scrictly simultaneously". right?

    My notion of multithreading is that: Considering a system with single cpu. when process is context switched to firefox. (suppose) each tab of firefox is a thread and all the threads are executing strictly at the same time. Not like one thread has executed for sometime then again another thread has taken until the context switch time is arrived.

  4. What happens if I run a multithreaded software on a processor which can't handle threads? I mean how does the cpu handle such software?

  5. If everything is good so far, now question is HOW MANY THREADS? It must be limited by hardware, I guess? If hardware can support only 2 threads and I start 10 threads in my process. How would cpu handle it? Pros/Cons? From software engineering point of view, while developing a software that will be used by the users in wide variety of systems, Then how would I decide should I go for multithreading? if so, how many threads?

解决方案

First, try to understand the concept of 'process' and 'thread'. A thread is a basic unit for execution: a thread is scheduled by operating system and executed by CPU. A process is a sort of container that holds multiple threads.

  1. Yes, either multi-processing or multi-threading is for parallel processing. More precisely, to exploit thread-level parallelism.

  2. Okay, multi-threading could mean hardware multi-threading (one example is HyperThreading). But, I assume that you just say multithreading in software. In this sense, CPU should support context switching.

  3. Context switching is needed to implement multi-tasking even in a physically single core by time division.

  4. Say there are two physical cores and four very busy threads. In this case, two threads are just waiting until they will get the chance to use CPU. Read some articles related to preemptive OS scheduling.

  5. The number of thread that can physically run in concurrent is just identical to # of logical processors. You are asking a general thread scheduling problem in OS literature such as round-robin..

I strongly suggest you to study basics of operating system first. Then move on multithreading issues. It seems like you're still unclear for the key concepts such as context switching and scheduling. It will take a couple of month, but if you really want to be an expert in computer software, then you should know such very basic concepts. Please take whatever OS books and lecture slides.

这篇关于线程 &进程 vs 多线程 &多核/多处理器:它们是如何映射的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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