Why is the process state in task_struct stored as type 'long'?

Dziugas

In the Linux kernel tree, the file /include/linux/sched.h contains task_struct which, among other data, defines a variable volatile long state.

According to this page, the number stored in state represents one of five states:

#define TASK_RUNNING            0
#define TASK_INTERRUPTIBLE      1
#define TASK_UNINTERRUPTIBLE    2
#define TASK_ZOMBIE             4
#define TASK_STOPPED            8

My question is as follows:

Why is such a large datatype used to store the state? Wouldn't a single byte be perfectly sufficient to store the states above? Surely, long wasn't chosen in anticipation of thousands/millions of possible states?

I'm aware that the definition of long will vary across the different architectures, but I thought char would have sufficed.

employee of the month

You can easily see these values are power-of-2. This is the standard way to handle /flags/. You can logically OR ('|') them to create cobined states. And then you can easily test them with AND ('&').

long on a 32-bit platform will only be 32-bit.

So, with long on a 32-bit platform you only have 32 distinct flags, as opposed to millions.

Finally, I strongly suggest inspecting actual sources and preferably from this decade, see:

#define TASK_RUNNING            0
#define TASK_INTERRUPTIBLE      1
#define TASK_UNINTERRUPTIBLE    2
#define __TASK_STOPPED          4
#define __TASK_TRACED           8
/* in tsk->exit_state */
#define EXIT_DEAD               16
#define EXIT_ZOMBIE             32
#define EXIT_TRACE              (EXIT_ZOMBIE | EXIT_DEAD)
/* in tsk->state again */
#define TASK_DEAD               64
#define TASK_WAKEKILL           128
#define TASK_WAKING             256
#define TASK_PARKED             512
#define TASK_NOLOAD             1024
#define TASK_STATE_MAX          2048

And flag usage below, e.g.

#define TASK_KILLABLE           (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)

So, the question is why would this be long instead of an int. That I don't know, but it is unlikely there is any particularly good reason.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is the internal data of BitSet in java stored as long[] instead of int[] in Java?

why sibling list is used to get the task_struct while fetching the children of a process

Linux Kernel: Threading vs Process - task_struct vs thread_info

Why is Linux syscall return type "long"?

Full process name from task_struct

Why is bool type in Go 16 bytes long?

Why ObjectMapper changes Date type into Long

use printf("%s",..) to print a struct, the struct's first variable type is 'char *', why can get a right string stored in 'char *'?

Getting a user ID and a process group ID from a task_struct and a pid_namespace

Why does MSVC pick a long long as the type for -2147483648?

Why does my MaybeT (State <type>) () ignore state changes?

Why celery worker puts a task in PENDING state for so long?

Why thread_info should be the first element in task_struct?

Why does it take so long time to process the async await

In Swift why would a Struct/Class with stored property of type Protocol won't conform to Codable by default?

Why won't auto assign values of type long long?

Why must a process in TASK_INTERRUPTIBLE state that is preempted stay in run queue?

why Android Studio build process cost too long time

Why are process management files on Linux stored under /var/run?

Why my process became T (Terminated)State ?

Why React Components being stored as variables or as state is bad design practice

Trying to read register values of a process from task_struct

Reference stored in Struct does not live long enough for closure

why JsonElement is struct type?

Why there is an extra name to array stored in state

IndexedDB: How to prevent long int type being stored as string?

Where will this struct be stored and why?

How can I add a custom 'weight' field to a Linux process in task_struct and ensure inheritance during forking?

Why variables are not stored in the process data area in Swift