Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is thread still running when the process in "Disk Sleep"?

Tags:

c

linux

sleep

disk

When a process go into the special kind of sleep, known as "D" or "Disk Sleep" in Linux, is its child thread still running normally ? So that the thread could tell me the process is in "Disk Sleep" state.

about the "D" state

BTW: Sorry for my poor English and thank you all.

like image 214
shlinux Avatar asked Aug 05 '13 08:08

shlinux


1 Answers

In Linux threads are defined as "tasks".

Each task is an individual execution unit within a process. They all have their individual task id tid - related to process ids (pids).

Each process comes with one main task when it starts and that "main" task identifies the process and indeed the process id pid is the task id tid of the main task.

The process state in terms of execution resembles that of the main task including the status R, D, S...

So if your process is marked as D (Disk sleep) it just means that the main task is in disk sleep. All other tasks (threads) could be doing anything else.

Check /proc/[pid]/task/[tid]/stat for individual task states.

Also ps -eLf to display ps entries for tasks.

Try this code:

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>

void * thread_sleep() {
    long long int i = -1;
    while (i--);
    exit(0);
    return NULL;
}

int main() {
    pthread_t thread;
    pthread_create(&thread, NULL, thread_sleep, NULL);
    getchar();
    return 0;
}

Run it and do

cat /proc/$PID/stat
cat /proc/$PID/task/*/stat

You will notice that the process has status S (waiting for terminal input) as does the first task (same tid as the process pid) while the other thread is R. If you set i to something smaller it will actually finish at some point.

like image 110
Sergey L. Avatar answered Sep 19 '22 14:09

Sergey L.