Skip to content

Commit 433a0cc

Browse files
committed
Implement subprocess
In .NET it's not trivial to run a non-child subprocess and read its output. An intermediary shell (sh/cmd) is required.
1 parent f9dcdcd commit 433a0cc

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

1brc/App.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class App : IDisposable
5151
public unsafe App(string filePath, int? threadCount = null, ProcessMode processMode = ProcessMode.Default)
5252
{
5353
if (processMode == default)
54-
processMode = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ProcessMode.RandomAccessAsync : ProcessMode.MmapViewPerChunkRandom;
54+
processMode = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ProcessMode.RandomAccessAsync : ProcessMode.MmapSingleSharedPos;
5555

5656
_processMode = processMode;
5757

@@ -426,7 +426,7 @@ public void PrintResult()
426426

427427
var strResult = sb.ToString();
428428
// File.WriteAllText($"D:/tmp/results/buybackoff_{DateTime.Now:yy-MM-dd_hhmmss}.txt", strResult);
429-
// Console.WriteLine(strResult);
429+
Console.WriteLine(strResult);
430430

431431
result.Dispose();
432432

1brc/Program.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics;
22
using System.Numerics;
3+
using System.Runtime.InteropServices;
34
using System.Runtime.Intrinsics;
45
using System.Runtime.Intrinsics.X86;
56
using System.Text;
@@ -10,14 +11,44 @@ internal class Program
1011
{
1112
private static void Main(string[] args)
1213
{
13-
var sw = Stopwatch.StartNew();
14+
var path = args.Length > 0 ? args[0] : "D:/tmp/measurements_1B.txt";
15+
1416
Console.OutputEncoding = Encoding.UTF8;
15-
var path = args.Length > 0 ? args[0] : "D:/tmp/measurements_1B_10K.txt";
17+
18+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || args.Contains("--worker"))
19+
DoWork(path);
20+
else
21+
StartSubprocess(path);
22+
}
23+
24+
private static void DoWork(string path)
25+
{
1626
using (var app = new App(path))
1727
{
28+
var sw = Stopwatch.StartNew();
1829
app.PrintResult();
30+
sw.Stop();
31+
Console.Out.Close();
1932
}
20-
sw.Stop();
21-
Console.WriteLine($"Finished in: {sw.ElapsedMilliseconds:N0} ms");
33+
}
34+
35+
private static void StartSubprocess(string path)
36+
{
37+
string parentProcessPath = Process.GetCurrentProcess().MainModule!.FileName;
38+
39+
Console.WriteLine($"CMD: -c \"{parentProcessPath} {path} --worker & \" ");
40+
41+
var processStartInfo = new ProcessStartInfo
42+
{
43+
FileName = "sh", // parentProcessPath,
44+
Arguments = $"-c \"{parentProcessPath} {path} --worker & \" ",
45+
RedirectStandardOutput = true,
46+
UseShellExecute = false,
47+
CreateNoWindow = false,
48+
};
49+
50+
var process = Process.Start(processStartInfo);
51+
string? output = process!.StandardOutput.ReadLine();
52+
Console.Write(output);
2253
}
2354
}

0 commit comments

Comments
 (0)