34

我想从在基于 linux 的系统上运行的 C 应用程序中获取系统正常运行时间。我不想调用 uptime(1) 并解析输出,我想调用我怀疑存在的底层 C API。任何人都知道是否有这样的调用,或者 uptime(1) 是否只是处理从 wtmp 获得的记录?

4

5 回答 5

45

您要查找的系统调用是 sysinfo()。

它在 sys/sysinfo.h 中定义

它的签名是:int sysinfo(struct sysinfo *info)

从内核 2.4 开始,结构看起来像这样:

struct sysinfo {
    long uptime;             /* Seconds since boot */
    unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
    unsigned long totalram;  /* Total usable main memory size */
    unsigned long freeram;   /* Available memory size */
    unsigned long sharedram; /* Amount of shared memory */
    unsigned long bufferram; /* Memory used by buffers */
    unsigned long totalswap; /* Total swap space size */
    unsigned long freeswap;  /* swap space still available */
    unsigned short procs;    /* Number of current processes */
    unsigned long totalhigh; /* Total high memory size */
    unsigned long freehigh;  /* Available high memory size */
    unsigned int mem_unit;   /* Memory unit size in bytes */
    char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding for libc5 */
};

玩得开心!

于 2009-10-09T14:22:57.177 回答
21

那将是这样的。

#include <stdio.h>
#include <errno.h>
#include <linux/unistd.h>       /* for _syscallX macros/related stuff */
#include <linux/kernel.h>       /* for struct sysinfo */
#include <sys/sysinfo.h>

long get_uptime()
{
    struct sysinfo s_info;
    int error = sysinfo(&s_info);
    if(error != 0)
    {
        printf("code error = %d\n", error);
    }
    return s_info.uptime;
}

有关详细信息,请参阅“man sysinfo”。

于 2010-06-16T10:05:21.210 回答
15

读取文件/proc/uptime并将第一个十进制数字作为正常运行时间(以秒为单位)。

来自man 5 proc

   /proc/uptime
          This file contains two numbers: the uptime of the  system  (sec‐
          onds), and the amount of time spent in idle process (seconds).
于 2009-10-08T21:34:26.320 回答
3

还有clock_gettime(可能需要-lrt)。我所看到的行为(我不会声称它是有保证的)但给出CLOCK_MONOTONICclk_id是它在给定struct timespec *参数中返回系统正常运行时间。

#include <stdio.h>
#include <time.h>

int main(int argc, char* argv[]) {
  struct timespec t;
  clock_gettime(CLOCK_MONOTONIC, &t);
  printf("tv_sec=%llu tv_nsec=%llu\n",
    (unsigned long long)t.tv_sec,
    (unsigned long long)t.tv_nsec);
  return 0;
}
于 2013-02-11T23:08:46.787 回答
1
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <grp.h>

int main() {
  struct sysinfo sys_info;
  struct group* grp;

    gid_t gid;
    char** users;

  int days, hours, mins, x = 1;

  system("clear");  
  printf("\033[4;40m            Systems information                   \033[0;m \n");
  if(sysinfo(&sys_info) != 0)
    perror("sysinfo");

  days = sys_info.uptime / 86400;
  hours = (sys_info.uptime / 3600) - (days * 24);
  mins = (sys_info.uptime / 60) - (days * 1440) - (hours * 60);

  printf("\033[1;33m  Uptime: \033[0;36m %ddays, %dhours, %dminutes, %ldseconds \033[0;m \n",
                      days, hours, mins, sys_info.uptime % 60);

  printf("\033[1;33m  Load Avgs: \033[0;m 1min(%ld) 5min(%ld) 15min(%ld) \n",
          sys_info.loads[0], sys_info.loads[1], sys_info.loads[2]);

  printf("\033[1;33m  Total Ram: \033[0;m %ldk \t Free: %ldk \n", sys_info.totalram / 1024, sys_info.freeram / 1024);
  printf(" \033[1;33m Shared Ram: \033[0;m %ldk ", sys_info.sharedram / 1024);
  printf("  Buffered Ram: %ldk \n", sys_info.bufferram / 1024);
  printf("\033[1;33m  Total Swap: \033[0;m %ldk \t Free swap: %ldk \n", sys_info.totalswap / 1024, sys_info.freeswap / 1024);
  printf("\033[1;33m  Total High Memory: \033[0;m %ldk  Free high memory: %ldk \033[0;m \n", sys_info.totalhigh / 1024, sys_info.freehigh / 1024);
  printf(" \n");
  printf("\033[1;44m Total Number of processes: %d \033[0;m \n", sys_info.procs);  
  gid = getgid();
    printf("  Group ID: \033[031m %d", gid);
    if((grp = getgrgid(gid)) == NULL ) return 1;
    printf("\033[0;m Group %s ", grp->gr_name );
    printf("\n  Users in your group ");
    for( users = grp->gr_mem; *users != NULL; users++,++x ); printf( "%d", ++x);     
    if(strcmp(grp->gr_passwd,"x") == 0) printf("  Password is protected by shadow file. \n");
    else printf("Password: %s ", grp->gr_passwd);   

    return 0;
}
于 2016-01-07T20:01:46.513 回答