DEV Community

Cover image for Why the heck the _state variable in task_struct structure is 4 bytes
Boussebha Wassim
Boussebha Wassim

Posted on

1

Why the heck the _state variable in task_struct structure is 4 bytes

According to Robert Love the Author of the book Linux Kernel developement & Linux kernel source :

In the Linux kernel , specifically in the PCB ( process control block ) which a structure that represents the process in the kernel .

The process state is represented by a combination of six state flags, which are:

TASK_RUNNING: The process is currently running or ready to run.

TASK_INTERRUPTIBLE: The process is waiting for a specific event to occur and can be interrupted by a signal.

TASK_UNINTERRUPTIBLE: The process is waiting for a specific event to occur and cannot be interrupted by a signal.

__TASK_STOPPED: The process has been stopped (e.g. by a SIGSTOP signal) and can be resumed later.

__TASK_TRACED: The process is being traced by another process (e.g. a debugger).

TASK_DEAD: The process has terminated and is waiting to be reaped by its parent process.

Each of these flags can be set or cleared to represent the different states that a process can be in. For example, a process that is both TASK_INTERRUPTIBLE and TASK_UNINTERRUPTIBLE might be waiting for a disk I/O operation to complete, and can be interrupted by a signal but cannot be killed until the I/O operation is finished.

In other words, a process can have multiple states at once, depending on which combination of flags is set.

This state is saved in an *UNSIGNED INTEGER *, that means 32 bits , that means 2³² possible values , but we need only 2⁶ , so 64 possible states as a maximum .

Image description

the reason is the following :

There are some predefined functions that manages the process's state such as READ_ONCE/WRITE_ONCE that obliges this variable to be an unsigned INT , it's used to be a volatile Long ; here are the commits :

https://github.com/torvalds/linux/commit/2f064a59a11ff9bc22e52e9678bc601404c7cb34#diff-f8d8a1568ae83bbff6f40f9c70559a4f7dbf426a397131ba9d4fbfb947ea5222R669

Thank you for reading <3 .

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay