Witness Running Many Programs At Once from Windows Command Prompt

In this blog post, you will witness how multi-core CPU executes different processes and do context switching.

We will use the following program, this program merely takes input from the user and prints input string after some time periodically.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
internal class Program
{
[DllImport("Kernel32.dll"), SuppressUnmanagedCodeSecurity]
public static extern int GetCurrentProcessorNumber();
private static void Main()
{
var currentProcess = Process.GetCurrentProcess();
Console.WriteLine($"Please enter any string for process {currentProcess.Id} which is executed by {GetCurrentProcessorNumber()}: ");
var readline = Console.ReadLine();
var i = 0;
while (i++ < 12)
{
Console.WriteLine($"For process {currentProcess.Id} which is executed by {GetCurrentProcessorNumber()}, you have entered: {readline}");
Thread.Sleep(1000);
}
}
}
OSPlayground,cs

I presume after compiling the above file, we get the output file: ‘OSPlayground.exe’.

Now open a command prompt and navigate to the path that contains the above binary. Run the following command and witness the magic.

Command to use: start /b OSPlayground && OSPlayground

Output:

The command prompt will ask you to enter the string a couple of times since we executed the program twice.
I had inputted EARTH! for the first process from the command prompt and MARS! for another one.

The above program is executed on a multicore processor.

Dattatraya Kale

Aspiring agile software craftsman, clean code, polyglot, in love with different programming paradigm. I am on a never-ending journey towards mastery of software.

Leave a Reply