Linux

在什麼情況下程序會處於狀態 X(死亡)?

  • June 8, 2021

根據fs/proc/array.c:130,以下數組定義了各種程序狀態:

/*
* The task state array is a strange "bitmap" of
* reasons to sleep. Thus "running" is zero, and
* you can test for combinations of others with
* simple bit tests.
*/
static const char * const task_state_array[] = {

   /* states in TASK_REPORT: */
   "R (running)",      /* 0x00 */
   "S (sleeping)",     /* 0x01 */
   "D (disk sleep)",   /* 0x02 */
   "T (stopped)",      /* 0x04 */
   "t (tracing stop)", /* 0x08 */
   "X (dead)",         /* 0x10 */
   "Z (zombie)",       /* 0x20 */
   "P (parked)",       /* 0x40 */

   /* states beyond TASK_REPORT: */
   "I (idle)",         /* 0x80 */
};

根據proc(5)X在核心 2.6.0 中添加了狀態:

X Dead(從 Linux 2.6.0 開始)

x 當機(僅限 Linux 2.6.33 至 3.13)

並且根據ps(1)X不應該被看到:

X 死了(永遠不應該被看到)

查看原始碼的其餘部分,它似乎核心內部使用的。在源文件kernel/sched.core.c:4176中,有一條註釋對其進行了簡要描述:

/*
* A task struct has one reference for the use as "current".
* If a task dies, then it sets TASK_DEAD in tsk->state and calls
* schedule one last time. The schedule call will never return, and
* the scheduled task must drop that reference.
*
* We must observe prev->state before clearing prev->on_cpu (in
* finish_task), otherwise a concurrent wakeup can get prev
* running on another CPU and we could rave with its RUNNING -> DEAD
* transition, resulting in a double drop.
*/

在某些情況下似乎也*需要它。*在kernel/fork.c:424

static void release_task_stack(struct task_struct *tsk)
{
   if (WARN_ON(tsk->state != TASK_DEAD))
       return;  /* Better to leak the stack than to free prematurely */

   account_kernel_stack(tsk, -1);
   free_thread_stack(tsk);
   tsk->stack = NULL;
#ifdef CONFIG_VMAP_STACK
   tsk->stack_vm_area = NULL;
#endif
}

在我看來,它TASK_DEAD是在程序終止時但在核心最終銷毀之前為程序設置的task_struct,因此除非存在無法清理程序的核心錯誤,否則它永遠不應顯示為程序狀態。還有這些講義強化了這個想法:

TASK_DEAD – 正在清理程序,正在刪除任務

所以對於我真正的問題:

在什麼情況下,程序會被報告ps為處於狀態 X?

“X”表示的任務狀態不是TASK_DEADEXIT_DEAD退出狀態TASK_DEAD本身不是可報告的狀態,雖然EXIT_DEAD是,但它不應該在實踐中可見

EXIT_DEAD的角色類似於您所描述的TASK_DEAD:任務的退出狀態在被刪除EXIT_DEAD之前不久被設置為;參見例如本身task_struct``release_taskde_threadrelease_taskexit_notify

鎖的細節我沒查過,程序狀態的變化讀者可以看到;然而,一個程序似乎不太可能EXIT_DEAD被另一個程序看到狀態。

無論是否可以看到,一個程序一旦完全退出並且task_struct即將被刪除,就處於狀態“X”。

引用自:https://unix.stackexchange.com/questions/653294