@@ -372,34 +372,42 @@ string LinuxParser::User(int pid)
372
372
}
373
373
374
374
// Read and return the uptime of a process
375
+ // This implementation only works with kernel > 2.6
376
+ // Otherwise it will retur zero.
375
377
long LinuxParser::UpTime (int pid)
376
- {
378
+ {
379
+ std::string kernel{ LinuxParser::Kernel () };
380
+ float kernel_version{ std::stof (kernel.substr (0 ,3 )) };
377
381
long int uptime{ 0 };
378
- std::string line;
379
382
380
- std::ifstream filestream (kProcDirectory + std::to_string (pid) + kStatFilename );
381
-
382
- if (filestream.is_open ())
383
- std::getline (filestream, line);
384
-
385
- // Sometimes there are process that dies and then the pseudo file disappears, which causes errors.
386
- // Thus, if no data is detected, the analysis is by passed
387
- if (line != " " )
383
+ if (kernel_version > 2.6 )
388
384
{
389
- std::istringstream linestream{ line };
390
- std::string tmp;
391
- int index {0 };
385
+ std::string line;
392
386
393
- while (linestream >> tmp)
394
- {
395
- ++index ;
396
- if (index == 22 )
397
- break ;
398
- }
387
+ std::ifstream filestream (kProcDirectory + std::to_string (pid) + kStatFilename );
399
388
400
- long int hertz{ sysconf (_SC_CLK_TCK) };
389
+ if (filestream.is_open ())
390
+ std::getline (filestream, line);
391
+
392
+ // Sometimes there are process that dies and then the pseudo file disappears, which causes errors.
393
+ // Thus, if no data is detected, the analysis is by passed
394
+ if (line != " " )
395
+ {
396
+ std::istringstream linestream{ line };
397
+ std::string tmp;
398
+ int index {0 };
401
399
402
- uptime = std::stol (tmp) / hertz;
400
+ while (linestream >> tmp)
401
+ {
402
+ ++index ;
403
+ if (index == 22 )
404
+ break ;
405
+ }
406
+
407
+ long int hertz{ sysconf (_SC_CLK_TCK) };
408
+
409
+ uptime = UpTime () - std::stol (tmp) / hertz;
410
+ }
403
411
}
404
412
405
413
return uptime;
0 commit comments