Environment
- Ubuntu 20.04.3 LTS
- Intel Core i5-7300HQ CPU @ 2.50GHz × 4 (4 cores)
- .Net Core version: 5.0.402
Problem
Print the CPU core number (not the number of available cores) that an item is running on.
Code
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Test {
class Program {
static void Main(string[] args) {
Run();
}
static void Run() {
string fileDirectory = "/path/to/files/";
List<string> symbols = new List<string> {
"AAPL",
"AMZ",
"NICK",
"ZEUS"
};
ParallelOptions parallelOptions = new ParallelOptions {MaxDegreeOfParallelism = Environment.ProcessorCount};
Console.WriteLine("parallelOptions.MaxDegreeOfParallelism = " + parallelOptions.MaxDegreeOfParallelism);
Parallel.ForEach(symbols, parallelOptions, symbol => {
// Pass symbol filepath to ProcessFile.
ProcessFile(String.Format("{0}{1}{2}", fileDirectory, symbol, ".csv"));
});
}
static void ProcessFile(string filePath){
Console.WriteLine("Processing " + filePath);
}
}
}
Current Output
parallelOptions.MaxDegreeOfParallelism = 4
Processing /path/to/files/AAPL.csv
Processing /path/to/files/AMZ.csv
Processing /path/to/files/ZEUS.csv
Processing /path/to/files/NICK.csv
Desired Output
parallelOptions.MaxDegreeOfParallelism = 4
Processing /path/to/files/AAPL.csv on CPU Core 1
Processing /path/to/files/AMZ.csv on CPU Core 4
Processing /path/to/files/ZEUS.csv on CPU Core 2
Processing /path/to/files/NICK.csv on CPU Core 3
Question
What is the correct way to get the CPU core number (not the number of available cores)? Edit: ideally looking for a .Net Core class/method instead of calling an external OS command (Top, PS, etc.).