Linux Process Architecture

2026-11-18 · virtualisation

notes / Operating Systems / 01_vir

OS

Linux Process Architecture

task_struct

Some important fields

struct task_struct {
    unsigned int            __state;

    void                *stack;

    int             prio;
    int             static_prio;
    int             normal_prio;
    unsigned int            rt_priority;

    struct sched_entity     se;
    struct sched_rt_entity      rt;
    struct sched_dl_entity      dl;
    struct sched_dl_entity      *dl_server;
    const struct sched_class    *sched_class;

    struct sched_info       sched_info;

    struct list_head        tasks;

    struct mm_struct        *mm;

    int             exit_code;

    pid_t               pid;  // note: thread id
    pid_t               tgid; // note: main thread id

    /*
     * Pointers to the (original) parent process, youngest child, younger sibling,
     * older sibling, respectively.  (p->father can be replaced with
     * p->real_parent->pid)
     */
    /* Real parent process: */
    struct task_struct __rcu    *real_parent;

    /* Recipient of SIGCHLD, wait4() reports: */
    struct task_struct __rcu    *parent;

    /*
     * Children/sibling form the list of natural children:
     */
    struct list_head        children;
    struct list_head        sibling;
    struct task_struct      *group_leader;

    /* PID/PID hash table linkage. */
    struct pid          *thread_pid;

    /* Signal handlers: */
    struct signal_struct        *signal;
    struct sighand_struct __rcu     *sighand;
    sigset_t            blocked;
    struct sigpending       pending;

} __attribute__ ((aligned (64)));

task_struct structures are allocated by the slab allocator at init and those objects are reused frequently, reducing allocations on the hot path.

static inline struct task_struct *alloc_task_struct_node(int node)
{
    return kmem_cache_alloc_node(
      task_struct_cachep,  // @cachep: cache to allocate from
      GFP_KERNEL,          // @flags: allocate normal kernel ram
      node                 // @nodeid: node number of target node
    );
}
❯ sudo cat /proc/slabinfo
slabinfo - version: 2.1
# name     <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <limit> <batchcount> <sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail>
ext4_inode_cache   36384  47190   1072   30    8 : tunables    0    0    0 : slabdata   1573   1573      0
task_struct         1600   1660   7104    4    8 : tunables    0    0    0 : slabdata    415    415      0
pid                 3239   3612    192   42    2 : tunables    0    0    0 : slabdata     86     86      0
radix_tree_node    33572  48748    584   28    4 : tunables    0    0    0 : slabdata   1741   1741      0
kmem_cache           222    224    256   32    2 : tunables    0    0    0 : slabdata      7      7      0

Question

number of task_structs is only 1600 but there are 33572 nodes in the radix tree?

On my machine task_struct is about 7KB in size

Process States

/* Used in tsk->__state: */
#define TASK_RUNNING            0x00000000
#define TASK_INTERRUPTIBLE      0x00000001
#define TASK_UNINTERRUPTIBLE        0x00000002
#define __TASK_STOPPED          0x00000004
#define __TASK_TRACED           0x00000008
/* Used in tsk->exit_state: */
#define EXIT_DEAD           0x00000010
#define EXIT_ZOMBIE         0x00000020
#define EXIT_TRACE          (EXIT_ZOMBIE | EXIT_DEAD)
/* Used in tsk->__state again: */
#define TASK_PARKED         0x00000040
#define TASK_DEAD           0x00000080
#define TASK_WAKEKILL           0x00000100
#define TASK_WAKING         0x00000200
#define TASK_NOLOAD         0x00000400
#define TASK_NEW            0x00000800
#define TASK_RTLOCK_WAIT        0x00001000
#define TASK_FREEZABLE          0x00002000
#define __TASK_FREEZABLE_UNSAFE        (0x00004000 * IS_ENABLED(CONFIG_LOCKDEP))
#define TASK_FROZEN         0x00008000
#define TASK_STATE_MAX          0x00010000

struct pid

Each "thread" gets its own task_struct, therefore the

pid_t pid;

is actually the thread id, and not actually the process pid

struct pid {
    refcount_t count;
    unsigned int level;
    spinlock_t lock;
    struct {
        u64 ino;
        struct rhash_head pidfs_hash;
        struct dentry *stashed;
        struct pidfs_attr *attr;
    };
    /* lists of tasks that use this pid */
    struct hlist_head tasks[PIDTYPE_MAX];
    struct hlist_head inodes;
    /* wait queue for pidfd notifications */
    wait_queue_head_t wait_pidfd;
    struct rcu_head rcu;
    struct upid numbers[];
} thread_pid;
enum pid_type {
    PIDTYPE_PID,  // 0
    PIDTYPE_TGID, // 1
    PIDTYPE_PGID, // 2
    PIDTYPE_SID,  // 3
    PIDTYPE_MAX,  // 4
};

PID Allocation

Process List

Creating a process

Threads

PID Namespaces

Syscalls