Skip to content

Instantly share code, notes, and snippets.

@PaluMacil
Created April 26, 2022 23:09
Show Gist options
  • Save PaluMacil/6490c8bc855fdbab5c9f8ac71d823978 to your computer and use it in GitHub Desktop.
Save PaluMacil/6490c8bc855fdbab5c9f8ac71d823978 to your computer and use it in GitHub Desktop.
output process info linux

finding libs whereis libjpeg or ldconfig -p | grep library or locate libjpeg

Dependencies

Dependencies from APT:

sudo apt install libprocps-dev

Other dependencies use vcpkg.

The following CMAKE command flag is needed to tell CMAKE to check for vcpkg libraries. In Jetbrains products, you can set this in File > Settings > Build, Execution, Deployment > CMake. Add the following verbatim, to CMAKE Options.

-DCMAKE_TOOLCHAIN_FILE=/opt/vcpkg/scripts/buildsystems/vcpkg.cmake
cmake_minimum_required(VERSION 3.16)
project(process_list)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
file(GLOB_RECURSE fileCollection "src/*.cpp")
add_executable(${PROJECT_NAME} ${fileCollection})
find_package(nlohmann_json CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME}
PRIVATE nlohmann_json nlohmann_json::nlohmann_json
procps
)
#include <iostream>
#include <cstring>
#include <proc/readproc.h>
using std::cout;
int main()
{
PROCTAB* proc = openproc(PROC_FILLMEM | PROC_FILLSTAT | PROC_FILLSTATUS);
proc_t proc_info;
memset(&proc_info, 0, sizeof(proc_info));
cout << "Program\tPID\tPPID\tMEM\tutime\tstime\n";
while (readproc(proc, &proc_info) != nullptr) {
cout << proc_info.cmd << "\t" << proc_info.tid;
cout << proc_info.ppid << "\t" << proc_info.resident;
cout << proc_info.euid << "\t" << proc_info.stime << "\n";
}
closeproc(proc);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment