How does Erlang implement concurrency without the use of OS threads? - multithreading

If Erlang does its own process creation and scheduling, without utilizing OS threads, how does it make use of multiple CPU cores? My limited understanding is that the OS assigns the CPU cores to OS threads.

Erlang runs on a virtual machine called BEAM.
The Erlang process runs a separate BEAM VM on each core (using OS threads).
See this related SO question.

Related

Can node.js worker threads run on all CPUs and all cores?

I understand that node.js can run on multiple cores on at least one CPU. What's not clear to me is whether it can run on more than one CPU. For example, if I have a 4-CPU machine with 10 cores in each CPU, can a single node.js process take advantage of all 40 cores, or just 10? Does it depend on the OS?
Other than performance effects (such as NUMA), multi-socket systems work exactly like single-socket-multi-core systems, whatever that implies for node.js. They're all SMP systems with multiple CPU cores and cache-coherent shared memory.
OSes will run threads across all physical cores in the system, so all that matters is that your workload is threaded at all.
The only thing that would be different is a cluster of machines with shared memory that's not cache-coherent; in that case you wouldn't be running a single instance of an OS across all the cores.

What Operating System services are necessary to support kernel-level threads?

I am studying Solaris and Linux and am viewing Kernel Level Threads (KLTs) as the fundamental entity that can be scheduled and dispatched by the OS. I know that a multi-threaded OS must store thread execution context and provide mechanisms to schedule and dispatch KLTs, and that kernel level threads handle interrupts, system calls, and provide an interface to the CPU as a resource at the user-kernel interface. I am not clear on what services are necessary to support KLTs in a multi-threaded OS.
I cannot determine if there is a core kernel process that is necessary to support all KLTs, or if KLTs run interdependently as the base-level of computing. I would like to understand what minimum set of operations (resource allocation, scheduling) is necessary to support an OS with KLTs.
I have looked at Tanenbaums Tanenbaum's discussion of threads in his distributed systems book, Understanding the Linux Kernel, and MultiThreading the SunOS kernel but I cannot find an answer to my question.
I believe that answering the question -- What Operating System services are necessary to support kernel-level threads? -- will help me understand how KLTs are implemented.

Multithreded applications on different CPUS

If, for example, there is a let's say embedded application which run on unicore CPU. And then that application would be ported on multi core CPU. Would that app run on single or multiple cores?
To be more specific I am interested in ARM CPU (but not only) and toolchain specifics e. g. standard C/C++ libraries.
The intention of this question is this: is it CPU's responsibility to "decide" to execute on multiple cores or compiler toolchain, developer and standard platfor specific libraries? And again, I am interested also in other systems' tendencies out there.
There are plenty of applications and RTOS (for example Linux) that run on different CPUs but the same architecture, so does that mean that they are compiled differently?
Generally speaking single-threaded code will always run on one core. To take advantage of multiple cores you need to have either multiple processes, multiple threads, or both.
There's nothing your compiler can do to help you here. This is an architectural consideration.
If you have multiple threads, for example, most multi-core systems will run them on whatever cores are available if the operating system you're running is properly compiled to support that. Running an OS that's been compiled single-core only will obviously limit your options here.
A single threaded program will run in one thread. It is theoretically possible for the thread to be scheduled to move to a different core, but the scheduler cannot turn a single thread into multiple threads and give you any parallel processing.
EDIT
I misunderstood your question. If there are multiple threads in the application, and that application is binary compatible with the new multicore CPU, the threads will indeed be scheduled to run on different CPUs, if the OS scheduler deems it appropriate.
Well it all depends on the software that if it wants to utilize other cores or not (if present). Lets take an example of Linux on ARM's cortexA53.
Initially a vendor provided boot loader runs on, FSBL (First state bootloader). It then passes control to Arm trusted firmware. ATF then runs uboot. All these run on a single core. Then uboot loads linux kernel and passes control to it. Linux then initializes some stuff and looks into some option, first in the bootargs for smp or nosmp flags. if smp it will get the number of CPUs assigned to it from dtb and then using SMC calls to ATF it will start other cores and then assign work to those cores to provide true feel of multiprocessing environment. This is normally called load balancing and in linux it is mostly done in fair.c file.

Hardware thread vs soft threads?

I have read that in a multi core processor each core contains 2 hardware threads for example in dual core processor 4 hardware threads are running. Now if i create 2 threads in java are those threads going to map with 2 hardware threads or those 2 java threads are executed by single hardware thread of a particular core ?
That is dependent on a lot of things, however the 2 hardware threads per core you are referring to is the Intel HyperThreading technology. This technology enables the CPU to have two Thread Context's in memory and be executing simultaneously, sharing execution resources.
What threads run where is OS implemention dependent and mostly resolved by the Thread Scheduler algorithm of your OS.

Threads and CPU Affinity

Lets say there are two processors on a machine. Thread A is running on P1 and Thread B is running on P2.
Thread A calls Sleep(10000);
Is it possible that when Thread A starts executing again, it runs on P2?
If yes, who decides this transition? If no, why not?
Does Processor store some data that which all threads it's running or OS binds each thread to Processor for its full lifetime ?
It is possible. This would be determined by the operating system process scheduler and may also be dependent on the application that is running. No information about previously running threads is kept by the processor, aside from whatever is in the cache.
This is dependent on many things, it behaves differently depending on the particular operating system. See also: Processor Affinity and Scheduling Algorithms. Under Windows you can pin a particular process to a processor core via the task manager.
Yes, it is possible. Though ultimately a thread inherits its CPU (or CPU core) from the process (executable.) In operating systems, which CPU or CPU core a process runs on for its current quanta (time slice) is decided by the Scheduler:
http://en.wikipedia.org/wiki/Scheduling_(computing)
-Oisin
The OS decides which processor to run the thread on, and it may easily change during the lifetime of that thread, especially if there is a context switch (caused by the sleep). It's completely possible if the system is loaded that both threads will be running on the same processor (or core), just at different times. Or if there isn't any load on the system, both threads may continue to run on separate processors.

Resources