Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threads & Processes Vs MultiThreading & Multi-Core/MultiProcessor : How they are mapped?

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?

like image 325
claws Avatar asked Nov 11 '09 07:11

claws


People also ask

How realistic is the movie Threads?

Threads is a masterclass in naturalistic, realistic portraiture. Jimmy, Ruth and their families, Clive Sutton and his wife are all established with a brilliant economy of both time and dialogue. There's also some wonderful, understated acting from the leads.

Where can I watch Threads?

Right now you can watch Threads on Shudder. You are able to stream Threads by renting or purchasing on Amazon Instant Video or Vudu. You are able to stream Threads for free on Tubi.

How did the movie Threads end?

One of the boys is killed, and Jane and the other boy engage in a struggle for the food that degenerates into "crude intercourse." Months later, Jane gives birth in a makeshift hospital, and the film ends as she looks in horror at her stillborn baby.


2 Answers

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.

like image 192
minjang Avatar answered Oct 07 '22 02:10

minjang


Threads running on the same core are not technically parallel. They only appear to be executed in parallel, as the CPU switches between them very fast (for us, humans). This switch is what is called context switch. Now, threads executing on different cores are executed in parallel. Most modern CPUs have a number of cores, however, most modern OSes (windows, linux and friends) usually execute much larger number of threads, which still causes context switches. Even if no user program is executed, still OS itself performs context switches for maintanance work.
This should answer 1-3.

About 4: basically, every processor can work with threads. it is much more a characteristic of operating system. Thread is basically: memory (optional), stack and registers, once those are replaced you are in another thread.

5: the number of threads is pretty high and is limited by OS. Usually it is higher than regular programmer can successfully handle :) The number of threads is dictated by your program:

is it IO bound?

  • can the task be divided into a number of smaller tasks?
  • how small is the task? the task can be too small to make it worth to spawn threads at all.
  • synchronization: if extensive synhronization is required, the penalty might be too heavy and you should reduce the number of threads.
like image 35
Philip Derbeko Avatar answered Oct 07 '22 01:10

Philip Derbeko