Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <thread>
- #include <chrono>
- #include <unistd.h>
- #include <sys/wait.h>
- using namespace std;
- void PrintMemoryInfo(pid_t processID)
- {
- cout << "\nID Processo: " << processID << endl;
- // Su Linux le informazioni sui processi risiedono in /proc/[PID]/status
- string statusPath = "/proc/" + to_string(processID) + "/status";
- ifstream statusFile(statusPath);
- if (!statusFile.is_open())
- {
- cout << "Impossibile aprire " << statusPath << endl;
- return;
- }
- string line;
- while (getline(statusFile, line))
- {
- // Filtriamo le metriche di memoria più comuni (es. VmRSS, VmSize)
- if (line.rfind("VmRSS:", 0) == 0 || line.rfind("VmSize:", 0) == 0 || line.rfind("VmMm", 0) == 0)
- {
- cout << line << endl;
- }
- }
- }
- void PrintModules(pid_t processID)
- {
- cout << "\n\nELENCO LIBRERIE / MEMORY MAPPINGS\n";
- // Su Linux l'equivalente dei moduli (DLL) sono i segmenti mappati in /proc/[PID]/maps
- string mapsPath = "/proc/" + to_string(processID) + "/maps";
- ifstream mapsFile(mapsPath);
- if (!mapsFile.is_open())
- {
- cout << "Impossibile aprire " << mapsPath << endl;
- return;
- }
- string line;
- int count = 0;
- while (getline(mapsFile, line) && count < 20) // Limitiamo l'output per leggibilità
- {
- cout << line << endl;
- count++;
- }
- cout << "... (elenco troncato per brevità) ...\n";
- }
- int main(int argc, char* argv[])
- {
- // Su Linux la creazione di un processo avviene duplicando il corrente (fork)
- // e sovrascrivendo l'immagine con un nuovo eseguibile (exec).
- pid_t pid = fork();
- if (pid < 0)
- {
- cerr << "Errore durante la fork." << endl;
- return 2;
- }
- else if (pid == 0)
- {
- // --- PROCESSO FIGLIO ---
- // Sostituiamo il processo corrente con l'eseguibile desiderato
- execl("/usr/bin/xfce4-about", "xfce4-about", NULL);
- // Se execl fallisce, arriva qui
- cerr << "Errore nell'avvio del processo figlio." << endl;
- _exit(1);
- }
- else
- {
- // --- PROCESSO PADRE ---
- this_thread::sleep_for(chrono::milliseconds(50));
- PrintMemoryInfo(pid);
- PrintModules(pid);
- // Metti in attesa il processo principale fino al completamento del figlio
- int status;
- waitpid(pid, &status, 0);
- cout << "\n\nIl controllo e` tornato al processo principale!" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment