Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6081765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:12:45+00:00 2026-05-23T11:12:45+00:00

Is there a way to get this information from the /proc directory? I want

  • 0

Is there a way to get this information from the /proc directory? I want to be able to get how long each process has been running on seconds.

EDIT: I needed to do this from C++. Sorry for the confusion.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:12:45+00:00Added an answer on May 23, 2026 at 11:12 am

    Okay guys, so after reading the top command’s source code, I figured out a non-hacky way of getting the start time of a process. The formula that they use is:

    Process_Time = (current_time - boot_time) - (process_start_time)/HZ.
    

    (You have to divide by HZ because process_start_time is in jiffies)

    Obtaining these values:

    • current_time – You can get this from the C command gettimeofday().
    • boot_time – This value is located in /proc/uptime. This file contains two numbers: the uptime of the system (seconds), and the amount of time spent in idle process (seconds). Take the first.
    • process_start_time – This value is located in /proc/[PID]/stat. The time difference (in jiffies) between system boot and when the process started. (The 22nd value in the file if you split on whitespace).

    The code (Sorry, I sometimes mix c and c++):

      int fd;
      char buff[128];
      char *p;
      unsigned long uptime;
      struct timeval tv;
      static time_t boottime;
    
    
      if ((fd = open("/proc/uptime", 0)) != -1)
      {
        if (read(fd, buff, sizeof(buff)) > 0)
        {
          uptime = strtoul(buff, &p, 10);
          gettimeofday(&tv, 0);
          boottime = tv.tv_sec - uptime;
    
        }
        close(fd);
      }
    
    
    ifstream procFile;
    procFile.open("/proc/[INSERT PID HERE]/stat");
    
    char str[255];
    procFile.getline(str, 255);  // delim defaults to '\n'
    
    
    vector<string> tmp;
    istringstream iss(str);
    copy(istream_iterator<string>(iss),
         istream_iterator<string>(),
         back_inserter<vector<string> >(tmp));
    
    process_time = (now - boottime) - (atof(tmp.at(21).c_str()))/HZ;
    

    Happy Coding!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to get the length of a collection from this? ParallelQuery<string>
Is there a way to get current information dynamically from the apps culture settings?
Is there a way I can get this array walk with my anonymous function
Is there a way to get single regex to satisfy this condition?? I am
Is there a way to get the exact name of the object that this
I'm sure this works in VS2010 but is there any way to get snippets
I can't believe I'm asking this but, is there any way to get Chrome
I'm not sure if this is possible, is there a way to get the
Is there way to get file from windows xp command prompt? I tried to
is there way how to get name ov event from Lambda expression like with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.