Linux 上进程的启动时间

发布于 2024-11-04 04:59:08 字数 303 浏览 0 评论 0原文

如何使用c语言在ubuntu linux机器上查找进程启动时间。在linux中,/proc/[pid]/stat文件提供了信息

starttime %lu /*系统启动后进程启动的时间(以jiffies为单位)*/
和文件 /proc/stat 给出了

btime %lu /*measurement of system boot time since Epoch in seconds*/  

要添加这两个值,我如何将前一个值转换为秒,因为它以 jiffies 为单位。

How to find a process start time on ubuntu linux machine using c language. In linux there is /proc/[pid]/stat file which give information

starttime %lu /*The time in jiffies the process started after system boot*/
and file /proc/stat that gives

btime %lu /*measurement of system boot time since Epoch in seconds*/  

For adding both these values how can I convert former value into seconds because it is in jiffies unit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榆西 2024-11-11 04:59:09

当编译 Linux 内核时,每秒的 Jiffies 是可配置的。

以下程序使用您正在运行的内核上每秒的 jiffies 数。它需要一个可选的命令行参数,即进程号。默认是正在运行的程序本身的进程号。它每秒输出指定进程的开始时间,包括本地时间和 UTC 时间。重复循环的唯一原因是证明该值不会改变。

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
int
find_nth_space(char *search_buffer,
               int   space_ordinality
              )
{
  int jndex;
  int space_count;

  space_count=0;

  for(jndex=0;
      search_buffer[jndex];
      jndex++
     )
  {
    if(search_buffer[jndex]==' ')
    {
      space_count++;

      if(space_count>=space_ordinality)
      {
        return jndex;
      }
    }
  }

  fprintf(stderr,"looking for too many spaces\n");

  exit(1);

} /* find_nth_space() */

int
main(int    argc,
     char **argv
    )
{
  int       field_begin;
  int       stat_fd;

  char      proc_buf[80];
  char      stat_buf[2048];

  long      jiffies_per_second;

  long long boot_time_since_epoch;
  long long process_start_time_since_boot;

  time_t    process_start_time_since_epoch;

  ssize_t   read_result;

  struct tm gm_buf;
  struct tm local_buf;

  jiffies_per_second=sysconf(_SC_CLK_TCK);

  if(argc<2)
  {
    strcpy(proc_buf,"/proc/self/stat");
  }
  else
  {
    sprintf(proc_buf,"/proc/%ld/stat",strtol(argv[1],NULL,0));
  }

  for(;;)
  {
    stat_fd=open(proc_buf,O_RDONLY);

    if(stat_fd<0)
    {
      fprintf(stderr,"open() fail\n");

      exit(1);
    }

    read_result=read(stat_fd,stat_buf,sizeof(stat_buf));

    if(read_result<0)
    {
      fprintf(stderr,"read() fail\n");

      exit(1);
    }

    if(read_result>=sizeof(stat_buf))
    {
      fprintf(stderr,"stat_buf is too small\n");

      exit(1);
    }

    field_begin=find_nth_space(stat_buf,21)+1;

    stat_buf[find_nth_space(stat_buf,22)]=0;

    sscanf(stat_buf+field_begin,"%llu",&process_start_time_since_boot);

    close(stat_fd);

    stat_fd=open("/proc/stat",O_RDONLY);

    if(stat_fd<0)
    {
      fprintf(stderr,"open() fail\n");

      exit(1);
    }

    read_result=read(stat_fd,stat_buf,sizeof(stat_buf));

    if(read_result<0)
    {
      fprintf(stderr,"read() fail\n");

      exit(1);
    }

    if(read_result>=sizeof(stat_buf))
    {
      fprintf(stderr,"stat_buf is too small\n");

      exit(1);
    }

    close(stat_fd);

    field_begin=strstr(stat_buf,"btime ")-stat_buf+6;

    sscanf(stat_buf+field_begin,"%llu",&boot_time_since_epoch);

    process_start_time_since_epoch
    =
    boot_time_since_epoch+process_start_time_since_boot/jiffies_per_second;

    localtime_r(&process_start_time_since_epoch,&local_buf);
    gmtime_r   (&process_start_time_since_epoch,&gm_buf   );

    printf("local time: %02d:%02d:%02d\n",
           local_buf.tm_hour,
           local_buf.tm_min,
           local_buf.tm_sec
          );

    printf("UTC:        %02d:%02d:%02d\n",
           gm_buf.tm_hour,
           gm_buf.tm_min,
           gm_buf.tm_sec
          );

    sleep(1);
  }

  return 0;
} /* main() */

