static void MaxOpenFiles(bool change) { var lim = new LibC.rlimit(); var ok = LibC.getrlimit((int)LibC.RLimit.NoFile, ref lim); Console.WriteLine($"getrlimit({lim.cur}, {lim.max}) => {ok}"); if (!change) { return; } int n = 9000; lim.cur = lim.max = (UInt64)n; ok = LibC.setrlimit((int)LibC.RLimit.NoFile, ref lim); Console.WriteLine($"setrlimit({lim.cur}, {lim.max}) => {ok}"); ok = LibC.getrlimit((int)LibC.RLimit.NoFile, ref lim); Console.WriteLine($"getrlimit({lim.cur}, {lim.max}) => {ok}"); lim.cur = (UInt64)4000; ok = LibC.setrlimit((int)LibC.RLimit.NoFile, ref lim); Console.WriteLine($"setrlimit({lim.cur}, {lim.max}) => {ok}"); ok = LibC.getrlimit((int)LibC.RLimit.NoFile, ref lim); Console.WriteLine($"getrlimit({lim.cur}, {lim.max}) => {ok}"); var a = new Stream[n]; var f = Path.GetTempFileName(); int i = 0; try { for (i = 0; i < n; ++i) { a[i] = new FileStream(f, FileMode.Open, FileAccess.Read); //var result = LibC.open(f, LibC.FileMode.S_IRUSR); //var result = LibC.fopen(f, "rb"); //if (result == IntPtr.Zero) //break; } } catch { } finally { for (int j = i - 1; j >= 0; --j) { a[j].Close(); } } Console.WriteLine($"#{i}"); }