src/linux_parser.cpp
// TODO: Read and return the uptime of a process
// REMOVE: [[maybe_unused]] once you define the function
Read and return the uptime of a process
REMOVE: [[maybe_unused]] once you define the function
Read and return the uptime of a process REMOVE : [[ maybe_unused ]] once you define the function
long LinuxParser::UpTime(int pid) { std::string line, value;
  long time = 0;
  std::ifstream filestream(kProcDirectory + std::to_string(pid) + kStatFilename);
  if (filestream.is_open()) {
    while (std::getline(filestream, line)) {
      std::istringstream linestream(line);
      for (int i = 1; i <= kStarttime_; i++) {
        linestream >> value;
        if (i==kStarttime_) {
          try {
            time = UpTime() - std::stol(value) / sysconf(_SC_CLK_TCK);
            return time;
          } catch (const std::invalid_argument& arg) {
            return 0;
          }
        }
      }
    }
  }
  return time;
 }
long LinuxParser :: UpTime ( int pid ) { std :: string line , value ; long time = 0 ; std :: ifstream filestream ( kProcDirectory + std :: to_string ( pid ) + kStatFilename ) ; if ( filestream . is_open ( ) ) { while ( std :: getline ( filestream , line ) ) { std :: istringstream linestream ( line ) ; for ( int i = 1 ; i <= kStarttime_ ; i ++ ) { linestream >> value ; if ( i == kStarttime_ ) { try { time = UpTime ( ) - std :: stol ( value ) / sysconf ( _SC_CLK_TCK ) ; return time ; } catch ( const std :: invalid_argument & arg ) { return 0 ; } } } } } return time ; }
TODO: Read and return the uptime of a process
REMOVE: [[maybe_unused]] once you define the function
TODO : Read and return the uptime of a process REMOVE : [[ maybe_unused ]] once you define the function
//The time the process started after system boot.  In kernels before Linux 2.6, this value was expressed //in jiffies.  Since Linux 2.6, the value is expressed in clock ticks (divide by sysconf(_SC_CLK_TCK)). //The format for this field was %lu before Linux 2.6.That means in order to get the unit of time it has been running since start  //you need to subtract it from the UpTime() of the system