The most efficient way to scan a list of Windows processes?

So, I'm currently working on a project that takes time when certain processes are running. I am trying to figure out the most efficient way to scan a list of processes, and then check the names of the process executables in the list of supported programs.

Essentially, the problem consists of two parts:

1) The most efficient way to get process executable names from a process list

2) The most effective way to compare this list with another list

For (1), one of the other developers played using the tasklist command and parsed the executable names. I also found out that C # has a list of System.Diagnostic processes that will do this automatically. We are still trying to solve between Java and C #, so I am probably leaning towards a language neutral solution, but this may be the deciding factor for C #.

For (2), the supported list of processes can be small on average (1-10 process names). We could just start each process on the list, but we thought that it could be too much for older PCs, so we gave up the idea of ​​creating an alphabetically balanced AVL tree containing the initial list of processes when the application starts, and checking everything against it first, and then check our list of supported process names if it is not in the tree.

Any suggestions are welcome.

Edit: Obviously, you can filter the task list by the name of the executable, so we could just do this for each process in the list of supported processes.

Edit 2: Is there an equivalent task list that works for Windows XP Home?

+3
source share
3 answers

, , , . . ( , , . 100 , .) , .

# Process.GetProcesses() - .

Java /. , Java, , / Java. , Runtime.getRuntime(). Exec ( "tasklist.exe" ), Windows exec ( "ps" ) Unix/Linux.

+4

, . , Windows? #

static void Main(string[] args)
{
    // Approved processes.
    List<string> approvedProcesses = new List<string>();
    approvedProcesses.Add("chrome");
    approvedProcesses.Add("svchost");

    // LINQ query for processes that match your approved processes.
    var processes = from p in System.Diagnostics.Process.GetProcesses()
                    where approvedProcesses.Contains(p.ProcessName)
                    select p;

    // Output matching processes to console.
    foreach (var process in processes)
        Console.WriteLine(process.ProcessName + " " + process.Id.ToString());

    Console.ReadLine();

}
+2

? , ( SO).

Less than 100 processes will usually work in a system, and even if it has several thousand, optimizing your data structures and developing the most efficient algorithms will not really save you much time.

Having said that, I suggest you get all running processes and compare this list with your approved list in memory. Any bottleneck is likely to be due to a Windows call asking about the processes, so doing this once rather than repeatedly will be useful.

+1
source

Source: https://habr.com/ru/post/1706694/


All Articles