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

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

问题描述

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

多处理,多线程,超线程,多核

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

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

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

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

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

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

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

    我的多线程概念是: 考虑具有单个cpu的系统.当进程上下文切换为Firefox时. (假设)firefox的每个选项卡都是一个线程,并且所有线程都严格同时执行.不像一个线程执行了一段时间,然后另一个线程再次占用了上下文切换时间.

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

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

解决方案

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

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

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

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

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

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

强烈建议您先学习操作系统的基础知识.然后继续讨论多线程问题.您似乎还不清楚诸如上下文切换和调度之类的关键概念.这将花费两个月的时间,但是如果您真的想成为计算机软件方面的专家,那么您应该了解这些非常基本的概念.请获取所有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.

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

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