⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 output.c

📁 linux下获取一些环境信息的代码
💻 C
📖 第 1 页 / 共 5 页
字号:
    case '+': strcpy(q, "  "); break;    case '|': strcpy(q, "  "); break;    case '\0': return q-outbuf;    /* redundant & not used */    }    if (rightward-2 < 0) {      *(q+rightward)='\0';      return max_rightward;    }    q += 2;    rightward -= 2;    p++;  }  return q-outbuf;   /* gcc likes this here */}/* XPG4-UNIX, according to Digital:The "args" and "command" specifiers show what was passed to the command.Modifications to the arguments are not shown.*//* * pp->cmd       short accounting name (comm & ucomm) * pp->cmdline   long name with args (args & command) * pp->environ   environment */// FIXME: some of these may hit the guard page in forest mode/* "command" is the same thing: long unless c */static int pr_args(char *restrict const outbuf, const proc_t *restrict const pp){  char *endp = outbuf;  unsigned flags;  int rightward=max_rightward;  if(forest_prefix){    int fh = forest_helper(outbuf);    endp += fh;    rightward -= fh;  }  if(bsd_c_option) flags = ESC_DEFUNCT;  else             flags = ESC_DEFUNCT | ESC_BRACKETS | ESC_ARGS;  endp += escape_command(endp, pp, OUTBUF_SIZE, &rightward, flags);  if(bsd_e_option && rightward>1){    const char **env = (const char**)pp->environ;    if(env && *env){      *endp++ = ' ';      rightward--;      endp += escape_strlist(endp, env, OUTBUF_SIZE, &rightward);    }  }  //return endp - outbuf;  return max_rightward-rightward;}/* "ucomm" is the same thing: short unless -f */static int pr_comm(char *restrict const outbuf, const proc_t *restrict const pp){  char *endp = outbuf;  unsigned flags;  int rightward=max_rightward;    if(forest_prefix){    int fh = forest_helper(outbuf);    endp += fh;    rightward -= fh;  }  if(unix_f_option) flags = ESC_DEFUNCT | ESC_BRACKETS | ESC_ARGS;  else              flags = ESC_DEFUNCT;  endp += escape_command(endp, pp, OUTBUF_SIZE, &rightward, flags);  if(bsd_e_option && rightward>1){    const char **env = (const char**)pp->environ;    if(env && *env){      *endp++ = ' ';      rightward--;      endp += escape_strlist(endp, env, OUTBUF_SIZE, &rightward);    }  }  //return endp - outbuf;  return max_rightward-rightward;}/* Non-standard, from SunOS 5 */static int pr_fname(char *restrict const outbuf, const proc_t *restrict const pp){  char *endp = outbuf;  int rightward = max_rightward;    if(forest_prefix){    int fh = forest_helper(outbuf);    endp += fh;    rightward -= fh;  }  if (rightward>8)  /* 8=default, but forest maybe feeds more */    rightward = 8;    endp += escape_str(endp, pp->cmd, OUTBUF_SIZE, &rightward);  //return endp - outbuf;  return max_rightward-rightward;}/* elapsed wall clock time, [[dd-]hh:]mm:ss format (not same as "time") */static int pr_etime(char *restrict const outbuf, const proc_t *restrict const pp){  unsigned long t;  unsigned dd,hh,mm,ss;  char *cp = outbuf;  t = seconds_since_boot - (unsigned long)(pp->start_time / Hertz);  ss = t%60;  t /= 60;  mm = t%60;  t /= 60;  hh = t%24;  t /= 24;  dd = t;  cp +=(     dd      ?  snprintf(cp, COLWID, "%u-", dd)           :  0 );  cp +=( (dd || hh)  ?  snprintf(cp, COLWID, "%02u:", hh)         :  0 );  cp +=                 snprintf(cp, COLWID, "%02u:%02u", mm, ss)       ;  return (int)(cp-outbuf);}/* "Processor utilisation for scheduling."  --- we use %cpu w/o fraction */static int pr_c(char *restrict const outbuf, const proc_t *restrict const pp){  unsigned long long total_time;   /* jiffies used by this process */  unsigned pcpu = 0;               /* scaled %cpu, 99 means 99% */  unsigned long long seconds;      /* seconds of process life */  total_time = pp->utime + pp->stime;  if(include_dead_children) total_time += (pp->cutime + pp->cstime);  seconds = seconds_since_boot - pp->start_time / Hertz;  if(seconds) pcpu = (total_time * 100ULL / Hertz) / seconds;  if (pcpu > 99U) pcpu = 99U;  return snprintf(outbuf, COLWID, "%2u", pcpu);}/* normal %CPU in ##.# format. */static int pr_pcpu(char *restrict const outbuf, const proc_t *restrict const pp){  unsigned long long total_time;   /* jiffies used by this process */  unsigned pcpu = 0;               /* scaled %cpu, 999 means 99.9% */  unsigned long long seconds;      /* seconds of process life */  total_time = pp->utime + pp->stime;  if(include_dead_children) total_time += (pp->cutime + pp->cstime);  seconds = seconds_since_boot - pp->start_time / Hertz;  if(seconds) pcpu = (total_time * 1000ULL / Hertz) / seconds;  if (pcpu > 999U)    return snprintf(outbuf, COLWID, "%u", pcpu/10U);  return snprintf(outbuf, COLWID, "%u.%u", pcpu/10U, pcpu%10U);}/* this is a "per-mill" format, like %cpu with no decimal point */static int pr_cp(char *restrict const outbuf, const proc_t *restrict const pp){  unsigned long long total_time;   /* jiffies used by this process */  unsigned pcpu = 0;               /* scaled %cpu, 999 means 99.9% */  unsigned long long seconds;      /* seconds of process life */  total_time = pp->utime + pp->stime;  if(include_dead_children) total_time += (pp->cutime + pp->cstime);  seconds = seconds_since_boot - pp->start_time / Hertz ;  if(seconds) pcpu = (total_time * 1000ULL / Hertz) / seconds;  if (pcpu > 999U) pcpu = 999U;  return snprintf(outbuf, COLWID, "%3u", pcpu);}static int pr_pgid(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%u", pp->pgrp);}static int pr_pid(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%u", pp->tgid);}static int pr_ppid(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%u", pp->ppid);}/* cumulative CPU time, [dd-]hh:mm:ss format (not same as "etime") */static int pr_time(char *restrict const outbuf, const proc_t *restrict const pp){  unsigned long t;  unsigned dd,hh,mm,ss;  int c;  t = (pp->utime + pp->stime) / Hertz;  ss = t%60;  t /= 60;  mm = t%60;  t /= 60;  hh = t%24;  t /= 24;  dd = t;  c  =( dd ? snprintf(outbuf, COLWID, "%u-", dd) : 0              );  c +=( snprintf(outbuf+c, COLWID, "%02u:%02u:%02u", hh, mm, ss)    );  return c;}/* HP-UX puts this (I forget, vsz or vsize?) in kB and uses "sz" for pages. * Unix98 requires "vsz" to be kB. * Tru64 does both vsize and vsz like "1.23M" * * Our pp->vm_size is kB and our pp->vsize is pages. * * TODO: add flag for "1.23M" behavior, on this and other columns. */static int pr_vsz(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%lu", pp->vm_size);}//////////////////////////////////////////////////////////////////////////////////////// "PRI" is created by "opri", or by "pri" when -c is used.//// Unix98 only specifies that a high "PRI" is low priority.// Sun and SCO add the -c behavior. Sun defines "pri" and "opri".// Linux may use "priority" for historical purposes.//// According to the kernel's fs/proc/array.c and kernel/sched.c source,// the kernel reports it in /proc via this://        p->prio - MAX_RT_PRIO// such that "RT tasks are offset by -200. Normal tasks are centered// around 0, value goes from -16 to +15" but who knows if that is// before or after the conversion...//// <linux/sched.h> says:// MAX_RT_PRIO is currently 100.       (so we see 0 in /proc)// RT tasks have a p->prio of 0 to 99. (so we see -100 to -1)// non-RT tasks are from 100 to 139.   (so we see 0 to 39)// Lower values have higher priority, as in the UNIX standard.//// In any case, pp->priority+100 should get us back to what the kernel// has for p->prio.//// Test results with the "yes" program on a 2.6.x kernel://// # ps -C19,_20 -o pri,opri,intpri,priority,ni,pcpu,pid,comm// PRI PRI PRI PRI  NI %CPU  PID COMMAND//   0  99  99  39  19 10.6 8686 19//  34  65  65   5 -20 94.7 8687 _20//// Grrr. So the UNIX standard "PRI" must NOT be from "pri".// Either of the others will do. We use "opri" for this.// (and use "pri" when the "-c" option is used)// Probably we should have Linux-specific "pri_for_l" and "pri_for_lc"//// sched_get_priority_min.2 says the Linux static priority is// 1..99 for RT and 0 for other... maybe 100 is kernel-only?//// A nice range would be -99..0 for RT and 1..40 for normal,// which is pp->priority+1. (3-digit max, positive is normal,// negative or 0 is RT, and meets the standard for PRI)//// legal as UNIX "PRI"// "priority"         (was -20..20, now -100..39)static int pr_priority(char *restrict const outbuf, const proc_t *restrict const pp){    /* -20..20 */    return snprintf(outbuf, COLWID, "%ld", pp->priority);}// legal as UNIX "PRI"// "intpri" and "opri" (was 39..79, now  -40..99)static int pr_opri(char *restrict const outbuf, const proc_t *restrict const pp){        /* 39..79 */    return snprintf(outbuf, COLWID, "%ld", 60 + pp->priority);}// legal as UNIX "PRI"// "pri_foo"   --  match up w/ nice values of sleeping processes (-120..19)static int pr_pri_foo(char *restrict const outbuf, const proc_t *restrict const pp){    return snprintf(outbuf, COLWID, "%ld", pp->priority - 20);}// legal as UNIX "PRI"// "pri_bar"   --  makes RT pri show as negative       (-99..40)static int pr_pri_bar(char *restrict const outbuf, const proc_t *restrict const pp){    return snprintf(outbuf, COLWID, "%ld", pp->priority + 1);}// legal as UNIX "PRI"// "pri_baz"   --  the kernel's ->prio value, as of Linux 2.6.8     (1..140)static int pr_pri_baz(char *restrict const outbuf, const proc_t *restrict const pp){    return snprintf(outbuf, COLWID, "%ld", pp->priority + 100);}// not legal as UNIX "PRI"// "pri"               (was 20..60, now    0..139)static int pr_pri(char *restrict const outbuf, const proc_t *restrict const pp){         /* 20..60 */    return snprintf(outbuf, COLWID, "%ld", 39 - pp->priority);}// not legal as UNIX "PRI"// "pri_api"   --  match up w/ RT API    (-40..99)static int pr_pri_api(char *restrict const outbuf, const proc_t *restrict const pp){    return snprintf(outbuf, COLWID, "%ld", -1 - pp->priority);}static int pr_nice(char *restrict const outbuf, const proc_t *restrict const pp){  if(pp->sched!=0 && pp->sched!=-1) return snprintf(outbuf, COLWID, "-");  return snprintf(outbuf, COLWID, "%ld", pp->nice);}// HP-UX   "cls": RT RR RR2 ???? HPUX FIFO KERN// Solaris "class": SYS TS FX IA RT FSS (FIFO is RR w/ Inf quant)//                  FIFO+RR share RT; FIFO has Inf quant//                  IA=interactive; FX=fixed; TS=timeshare; SYS=system//                  FSS=fairshare; INTS=interrupts// Tru64   "policy": FF RR TS// IRIX    "class": RT TS B BC WL GN//                  RT=real-time; TS=time-share; B=batch; BC=batch-critical//                  WL=weightless; GN=gang-scheduled//                  see miser(1) for this; PRI has some letter codes toostatic int pr_class(char *restrict const outbuf, const proc_t *restrict const pp){  switch(pp->sched){  case -1: return snprintf(outbuf, COLWID, "-");   // not reported  case  0: return snprintf(outbuf, COLWID, "TS");  // SCHED_OTHER  case  1: return snprintf(outbuf, COLWID, "FF");  // SCHED_FIFO

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -