Question

I need some help to understand the basics of concurrent programming. In fact, the more I read and the more I'm getting confused. So, I understood that a process is an instance of a program that is being executed sequentially and that it can consist of one or more threads. In a single core CPU, only one thread can be executed at once, while in a multi-core CPU, many thread can run concurrently. Now the questions are:

1) How many threads and processes can be run concurrently with a, for example, quad-core CPU? Only four threads at the same time? and only 1 process?

2) Is the parallelism possible only for threads and not for processes? For example, if I'm running three different applications (=processes) on a three-core CPU, like a browser web, a word processor and a calculator, can they use just a core for each and run concurrently?

3) In many books, you can find the explanation of three states: runnable, blocked and running. Do they refer to processes or threads or both? I don't understand. Thank you.

Was it helpful?

Solution

From a conceptual level, you can consider processes and threads to be very analogous. The main difference is that usually threads will only have access to memory contained within the process that spawned it, and processes make for an easy way to kill a bundle of threads. So while an operating system spawns multiple processes, a process spawns multiple threads. I'm sure someone can pick apart those two statements, but I find them to be true on the surface at the very least.

I have to start by answering your third question, to give you a better context for your other questions. Threads and processes start in the RUNNABLE state, which means it may be selected by the thread/process scheduler as the next thread/process to be run on a given core. Once it has been selected, it is loaded into that core's context and enters the RUNNING state. This continues until the thread/process has completed its operation and dies, or it waits on a condition. That wait puts it in the BLOCKED state. Once that condition is lifted, it enters the RUNNABLE state (even if that condition becomes true later!)

For your first question, you can spawn as many processes and threads as you have system resources for, but only one per virtual core will be RUNNING at any given time. (This is the selection process outlined above.) Note when I say virtual core, I mean what the operating system reports as a core. Advanced technologies allow us to put what appears to be multiple cores on the same core. So you may have a 4-core system with dual hyper-threading which will report 8 cores to the operating system. This is an abstraction that we typically are not concerned with - we just care about how many cores the operating system says we have and go with that.

For your second question, parallelism is possible with both, but you'll need to manage their memory different. Processes will often use the file system or a database to share their memory (although inter-process communication is possible, it's just more difficult in my experience). Threads have the advantage of a shared memory space, but that also means they must be on the same system - with a database accessible from different machines, you can parallelize processes on different machines.

OTHER TIPS

1) Yes, on a 4 cores/4 threads CPU, only 4 threads may be running at a time. Things would be a little more complicated if we were talking about CPUs with hyper-threading, but to be honest I wouldn't be the right person to explain it.

2) Yes, that is possible. A quad-core CPU would be able to run 4 threads at a time - these threads may all belong to the same process, or to 4 distinct processes.

3) They refer to threads. A running thread is, well, a thread that was given a time slice and is currently being executed by the CPU. A blocked thread is one who is, for example, waiting for I/O results. A runnable thread is one that is no longer blocked, is ready to be run, but hasn't yet been given a time slice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top