Jiffies per second is configurable when one compiles the Linux kernel.

The following program uses the number of jiffies per second on the kernel you're running. It takes an optional command line parameter, which is the process number. The default is the process number of the running program itself. Each second, it outputs the start time of the specified process, both as local time and UTC. The only reason for the repeat loop is to demonstrate that the value doesn't change.

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
int
find_nth_space(char *search_buffer,
               int   space_ordinality
              )
{
  int jndex;
  int space_count;

  space_count=0;

  for(jndex=0;
      search_buffer[jndex];
      jndex++
     )
  {
    if(search_buffer[jndex]==' ')
    {
      space_count++;

      if(space_count>=space_ordinality)
      {
        return jndex;
      }
    }
  }

  fprintf(stderr,"looking for too many spaces\n");

  exit(1);

} /* find_nth_space() */

int
main(int    argc,
     char **argv
    )
{
  int       field_begin;
  int       stat_fd;

  char      proc_buf[80];
  char      stat_buf[2048];

  long      jiffies_per_second;

  long long boot_time_since_epoch;
  long long process_start_time_since_boot;

  time_t    process_start_time_since_epoch;

  ssize_t   read_result;

  struct tm gm_buf;
  struct tm local_buf;

  jiffies_per_second=sysconf(_SC_CLK_TCK);

  if(argc<2)
  {
    strcpy(proc_buf,"/proc/self/stat");
  }
  else
  {
    sprintf(proc_buf,"/proc/%ld/stat",strtol(argv[1],NULL,0));
  }

  for(;;)
  {
    stat_fd=open(proc_buf,O_RDONLY);

    if(stat_fd<0)
    {
      fprintf(stderr,"open() fail\n");

      exit(1);
    }

    read_result=read(stat_fd,stat_buf,sizeof(stat_buf));

    if(read_result<0)
    {
      fprintf(stderr,"read() fail\n");

      exit(1);
    }

    if(read_result>=sizeof(stat_buf))
    {
      fprintf(stderr,"stat_buf is too small\n");

      exit(1);
    }

    field_begin=find_nth_space(stat_buf,21)+1;

    stat_buf[find_nth_space(stat_buf,22)]=0;

    sscanf(stat_buf+field_begin,"%llu",&process_start_time_since_boot);

    close(stat_fd);

    stat_fd=open("/proc/stat",O_RDONLY);

    if(stat_fd<0)
    {
      fprintf(stderr,"open() fail\n");

      exit(1);
    }

    read_result=read(stat_fd,stat_buf,sizeof(stat_buf));

    if(read_result<0)
    {
      fprintf(stderr,"read() fail\n");

      exit(1);
    }

    if(read_result>=sizeof(stat_buf))
    {
      fprintf(stderr,"stat_buf is too small\n");

      exit(1);
    }

    close(stat_fd);

    field_begin=strstr(stat_buf,"btime ")-stat_buf+6;

    sscanf(stat_buf+field_begin,"%llu",&boot_time_since_epoch);

    process_start_time_since_epoch
    =
    boot_time_since_epoch+process_start_time_since_boot/jiffies_per_second;

    localtime_r(&process_start_time_since_epoch,&local_buf);
    gmtime_r   (&process_start_time_since_epoch,&gm_buf   );

    printf("local time: %02d:%02d:%02d\n",
           local_buf.tm_hour,
           local_buf.tm_min,
           local_buf.tm_sec
          );

    printf("UTC:        %02d:%02d:%02d\n",
           gm_buf.tm_hour,
           gm_buf.tm_min,
           gm_buf.tm_sec
          );

    sleep(1);
  }

  return 0;
} /* main() */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文