栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Linux上进程的开始时间

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux上进程的开始时间

当人们编译Linux内核时,每秒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>intfind_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 spacesn");  exit(1);} intmain(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() failn");      exit(1);    }    read_result=read(stat_fd,stat_buf,sizeof(stat_buf));    if(read_result<0)    {      fprintf(stderr,"read() failn");      exit(1);    }    if(read_result>=sizeof(stat_buf))    {      fprintf(stderr,"stat_buf is too smalln");      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() failn");      exit(1);    }    read_result=read(stat_fd,stat_buf,sizeof(stat_buf));    if(read_result<0)    {      fprintf(stderr,"read() failn");      exit(1);    }    if(read_result>=sizeof(stat_buf))    {      fprintf(stderr,"stat_buf is too smalln");      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:%02dn",local_buf.tm_hour,local_buf.tm_min,local_buf.tm_sec          );    printf("UTC:        %02d:%02d:%02dn",gm_buf.tm_hour,gm_buf.tm_min,gm_buf.tm_sec          );    sleep(1);  }  return 0;} 


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/403283.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号