In C++, we have the one that may need the running tasks list for different purposes. In OS, we can get that list from the terminal using the tasklist system call. In this article, we will learn how to get the list of all the running processes using C++.
Run Tasklist in C++
C++ provides the system() function that is used to make the system calls. We can run the tasklist command using this system() function in C++. Running the tasklist command allows you to retrieve a list of the currently running processes on a Windows system and display it to the user.
The system function allows us to execute a command in the operating system's shell. We'll use the "tasklist" command to retrieve the list of the running processes.
Syntax
system("tasklist");
The function returns zero if the command is executed successfully. It will print the task list on the console.
C++ Program to Get the List of All Running Tasks
// C++ program to illustrate how to get the list of all the
// running programs
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
// Print the objective of program
cout << "Running tasklist command to retrieve "
"list of running processes:"
<< endl;
// Execute the tasklist command and display the output
int returnValue = system("tasklist");
if (returnValue != 0) {
cerr << "Error executing tasklist command" << endl;
return 1;
}
return 0;
}
Output
Running tasklist command to retrieve list of running processes System 4 Services 0 140 K Registry 136 Services 0 57,756 K smss.exe 584 Services 0 948 K csrss.exe 916 Services 0 5,456 K wininit.exe 508 Services 0 6,372 K ...........
Note: This command will only run on Windows Operating System.