/// <summary> /// Get the Unix Process by searching the /proc files. The /proc directory is a virtual file system, /// sometimes reffered to as the process information pseudo file system.It doesn't contain 'real' files but runtime system information. /// Each of the numbered directories corresponds to an actual process ID. Details of this process can /// be obtained by looking at the associated files in the directory for this process, for example /proc/460. Inside this folder we can find more directories: /// - /proc/PID/cmdline for command line arguments that were used to lanch the process /// - /proc/PID/stat for process status /// </summary> /// <returns></returns> public static LinuxProcess[] GetProcesses() { const string PROC_DIR = "/proc"; IList <LinuxProcess> processList = new List <LinuxProcess>(); foreach (var directory in Directory.GetDirectories(PROC_DIR)) { var directoryName = Path.GetFileName(directory); //We want to search only through the files that are associated with a PID, that have //filenames represented as numbers. So, if we can not convert it to an int, then we skip the //directory int pid; bool isPidDirectory = int.TryParse(directoryName, out pid); if (!isPidDirectory) { continue; } //Parse the process status file in order to get the filename of that lanched the process. If no stat //file is present, than we skip and continue with the next directory LinuxProcessStatusFile processStatusFile = ParseLinuxProcessStatusFile(pid); if (processStatusFile == null) { continue; } //Build Linux process and add it to our result set LinuxProcess linuxProcess = BuildProcess(processStatusFile); processList.Add(linuxProcess); } return(processList.ToArray()); }
private static LinuxProcess BuildProcess(LinuxProcessStatusFile processStatusFile) { string processName = GetProcessName(processStatusFile); LinuxProcess linuxProcess = new LinuxProcess(processStatusFile.Pid, processName); return(linuxProcess); }
public override void RefreshInformation() { foreach (var basicProcessInformation in BasicProcessList) { LinuxProcess p = LinuxProcess.GetProcesses().SingleOrDefault(x => x.ProcessName == basicProcessInformation.ProcessName); basicProcessInformation.Refresh(p); } }
/// <summary> /// Refresh the information about the associated Linux process. /// </summary> /// <param name="process">The associated process where the information is retrieved from</param> public void Refresh(LinuxProcess process) { //if process can not be found, re-initialize all members of the process information object if (process == null) { Pid = default(int); ; StartDateTime = default(DateTime); MemoryUsageMb = default(int); UptimeInSeconds = default(int); State = ProcessState.NotRunning; return; } //else update the information of this object from the associated process Pid = process.Id; StartDateTime = process.StartTime; MemoryUsageMb = (int)(process.WorkingSet64 / 1024 / 1024); UptimeInSeconds = (int)(DateTime.Now - process.StartTime).TotalSeconds; State = ProcessState.Running; }
private static LinuxProcess BuildProcess(LinuxProcessStatusFile processStatusFile) { string processName = GetProcessName(processStatusFile); LinuxProcess linuxProcess = new LinuxProcess(processStatusFile.Pid, processName); return linuxProcess; }