21

我在 osx 上使用 htop,我似乎无法找出“S”状态列中的“C”状态对进程状态意味着什么。

htop中的C进程状态是什么意思?

4

3 回答 3

66

以下是 s、stat 和 state 输出说明符(标题“STAT”或“S”)将显示以描述进程状态的不同值:

  • D不间断睡眠(通常是 IO)
  • R正在运行或可运行(在运行队列上)
  • S可中断睡眠(等待事件完成)
  • T由于作业控制信号或正在跟踪而停止。
  • W Paging(从 2.6.xx 内核开始无效)
  • X Dead(永远不应该被看到)
  • Z已失效(“僵尸”)进程,已终止但未由其父进程收割。

来源:man ps

我最近遇到了第二个列表:

  • R 运行
  • S 在可中断的等待中休眠
  • D 在不间断的磁盘睡眠中等待
  • Z 僵尸
  • T 停止(在信号上)或(在 Linux 2.6.33 之前)跟踪停止
  • t 跟踪停止(Linux 2.6.33 及更高版本)
  • W 分页(仅在 Linux 2.6.0 之前)
  • X Dead(从 Linux 2.6.0 开始)
  • x 死机(仅限 Linux 2.6.33 至 3.13)
  • K Wakekill(仅限 Linux 2.6.33 至 3.13)
  • W 唤醒(仅限 Linux 2.6.33 到 3.13)
  • P Parked(仅限 Linux 3.9 到 3.13)

http://man7.org/linux/man-pages/man5/proc.5.html在“/proc/[pid]/stat”部分:

于 2013-08-27T15:53:09.207 回答
37

htop 作者在这里。我不知道 htop 代码库中有这样的状态代码。请记住,htop 仅针对 Linux 编写,因此不支持 macOS/OSX。当我听说有人在 OSX 上运行它时,他们经常使用过时的、不受支持的 fork(最新版本的 htop 是 2.0.1,包括 macOS 支持)。

于 2013-08-27T18:19:46.747 回答
20

我最近也有同样的问题。我们可以尝试在htop 源中查找它:

process->state =
    ProcessList_decodeState( p->p_stat == SZOMB ? SZOMB : ki->state );

static int
ProcessList_decodeState( int st ) {
  switch ( st ) {
  case SIDL:
    return 'C';
  case SRUN:
    return 'R';
  case SSLEEP:
    return 'S';
  case SSTOP:
    return 'T';
  case SZOMB:
    return 'Z';
  default:
    return '?';
  }
}

所以我们去 /usr/include/sys/proc.h 中的进程状态的 Unix 定义:

/* Status values. */
#define SIDL    1       /* Process being created by fork. */
#define SRUN    2       /* Currently runnable. */
#define SSLEEP  3       /* Sleeping on an address. */
#define SSTOP   4       /* Process debugging or suspension. */
#define SZOMB   5       /* Awaiting collection by parent. */

因此,“C”状态意味着“进程正在由 fork 创建”。它是什么?根据旧的 unix 来源,这是一种瞬态,当派生进程时没有足够的内存并且需要换出父进程时会发生这种情况。

什么??

返回 htop 源。我们从哪里得到ki->state

// For all threads in process:
error = thread_info( ki->thread_list[j], THREAD_BASIC_INFO,
                     ( thread_info_t ) & ki->thval[j].tb,
                     &thread_info_count );

tstate = ProcessList_machStateOrder( ki->thval[j].tb.run_state,
                                     ki->thval[j].tb.sleep_time );
if ( tstate < ki->state )
  ki->state = tstate;

// Below...
static int
ProcessList_machStateOrder( int s, long sleep_time ) {
  switch ( s ) {
  case TH_STATE_RUNNING:
    return 1;
  case TH_STATE_UNINTERRUPTIBLE:
    return 2;
  case TH_STATE_WAITING:
    return ( sleep_time > 20 ) ? 4 : 3;
  case TH_STATE_STOPPED:
    return 5;
  case TH_STATE_HALTED:
    return 6;
  default:
    return 7;
  }
}

// In mach/thread_info.h: 
#define TH_STATE_RUNNING  1 /* thread is running normally */
#define TH_STATE_STOPPED  2 /* thread is stopped */
#define TH_STATE_WAITING  3 /* thread is waiting normally */
#define TH_STATE_UNINTERRUPTIBLE 4  /* thread is in an uninterruptible wait */
#define TH_STATE_HALTED   5 /* thread is halted at a clean point */

我们有以下(混乱的)映射:

Thread state             | Mapped to | htop state| 'top' state | 'ps' state
---------------------------------------------------------------------------- 
TH_STATE_RUNNING         | SIDL(1)   |       'C' | running     | 'R'
TH_STATE_UNINTERRUPTIBLE | SRUN(2)   |       'R' | stuck       | 'U'
TH_STATE_WAITING (short) | SSLEEP(3) |       'S' | sleeping    | 'S'
TH_STATE_WAITING (long)  | SSTOP(4)  |       'T' | idle        | 'I'
TH_STATE_STOPPED         | SZOMB(5)  |       'Z' | stopped     | 'T'

所以,真正的答案是:“C”表示进程当前正在运行。

这是怎么发生的?似乎处理是从源代码ki->state借来的,并且没有针对 Unix 进程代码进行调整。ps

更新:此错误已修复。耶开源!

于 2015-04-11T07:53:59.820 回答