cmd_ps

ps命令查看某进程的调度策略

$ ps -o cls <...>

## example following:
$ ps -eo pid,comm,cls
    PID COMMAND         CLS
      1 systemd          TS
      2 kthreadd         TS

查看ps命令的源码包

ps命令如何获得某进程的调度策略

当ps命令指定-o cls参数时,会执行pr_class()函数,如下:

$ vim procps-3.3.16/ps/output.c 
static 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 SCHED_NORMAL
  case  1: return snprintf(outbuf, COLWID, "FF");  // SCHED_FIFO
  case  2: return snprintf(outbuf, COLWID, "RR");  // SCHED_RR
  case  3: return snprintf(outbuf, COLWID, "B");   // SCHED_BATCH
  case  4: return snprintf(outbuf, COLWID, "ISO"); // reserved for SCHED_ISO (Con Kolivas)
  case  5: return snprintf(outbuf, COLWID, "IDL"); // SCHED_IDLE
  case  6: return snprintf(outbuf, COLWID, "DLN"); // SCHED_DEADLINE
  case  7: return snprintf(outbuf, COLWID, "#7");  //
  case  8: return snprintf(outbuf, COLWID, "#8");  //
  case  9: return snprintf(outbuf, COLWID, "#9");  //
  default: return snprintf(outbuf, COLWID, "?");   // unknown value
  }
}

pp->sched在哪里赋值?

通过查找 ->sched 可知,在stat2proc()函数中,如下:

由此可知,ps命令是通过/proc/[pid]/stat获得进程的调度策略,即 通过procfs获得进程的调度策略

Last updated

Was this helpful?