static void Main(string[] args) { const int numOfProcesses = 100; const int cpuMinTimeOfProcess = 10; const int cpuMaxTimeOfProcess = 50; const int processGenerationIntervalMin = 2; const int processGenerationIntervalMax = 20; var cpuQueue = new CPUQueue(); var cpu = new CPU(cpuMinTimeOfProcess, cpuMaxTimeOfProcess, cpuQueue); var thread = new Thread(cpu.StartProcessing); thread.Start(); for (int i = 0; i < numOfProcesses; i++) { cpu.AddProcessToQueue(new CPUProcess(i)); Thread.Sleep(_random.Next(processGenerationIntervalMin, processGenerationIntervalMax)); } while (cpu.IsProcessing) { ; } cpu.StopProcessing(); Console.WriteLine(cpuQueue.ProcessedFromQueue1); Console.WriteLine(cpuQueue.ProcessedFromQueue2); Console.ReadKey(); }
public CPUProcess(int cpuProcessId, CPUQueue queue, CPU cpu, int minProcessDurationMs = 8000, int maxProcessDurationMs = 20000) { _cpuProcessId = cpuProcessId; _queue = queue ?? throw new ArgumentNullException(nameof(queue)); _cpu = cpu ?? throw new ArgumentNullException(nameof(cpu)); _minProcessDurationMs = minProcessDurationMs; _maxProcessDurationMs = maxProcessDurationMs; }
static void Main(string[] args) { var firstProcessesQueue = new CPUQueue(maxSize: 7); var secondProcessesQueue = new CPUQueue(maxSize: 7); var cpu = new CPU(firstProcessesQueue, secondProcessesQueue); var firstCpuProcess = new CPUProcess( cpuProcessId: 1, firstProcessesQueue, cpu, minProcessDurationMs: 3000, maxProcessDurationMs: 6000); var secondCpuProcess = new CPUProcess( cpuProcessId: 2, secondProcessesQueue, cpu, minProcessDurationMs: 2000, maxProcessDurationMs: 5000); var cpuThread = new Thread(cpu.Run); var firstCpuProcessThread = new Thread(firstCpuProcess.GenerateProcesses); var secondCpuProcessThread = new Thread(secondCpuProcess.GenerateProcesses); cpuThread.Start(); firstCpuProcessThread.Start(); secondCpuProcessThread.Start(); }
public CPU(CPUQueue firstQueue, CPUQueue secondQueue) { _firstQueue = firstQueue ?? throw new ArgumentNullException(nameof(firstQueue)); _secondQueue = secondQueue ?? throw new ArgumentNullException(nameof(secondQueue)); IsBusy = false; }