Multi-Processes Experiments – When Can Windows Utilize All the Cores?


I have raised a question in SO: How can the OS use all cores for my application? basically, my single-threaded applications seem not utilizing all the cores if running several copies.

The answer “multiple threads are necessary in order for any applications to utilize all cores” makes sense and I make the following experiments.

Less Intensive Processes Do Not Trouble CPUs

It makes sense that if a process does low-computational-intensive work, or fight over the same resources (e.g. exclusive access to the same file), multiple processes of the same kind will not trouble the CPU too much.

Let’s create a VBScript and name it job3.vbs which has only 1 line:

1
WScript.Sleep(60000)
WScript.Sleep(60000)

And we will use the cscript.exe to interpret the VBScript under Windows Scripting Host. Let’s launch 20 copies of them, so the Windows Task Manager should see 20 processes.

for /l %x in (1,1,20) do start cscript.exe job3.vbs

We use the start command to launch the script asynchronously so basically these 20 cscript.exe processes will start roughly the same time e.g. without this keyword, they will start one by one after the previous process exits and returns to the command prompt.

less-intensive-jobs-do-not-trouble-cpu Multi-Processes Experiments - When Can Windows Utilize All the Cores? javascript jscript vbscript windows scripting host

less-intensive-jobs-do-not-trouble-cpu

With no surprise, these processes (idle) do not trouble CPU cores at all, e.g. you could have hundreds of them. On the HPZ800 Server with 2 CPUs (X5650 total 24 logical cores), it is far from utilizing a single logical core (which will be 1/24 = 4%).

Computation-Intensive Processes Utilize All Cores

A process has at least one thread. If it has several threads running at the same time, then it is known as a multithreading program.

How is a thread different than a process?

A process has its own stacks, heaps and other resources. A thread is a light-weight process but several threads share the same heap, stack and other resources. Creating a thread is cheap whilst creating a process is expensive. Threads synchronization (e.g. mutex, critical sessions) is in general easier to write than the process synchronization (e.g. pipe, file mapping, sockets).

Can single-threaded process utilize all cores?

Here, in order to utilize all cores, you must convince the OS that a process is computation-intensive. Computation-Intensive Processes are not necessary multi threading. However, most of the cases, multithreaded application is computation-intensive except those threads related to I/O e.g. A thread can send some data over the socket while another thread is fetching some data from the hard disk.

A single-threaded process can utilize at most a core. However, depending on how ‘busy’ it is, the OS may decide allocate separate cores if you launch many processes of the same kind simutanenously.

When it comes to managing processes you are completely at mercy of how the OS wants to manage its processes. Multi processes simultaneously are known as multi-tasking. However, it does not guarantee performance speed-ups.

Let’s create a JScript (Microsoft’s Javascript Implementation) and run it multiple times at Windows Scripting Host.

1
2
3
var s = 0;
for (var i = 0; i < 99999999999; ++i) s ++;
WScript.Echo (s);
var s = 0;
for (var i = 0; i < 99999999999; ++i) s ++;
WScript.Echo (s);

This script pretends to do some serious calculations, and likewise, we run it 20 copies at the same time.

for /l %x in (1,1,20) do start cscript.exe job2.js

Surprisingly, each process takes up around 4.5%, which is slightly more than a logical core.

multiple-processes-of-single-threaded-utilizes-all-cores Multi-Processes Experiments - When Can Windows Utilize All the Cores? javascript jscript vbscript windows scripting host

multiple-processes-of-single-threaded-utilizes-all-cores

windows-task-manager-multiple-processes-of-single-threaded-utilizes-all-cores Multi-Processes Experiments - When Can Windows Utilize All the Cores? javascript jscript vbscript windows scripting host

windows-task-manager-multiple-processes-of-single-threaded-utilizes-all-cores

Each cscript process is obviously single-threaded. However it is busy spinning the CPU cycle and that is why the OS thinks it is computation-intensive, giving it a full core (at most). If you launch multiple of them, OS may be likely to allocate another separate core a another process.

From the below table comparing the Pros and Cons of MultiCores, we know that if you really want to make your application utilize all cores, it is better to parallelize your core algorithms in the first place.

pros-and-cons-of-more-cores-versus-fewer-cores-cpu Multi-Processes Experiments - When Can Windows Utilize All the Cores? javascript jscript vbscript windows scripting host

pros-and-cons-of-more-cores-versus-fewer-cores-cpu

Remember, Using multiple threads in the same process means you can control it and utilize all available resources. If the process is just single-threaded,

it is entirely up to the OS to decide how many resources (cores) to allocate. Click To Tweet

But, again, it is worth pointing out that a single-threaded process can use at most a core.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
994 words
Last Post: How to Get the GUID using VBScript or Javascript (JScript) using Scriptlet.TypeLib?
Next Post: How to Async and Await in C++11?

The Permanent URL is: Multi-Processes Experiments – When Can Windows Utilize All the Cores?

Leave a Reply