/////////////////////////////////////////////////////////////////////////////// // // Microsoft Research Singularity // // Copyright (c) Microsoft Corporation. All rights reserved. // // Note: Allocate strings and encourage GCs // using System; using System.Collections; using System.Threading; using Microsoft.Singularity.UnitTest; namespace Microsoft.Singularity.Applications { public class ForkTest { static readonly string []! ProcessArgs = new string[] { "testpe" }; static readonly TimeSpan ProcessTimeout = TimeSpan.FromSeconds(10); const int IterationsPerThread = 64; static object! monitor = new object(); static AutoResetEvent! exitEvent = new AutoResetEvent(false); static int threadCount; private static void ThreadMain() { lock (ForkTest.monitor) { ForkTest.threadCount++; } for (int i = 0; i < IterationsPerThread; i++) { Process process = new Process(ForkTest.ProcessArgs); process.Start(); Assert.True(process.Join(ProcessTimeout), "Failed to join process."); } lock (ForkTest.monitor) { ForkTest.threadCount--; if (ForkTest.threadCount == 0) { ForkTest.exitEvent.Set(); } } } public static void Run() { ForkTest.monitor = new object(); ForkTest.exitEvent = new AutoResetEvent(false); ForkTest.threadCount = 0; for (int i = 0; i < Settings.MaxThreads; i++) { Thread t = new Thread(new ThreadStart(ThreadMain)); t.Start(); } ForkTest.exitEvent.WaitOne(); } } }