views:

1054

answers:

6

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?

A: 
  1. Broadly speaking, yeah, but "parallel" can mean different things.

  2. It depends what tasks you want to run in parallel.

  3. Not necessarily. Some (indeed most) threads spend a lot of time doing nothing. Might as well switch away from them to a thread that wants to do something.

  4. The OS handles thread switching. It will delegate to different cores if it wants to. If there's only one core it'll divide time between the different threads and processes.

  5. The number of threads is limited by software and hardware. Threads consume processor and memory in varying degrees depending on what they're doing. The thread management software may impose its own limits as well.

Moishe
A: 

Multiple threads are separate 'chains' of commands within one process. From CPU point of view threads are more or less like processes. Each thread has its own set of registers and its own stack.

The reason why you can have more threads than CPUs is that most threads don't need CPU all the time. Thread can be waiting for user input, downloading something from the web or writing to disk. While it is doing that, it does not need CPU, so CPU is free to execute other threads.

In your example, each tab of Firefox probably can even have several threads. Or they can share some threads. You need one for downloading, one for rendering, one for message loop (user input), and perhaps one to run Javascript. You cannot easily combine them because while you download you still need to react to user's input. However, download thread is sleeping most of the time, and even when it's downloading it needs CPU only occasionally, and message loop thread only wakes up when you press a button.

If you go to task manager you'll see that despite all these threads your CPU use is still quite low.

Of course if all your threads do some number-crunching tasks, then you shouldn't create too many of them as you get no performance benefit (though there may be architectural benefits!).

However, if they are mainly I/O bound then create as many threads as your architecture dictates. It's hard to give advice without knowing your particular task.

yu_sha
+2  A: 

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.

minjang
+1 but one symantic problem: "5. The number of... _concurrent_ is just identical to # of logical processors." Should be _simultaneously_. Multiple threads executing on a single logical processor execute concurrently if there is switching.
SnOrfus
A: 
Philip Derbeko
A: 

The key thing to remember is the separation between logical/virtual parallelism and real/hardware parallelism. With your average OS, a system call is performed to spawn a new thread. What actually happens (whether it is mapped to a different core, a different hardware thread on the same core, or queued into the pool of software threads) is up to the OS.

  1. Parallel processing uses all the methods not just multi-threading.
  2. Generally speaking, if you want to have real parallel processing, you need to perform it in hardware. Take the example of the Niagara, it has up to 8-cores each capable of executing 4-threads in hardware.
  3. Context switching is needed when there are more threads than is capable of being executed in parallel in hardware. Even then, when executed in series (switching between one thread to the next), they are considered concurrent because there is no guarantee on the order of switching. So, it may go T0, T1, T2, T1, T3, T0, T2 and so on. For all intents and purposes, the threads are parallel.
  4. Time slicing.
  5. That would be up to the OS.
sybreon
A: 

I'm a bit confused. Lets say we have 1 processor with 2 cores. So far the discussion has been focused on threads. However, what if I have an application that does not use threads, but proceesses. This application consists of a program that creates 2 child processes by means of fork and then exec. Lets say that the child processes are left to execute in a non blocking way (e.g. WNOHANG). Will each of the child processes be executed by each core in parallel? Again, I'm not using threads here, ONLY processes.

thanks

ze
1. You shouldn't ask this here. Create a new thread for that.2. It is responsibility of the OS running to utilize the underlying hardware resources Efficiently. Modern operating systems do execute these processes in parallel. If you are running Windows 2000 while running such instruction. It may not be able to take advantage.
claws
@claws: Why would Windows 2000 *not* be able to make use of multiple (well, only 2 for W2k Professional, but there are similar limits for more modern Windows versions too) cores or processors?
mghie
Because its old. It released prior to the release of multi-core processor.
claws