Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Operating Systems 1 - Week 05 Lab
Launching, inspecting and controlling external processes with System.Diagnostics, and capturing what they print back.
PIDusing System.Diagnostics;
Process.Start("xed"); // launch a program
Process.Start("xed", "example.txt"); // launch and open a fileSystem.Diagnostics, and the simple case is one line.| Property | What it holds |
|---|---|
ProcessName | the process name, such as notepad |
Id | the unique Process ID |
StartTime | the date and time it started |
HasExited | whether it is still running |
WorkingSet64 | memory usage in bytes |
Process process = Process.Start("xed");
Console.WriteLine($"{process.ProcessName} (PID: {process.Id})");Process.GetProcesses returns every process running on the systemProcess.GetCurrentProcess returns your own application's processProcess.GetProcessesByName returns all processes matching a nameProcess.GetProcessById returns one by PID, throwing ArgumentException when it is goneProcess[] chromeProcesses = Process.GetProcessesByName("chrome");
Process me = Process.GetCurrentProcess();Process process = Process.Start("xed");
bool exited = process.WaitForExit(5000);
Console.WriteLine(exited ? "Exited in time" : "Still running");| Call | What actually happens |
|---|---|
Kill() | terminates the process immediately, with no chance to clean up |
Close() | releases your tracking of it, and the process keeps running |
var startInfo = new ProcessStartInfo
{
FileName = "xed",
Arguments = "example.txt",
WindowStyle = ProcessWindowStyle.Maximized
};
Process.Start(startInfo);WorkingDirectory and CreateNoWindowRedirectStandardOutput, RedirectStandardInput and RedirectStandardError| Value | What you gain, and what you lose |
|---|---|
true, the default | opens URLs and documents with their default program, but cannot redirect input or output |
false | runs a program directly with redirection, but cannot open a URL |
true plus the URL itself as the FileNamevar startInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = "-c \"date\"",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
Process process = Process.Start(startInfo);
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();using System.Runtime.InteropServices;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Console.WriteLine("Running on Linux");
}notepad.exe and cmd.exe with /cxed, nano or vim, and /bin/bash with -cHasExited before reading properties of a process that may be goneStart, GetProcesses, GetCurrentProcess, GetProcessesByName and GetProcessById cover launching and findingWaitForExit waits, Kill terminates forcefully, and Close only stops trackingProcessStartInfo gives the fine control, and UseShellExecute is the switch between shell integration and redirectionUseShellExecute = false together with RedirectStandardOutput = trueRuntimeInformation.IsOSPlatformMahmoud Abas|Operating Systems Lab - Week 5: Process Management in C#