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

📄 output.c

📁 Linux下进程监控相关源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
  ss = t%60;  t /= 60;  mm = t%60;  t /= 60;  hh = t%24;  t /= 24;  dd = t;  c +=(      dd      ?  sprintf(outbuf, "%u-", dd)    :  sprintf(outbuf, "   ")   );  c +=(  (dd || hh)  ?  sprintf(outbuf+c, "%02u:", hh)  :  sprintf(outbuf+c, "   ")   );  c +=(                 sprintf(outbuf+c, "%02u:%02u", mm, ss)             );  return c;}static int pr_nice(void){  return sprintf(outbuf, "%ld", pp->nice);}/* "Processor utilisation for scheduling."  --- we use %cpu w/o fraction */static int pr_c(void){  return sprintf(outbuf, "%2u", (unsigned)(pp->pcpu / 10));}static int pr_pcpu(void){  return sprintf(outbuf, "%2u.%u",  		(unsigned)(pp->pcpu / 10), (unsigned)(pp->pcpu % 10));}static int pr_pgid(void){  return sprintf(outbuf, "%u", pp->pgrp);}static int pr_pid(void){  return sprintf(outbuf, "%u", pp->pid);}static int pr_ppid(void){  return sprintf(outbuf, "%u", pp->ppid);}/* cumulative CPU time, [dd-]hh:mm:ss format (not same as "etime") */static int pr_time(void){  int t, dd,hh,mm,ss;  int c = 0;  t = pp->utime + pp->stime;  t /= Hertz;  ss = t%60;  t /= 60;  mm = t%60;  t /= 60;  hh = t%24;  t /= 24;  dd = t;  c +=( dd ? sprintf(outbuf, "%d-", dd) : 0              );  c +=( sprintf(outbuf+c, "%02d:%02d:%02d", hh, mm, ss)    );  return c;}/* HP-UX puts this in kB and uses "sz" for pages */static int pr_vsz(void){  return sprintf(outbuf, "%lu", pp->vm_size);}/* * internal terms:  ruid  euid  suid  fuid * kernel vars:      uid  euid  suid fsuid * command args:    ruid   uid svuid   n/a */static int pr_ruser(void){  if(user_is_number || (strlen(pp->ruser)>max_rightward))    return sprintf(outbuf, "%d", pp->ruid);  return sprintf(outbuf, "%s", pp->ruser);}static int pr_egroup(void){  if(strlen(pp->egroup)>max_rightward) return sprintf(outbuf, "%d", pp->egid);  return sprintf(outbuf, "%s", pp->egroup);}static int pr_rgroup(void){  if(strlen(pp->rgroup)>max_rightward) return sprintf(outbuf, "%d", pp->rgid);  return sprintf(outbuf, "%s", pp->rgroup);}static int pr_euser(void){  if(user_is_number || (strlen(pp->euser)>max_rightward)) return sprintf(outbuf, "%d", pp->euid);  return sprintf(outbuf, "%s", pp->euser);}/********* maybe standard (Unix98 only defines the header) **********//* * "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. */static int pr_priority(void){    /* -20..20 */    return sprintf(outbuf, "%ld", pp->priority);}static int pr_pri(void){         /* 20..60 */    return sprintf(outbuf, "%ld", 39 - pp->priority);}static int pr_opri(void){        /* 39..79 */    return sprintf(outbuf, "%ld", 60 + pp->priority);}static int pr_wchan(void){/* * Unix98 says "blank if running" and also "no blanks"! :-( * Unix98 also says to use '-' if something is meaningless. * Digital uses both '*' and '-', with undocumented differences. * (the '*' for -1 (rare) and the '-' for 0) * Sun claims to use a blank AND use '-', in the same man page. * Perhaps "blank" should mean '-'. * * AIX uses '-' for running processes, the location when there is * only one thread waiting in the kernel, and '*' when there is * more than one thread waiting in the kernel. */    if(!(pp->wchan & 0xffffff)) return sprintf(outbuf, "%s", "-");    if(wchan_is_number) return sprintf(outbuf, "%lx", pp->wchan & 0xffffff);    return sprintf(outbuf, "%s", wchan(pp->wchan));}/* Terrible trunctuation, like BSD crap uses: I999 J999 K999 *//* FIXME: disambiguate /dev/tty69 and /dev/pts/69. */static int pr_tty4(void){/* sprintf(outbuf, "%02x:%02x", pp->tty>>8, pp->tty&0xff); */  return dev_to_tty(outbuf, 4, pp->tty, pp->pid, ABBREV_DEV|ABBREV_TTY|ABBREV_PTS);}/* Unix98: format is unspecified, but must match that used by who(1). */static int pr_tty8(void){/* sprintf(outbuf, "%02x:%02x", pp->tty>>8, pp->tty&0xff); */  return dev_to_tty(outbuf, PAGE_SIZE-1, pp->tty, pp->pid, ABBREV_DEV);}#if 0/* This BSD state display may contain spaces, which is illegal. */static int pr_oldstate(void){    return sprintf(outbuf, "%s", status(pp));}#endif/* This state display is Unix98 compliant and has lots of info like BSD. */static int pr_stat(void){    int end = 0;    outbuf[end++] = pp->state;    if(pp->rss == 0 && pp->state != 'Z')    outbuf[end++] = 'W';    if(pp->nice < 0)                        outbuf[end++] = '<';    if(pp->nice > 0)                        outbuf[end++] = 'N';    if(pp->vm_lock)                         outbuf[end++] = 'L';    outbuf[end] = '\0';    return end;}/* This minimal state display is Unix98 compliant, like SCO and SunOS 5 */static int pr_s(void){    outbuf[0] = pp->state;    outbuf[1] = '\0';    return 1;}static int pr_flag(void){    /* Unix98 requires octal -- good thing Linux hex looks octal! */    return sprintf(outbuf, "%03lx", (pp->flags)&0x777);}static int pr_euid(void){  return sprintf(outbuf, "%d", pp->euid);}/*********** non-standard ***********//*** BSDsess	session pointer(SCO has:Process session leader ID as a decimal value. (SESSION))jobc	job control countcpu	short-term cpu usage factor (for scheduling)sl	sleep time (in seconds; 127 = infinity)re	core residency time (in seconds; 127 = infinity)pagein	pageins (same as majflt)lim	soft memory limittsiz	text size (in Kbytes)***/static int pr_stackp(void){    return sprintf(outbuf, "%08lx", pp->start_stack);}static int pr_esp(void){    return sprintf(outbuf, "%08lx", pp->kstk_esp);}static int pr_eip(void){    return sprintf(outbuf, "%08lx", pp->kstk_eip);}/* This function helps print old-style time formats */static int old_time_helper(char *dst, unsigned long t, unsigned long rel) {    if(!t)      return sprintf(dst, "    -");    if((long)t == -1) return sprintf(dst, "   xx");    if((long)(t-=rel) < 0)  t=0;    if(t>9999)  return sprintf(dst, "%5lu", t/100);    else        return sprintf(dst, "%2lu.%02lu", t/100, t%100);}static int pr_bsdtime(void){    unsigned long t;    t = pp->utime + pp->stime;    if(include_dead_children) t += (pp->cutime + pp->cstime);    t /= Hertz;    return sprintf(outbuf, "%3ld:%02d", t/60, (int)(t%60));}static int pr_bsdstart(void){  time_t start;  time_t seconds_ago;  start = time_of_boot + pp->start_time/Hertz;  seconds_ago = seconds_since_1970 - start;  if(seconds_ago < 0) seconds_ago=0;  if(seconds_ago > 3600*24)  strcpy(outbuf, ctime(&start)+4);  else                       strcpy(outbuf, ctime(&start)+10);  outbuf[6] = '\0';  return 6;}static int pr_timeout(void){    return old_time_helper(outbuf, pp->timeout, seconds_since_boot*Hertz);}static int pr_alarm(void){    return old_time_helper(outbuf, pp->it_real_value, 0);}/* HP-UX puts this in pages and uses "vsz" for kB */static int pr_sz(void){  return sprintf(outbuf, "%lu", (pp->vm_size)/(PAGE_SIZE/1024));}/* * FIXME: trs,drs,tsiz,dsiz,m_trs,m_drs,vm_exe,vm_data,trss * I suspect some/all of those are broken. They seem to have been * inherited by Linux and AIX from early BSD systems. FreeBSD only * retains tsiz. The prefixed versions come from Debian. * Sun and Digital have none of this crap. The code here comes * from an old Linux ps, and might not be correct for ELF executables. * * AIX            TRS    size of resident-set (real memory) of text * AIX            TSIZ   size of text (shared-program) image * FreeBSD        tsiz   text size (in Kbytes) * 4.3BSD NET/2   trss   text resident set size (in Kbytes) * 4.3BSD NET/2   tsiz   text size (in Kbytes) *//* kB data size. See drs, tsiz & trs. */static int pr_dsiz(void){    long dsiz = 0;    if(pp->vsize) dsiz += (pp->vsize - pp->end_code + pp->start_code) >> 10;    return sprintf(outbuf, "%ld", dsiz);}/* kB text (code) size. See trs, dsiz & drs. */static int pr_tsiz(void){    long tsiz = 0;    if(pp->vsize) tsiz += (pp->end_code - pp->start_code) >> 10;    return sprintf(outbuf, "%ld", tsiz);}/* kB _resident_ data size. See dsiz, tsiz & trs. */static int pr_drs(void){    long drs = 0;    if(pp->vsize) drs += (pp->vsize - pp->end_code + pp->start_code) >> 10;    return sprintf(outbuf, "%ld", drs);}/* kB text _resident_ (code) size. See tsiz, dsiz & drs. */static int pr_trs(void){    long trs = 0;    if(pp->vsize) trs += (pp->end_code - pp->start_code) >> 10;    return sprintf(outbuf, "%ld", trs);}static int pr_minflt(void){    long flt = pp->min_flt;    if(include_dead_children) flt += pp->cmin_flt;    return sprintf(outbuf, "%ld", flt);}static int pr_majflt(void){    long flt = pp->maj_flt;    if(include_dead_children) flt += pp->cmaj_flt;    return sprintf(outbuf, "%ld", flt);}static int pr_lim(void){    if(pp->rss_rlim == RLIM_INFINITY) return sprintf(outbuf, "%s", "xx");    return sprintf(outbuf, "%5ld", pp->rss_rlim >> 10);}static int pr_nwchan(void){    if(!(pp->wchan & 0xffffff)) return sprintf(outbuf, "-");    return sprintf(outbuf, "%lx", pp->wchan & 0xffffff);}static int pr_rss(void){  return sprintf(outbuf, "%lu", pp->rss << (page_shift - 10));}static int pr_pmem(void){  unsigned long pmem = 0;  pmem = pp->rss * 1000 / (bytes_main_memory >> page_shift);  return sprintf(outbuf, "%2u.%u", (unsigned)(pmem/10), (unsigned)(pmem%10));}static int pr_lstart(void){  time_t t;  t = (           ((unsigned long)time_of_boot)         + ((unsigned long)pp->start_time)         / Hertz  );  return sprintf(outbuf, "%24.24s", ctime(&t));}/* Unix98 specifies a STIME header for a column that shows the start * time of the process, but does not specify a format or format specifier. * From the general Unix98 rules, we know there must not be any spaces. * Most systems violate that rule, though the Solaris documentation * claims to print the column without spaces. (NOT!) * * So this isn't broken, but could be renamed to u98_std_stime, * as long as it still shows as STIME when using the -f option. */static int pr_stime(void){  struct tm *proc_time;  struct tm *our_time;  time_t t;  char *fmt;  int tm_year;  int tm_yday;  our_time = localtime(&seconds_since_1970);   /* not reentrant */  tm_year = our_time->tm_year;  tm_yday = our_time->tm_yday;  t = (time_t)(            ((unsigned long)time_of_boot)          + ((unsigned long)pp->start_time)          / Hertz  );  proc_time = localtime(&t); /* not reentrant, this corrupts our_time */  fmt = "%H:%M";                                   /* 03:02 23:59 */  if(tm_yday != proc_time->tm_yday) fmt = "%b%d";  /* Jun06 Aug27 */  if(tm_year != proc_time->tm_year) fmt = "%Y";    /* 1991 2001 */  return strftime(outbuf, 42, fmt, proc_time);}static int pr_start(void){  time_t t;  char *str;  t = (           ((unsigned long)time_of_boot)         + ((unsigned long)pp->start_time)         / Hertz  );  str = ctime(&t);  if(str[8]==' ')  str[8]='0';  if(str[11]==' ') str[11]='0';  if((unsigned long)t+60*60*24 > seconds_since_1970)    return sprintf(outbuf, "%8.8s", str+11);  return sprintf(outbuf, "  %6.6s", str+4);}#ifdef SIGNAL_STRINGstatic int help_pr_sig(const char *sig){

⌨️ 快捷键说明

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