fcamuso

Corso Sistemi Operativi: leggere PCB, programma che legge il PCB (x Linux))

Aug 23rd, 2026
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <thread>
  6. #include <chrono>
  7. #include <unistd.h>
  8. #include <sys/wait.h>
  9.  
  10. using namespace std;
  11.  
  12. void PrintMemoryInfo(pid_t processID)
  13. {
  14.     cout << "\nID Processo: " << processID << endl;
  15.  
  16.     // Su Linux le informazioni sui processi risiedono in /proc/[PID]/status
  17.     string statusPath = "/proc/" + to_string(processID) + "/status";
  18.     ifstream statusFile(statusPath);
  19.  
  20.     if (!statusFile.is_open())
  21.     {
  22.         cout << "Impossibile aprire " << statusPath << endl;
  23.         return;
  24.     }
  25.  
  26.     string line;
  27.     while (getline(statusFile, line))
  28.     {
  29.         // Filtriamo le metriche di memoria più comuni (es. VmRSS, VmSize)
  30.         if (line.rfind("VmRSS:", 0) == 0 || line.rfind("VmSize:", 0) == 0 || line.rfind("VmMm", 0) == 0)
  31.         {
  32.             cout << line << endl;
  33.         }
  34.     }
  35. }
  36.  
  37. void PrintModules(pid_t processID)
  38. {
  39.     cout << "\n\nELENCO LIBRERIE / MEMORY MAPPINGS\n";
  40.  
  41.     // Su Linux l'equivalente dei moduli (DLL) sono i segmenti mappati in /proc/[PID]/maps
  42.     string mapsPath = "/proc/" + to_string(processID) + "/maps";
  43.     ifstream mapsFile(mapsPath);
  44.  
  45.     if (!mapsFile.is_open())
  46.     {
  47.         cout << "Impossibile aprire " << mapsPath << endl;
  48.         return;
  49.     }
  50.  
  51.     string line;
  52.     int count = 0;
  53.     while (getline(mapsFile, line) && count < 20) // Limitiamo l'output per leggibilità
  54.     {
  55.         cout << line << endl;
  56.         count++;
  57.     }
  58.     cout << "... (elenco troncato per brevità) ...\n";
  59. }
  60.  
  61. int main(int argc, char* argv[])
  62. {
  63.     // Su Linux la creazione di un processo avviene duplicando il corrente (fork)
  64.     // e sovrascrivendo l'immagine con un nuovo eseguibile (exec).
  65.     pid_t pid = fork();
  66.  
  67.     if (pid < 0)
  68.     {
  69.         cerr << "Errore durante la fork." << endl;
  70.         return 2;
  71.     }
  72.     else if (pid == 0)
  73.     {
  74.         // --- PROCESSO FIGLIO ---
  75.         // Sostituiamo il processo corrente con l'eseguibile desiderato
  76.         execl("/usr/bin/xfce4-about", "xfce4-about", NULL);
  77.  
  78.         // Se execl fallisce, arriva qui
  79.         cerr << "Errore nell'avvio del processo figlio." << endl;
  80.         _exit(1);
  81.     }
  82.     else
  83.     {
  84.         // --- PROCESSO PADRE ---
  85.         this_thread::sleep_for(chrono::milliseconds(50));
  86.  
  87.         PrintMemoryInfo(pid);
  88.         PrintModules(pid);
  89.  
  90.         // Metti in attesa il processo principale fino al completamento del figlio
  91.         int status;
  92.         waitpid(pid, &status, 0);
  93.  
  94.         cout << "\n\nIl controllo e` tornato al processo principale!" << endl;
  95.     }
  96.  
  97.     return 0;
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment