<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vadher Nikunj</title>
    <description>The latest articles on DEV Community by Vadher Nikunj (@vadher_nikunj_47e8c86d866).</description>
    <link>https://dev.to/vadher_nikunj_47e8c86d866</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4200579%2F57b1a3ea-6f11-4d40-be59-ddbdac0d0a9b.jpg</url>
      <title>DEV Community: Vadher Nikunj</title>
      <link>https://dev.to/vadher_nikunj_47e8c86d866</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vadher_nikunj_47e8c86d866"/>
    <language>en</language>
    <item>
      <title>typedef struct: Giving Your Struct a Short Name (Linux Kernel Examples)</title>
      <dc:creator>Vadher Nikunj</dc:creator>
      <pubDate>Thu, 19 Nov 2026 12:59:02 +0000</pubDate>
      <link>https://dev.to/vadher_nikunj_47e8c86d866/typedef-struct-giving-your-struct-a-short-name-linux-kernel-examples-21gk</link>
      <guid>https://dev.to/vadher_nikunj_47e8c86d866/typedef-struct-giving-your-struct-a-short-name-linux-kernel-examples-21gk</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was first published on &lt;a href="https://www.jupiuslearning.com/blog/typedef-struct-short-name-linux-kernel/" rel="noopener noreferrer"&gt;Jupius Learning&lt;/a&gt;, where you can read it free with diagrams and practice exercises.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;typedef&lt;/code&gt; gives an existing type a second name. When you write &lt;code&gt;typedef struct student student_t&lt;/code&gt;, you can make a variable with &lt;code&gt;student_t s1&lt;/code&gt; instead of &lt;code&gt;struct student s1&lt;/code&gt;. Nothing about the data changes. The typedef does not create a new kind of object, and it does not set aside any memory. It is only another name for a type you already have.&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://www.jupiuslearning.com/blog/your-first-c-struct-declare-fill-read-linux-kernel/" rel="noopener noreferrer"&gt;last post&lt;/a&gt; we declared a &lt;code&gt;struct student&lt;/code&gt;, filled it and read it with a dot. You probably noticed that writing &lt;code&gt;struct student&lt;/code&gt; every time is a bit long. This post is about that shorter name, and about why the Linux kernel uses typedef for some types and avoids it for most.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does typedef do to a C struct?
&lt;/h2&gt;

&lt;p&gt;Start with the struct from last time, then give it a typedef name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;roll_no&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt;  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="n"&gt;student_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also write both in one go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;roll_no&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt;  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;student_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After either of those, these two lines make the same kind of variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Asha"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;87.5f&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;student_t&lt;/span&gt;      &lt;span class="n"&gt;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Ravi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;91.0f&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;struct student&lt;/code&gt; is still a valid type name. The word &lt;code&gt;student&lt;/code&gt; after &lt;code&gt;struct&lt;/code&gt; is the tag. &lt;code&gt;student_t&lt;/code&gt; is the typedef name. They describe the same layout. &lt;code&gt;s1.marks&lt;/code&gt; and &lt;code&gt;s2.marks&lt;/code&gt; work the same way, with the same dot we used last time.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;_t&lt;/code&gt; at the end is only a naming habit. It is common in C, but it is not required. I will stick to &lt;code&gt;student_t&lt;/code&gt; here because you will see that pattern a lot.&lt;/p&gt;

&lt;h2&gt;
  
  
  typedef with an anonymous struct
&lt;/h2&gt;

&lt;p&gt;You do not have to give the struct a tag at all. If you leave the tag out, the typedef name is the only name the type has.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;roll_no&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt;  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;student_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;student_t s1;&lt;/code&gt; works, and &lt;code&gt;struct student s1;&lt;/code&gt; does not, because there is no tag called &lt;code&gt;student&lt;/code&gt;. The struct is anonymous. This is shorter to type. The cost is that the type has only one name, which matters as soon as you need to mention the type before it is fully defined.&lt;/p&gt;

&lt;p&gt;The Linux kernel uses this shape for &lt;code&gt;sigset_t&lt;/code&gt;, a signal mask. In a modern kernel it looks like this, in simplified form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Simplified from the Linux kernel */&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;sig&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;_NSIG_WORDS&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;sigset_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;_NSIG_WORDS&lt;/code&gt; is a kernel macro for how many &lt;code&gt;unsigned long&lt;/code&gt; values it takes to hold every signal bit. On an older 32-bit kernel you may see &lt;code&gt;sig[2]&lt;/code&gt;, which is the same idea with the number filled in. You treat &lt;code&gt;sigset_t&lt;/code&gt; as its own type, and you usually change it through helper functions rather than writing to the &lt;code&gt;sig&lt;/code&gt; field yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  The forward-declaration case
&lt;/h2&gt;

&lt;p&gt;Sometimes you need to mention a struct type before you have written its fields. A function prototype is a common reason. A field that points to the same type of struct is another. In C you can give the compiler the typedef name early, as long as the struct has a tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="n"&gt;student_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="cm"&gt;/* the typedef comes first */&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;print_student&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;student_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;roll_no&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt;  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;student_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;partner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="cm"&gt;/* a pointer is fine here */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The typedef line does not describe the fields yet. It only says there is a &lt;code&gt;struct student&lt;/code&gt;, and &lt;code&gt;student_t&lt;/code&gt; is another name for it. A pointer to an incomplete type is allowed, so the prototype and the &lt;code&gt;partner&lt;/code&gt; field both compile. You cannot make a &lt;code&gt;student_t&lt;/code&gt; variable until the braces that list the fields have been seen.&lt;/p&gt;

&lt;p&gt;If you try to put the typedef name inside the same typedef that creates it, that does not work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;roll_no&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* student_t *partner; */&lt;/span&gt;    &lt;span class="cm"&gt;/* too early: student_t does not exist yet */&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;partner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="cm"&gt;/* this is fine: the tag already exists */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;student_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tag &lt;code&gt;student&lt;/code&gt; is visible from the opening brace. The typedef name &lt;code&gt;student_t&lt;/code&gt; exists only after the semicolon that finishes the typedef.&lt;/p&gt;

&lt;p&gt;An anonymous typedef cannot be written this way. There is no tag to mention in a forward line, so you cannot say the type exists and fill in the fields later. If you think you will need a forward declaration, keep the tag.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-&amp;gt;&lt;/code&gt; in a function like &lt;code&gt;print_student&lt;/code&gt; is the arrow I mentioned last time. You use it when you have a pointer to a struct. We will spend a later post on pointers. For the rest of this post, the examples in &lt;code&gt;main&lt;/code&gt; still use a plain variable and a dot.&lt;/p&gt;

&lt;h2&gt;
  
  
  A typedef that hides a pointer
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;typedef&lt;/code&gt; can name a pointer type too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;student_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;student_ptr&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;p&lt;/code&gt; is a pointer, but the name does not show that. &lt;code&gt;student_ptr p;&lt;/code&gt; looks a lot like &lt;code&gt;student_t s;&lt;/code&gt;, which is a struct variable, not a pointer. If you later write &lt;code&gt;p.marks&lt;/code&gt;, the compiler will complain, because a pointer needs &lt;code&gt;p-&amp;gt;marks&lt;/code&gt;. That mix-up is easy to miss when you read someone else's code.&lt;/p&gt;

&lt;p&gt;This is the main reason the Linux kernel coding style says not to typedef ordinary structs or pointers. When you see a line like &lt;code&gt;vps_t a;&lt;/code&gt;, you cannot tell if &lt;code&gt;a&lt;/code&gt; is a struct or a pointer. It could even be an integer. When you see &lt;code&gt;struct virtual_container *a;&lt;/code&gt;, you can. I will come back to that rule after we look at the typedefs the kernel does keep.&lt;/p&gt;

&lt;h2&gt;
  
  
  A complete C example you can run
&lt;/h2&gt;

&lt;p&gt;Here is the named typedef, used the same way we used &lt;code&gt;struct student&lt;/code&gt; in the last post.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;string.h&amp;gt;&lt;/span&gt;

&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;roll_no&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt;  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;student_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Asha"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;87.5f&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;student_t&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;roll_no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;strcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Ravi"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;91.0f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%d %s %.1f\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;roll_no&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%d %s %.1f\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;roll_no&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save it as &lt;code&gt;student_t.c&lt;/code&gt;, then compile and run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc student_t.c -o student_t
./student_t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On Windows with MinGW installed, the second line in PowerShell is &lt;code&gt;.\student_t.exe&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 Asha 87.5
2 Ravi 91.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;s1&lt;/code&gt; uses the tag name. &lt;code&gt;s2&lt;/code&gt; uses the typedef name. Both are the same type, so the rest of the program does not care which spelling you picked.&lt;/p&gt;

&lt;h2&gt;
  
  
  typedef in the Linux kernel: wait_queue_head_t
&lt;/h2&gt;

&lt;p&gt;The Linux kernel does use typedef on some structs, including a few older names that are still around. One you will meet often is &lt;code&gt;wait_queue_head_t&lt;/code&gt;. A wait queue head is the handle kernel code uses when a task needs to sleep until something happens. Here is the typedef, simplified from a current kernel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Simplified from the Linux kernel */&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;wait_queue_head&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;spinlock_t&lt;/span&gt;        &lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;list_head&lt;/span&gt;  &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;wait_queue_head&lt;/span&gt; &lt;span class="n"&gt;wait_queue_head_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the same pattern as &lt;code&gt;typedef struct student student_t&lt;/code&gt;. The struct keeps a tag, &lt;code&gt;wait_queue_head&lt;/code&gt;, and the typedef gives it a short name. In older kernels the tag was &lt;code&gt;__wait_queue_head&lt;/code&gt;, and the typedef name was already &lt;code&gt;wait_queue_head_t&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The item on the queue used to have its own typedef, &lt;code&gt;wait_queue_t&lt;/code&gt;. Today that struct is &lt;code&gt;struct wait_queue_entry&lt;/code&gt;, and the typedef is &lt;code&gt;wait_queue_entry_t&lt;/code&gt;. If you read older kernel material and see &lt;code&gt;wait_queue_t&lt;/code&gt;, that is the old name.&lt;/p&gt;

&lt;p&gt;Notice &lt;code&gt;spinlock_t&lt;/code&gt; in the first field. That is another typedef. I will come back to it in a moment. The &lt;code&gt;head&lt;/code&gt; field links waiting tasks together. Lists get their own series later, so we will not unpack that here.&lt;/p&gt;

&lt;p&gt;Even here, a lot of current kernel code says &lt;code&gt;struct wait_queue_head&lt;/code&gt; rather than &lt;code&gt;wait_queue_head_t&lt;/code&gt;. The typedef still exists so older code keeps compiling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the Linux kernel typedefs pte_t and pgd_t
&lt;/h2&gt;

&lt;p&gt;Some typedefs in the Linux kernel are not there to save typing. They exist so the compiler will stop you mixing up two values that happen to hold the same kind of number.&lt;/p&gt;

&lt;p&gt;The kernel's page tables are a good example. On a typical x86-64 machine, a page table entry and a page global directory entry are both an unsigned long underneath. If they used the same type, you could assign one to the other and the compiler would stay quiet. They are not the same thing, so that assignment should be a mistake.&lt;/p&gt;

&lt;p&gt;The kernel wraps each one in its own struct, then typedefs that struct. This is a shortened form of the x86-64 definitions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Simplified from the Linux kernel, x86-64 */&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;pte&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;pte_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;pgd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;pgd_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;pte_t&lt;/code&gt; and &lt;code&gt;pgd_t&lt;/code&gt; each hold one number, but they are different types. C will not let you assign a &lt;code&gt;pte_t&lt;/code&gt; to a &lt;code&gt;pgd_t&lt;/code&gt;. The kernel coding style calls types like these opaque. Ordinary code is not supposed to read the field by name. It uses helper functions such as &lt;code&gt;pte_val()&lt;/code&gt; and &lt;code&gt;__pte()&lt;/code&gt;, or &lt;code&gt;pgd_val()&lt;/code&gt; and &lt;code&gt;__pgd()&lt;/code&gt;, so you cannot mix them up by accident.&lt;/p&gt;

&lt;p&gt;You can see the type check with a tiny program. It prints the two values, and it leaves one bad assignment commented out.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;

&lt;span class="cm"&gt;/* Same idea as the kernel's wrapper typedefs */&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;pte&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;pte_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;pgd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;pgd_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pte_t&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mh"&gt;0xabc&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;pgd_t&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mh"&gt;0x1000&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"pte=0x%lx pgd=0x%lx\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pgd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cm"&gt;/* d = e; */&lt;/span&gt;
    &lt;span class="cm"&gt;/* If you uncomment that line, gcc says:
       incompatible types when assigning to type 'pgd_t' from type 'pte_t' */&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save it as &lt;code&gt;pte_pgd.c&lt;/code&gt;, compile it with &lt;code&gt;gcc pte_pgd.c -o pte_pgd&lt;/code&gt;, and run &lt;code&gt;./pte_pgd&lt;/code&gt;. In PowerShell the run command is &lt;code&gt;.\pte_pgd.exe&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pte=0xabc pgd=0x1000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am reading &lt;code&gt;e.pte&lt;/code&gt; and &lt;code&gt;d.pgd&lt;/code&gt; here only to print them. In real Linux kernel code you would go through the helpers, so most files never mention the field name at all. If those two types had been a plain &lt;code&gt;unsigned long&lt;/code&gt;, the assignment &lt;code&gt;d = e&lt;/code&gt; would have compiled, and a bug like that could have been hard to spot.&lt;/p&gt;

&lt;h2&gt;
  
  
  atomic_t and spinlock_t in the Linux kernel
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;atomic_t&lt;/code&gt; and &lt;code&gt;spinlock_t&lt;/code&gt; are two more typedefs you will keep seeing in Linux kernel structs. Both are there so that callers use the official helper functions, rather than touching the inside of the type directly.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;atomic_t&lt;/code&gt; is a counter that the kernel can update without being interrupted halfway through. A simplified form of the current typedef is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Simplified from the Linux kernel */&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;atomic_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looks like a struct with one &lt;code&gt;int&lt;/code&gt; in it. The point of the typedef is that kernel code should not write &lt;code&gt;v.counter++&lt;/code&gt;. A C &lt;code&gt;++&lt;/code&gt; is not guaranteed to be atomic, so the kernel gives you helpers such as &lt;code&gt;atomic_read()&lt;/code&gt;, &lt;code&gt;atomic_set()&lt;/code&gt; and &lt;code&gt;atomic_inc()&lt;/code&gt;. Those are implemented as single atomic instructions. Ordinary code is meant to go through the helpers, not treat the counter as a plain &lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;spinlock_t&lt;/code&gt; is a typedef for &lt;code&gt;struct spinlock&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Simplified from the Linux kernel */&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;spinlock&lt;/span&gt; &lt;span class="n"&gt;spinlock_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A spin lock is how one CPU holds a short lock while another CPU waits. The inside of &lt;code&gt;struct spinlock&lt;/code&gt; changes with kernel config options, so most code never mentions the fields. It uses helpers such as &lt;code&gt;spin_lock()&lt;/code&gt; and &lt;code&gt;spin_unlock()&lt;/code&gt;. In older kernels you may see field names such as &lt;code&gt;slock&lt;/code&gt;. Today you should treat &lt;code&gt;spinlock_t&lt;/code&gt; as a type you pass to the helpers, not as a struct you fill in by hand.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the Linux kernel mostly avoids typedef
&lt;/h2&gt;

&lt;p&gt;If typedef is so handy, why does the Linux kernel not use it on &lt;code&gt;task_struct&lt;/code&gt;, &lt;code&gt;timespec&lt;/code&gt; and most of the other structs in this series?&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://www.kernel.org/doc/html/latest/process/coding-style.html#typedefs" rel="noopener noreferrer"&gt;Linux kernel coding style&lt;/a&gt; is direct about this. It says it is a mistake to typedef ordinary structures and pointers. The example in the document is the one I used earlier. &lt;code&gt;vps_t a;&lt;/code&gt; hides what &lt;code&gt;a&lt;/code&gt; is. &lt;code&gt;struct virtual_container *a;&lt;/code&gt; does not. The rule of thumb is: a pointer, or a struct whose fields can reasonably be accessed directly, should never be a typedef.&lt;/p&gt;

&lt;p&gt;That is why you write &lt;code&gt;struct task_struct *&lt;/code&gt; in kernel code, not &lt;code&gt;task_t&lt;/code&gt; or &lt;code&gt;task_ptr&lt;/code&gt;. You can see it is a struct, and you can see it is a pointer. The same goes for &lt;code&gt;struct timespec&lt;/code&gt; from the last post.&lt;/p&gt;

&lt;p&gt;The same document says typedefs are allowed when they fall into a few special cases. The ones that matter for structs are these.&lt;/p&gt;

&lt;p&gt;Opaque types, such as &lt;code&gt;pte_t&lt;/code&gt;. You are not supposed to look inside. You only use the accessor functions. The coding style mentions &lt;code&gt;pte_t&lt;/code&gt; by name as the example.&lt;/p&gt;

&lt;p&gt;Wrapper types that exist so the compiler can type-check two values that would otherwise look the same. &lt;code&gt;pte_t&lt;/code&gt; and &lt;code&gt;pgd_t&lt;/code&gt; are this idea in struct form. The kernel also uses a tool called sparse, which can turn an integer typedef into a distinct type for checking.&lt;/p&gt;

&lt;p&gt;Fixed-size integers, such as &lt;code&gt;u32&lt;/code&gt;. These are typedefs for integers of a known width, so you are not left wondering whether a value is an &lt;code&gt;int&lt;/code&gt; or a &lt;code&gt;long&lt;/code&gt;. The coding style also allows the &lt;code&gt;__u32&lt;/code&gt; form in headers that userspace may include, because those headers cannot always use the C99 names or the kernel's &lt;code&gt;u32&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The document is strict about the closing rule: do not add a typedef unless you can clearly match one of those cases. Older kernel code does not always follow today's rule. &lt;code&gt;wait_queue_head_t&lt;/code&gt; is a leftover of that kind. Its fields are meant to be used, and current code often writes &lt;code&gt;struct wait_queue_head&lt;/code&gt; instead. For new kernel code, and for your own structs as you learn, the safer default is the one we used in the last post: keep the &lt;code&gt;struct&lt;/code&gt; tag visible, and add a typedef only when you have a reason like the ones above.&lt;/p&gt;

&lt;p&gt;In your own user programs, &lt;code&gt;typedef struct student student_t&lt;/code&gt; is still fine. Plenty of good C code does it. Just be careful not to typedef a pointer, and remember that the typedef is only a name. When you start reading Linux kernel sources, expect &lt;code&gt;struct&lt;/code&gt; to be spelled out most of the time.&lt;/p&gt;

&lt;h2&gt;
  
  
  What comes next
&lt;/h2&gt;

&lt;p&gt;We now have two ways to name a struct type: the &lt;code&gt;struct&lt;/code&gt; tag, and a typedef. We also know why Linux kernel typedefs such as &lt;code&gt;pte_t&lt;/code&gt; and &lt;code&gt;atomic_t&lt;/code&gt; keep those short names, and why &lt;code&gt;struct task_struct&lt;/code&gt; does not.&lt;/p&gt;

&lt;p&gt;In the next post, How a Struct Sits in Memory: Size, Padding and Alignment, we will look at what happens when those fields are laid out in memory, and why a struct is sometimes larger than the sum of its fields.&lt;/p&gt;

&lt;p&gt;If you want to practice this with small exercises that check your code as you go, the &lt;a href="https://www.jupiuslearning.com/courses/c-prog-a/" rel="noopener noreferrer"&gt;C programming course&lt;/a&gt; at Jupius Learning starts from exactly here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What does typedef struct do in C?
&lt;/h3&gt;

&lt;p&gt;It gives the struct a second type name. After &lt;code&gt;typedef struct student student_t;&lt;/code&gt;, you can write &lt;code&gt;student_t s1;&lt;/code&gt; instead of &lt;code&gt;struct student s1;&lt;/code&gt;. The typedef does not change the fields, and it does not create a variable on its own.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the difference between a struct tag and a typedef name?
&lt;/h3&gt;

&lt;p&gt;The tag is the word after &lt;code&gt;struct&lt;/code&gt;, such as &lt;code&gt;student&lt;/code&gt; in &lt;code&gt;struct student&lt;/code&gt;. The typedef name, such as &lt;code&gt;student_t&lt;/code&gt;, is an alias for that type. You can have both, or you can typedef an anonymous struct and keep only the typedef name. A forward declaration needs the tag.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why can I not use student_t inside its own typedef?
&lt;/h3&gt;

&lt;p&gt;Because the typedef name exists only after the finishing semicolon. Inside the braces, the compiler already knows the tag, so you write &lt;code&gt;struct student *partner&lt;/code&gt;, not &lt;code&gt;student_t *partner&lt;/code&gt;. If you typedef the tag first, on its own line, then &lt;code&gt;student_t *&lt;/code&gt; is fine after that.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does the Linux kernel avoid typedef on most structs?
&lt;/h3&gt;

&lt;p&gt;The Linux kernel coding style says a typedef on an ordinary struct or pointer hides what the type is. &lt;code&gt;struct task_struct *p&lt;/code&gt; is clear. A name like &lt;code&gt;task_t&lt;/code&gt; or &lt;code&gt;task_ptr&lt;/code&gt; is not. The kernel keeps typedef for opaque types and for wrappers that help with type checking. It also typedefs fixed-size integers such as &lt;code&gt;u32&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does the Linux kernel typedef pte_t, atomic_t and spinlock_t?
&lt;/h3&gt;

&lt;p&gt;So that each one is a distinct type, and so that most code uses helper functions instead of reading the fields directly. &lt;code&gt;pte_t&lt;/code&gt; and &lt;code&gt;pgd_t&lt;/code&gt; stop you assigning one kind of page-table value to another. &lt;code&gt;atomic_t&lt;/code&gt; and &lt;code&gt;spinlock_t&lt;/code&gt; steer you toward helpers such as &lt;code&gt;atomic_inc()&lt;/code&gt; and &lt;code&gt;spin_lock()&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>c</category>
      <category>linux</category>
      <category>kernel</category>
      <category>programming</category>
    </item>
    <item>
      <title>task_struct in the Linux Kernel: Every Field Explained</title>
      <dc:creator>Vadher Nikunj</dc:creator>
      <pubDate>Tue, 17 Nov 2026 07:30:20 +0000</pubDate>
      <link>https://dev.to/vadher_nikunj_47e8c86d866/taskstruct-in-the-linux-kernel-every-field-explained-5269</link>
      <guid>https://dev.to/vadher_nikunj_47e8c86d866/taskstruct-in-the-linux-kernel-every-field-explained-5269</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was first published on &lt;a href="https://www.jupiuslearning.com/blog/task-struct-linux-kernel-every-field-explained/" rel="noopener noreferrer"&gt;Jupius Learning&lt;/a&gt;, where you can read it free with diagrams and practice exercises.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;task_struct&lt;/code&gt; is the C struct the Linux kernel uses to describe one task. A task is a process or a thread. Every running program on your Linux machine has at least one &lt;code&gt;task_struct&lt;/code&gt;, and it holds or points to everything the kernel knows about that task: its state, its IDs, its parent, its memory, its open files, its signals and its scheduling data.&lt;/p&gt;

&lt;p&gt;The struct is defined in &lt;code&gt;include/linux/sched.h&lt;/code&gt;. It is also called the process descriptor.&lt;/p&gt;

&lt;p&gt;This post is part of my series on C structs in the Linux kernel. In the &lt;a href="https://www.jupiuslearning.com/blog/the-humble-c-struct-that-still-runs-billions-of-machines/" rel="noopener noreferrer"&gt;first post&lt;/a&gt; I said the kernel is built out of structs. &lt;code&gt;task_struct&lt;/code&gt; is the biggest example of that idea, so it gets a full post of its own.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is task_struct so big in the Linux kernel?
&lt;/h2&gt;

&lt;p&gt;Because it has to answer every question the kernel may ask about a task. Is it running or sleeping? Who started it? Which memory does it use? Which files does it have open? Which signals is it waiting for? How much CPU time has it used?&lt;/p&gt;

&lt;p&gt;In a modern kernel the struct has a few hundred fields. Many of them only exist when a certain config option is turned on, like lockdep, KASAN, NUMA balancing or perf. You do not need all of those to understand a process. So in this post I group the important fields into sections. For each section I show a short, simplified listing first. Then I explain each field: its type, what it means, and the older name if the field had a different name in 2.6 era kernels. Many tutorials and books still show the old names, so it helps to know both.&lt;/p&gt;

&lt;p&gt;The field names here are checked against mainline &lt;code&gt;include/linux/sched.h&lt;/code&gt; as of November 2026 (Linux 7.4-rc3). The listings below are trimmed. The real struct has more fields, and they are not in this exact order.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does task_struct connect to other kernel structs?
&lt;/h2&gt;

&lt;p&gt;Most of the heavy data is not stored inside &lt;code&gt;task_struct&lt;/code&gt; itself. The struct holds pointers to other structs, and several tasks can share the same one. Threads of one program share one &lt;code&gt;mm_struct&lt;/code&gt;, for example. Here is the picture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   real_parent, parent
          ^
          |
  +-------+---------------+
  |      task_struct      |        +----------------+
  |                       |--mm---&amp;gt;| mm_struct      |  memory map
  |  __state, flags       |        +----------------+
  |  pid, tgid, comm      |        +----------------+
  |  prio, se, policy     |-files-&amp;gt;| files_struct   |  open files
  |                       |        +----------------+
  |  children  (head) ----+--&amp;gt; list of child tasks
  |  sibling   (node) ----+--&amp;gt; link in parent's children list
  |  tasks     (node) ----+--&amp;gt; link in list of all processes
  |                       |        +----------------+
  |                       |--fs---&amp;gt;| fs_struct      |  root, cwd, umask
  |                       |        +----------------+
  |                       |        +----------------+
  |                       |-signal&amp;gt;| signal_struct  |  shared signal data, rlimits
  |                       |        +----------------+
  |                       |        +----------------+
  |                       |sighand&amp;gt;| sighand_struct |  signal handlers
  |                       |        +----------------+
  |                       |        +----------------+
  |                       |--cred-&amp;gt;| cred           |  uid, gid, capabilities
  |                       |        +----------------+
  |                       |        +----------------+
  |                       |nsproxy&amp;gt;| nsproxy        |  namespaces
  +-----------------------+        +----------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The parent and child links are just pointers and &lt;code&gt;list_head&lt;/code&gt; nodes inside &lt;code&gt;task_struct&lt;/code&gt;. The arrows on the right are plain pointers. If two threads share memory, their &lt;code&gt;mm&lt;/code&gt; pointers hold the same address.&lt;/p&gt;

&lt;h2&gt;
  
  
  Process state and flags in task_struct
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;__state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="cm"&gt;/* TASK_RUNNING, TASK_INTERRUPTIBLE, ... */&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;saved_state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="cm"&gt;/* PF_* flags */&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;ptrace&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;  &lt;span class="n"&gt;atomic_flags&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;  &lt;span class="n"&gt;jobctl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* ... */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;__state&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The current state of the task. It holds values like &lt;code&gt;TASK_RUNNING&lt;/code&gt;, &lt;code&gt;TASK_INTERRUPTIBLE&lt;/code&gt;, &lt;code&gt;TASK_UNINTERRUPTIBLE&lt;/code&gt;, &lt;code&gt;__TASK_STOPPED&lt;/code&gt; or &lt;code&gt;__TASK_TRACED&lt;/code&gt;. In 2.6 kernels this field was called &lt;code&gt;state&lt;/code&gt; and was a &lt;code&gt;long&lt;/code&gt;. It was renamed in Linux 5.14 so that code has to use helpers like &lt;code&gt;READ_ONCE()&lt;/code&gt; and &lt;code&gt;set_current_state()&lt;/code&gt; instead of touching it directly.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;saved_state&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A place to keep the old state while a task sleeps on a lock in real time kernels. The kernel puts the old state back when the lock is taken.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;flags&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Per task flags that start with &lt;code&gt;PF_&lt;/code&gt;. For example &lt;code&gt;PF_KTHREAD&lt;/code&gt; means this is a kernel thread, &lt;code&gt;PF_EXITING&lt;/code&gt; means it is shutting down and &lt;code&gt;PF_IDLE&lt;/code&gt; marks the idle task.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ptrace&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Non zero when a debugger is tracing this task. It holds &lt;code&gt;PT_*&lt;/code&gt; bits about how it is traced.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;atomic_flags&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned long&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A second set of flags that must be changed atomically, like &lt;code&gt;PFA_NO_NEW_PRIVS&lt;/code&gt; and some CPU security mitigation bits.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;jobctl&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned long&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Job control bits, used when a task is being stopped with &lt;code&gt;SIGSTOP&lt;/code&gt; or traced.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;What does a task state mean in plain words? &lt;code&gt;TASK_RUNNING&lt;/code&gt; means the task is on a CPU or ready to run. &lt;code&gt;TASK_INTERRUPTIBLE&lt;/code&gt; means it is sleeping and a signal can wake it up. &lt;code&gt;TASK_UNINTERRUPTIBLE&lt;/code&gt; means it is sleeping and signals will not wake it. That is the &lt;code&gt;D&lt;/code&gt; you see in &lt;code&gt;ps&lt;/code&gt;. Newer kernels also combine bits, like &lt;code&gt;TASK_KILLABLE&lt;/code&gt;, which is uninterruptible sleep that still wakes up for a fatal signal, and &lt;code&gt;TASK_IDLE&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When you read &lt;code&gt;State:&lt;/code&gt; in &lt;code&gt;/proc/&amp;lt;pid&amp;gt;/status&lt;/code&gt;, the letter you see (&lt;code&gt;R&lt;/code&gt;, &lt;code&gt;S&lt;/code&gt;, &lt;code&gt;D&lt;/code&gt;, &lt;code&gt;T&lt;/code&gt;, &lt;code&gt;Z&lt;/code&gt; and so on) comes from this field plus &lt;code&gt;exit_state&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Identity: pid and tgid in the Linux kernel
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pid_t&lt;/span&gt;               &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;pid_t&lt;/span&gt;               &lt;span class="n"&gt;tgid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;pid&lt;/span&gt;         &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;thread_pid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;hlist_node&lt;/span&gt;   &lt;span class="n"&gt;pid_links&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;PIDTYPE_MAX&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;list_head&lt;/span&gt;    &lt;span class="n"&gt;thread_node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt;                &lt;span class="n"&gt;comm&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;TASK_COMM_LEN&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="cm"&gt;/* TASK_COMM_LEN is 16 */&lt;/span&gt;
    &lt;span class="cm"&gt;/* ... */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pid&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pid_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The ID of this one task. Every thread has its own &lt;code&gt;pid&lt;/code&gt;. In user space this is what &lt;code&gt;gettid()&lt;/code&gt; returns.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;tgid&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pid_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The thread group ID. It is the &lt;code&gt;pid&lt;/code&gt; of the first thread in the group, the group leader. In user space this is what &lt;code&gt;getpid()&lt;/code&gt; returns, so all threads of one program report the same number.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;thread_pid&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct pid *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A pointer to the kernel's &lt;code&gt;struct pid&lt;/code&gt; object for this task. That object knows the task's number in each PID namespace.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pid_links&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct hlist_node[]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Links that hook this task onto the task lists of its &lt;code&gt;struct pid&lt;/code&gt; objects, one per PID type (PID, TGID, PGID, SID). In 2.6 kernels this was the &lt;code&gt;pids[]&lt;/code&gt; array, which was also used for the PID hash tables. The current layout came in around Linux 4.19.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;thread_node&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct list_head&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;This task's link in the list of all threads of its thread group. That list lives in &lt;code&gt;signal_struct&lt;/code&gt;. Older kernels also had a &lt;code&gt;thread_group&lt;/code&gt; list in the task itself.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;comm&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;char[16]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The short name of the program, without the path. It is what &lt;code&gt;ps&lt;/code&gt; and &lt;code&gt;top&lt;/code&gt; show, and it is cut to 15 characters plus the ending zero. Kernel code should change it with &lt;code&gt;set_task_comm()&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So what is the difference between pid and tgid? For a single threaded program they are equal. When a program starts more threads, each new thread gets a new &lt;code&gt;pid&lt;/code&gt;, but they all keep the leader's &lt;code&gt;tgid&lt;/code&gt;. That is why the kernel calls a thread "a task" too. It is a full &lt;code&gt;task_struct&lt;/code&gt; that shares some pointers with its siblings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Family links: real_parent, parent, children and sibling
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="n"&gt;__rcu&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;real_parent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="n"&gt;__rcu&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;list_head&lt;/span&gt;           &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;list_head&lt;/span&gt;           &lt;span class="n"&gt;sibling&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt;        &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;group_leader&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;list_head&lt;/span&gt;           &lt;span class="n"&gt;ptraced&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;list_head&lt;/span&gt;           &lt;span class="n"&gt;ptrace_entry&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;list_head&lt;/span&gt;           &lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* ... */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;real_parent&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct task_struct *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The task that created this one with &lt;code&gt;fork()&lt;/code&gt; or &lt;code&gt;clone()&lt;/code&gt;. If that parent exits first, the child gets a new parent, usually &lt;code&gt;init&lt;/code&gt; (PID 1) or a subreaper. This is what &lt;code&gt;getppid()&lt;/code&gt; reports.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;parent&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct task_struct *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The task that gets notified with &lt;code&gt;SIGCHLD&lt;/code&gt; and &lt;code&gt;wait()&lt;/code&gt; results when this task changes state. It is normally the same as &lt;code&gt;real_parent&lt;/code&gt;. It differs while a debugger is tracing the task with &lt;code&gt;ptrace()&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;children&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct list_head&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The head of the list of this task's children.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sibling&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct list_head&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;This task's node in its parent's &lt;code&gt;children&lt;/code&gt; list. So the parent holds the list head and each child holds a node.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;group_leader&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct task_struct *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Points to the leader of this thread group. For the leader it points to itself.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ptraced&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct list_head&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The list of tasks this task is tracing as a debugger. In 2.6 kernels it was &lt;code&gt;ptrace_children&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ptrace_entry&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct list_head&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;This task's node in its tracer's &lt;code&gt;ptraced&lt;/code&gt; list. In 2.6 kernels it was &lt;code&gt;ptrace_list&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;tasks&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct list_head&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;This task's node in the big list of all processes. The list starts at &lt;code&gt;init_task&lt;/code&gt;, the descriptor of process 0. The &lt;code&gt;for_each_process()&lt;/code&gt; macro walks it.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;children&lt;/code&gt;, &lt;code&gt;sibling&lt;/code&gt; and &lt;code&gt;tasks&lt;/code&gt; fields are &lt;code&gt;list_head&lt;/code&gt; nodes embedded in the struct. The kernel walks from a node back to its &lt;code&gt;task_struct&lt;/code&gt; with &lt;code&gt;container_of()&lt;/code&gt;. I will cover how that trick works in the linked list posts later, so I am keeping it short here.&lt;/p&gt;

&lt;p&gt;Process group and session IDs used to be fields in the task too (&lt;code&gt;pgrp&lt;/code&gt; and &lt;code&gt;session&lt;/code&gt; in very old kernels, then &lt;code&gt;signal-&amp;gt;pgrp&lt;/code&gt;). Today the kernel gets them through &lt;code&gt;signal_struct&lt;/code&gt; and &lt;code&gt;struct pid&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scheduling fields in the Linux kernel task_struct
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;u8&lt;/span&gt;                           &lt;span class="n"&gt;on_cpu&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;u8&lt;/span&gt;                           &lt;span class="n"&gt;on_rq&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;                          &lt;span class="n"&gt;prio&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;                          &lt;span class="n"&gt;static_prio&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;                          &lt;span class="n"&gt;normal_prio&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;                 &lt;span class="n"&gt;rt_priority&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;sched_entity&lt;/span&gt;          &lt;span class="n"&gt;se&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;sched_rt_entity&lt;/span&gt;       &lt;span class="n"&gt;rt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;sched_dl_entity&lt;/span&gt;       &lt;span class="n"&gt;dl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;sched_class&lt;/span&gt;    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sched_class&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;                 &lt;span class="n"&gt;policy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;                          &lt;span class="n"&gt;nr_cpus_allowed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;cpumask_t&lt;/span&gt;             &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cpus_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;cpumask_t&lt;/span&gt;                    &lt;span class="n"&gt;cpus_mask&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;                          &lt;span class="n"&gt;recent_used_cpu&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;                          &lt;span class="n"&gt;wake_cpu&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;sched_info&lt;/span&gt;            &lt;span class="n"&gt;sched_info&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;                &lt;span class="n"&gt;nvcsw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;                &lt;span class="n"&gt;nivcsw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* ... */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;on_cpu&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;u8&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Set to 1 while the task is actually running on a CPU.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;on_rq&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;u8&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Says whether the task is on a run queue, meaning it is ready to run and waiting its turn.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;prio&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The priority the scheduler really uses right now. It can be raised for a short time, for example when this task holds a lock a higher priority task needs. Lower number means higher priority.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;static_prio&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The priority that comes from the nice value. Nice 0 maps to 120, nice -20 to 100 and nice 19 to 139.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;normal_prio&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The priority the task would have without any temporary boost. It is worked out from &lt;code&gt;static_prio&lt;/code&gt; or &lt;code&gt;rt_priority&lt;/code&gt; and the policy.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rt_priority&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The real time priority, from 1 to 99, for tasks using &lt;code&gt;SCHED_FIFO&lt;/code&gt; or &lt;code&gt;SCHED_RR&lt;/code&gt;. It is 0 for normal tasks.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;se&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct sched_entity&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The data the fair scheduler (CFS, and EEVDF since 6.6) keeps for this task. It includes &lt;code&gt;vruntime&lt;/code&gt; and &lt;code&gt;sum_exec_runtime&lt;/code&gt;, the total CPU time used.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rt&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct sched_rt_entity&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Data for the real time scheduler. Its &lt;code&gt;run_list&lt;/code&gt; field links the task into a real time priority queue.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;dl&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct sched_dl_entity&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Data for the deadline scheduler (&lt;code&gt;SCHED_DEADLINE&lt;/code&gt;), like runtime, deadline and period.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sched_class&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;const struct sched_class *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A pointer to a table of functions for the scheduler class that owns this task: fair, real time, deadline, idle or stop. This is the kernel's "ops table" idea, which I will cover later in the series.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;policy&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The scheduling policy: &lt;code&gt;SCHED_NORMAL&lt;/code&gt;, &lt;code&gt;SCHED_BATCH&lt;/code&gt;, &lt;code&gt;SCHED_IDLE&lt;/code&gt;, &lt;code&gt;SCHED_FIFO&lt;/code&gt;, &lt;code&gt;SCHED_RR&lt;/code&gt; or &lt;code&gt;SCHED_DEADLINE&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;nr_cpus_allowed&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;How many CPUs this task may run on.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cpus_mask&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;cpumask_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The set of CPUs this task may run on, the CPU affinity. &lt;code&gt;taskset&lt;/code&gt; changes it. In 2.6 kernels it was called &lt;code&gt;cpus_allowed&lt;/code&gt;. It was renamed in Linux 5.3.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cpus_ptr&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;const cpumask_t *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A pointer the scheduler reads instead of &lt;code&gt;cpus_mask&lt;/code&gt; directly. It normally points to &lt;code&gt;cpus_mask&lt;/code&gt;, but it can point somewhere else for a short time, like when migration is disabled.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;recent_used_cpu&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A CPU this task ran on recently. The scheduler uses it to pick a warm CPU when the task wakes up.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;wake_cpu&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The CPU picked to run the task when it is woken up.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sched_info&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct sched_info&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Scheduler statistics like how long the task waited on the run queue and how many times it ran.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;nvcsw&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned long&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Number of voluntary context switches. The task gave up the CPU on its own, for example to wait for I/O. Shown as &lt;code&gt;voluntary_ctxt_switches&lt;/code&gt; in &lt;code&gt;/proc/&amp;lt;pid&amp;gt;/status&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;nivcsw&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned long&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Number of involuntary context switches. The scheduler took the CPU away. Shown as &lt;code&gt;nonvoluntary_ctxt_switches&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you read older material you will see different scheduling fields. The 2.6 O(1) scheduler had &lt;code&gt;run_list&lt;/code&gt; and &lt;code&gt;array&lt;/code&gt; directly in the task, pointing into a &lt;code&gt;prio_array_t&lt;/code&gt; with 140 priority lists. It also had &lt;code&gt;sleep_avg&lt;/code&gt;, &lt;code&gt;timestamp&lt;/code&gt;, &lt;code&gt;last_ran&lt;/code&gt;, &lt;code&gt;activated&lt;/code&gt;, &lt;code&gt;time_slice&lt;/code&gt; and &lt;code&gt;first_time_slice&lt;/code&gt;. All of those went away in Linux 2.6.23 when CFS arrived and the per task data moved into &lt;code&gt;struct sched_entity se&lt;/code&gt;. The &lt;code&gt;sched_class&lt;/code&gt; pointer came in at the same time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Memory: mm and active_mm
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;mm_struct&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;mm&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;mm_struct&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;active_mm&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* ... */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;mm&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct mm_struct *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The memory descriptor for this task's user address space. It holds the list of memory areas, the page tables and counters like resident memory. All threads of a process point to the same &lt;code&gt;mm_struct&lt;/code&gt;. For kernel threads &lt;code&gt;mm&lt;/code&gt; is &lt;code&gt;NULL&lt;/code&gt;, because they have no user space.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;active_mm&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct mm_struct *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The address space that is actually loaded while this task runs. For normal tasks it equals &lt;code&gt;mm&lt;/code&gt;. A kernel thread borrows the &lt;code&gt;mm&lt;/code&gt; of the task that ran before it, so it does not have to switch page tables. This is called lazy TLB.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A simple test you will see in kernel code is &lt;code&gt;if (!p-&amp;gt;mm)&lt;/code&gt;. It is one quick way to tell a kernel thread from a user task, though modern code prefers checking &lt;code&gt;PF_KTHREAD&lt;/code&gt; in &lt;code&gt;flags&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credentials: cred and real_cred
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;cred&lt;/span&gt; &lt;span class="n"&gt;__rcu&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptracer_cred&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;cred&lt;/span&gt; &lt;span class="n"&gt;__rcu&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;real_cred&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;cred&lt;/span&gt;        &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cred&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* ... */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;real_cred&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;const struct cred *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The objective credentials. This is who the task really is when other tasks act on it, for example when someone sends it a signal.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cred&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;const struct cred *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The subjective credentials. This is who the task acts as when it opens a file or does other work. It is usually the same object as &lt;code&gt;real_cred&lt;/code&gt;, but the kernel can override it for a short time.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ptracer_cred&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;const struct cred *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The credentials of the debugger at the moment it attached. Used for security checks while tracing.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Inside &lt;code&gt;struct cred&lt;/code&gt; you find &lt;code&gt;uid&lt;/code&gt;, &lt;code&gt;gid&lt;/code&gt;, &lt;code&gt;euid&lt;/code&gt;, &lt;code&gt;egid&lt;/code&gt;, &lt;code&gt;suid&lt;/code&gt;, &lt;code&gt;sgid&lt;/code&gt;, &lt;code&gt;fsuid&lt;/code&gt;, &lt;code&gt;fsgid&lt;/code&gt;, the supplementary groups and the capability sets. In 2.6 kernels up to 2.6.28, those IDs were plain fields right inside &lt;code&gt;task_struct&lt;/code&gt;, like &lt;code&gt;current-&amp;gt;uid&lt;/code&gt; and &lt;code&gt;current-&amp;gt;euid&lt;/code&gt;. Since 2.6.29 you read them through the cred, for example with &lt;code&gt;current_uid()&lt;/code&gt; or &lt;code&gt;task_euid(p)&lt;/code&gt;. A cred is never changed in place. To change one the kernel makes a new copy and swaps the pointer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Files and filesystem: files and fs
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;fs_struct&lt;/span&gt;     &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;files_struct&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;nameidata&lt;/span&gt;     &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nameidata&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* ... */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;fs&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct fs_struct *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Filesystem context: the root directory, the current working directory and the &lt;code&gt;umask&lt;/code&gt;. &lt;code&gt;chdir()&lt;/code&gt; and &lt;code&gt;chroot()&lt;/code&gt; change what this points to.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;files&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct files_struct *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The open file table. A file descriptor like 0, 1 or 3 is an index into the array of &lt;code&gt;struct file&lt;/code&gt; pointers inside it. Threads usually share one &lt;code&gt;files_struct&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;nameidata&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct nameidata *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Temporary state used while the kernel walks a path name like &lt;code&gt;/home/me/a.txt&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Mainline also has a &lt;code&gt;real_fs&lt;/code&gt; field next to &lt;code&gt;fs&lt;/code&gt;. It is a very recent addition in the 7.x kernels and is not in 6.12, so you will not see it in most material yet.&lt;/p&gt;

&lt;p&gt;When you run &lt;code&gt;ls -l /proc/self/fd&lt;/code&gt;, the kernel is reading this task's &lt;code&gt;files&lt;/code&gt; table. When you look at &lt;code&gt;/proc/self/cwd&lt;/code&gt;, it reads &lt;code&gt;fs&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Signals in the Linux kernel task_struct
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;signal_struct&lt;/span&gt;         &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;sighand_struct&lt;/span&gt; &lt;span class="n"&gt;__rcu&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sighand&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;sigset_t&lt;/span&gt;                      &lt;span class="n"&gt;blocked&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;sigset_t&lt;/span&gt;                      &lt;span class="n"&gt;real_blocked&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;sigset_t&lt;/span&gt;                      &lt;span class="n"&gt;saved_sigmask&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;sigpending&lt;/span&gt;             &lt;span class="n"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;                 &lt;span class="n"&gt;sas_ss_sp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt;                        &lt;span class="n"&gt;sas_ss_size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;                  &lt;span class="n"&gt;sas_ss_flags&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;                           &lt;span class="n"&gt;pdeath_signal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* ... */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;signal&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct signal_struct *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Signal and process wide data shared by all threads in the group. It has the shared pending signals, the thread list, resource limits in &lt;code&gt;rlim[]&lt;/code&gt;, process group and session info, and group wide CPU time totals.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sighand&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct sighand_struct *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The table of signal handlers, what to do for each signal. Threads share it, which is why a handler set in one thread applies to all.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;blocked&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sigset_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The mask of signals this thread has blocked. &lt;code&gt;sigprocmask()&lt;/code&gt; and &lt;code&gt;pthread_sigmask()&lt;/code&gt; change it.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;real_blocked&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sigset_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A saved copy of the mask, used by calls like &lt;code&gt;rt_sigtimedwait()&lt;/code&gt; that change the mask for a while.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;saved_sigmask&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sigset_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The mask to restore after calls like &lt;code&gt;sigsuspend()&lt;/code&gt; or &lt;code&gt;ppoll()&lt;/code&gt; return.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pending&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct sigpending&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Signals sent to this one thread that are not handled yet. Signals sent to the whole process go into &lt;code&gt;signal-&amp;gt;shared_pending&lt;/code&gt; instead.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sas_ss_sp&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned long&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Start address of the alternate signal stack set with &lt;code&gt;sigaltstack()&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sas_ss_size&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;size_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Size of that alternate stack.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sas_ss_flags&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Flags for the alternate stack.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pdeath_signal&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The signal this task gets when its parent dies. You set it with &lt;code&gt;prctl(PR_SET_PDEATHSIG)&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Resource limits like &lt;code&gt;RLIMIT_NOFILE&lt;/code&gt; live in &lt;code&gt;current-&amp;gt;signal-&amp;gt;rlim&lt;/code&gt;, not in the task. That has been the case since 2.6, and it is because limits are per process, not per thread.&lt;/p&gt;

&lt;h2&gt;
  
  
  Kernel stack and thread_info: where does the task live?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="cp"&gt;#ifdef CONFIG_THREAD_INFO_IN_TASK&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;thread_info&lt;/span&gt;   &lt;span class="n"&gt;thread_info&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="cm"&gt;/* must be first */&lt;/span&gt;
&lt;span class="cp"&gt;#endif&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt;                &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;refcount_t&lt;/span&gt;           &lt;span class="n"&gt;stack_refcount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;vm_struct&lt;/span&gt;    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;stack_vm_area&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* ... */&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;thread_struct&lt;/span&gt; &lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="cm"&gt;/* must be last */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;thread_info&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct thread_info&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Small, arch specific data that low level code needs fast, like the &lt;code&gt;TIF_NEED_RESCHED&lt;/code&gt; and &lt;code&gt;TIF_SIGPENDING&lt;/code&gt; flags. On x86 and arm64 it is the very first member of &lt;code&gt;task_struct&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;stack&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;void *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Points to the bottom of this task's kernel stack. Each task has its own kernel stack, used when it runs in kernel mode. On x86-64 it is 16 KB.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;stack_vm_area&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct vm_struct *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;When the kernel stack is allocated with vmalloc (&lt;code&gt;CONFIG_VMAP_STACK&lt;/code&gt;), this points to that area. A guard page there turns a stack overflow into a clean crash instead of silent memory damage.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;stack_refcount&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;refcount_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A count of users of the stack, so it is not freed while someone still needs it.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;thread&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct thread_struct&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The CPU specific saved state used during a context switch, like the saved stack pointer, segment and debug registers, and FPU state. It is kept as the last member because its size can change.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This is one of the areas that changed the most. In 2.6 kernels on x86, &lt;code&gt;thread_info&lt;/code&gt; sat at the bottom of an 8 KB kernel stack area (a &lt;code&gt;union thread_union&lt;/code&gt;). The task had a &lt;code&gt;thread_info&lt;/code&gt; pointer to it and &lt;code&gt;thread_info&lt;/code&gt; had a &lt;code&gt;task&lt;/code&gt; pointer back. To find the current task, the kernel masked the low bits of the stack pointer to reach &lt;code&gt;thread_info&lt;/code&gt;, then followed &lt;code&gt;-&amp;gt;task&lt;/code&gt;. Today x86 sets &lt;code&gt;CONFIG_THREAD_INFO_IN_TASK&lt;/code&gt; (since Linux 4.9), so &lt;code&gt;thread_info&lt;/code&gt; moved inside &lt;code&gt;task_struct&lt;/code&gt; and the stack area holds only the stack.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does the Linux kernel find the current task_struct?
&lt;/h2&gt;

&lt;p&gt;Kernel code writes &lt;code&gt;current&lt;/code&gt; and gets a pointer to the running task. For example &lt;code&gt;current-&amp;gt;pid&lt;/code&gt; or &lt;code&gt;current-&amp;gt;comm&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On modern x86-64, &lt;code&gt;current&lt;/code&gt; reads a per CPU variable called &lt;code&gt;current_task&lt;/code&gt;. The scheduler writes the new task's address into it on every context switch, so reading it is a single instruction. Other architectures keep the pointer in a register, for example &lt;code&gt;sp_el0&lt;/code&gt; on arm64. On old 2.6 x86 kernels, &lt;code&gt;current&lt;/code&gt; was found from the stack pointer as I described in the section above.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time accounting fields in the Linux kernel task_struct
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;u64&lt;/span&gt;            &lt;span class="n"&gt;utime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;u64&lt;/span&gt;            &lt;span class="n"&gt;stime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;u64&lt;/span&gt;            &lt;span class="n"&gt;gtime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;u64&lt;/span&gt;            &lt;span class="n"&gt;start_time&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;u64&lt;/span&gt;            &lt;span class="n"&gt;start_boottime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;  &lt;span class="n"&gt;min_flt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;  &lt;span class="n"&gt;maj_flt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_io_accounting&lt;/span&gt; &lt;span class="n"&gt;ioac&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* ... */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;utime&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;u64&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;CPU time this task spent in user mode, in nanoseconds.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;stime&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;u64&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;CPU time this task spent in kernel mode, in nanoseconds.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;gtime&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;u64&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Time spent running a virtual machine guest, for KVM vCPU threads.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;start_time&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;u64&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;When the task started, in nanoseconds of monotonic time. Monotonic time does not count time while the machine was suspended.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;start_boottime&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;u64&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;When the task started, in nanoseconds since boot including suspend time. This is what feeds the start time column in &lt;code&gt;/proc/&amp;lt;pid&amp;gt;/stat&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;min_flt&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned long&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Minor page faults: the page was already in memory and only needed mapping.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;maj_flt&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned long&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Major page faults: the page had to be read from disk.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ioac&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct task_io_accounting&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Bytes read and written, shown in &lt;code&gt;/proc/&amp;lt;pid&amp;gt;/io&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In 2.6 kernels &lt;code&gt;utime&lt;/code&gt; and &lt;code&gt;stime&lt;/code&gt; were of type &lt;code&gt;cputime_t&lt;/code&gt;, counted in ticks, and &lt;code&gt;start_time&lt;/code&gt; was a &lt;code&gt;struct timespec&lt;/code&gt;. Today they are plain &lt;code&gt;u64&lt;/code&gt; nanoseconds. Totals for the whole thread group, including dead threads, are kept in &lt;code&gt;signal_struct&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exit fields: exit_state, exit_code and exit_signal
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;  &lt;span class="n"&gt;exit_state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;  &lt;span class="n"&gt;exit_code&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;  &lt;span class="n"&gt;exit_signal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;completion&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;vfork_done&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;__user&lt;/span&gt;        &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;set_child_tid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;__user&lt;/span&gt;        &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;clear_child_tid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* ... */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;exit_state&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Set when the task has finished. &lt;code&gt;EXIT_ZOMBIE&lt;/code&gt; means it is dead but the parent has not called &lt;code&gt;wait()&lt;/code&gt; yet. &lt;code&gt;EXIT_DEAD&lt;/code&gt; means it is being removed for good.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;exit_code&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The exit status and signal info that the parent reads with &lt;code&gt;wait()&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;exit_signal&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The signal sent to the parent when this task exits, normally &lt;code&gt;SIGCHLD&lt;/code&gt;. For threads it is -1, because a thread exiting does not notify the parent process.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;vfork_done&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct completion *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Used by &lt;code&gt;vfork()&lt;/code&gt;. The parent sleeps on it until the child calls &lt;code&gt;exec&lt;/code&gt; or exits.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;set_child_tid&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int __user *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A user space address where the kernel writes the new thread's ID at start. Thread libraries use it.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;clear_child_tid&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int __user *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A user space address that the kernel clears and wakes with a futex when the thread exits. This is how &lt;code&gt;pthread_join()&lt;/code&gt; knows a thread is done.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A zombie is just a &lt;code&gt;task_struct&lt;/code&gt; that stays around holding &lt;code&gt;exit_code&lt;/code&gt; and the time totals until the parent collects them. Almost all its other resources are already freed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Namespaces and cgroups: how containers use task_struct in the Linux kernel
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;nsproxy&lt;/span&gt;        &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nsproxy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;css_set&lt;/span&gt; &lt;span class="n"&gt;__rcu&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cgroups&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;list_head&lt;/span&gt;       &lt;span class="n"&gt;cg_list&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* ... */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;nsproxy&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct nsproxy *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Points to the set of namespaces the task lives in: UTS, IPC, mount, network, PID for children, time and cgroup. Containers are built on this. In early 2.6 kernels there was only a &lt;code&gt;namespace&lt;/code&gt; field for mount namespaces. &lt;code&gt;nsproxy&lt;/code&gt; came in 2.6.19.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cgroups&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct css_set *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The set of control groups this task belongs to. Limits like memory and CPU for containers and systemd services come from here.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cg_list&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct list_head&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;This task's node in the list of tasks that share the same &lt;code&gt;css_set&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The user and PID namespaces are not stored directly in &lt;code&gt;nsproxy&lt;/code&gt; for the task itself. The user namespace sits in &lt;code&gt;cred&lt;/code&gt;, and the task's own PID namespace comes from its &lt;code&gt;struct pid&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference counting and lifetime
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;refcount_t&lt;/span&gt;       &lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;refcount_t&lt;/span&gt;       &lt;span class="n"&gt;rcu_users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;rcu_head&lt;/span&gt;  &lt;span class="n"&gt;rcu&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;spinlock_t&lt;/span&gt;       &lt;span class="n"&gt;alloc_lock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;raw_spinlock_t&lt;/span&gt;   &lt;span class="n"&gt;pi_lock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* ... */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;usage&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;refcount_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;How many places hold a reference to this &lt;code&gt;task_struct&lt;/code&gt;. &lt;code&gt;get_task_struct()&lt;/code&gt; adds one and &lt;code&gt;put_task_struct()&lt;/code&gt; drops one. The struct is freed when it reaches zero. In 2.6 kernels it was an &lt;code&gt;atomic_t&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rcu_users&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;refcount_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A second count for users that read the task under RCU. When it drops to zero, the kernel waits for an RCU grace period and then drops one &lt;code&gt;usage&lt;/code&gt; reference.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rcu&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct rcu_head&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Used to free the task safely after all RCU readers are done.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;alloc_lock&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;spinlock_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Protects several pointers like &lt;code&gt;mm&lt;/code&gt;, &lt;code&gt;files&lt;/code&gt;, &lt;code&gt;fs&lt;/code&gt; and &lt;code&gt;comm&lt;/code&gt; while they are being changed. &lt;code&gt;task_lock(p)&lt;/code&gt; takes it.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pi_lock&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;raw_spinlock_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Protects the priority inheritance data and is also taken when waking the task up.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Why two counts? Many parts of the kernel only peek at a task under &lt;code&gt;rcu_read_lock()&lt;/code&gt; and do not want to pay for a full reference. The two counters let both styles of access work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other fields worth knowing
&lt;/h2&gt;

&lt;p&gt;These do not fit neatly in the groups above, but you will meet them when reading kernel code.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;personality&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Execution domain flags, set with &lt;code&gt;personality()&lt;/code&gt;. Used for things like disabling address space randomization for one program.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;restart_block&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct restart_block&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Information to restart a system call that was interrupted, like &lt;code&gt;nanosleep()&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;stack_canary&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned long&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A random value used by the compiler's stack protector to catch stack smashing in kernel code.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;seccomp&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct seccomp&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The seccomp mode and filters for this task. Browsers and container runtimes use it to limit which system calls a process may make.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;audit_context&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct audit_context *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Data for the audit subsystem while a system call is being audited.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;loginuid&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;kuid_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The user ID that logged in, kept for auditing even after &lt;code&gt;su&lt;/code&gt; or &lt;code&gt;sudo&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sessionid&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unsigned int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The audit login session ID.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;self_exec_id&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;u64&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Goes up by one on every &lt;code&gt;exec&lt;/code&gt;. The kernel uses it, for example, to decide if a child's exit signal should be reset because the parent ran a new program.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;parent_exec_id&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;u64&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The parent's &lt;code&gt;self_exec_id&lt;/code&gt; at fork time.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;journal_info&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;void *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Pointer used by journaling filesystems like ext4 to track the current transaction.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;io_context&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct io_context *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Block I/O scheduler data for this task, like its I/O priority.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sysvsem&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct sysv_sem&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;System V semaphore undo info, so semaphore changes can be undone if the task dies.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;mempolicy&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct mempolicy *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The NUMA memory policy set with &lt;code&gt;set_mempolicy()&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;mems_allowed&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;nodemask_t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The NUMA nodes this task may take memory from, set by cpusets.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;delays&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct task_delay_info *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Delay accounting: time spent waiting for CPU, I/O and memory.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;task_works&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct callback_head *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A list of small jobs to run when the task next returns to user space.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;io_uring&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct io_uring_task *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Per task data for io_uring.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;perf_event_ctxp&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct perf_event_context *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Performance counter data used by &lt;code&gt;perf&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;security&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;void *&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Private data for security modules like SELinux or AppArmor.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;timer_slack_ns&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;u64&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;How much the kernel may delay this task's timers to group wakeups and save power.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pagefault_disabled&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Counter that is non zero while page faults are disabled for this task.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There are many more, mostly for debugging tools (lockdep, KASAN, KCSAN, KCOV, ftrace), NUMA balancing, real time locking and memory cgroups. They are each behind a config option, and most kernel developers never touch them.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you see task_struct fields from user space in Linux?
&lt;/h2&gt;

&lt;p&gt;You cannot read &lt;code&gt;task_struct&lt;/code&gt; directly from a normal program. It lives in kernel memory. But the kernel copies many of its fields out to you through system calls and through &lt;code&gt;/proc&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is a small program that shows the difference between &lt;code&gt;pid&lt;/code&gt; and &lt;code&gt;tgid&lt;/code&gt;. It starts one extra thread and prints the IDs from both threads.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define _GNU_SOURCE&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;pthread.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;unistd.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;sys/syscall.h&amp;gt;&lt;/span&gt;

&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cm"&gt;/* getpid() returns tgid, gettid() returns this thread's own pid */&lt;/span&gt;
    &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"thread: getpid() = %d  gettid() = %ld\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="nf"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;syscall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SYS_gettid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pthread_t&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"main:   getpid() = %d  gettid() = %ld  getppid() = %d\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="nf"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;syscall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SYS_gettid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;getppid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;pthread_create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;pthread_join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Build and run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc -Wall -o ids ids.c -pthread
./ids
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sample output (your numbers will differ):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;main:   getpid() = 16724  gettid() = 16724  getppid() = 16707
thread: getpid() = 16724  gettid() = 16725
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both threads print the same &lt;code&gt;getpid()&lt;/code&gt;. That is &lt;code&gt;tgid&lt;/code&gt;. The thread has its own &lt;code&gt;gettid()&lt;/code&gt;. That is its own &lt;code&gt;pid&lt;/code&gt; field. The main thread is the group leader, so its &lt;code&gt;pid&lt;/code&gt; and &lt;code&gt;tgid&lt;/code&gt; match. &lt;code&gt;getppid()&lt;/code&gt; comes from &lt;code&gt;real_parent&lt;/code&gt;, which here is the shell.&lt;/p&gt;

&lt;p&gt;The second program reads &lt;code&gt;/proc/self/status&lt;/code&gt;. This file is built by the kernel from the current &lt;code&gt;task_struct&lt;/code&gt; and the structs it points to. The program prints only the lines that map to the fields in this post.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;string.h&amp;gt;&lt;/span&gt;

&lt;span class="cm"&gt;/* Print the lines of /proc/self/status that come from task_struct */&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"Name:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"State:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Tgid:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Pid:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"PPid:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"Uid:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Gid:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Threads:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Cpus_allowed_list:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"voluntary_ctxt_switches:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"nonvoluntary_ctxt_switches:"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;nkeys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;256&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;FILE&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/proc/self/status"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"r"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;perror&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fopen"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;fgets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nkeys&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;strncmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;fputs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;fclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc -Wall -o status status.c
./status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sample output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name:   status
State:  R (running)
Tgid:   16726
Pid:    16726
PPid:   16707
Uid:    1000    1000    1000    1000
Gid:    1000    1000    1000    1000
Threads:    1
Cpus_allowed_list:  0-7
voluntary_ctxt_switches:    0
nonvoluntary_ctxt_switches: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is where each line comes from:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;
&lt;code&gt;/proc/self/status&lt;/code&gt; line&lt;/th&gt;
&lt;th&gt;Comes from&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Name&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;comm&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;State&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;__state&lt;/code&gt; and &lt;code&gt;exit_state&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;Tgid&lt;/code&gt;, &lt;code&gt;Pid&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;tgid&lt;/code&gt;, &lt;code&gt;pid&lt;/code&gt; (as seen in your PID namespace)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;PPid&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;real_parent&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;Uid&lt;/code&gt;, &lt;code&gt;Gid&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;real_cred&lt;/code&gt; (real, effective, saved and filesystem IDs)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Threads&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;signal-&amp;gt;nr_threads&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Cpus_allowed_list&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;cpus_ptr&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;voluntary_ctxt_switches&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;nvcsw&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;nonvoluntary_ctxt_switches&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;nivcsw&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;State&lt;/code&gt; is &lt;code&gt;R&lt;/code&gt; because the program is running while it reads its own status. Try &lt;code&gt;cat /proc/1/status&lt;/code&gt; or look at another process with &lt;code&gt;ps&lt;/code&gt; and you will see &lt;code&gt;S&lt;/code&gt; for sleeping.&lt;/p&gt;

&lt;h2&gt;
  
  
  A tiny Linux kernel module that reads task_struct
&lt;/h2&gt;

&lt;p&gt;If you want to touch the real struct, a kernel module can. This one prints the task that loaded it, then walks the process list and prints the first ten tasks with their parent's PID.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ts_demo.c: print the current task and the first few processes&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;linux/module.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;linux/sched.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;linux/sched/signal.h&amp;gt;   /* for_each_process() */&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;linux/rcupdate.h&amp;gt;&lt;/span&gt;

&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;__init&lt;/span&gt; &lt;span class="nf"&gt;ts_demo_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;pr_info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ts_demo: loaded by %s, pid=%d tgid=%d\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;comm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;tgid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;rcu_read_lock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;for_each_process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;pr_info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ts_demo: %-16s pid=%d ppid=%d\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;comm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nf"&gt;rcu_dereference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;real_parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;tgid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;rcu_read_unlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;__exit&lt;/span&gt; &lt;span class="nf"&gt;ts_demo_exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;pr_info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ts_demo: unloaded\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;module_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ts_demo_init&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;module_exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ts_demo_exit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;MODULE_LICENSE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"GPL"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;MODULE_DESCRIPTION&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Walk task_struct fields from a module"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: I have not compiled or loaded this module. It is written against the mainline headers, but treat it as a sketch. To build it you need the headers for your running kernel and a two line Makefile with &lt;code&gt;obj-m += ts_demo.o&lt;/code&gt;. After &lt;code&gt;sudo insmod ts_demo.ko&lt;/code&gt;, read the output with &lt;code&gt;sudo dmesg | tail&lt;/code&gt;, and remove it with &lt;code&gt;sudo rmmod ts_demo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A few things to notice. &lt;code&gt;current&lt;/code&gt; is the task that ran &lt;code&gt;insmod&lt;/code&gt;, so its &lt;code&gt;comm&lt;/code&gt; is &lt;code&gt;insmod&lt;/code&gt;. The walk is done inside &lt;code&gt;rcu_read_lock()&lt;/code&gt;, because tasks can exit while we look at the list, and &lt;code&gt;real_parent&lt;/code&gt; is an RCU protected pointer. Do not try this on a machine you care about. A bad pointer inside a module can crash the whole system.&lt;/p&gt;

&lt;p&gt;That is the whole process descriptor, one group at a time. In the next posts of this C struct series we go back to smaller building blocks, and you will keep seeing pieces of &lt;code&gt;task_struct&lt;/code&gt; as examples. If you want to get solid on C structs and pointers first, the &lt;a href="https://www.jupiuslearning.com/courses/c-prog-a/" rel="noopener noreferrer"&gt;Jupius C programming course&lt;/a&gt; covers them with live evaluation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is task_struct in Linux?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;task_struct&lt;/code&gt; is the C struct the Linux kernel uses to describe one process or thread. It holds the task's state, IDs, parent links, scheduling data and pointers to its memory, open files, credentials and signal handlers. It is also called the process descriptor.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where is task_struct defined in the Linux kernel?
&lt;/h3&gt;

&lt;p&gt;It is defined in &lt;code&gt;include/linux/sched.h&lt;/code&gt; in the kernel source tree. Some parts it points to are defined elsewhere, like &lt;code&gt;mm_struct&lt;/code&gt; in &lt;code&gt;include/linux/mm_types.h&lt;/code&gt; and &lt;code&gt;signal_struct&lt;/code&gt; in &lt;code&gt;include/linux/sched/signal.h&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How big is task_struct?
&lt;/h3&gt;

&lt;p&gt;It is several kilobytes on a typical modern 64-bit kernel, and the exact size varies with the kernel version, the CPU architecture and which config options are on. Distribution kernels with more debug and tracing options turned on are often bigger.&lt;/p&gt;

&lt;p&gt;You can check the size on your own machine. One way is &lt;code&gt;sudo grep task_struct /proc/slabinfo&lt;/code&gt;. The &lt;code&gt;objsize&lt;/code&gt; column is the size of one &lt;code&gt;task_struct&lt;/code&gt; in bytes. Another way, if you have the kernel debug info and the &lt;code&gt;pahole&lt;/code&gt; tool, is to run &lt;code&gt;pahole -C task_struct&lt;/code&gt; against the kernel's &lt;code&gt;vmlinux&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the difference between pid and tgid in Linux?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;pid&lt;/code&gt; is the ID of one task, and every thread has its own. &lt;code&gt;tgid&lt;/code&gt; is the thread group ID, which is the &lt;code&gt;pid&lt;/code&gt; of the first thread in the process. The &lt;code&gt;getpid()&lt;/code&gt; call returns &lt;code&gt;tgid&lt;/code&gt;, and &lt;code&gt;gettid()&lt;/code&gt; returns &lt;code&gt;pid&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does the Linux kernel find the current task_struct?
&lt;/h3&gt;

&lt;p&gt;Kernel code uses the &lt;code&gt;current&lt;/code&gt; macro. On x86-64 it reads a per CPU variable that the scheduler updates on every context switch. Old 2.6 x86 kernels found it by masking the stack pointer to reach &lt;code&gt;thread_info&lt;/code&gt; and then following its &lt;code&gt;task&lt;/code&gt; pointer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is a thread a task_struct in Linux?
&lt;/h3&gt;

&lt;p&gt;Yes. Every thread has its own &lt;code&gt;task_struct&lt;/code&gt; with its own &lt;code&gt;pid&lt;/code&gt;, state and kernel stack. Threads of the same process share the same &lt;code&gt;tgid&lt;/code&gt; and point to the same &lt;code&gt;mm_struct&lt;/code&gt;, &lt;code&gt;files_struct&lt;/code&gt;, &lt;code&gt;fs_struct&lt;/code&gt;, &lt;code&gt;signal_struct&lt;/code&gt; and &lt;code&gt;sighand_struct&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://www.jupiuslearning.com/blog/task-struct-linux-kernel-every-field-explained/" rel="noopener noreferrer"&gt;Jupius Learning&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>c</category>
      <category>linux</category>
      <category>kernel</category>
      <category>programming</category>
    </item>
    <item>
      <title>Your First C Struct: Declare, Fill and Read It (Linux Kernel Example)</title>
      <dc:creator>Vadher Nikunj</dc:creator>
      <pubDate>Tue, 17 Nov 2026 07:27:17 +0000</pubDate>
      <link>https://dev.to/vadher_nikunj_47e8c86d866/your-first-c-struct-declare-fill-and-read-it-linux-kernel-example-b1k</link>
      <guid>https://dev.to/vadher_nikunj_47e8c86d866/your-first-c-struct-declare-fill-and-read-it-linux-kernel-example-b1k</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was first published on &lt;a href="https://www.jupiuslearning.com/blog/your-first-c-struct-declare-fill-read-linux-kernel/" rel="noopener noreferrer"&gt;Jupius Learning&lt;/a&gt;, where you can read it free with diagrams and practice exercises.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To use a struct in C, you first declare it. That means you write &lt;code&gt;struct&lt;/code&gt; with a name and list its fields inside braces. Then you create a variable of that type and give each field a value, either all at once with &lt;code&gt;{ }&lt;/code&gt; or one field at a time. You read any field back with a dot, like &lt;code&gt;s1.marks&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://www.jupiuslearning.com/blog/the-humble-c-struct-that-still-runs-billions-of-machines/" rel="noopener noreferrer"&gt;last post&lt;/a&gt; I talked about why the plain C struct matters so much, and how the Linux kernel is built out of structs. This time we write one ourselves. Nothing fancy. We will declare a struct, put values in it and read them back. Then we will look at a tiny struct the Linux kernel uses to keep track of the time, and see that it works the same way.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you declare a struct in C?
&lt;/h2&gt;

&lt;p&gt;A struct declaration describes the shape of your data. It says what fields there are and what type each one has.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;roll_no&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt;  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few small things to notice here. The word after &lt;code&gt;struct&lt;/code&gt; is the tag, here &lt;code&gt;student&lt;/code&gt;. Together, &lt;code&gt;struct student&lt;/code&gt; is the name of your new type. Each line inside the braces is one field, also called a member. And there is a semicolon after the closing brace. Beginners forget that one a lot, and the compiler error you get is not always clear.&lt;/p&gt;

&lt;p&gt;This code does not create any student yet. It only tells the compiler what a student looks like. Think of it as a form with empty boxes. No memory is used for data until you make a variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you fill a struct with values?
&lt;/h2&gt;

&lt;p&gt;Now we make a variable of that type and put values into it. There are two common ways.&lt;/p&gt;

&lt;p&gt;The first way is to give all the values when you create the variable. The values go inside braces, in the same order as the fields.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Asha"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;87.5f&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So &lt;code&gt;roll_no&lt;/code&gt; gets 1, &lt;code&gt;name&lt;/code&gt; gets "Asha" and &lt;code&gt;marks&lt;/code&gt; gets 87.5. If the order is wrong, C will not always complain. It just puts the values in the wrong boxes, so be careful.&lt;/p&gt;

&lt;p&gt;The second way is to create the variable first and set each field on its own line. Starting with &lt;code&gt;= { 0 }&lt;/code&gt; sets every field to zero, so nothing holds garbage while you fill it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;roll_no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;strcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Ravi"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;91.0f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the &lt;code&gt;strcpy&lt;/code&gt; for the name. You cannot write &lt;code&gt;s2.name = "Ravi";&lt;/code&gt; because &lt;code&gt;name&lt;/code&gt; is an array, and C does not let you assign to an array after it is created. Inside the &lt;code&gt;{ }&lt;/code&gt; at creation time it is fine, which is why &lt;code&gt;s1&lt;/code&gt; worked.&lt;/p&gt;

&lt;p&gt;There is a third style, where you name each field inside the braces. The Linux kernel uses it all over the place. It gets its own post later in this series, so I will leave it for now.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you read a struct field? The dot operator
&lt;/h2&gt;

&lt;p&gt;To get a value out, you write the variable name, a dot, then the field name. &lt;code&gt;s1.name&lt;/code&gt; is Asha's name. &lt;code&gt;s1.marks&lt;/code&gt; is her marks. You can use these anywhere you would use a normal variable. You can print them, or do math with them just like any other number.&lt;/p&gt;

&lt;p&gt;You may have seen the arrow, &lt;code&gt;-&amp;gt;&lt;/code&gt;, in kernel code. That is for when you have a pointer to a struct instead of the struct itself. We will get to it a few posts from now. For today, everything is a plain variable, so the dot is all we need.&lt;/p&gt;

&lt;h2&gt;
  
  
  A complete C example you can run
&lt;/h2&gt;

&lt;p&gt;Here is everything together in one program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;string.h&amp;gt;&lt;/span&gt;

&lt;span class="cm"&gt;/* Step 1: declare the struct type */&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;roll_no&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt;  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* Step 2a: fill it when you create it */&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Asha"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;87.5f&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cm"&gt;/* Step 2b: or create it empty and fill it field by field */&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;roll_no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;strcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Ravi"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;91.0f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cm"&gt;/* Step 3: read the fields with the dot operator */&lt;/span&gt;
    &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%d %s %.1f\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;roll_no&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%d %s %.1f\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;roll_no&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cm"&gt;/* You can change a field later too */&lt;/span&gt;
    &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;2.5f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s now has %.1f\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save it as &lt;code&gt;student.c&lt;/code&gt;, then compile and run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc student.c -o student
./student
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On Windows with MinGW installed, the second line in PowerShell is &lt;code&gt;.\student.exe&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 Asha 87.5
2 Ravi 91.0
Asha now has 90.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the whole idea. Declare the shape once. Make as many variables of that type as you like. Each one keeps its own copy of the fields, so changing &lt;code&gt;s1.marks&lt;/code&gt; does not touch &lt;code&gt;s2&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A real Linux kernel struct: timespec
&lt;/h2&gt;

&lt;p&gt;Now let us look at the Linux kernel. One of the simplest structs in it is &lt;code&gt;struct timespec&lt;/code&gt;. The kernel keeps the current date and time in a variable called &lt;code&gt;xtime&lt;/code&gt;, and &lt;code&gt;xtime&lt;/code&gt; is a &lt;code&gt;struct timespec&lt;/code&gt;. It has just two fields.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Simplified from the Linux kernel */&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;timespec&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;tv_sec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="cm"&gt;/* seconds since midnight, 1 January 1970 (UTC) */&lt;/span&gt;
    &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;tv_nsec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="cm"&gt;/* nanoseconds within the current second, 0 to 999,999,999 */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is a struct declaration, exactly like our student. Two fields that belong together, under one name. A time on its own in seconds is not precise enough, and nanoseconds on their own would overflow fast. Together they describe one moment.&lt;/p&gt;

&lt;p&gt;The kernel fills &lt;code&gt;xtime&lt;/code&gt; when the machine boots, by reading the seconds from the hardware clock. After that it keeps updating it as the timer ticks. On a typical setup, each tick adds 1,000,000 nanoseconds to &lt;code&gt;xtime.tv_nsec&lt;/code&gt;. When &lt;code&gt;tv_nsec&lt;/code&gt; goes past 999,999,999, a full second has passed, so the kernel bumps &lt;code&gt;tv_sec&lt;/code&gt; by one and brings &lt;code&gt;tv_nsec&lt;/code&gt; back down.&lt;/p&gt;

&lt;p&gt;Look at that sentence again. &lt;code&gt;xtime.tv_nsec&lt;/code&gt;. That is the same dot we used for &lt;code&gt;s1.marks&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the Linux kernel updates a struct field: try it yourself
&lt;/h2&gt;

&lt;p&gt;We can copy that logic in a small program. I named the struct &lt;code&gt;my_timespec&lt;/code&gt; so it does not clash with the real &lt;code&gt;struct timespec&lt;/code&gt; that your system headers already define.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;

&lt;span class="cm"&gt;/* Same two fields as the kernel's struct timespec */&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;my_timespec&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;tv_sec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="cm"&gt;/* seconds since 1 Jan 1970 (UTC) */&lt;/span&gt;
    &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;tv_nsec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="cm"&gt;/* nanoseconds inside the current second */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* Fill it: almost at the end of a second */&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;my_timespec&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;1700000000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;999500000&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"before: %ld s, %ld ns\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tv_sec&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tv_nsec&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cm"&gt;/* One tick adds 1,000,000 ns (one millisecond) */&lt;/span&gt;
    &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tv_nsec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tv_nsec&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cm"&gt;/* If nanoseconds go past one full second, carry into seconds */&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tv_nsec&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;999999999&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tv_nsec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tv_nsec&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1000000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tv_sec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tv_sec&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"after:  %ld s, %ld ns\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tv_sec&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tv_nsec&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;before: 1700000000 s, 999500000 ns
after:  1700000001 s, 500000 ns
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We started half a millisecond before the end of a second. One tick pushed &lt;code&gt;tv_nsec&lt;/code&gt; past a full second, so the code carried one second over into &lt;code&gt;tv_sec&lt;/code&gt;. The real kernel code has more going on, like locking so that two CPUs do not update the time at once. But the struct part is just this: read a field with a dot, change it, write it back.&lt;/p&gt;

&lt;h2&gt;
  
  
  Filling a struct the Linux kernel reads: struct flock
&lt;/h2&gt;

&lt;p&gt;Here is one more, and this time you fill a struct and the Linux kernel reads it. When a program wants to lock part of a file, it fills a &lt;code&gt;struct flock&lt;/code&gt; and hands it to the &lt;code&gt;fcntl()&lt;/code&gt; system call. The kernel then reads the fields to find out what you asked for. You can use this one directly on Linux, because it comes from &lt;code&gt;&amp;lt;fcntl.h&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;fcntl.h&amp;gt;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;flock&lt;/span&gt; &lt;span class="n"&gt;lock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="cm"&gt;/* start with every field at zero */&lt;/span&gt;

    &lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;l_type&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;F_WRLCK&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="cm"&gt;/* I want a write lock */&lt;/span&gt;
    &lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;l_whence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SEEK_SET&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="cm"&gt;/* count from the start of the file */&lt;/span&gt;
    &lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;l_start&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="cm"&gt;/* begin at byte 0 */&lt;/span&gt;
    &lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;l_len&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="cm"&gt;/* lock 100 bytes */&lt;/span&gt;

    &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"type=%d whence=%d start=%ld len=%ld\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;l_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;l_whence&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;l_start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;l_len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cm"&gt;/* In a real program you would now call:
       fcntl(fd, F_SETLK, &amp;amp;lock);
       and the kernel reads these same fields. */&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output on Linux:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type=1 whence=0 start=0 len=100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;F_WRLCK&lt;/code&gt; happens to be 1 and &lt;code&gt;SEEK_SET&lt;/code&gt; is 0 on Linux, which is why you see those numbers. The point is that a struct is how your program and the kernel agree on a message. You put values in the fields, and the other side reads the same fields by name.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;amp;lock&lt;/code&gt; in that comment means "the address of lock". It has to do with pointers, which we will cover properly a bit later.&lt;/p&gt;

&lt;h2&gt;
  
  
  What comes next
&lt;/h2&gt;

&lt;p&gt;We now know how to declare a struct, fill it and read it. You probably noticed that writing &lt;code&gt;struct student&lt;/code&gt; every time is a bit long. In the next post we will look at &lt;code&gt;typedef struct&lt;/code&gt;, which lets you give your struct a short name, and why the Linux kernel uses it for some types but avoids it for most.&lt;/p&gt;

&lt;p&gt;If you want to practice this with small exercises that check your code as you go, the &lt;a href="https://www.jupiuslearning.com/courses/c-prog-a/" rel="noopener noreferrer"&gt;C programming course&lt;/a&gt; at Jupius Learning starts from exactly here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How do you initialize a struct in C?
&lt;/h3&gt;

&lt;p&gt;You can put the values in braces when you create the variable, in the same order as the fields: &lt;code&gt;struct student s1 = { 1, "Asha", 87.5f };&lt;/code&gt;. You can also start with &lt;code&gt;= { 0 }&lt;/code&gt; to set every field to zero, then assign each field with a dot. Any field you leave out of the braces is set to zero.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the difference between declaring a struct and creating a struct variable?
&lt;/h3&gt;

&lt;p&gt;Declaring the struct, like &lt;code&gt;struct student { ... };&lt;/code&gt;, only describes its fields. It does not store any data. Creating a variable, like &lt;code&gt;struct student s1;&lt;/code&gt;, sets aside memory for one student with its own copy of every field. You declare the type once and can create as many variables of it as you need.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does the dot operator do in C?
&lt;/h3&gt;

&lt;p&gt;The dot reads or changes one field of a struct variable. &lt;code&gt;s1.marks&lt;/code&gt; gives you the marks field of &lt;code&gt;s1&lt;/code&gt;, and &lt;code&gt;s1.marks = 90.0f;&lt;/code&gt; changes it. If you have a pointer to a struct instead of the struct itself, you use the arrow &lt;code&gt;-&amp;gt;&lt;/code&gt; instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can a C struct hold different data types?
&lt;/h3&gt;

&lt;p&gt;Yes. That is the main reason structs exist. One struct can mix &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;float&lt;/code&gt;, &lt;code&gt;char&lt;/code&gt; arrays, pointers and even other structs. Our &lt;code&gt;student&lt;/code&gt; struct holds an &lt;code&gt;int&lt;/code&gt;, a &lt;code&gt;char&lt;/code&gt; array and a &lt;code&gt;float&lt;/code&gt; side by side.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does the Linux kernel use structs?
&lt;/h3&gt;

&lt;p&gt;The Linux kernel uses structs to describe almost everything it manages. Small ones like &lt;code&gt;struct timespec&lt;/code&gt; hold a single value, such as the current time. Big ones like &lt;code&gt;task_struct&lt;/code&gt; describe a whole running program. The kernel reads and writes their fields with the same dot and arrow operators you use in your own C code.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://www.jupiuslearning.com/blog/your-first-c-struct-declare-fill-read-linux-kernel/" rel="noopener noreferrer"&gt;Jupius Learning&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>c</category>
      <category>linux</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>The Humble C Struct That Still Runs Billions of Linux Machines</title>
      <dc:creator>Vadher Nikunj</dc:creator>
      <pubDate>Wed, 11 Nov 2026 10:20:48 +0000</pubDate>
      <link>https://dev.to/vadher_nikunj_47e8c86d866/the-humble-c-struct-that-still-runs-billions-of-linux-machines-5bcc</link>
      <guid>https://dev.to/vadher_nikunj_47e8c86d866/the-humble-c-struct-that-still-runs-billions-of-linux-machines-5bcc</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was first published on &lt;a href="https://www.jupiuslearning.com/blog/the-humble-c-struct-that-still-runs-billions-of-machines/" rel="noopener noreferrer"&gt;Jupius Learning&lt;/a&gt;, where you can read it free with diagrams and practice exercises.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Later on we will get to linked lists, queues and trees. But first I want to stop and talk about something smaller. The struct.&lt;/p&gt;

&lt;p&gt;Most beginners meet the struct in C, write one or two small examples, and move on. It feels like a minor feature.&lt;/p&gt;

&lt;p&gt;But this small feature is doing a huge amount of work in the real world. The Linux kernel is written in C, and it is built almost entirely out of structs. And Linux runs on a lot of machines.&lt;/p&gt;

&lt;p&gt;So in this post I will first explain what a struct is, as simply as I can. Then I will show how the Linux kernel uses that same idea to keep real machines running.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where does the Linux kernel run?
&lt;/h2&gt;

&lt;p&gt;I want to keep the claims here honest, so I will only use numbers that are easy to check.&lt;/p&gt;

&lt;p&gt;Android phones run on the Linux kernel. Google has said there are more than three billion active Android devices. That covers phones, tablets, TVs and cars.&lt;/p&gt;

&lt;p&gt;Every one of the TOP500 supercomputers has run a Linux based operating system since November 2017. That is still true on the June 2026 list.&lt;/p&gt;

&lt;p&gt;On top of that, Linux is very common on web servers, cloud machines, home routers and all kinds of small embedded boards. I will not throw a precise number at you for those, because the counts vary by survey. But "a lot" is a fair word.&lt;/p&gt;

&lt;p&gt;Now here is the part I find interesting. Inside all of these machines, the kernel keeps track of what is going on using structs. Plain C structs. The same kind you can write in your first week of learning C.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a C struct?
&lt;/h2&gt;

&lt;p&gt;A struct lets you put related pieces of data together under one name.&lt;/p&gt;

&lt;p&gt;Say you want to store a student's roll number and marks along with the name. You could keep separate variables, but that gets messy fast once you have 50 students. A struct fixes this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* A tiny beginner example */&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;roll_no&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt;  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;student&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Asha"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;87.5&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s scored %.1f\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is it. &lt;code&gt;struct student&lt;/code&gt; is now a new type, and each variable of that type carries its own copy of the fields. You reach a field with a dot, like &lt;code&gt;s1.name&lt;/code&gt;. If you have a pointer to it, you use an arrow, like &lt;code&gt;p-&amp;gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A struct can also hold other structs, or pointers to them. That is where things get interesting, and it is exactly what the Linux kernel does all day.&lt;/p&gt;

&lt;h2&gt;
  
  
  task_struct: one kernel struct for every running program
&lt;/h2&gt;

&lt;p&gt;When you open an app on your phone, the kernel creates a new process for it. To keep track of that process, it fills in one struct. In the Linux kernel this is called &lt;code&gt;task_struct&lt;/code&gt;, also known as the process descriptor.&lt;/p&gt;

&lt;p&gt;Every running program on a Linux machine has one. Your phone right now likely has hundreds of them in memory.&lt;/p&gt;

&lt;p&gt;Here is a very small slice of it. This is simplified. The real struct has a lot more fields.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Simplified. Only a few of the real fields are shown. */&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="cm"&gt;/* running, sleeping, stopped... */&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;mm_struct&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;mm&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="cm"&gt;/* this process's memory */&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;task_struct&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="cm"&gt;/* who started it */&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;list_head&lt;/span&gt; &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="cm"&gt;/* its child processes */&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;list_head&lt;/span&gt; &lt;span class="n"&gt;sibling&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="cm"&gt;/* its brothers and sisters */&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;list_head&lt;/span&gt; &lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="cm"&gt;/* link in the list of all processes */&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;files_struct&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="cm"&gt;/* its open files */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at it next to the student struct. It is the same pattern. The difference is that many of these fields are pointers to other structs. The &lt;code&gt;mm&lt;/code&gt; field points to a struct about memory. The &lt;code&gt;files&lt;/code&gt; field points to a struct about open files. One struct leads to another, and together they describe one running program.&lt;/p&gt;

&lt;p&gt;Some fields are not pointers at all. A field like &lt;code&gt;children&lt;/code&gt; is a whole small struct sitting inside the big one. So a struct can hold plain values and pointers, and it can also hold whole structs. That freedom is what lets one type describe something as messy as a running program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Kernel structs that act like objects
&lt;/h2&gt;

&lt;p&gt;C does not have classes. But the kernel still gets something very close to objects, using structs full of function pointers.&lt;/p&gt;

&lt;p&gt;When a program opens a file, the kernel creates a &lt;code&gt;file&lt;/code&gt; struct for it. One of its fields is &lt;code&gt;f_op&lt;/code&gt;, which points to a &lt;code&gt;file_operations&lt;/code&gt; struct. That second struct is basically a table of functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Simplified. The real table has many more entries. */&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;file_operations&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;ssize_t&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;file&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;loff_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;ssize_t&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;file&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;loff_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;     &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;inode&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;file&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;     &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;release&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;inode&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;file&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;file&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;file_operations&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;f_op&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="cm"&gt;/* what this file can do */&lt;/span&gt;
    &lt;span class="n"&gt;loff_t&lt;/span&gt; &lt;span class="n"&gt;f_pos&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                   &lt;span class="cm"&gt;/* where we are in the file */&lt;/span&gt;
    &lt;span class="cm"&gt;/* ...many more fields... */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is why this matters. A file on a disk, a pipe, and a device all need to be read in very different ways. But the kernel does not care. It just calls &lt;code&gt;file-&amp;gt;f_op-&amp;gt;read(...)&lt;/code&gt;. Each kind of file fills the table with its own functions. So the same line of code does the right thing for each one.&lt;/p&gt;

&lt;p&gt;If you know a bit of Java or C++, this is the same idea as a method, just built by hand.&lt;/p&gt;

&lt;h2&gt;
  
  
  One more small idea: the kernel's thread_union
&lt;/h2&gt;

&lt;p&gt;The kernel uses a union called &lt;code&gt;thread_union&lt;/code&gt;. A union is like a struct, except all its fields share the same memory. In &lt;code&gt;thread_union&lt;/code&gt;, a small struct called &lt;code&gt;thread_info&lt;/code&gt; and the kernel stack for a process live in the same block of memory. On older 32-bit x86 systems that block was 8 KB. It saves space, and it lets the kernel find &lt;code&gt;thread_info&lt;/code&gt; quickly from the stack.&lt;/p&gt;

&lt;h2&gt;
  
  
  A struct is a design tool in the Linux kernel
&lt;/h2&gt;

&lt;p&gt;Put all of this together and a pattern shows up. The kernel decides what a thing is, writes one struct for it, and then makes one copy of that struct for every real thing in the machine. One &lt;code&gt;task_struct&lt;/code&gt; per program. One &lt;code&gt;file&lt;/code&gt; per open file. A device or a mounted disk gets its own struct too.&lt;/p&gt;

&lt;p&gt;That is a way of thinking, not just a C feature. When you learn to design a good struct, you are learning to model the real world in code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why spend a whole series on it?
&lt;/h2&gt;

&lt;p&gt;Because almost everything else in C is built on top of it. Every data structure you will meet later, from a simple list to a tree, is a struct with some extra pointers. If structs feel shaky, those topics will feel shaky too.&lt;/p&gt;

&lt;p&gt;And this is not only for classrooms. It is running inside your phone as you read this, and inside the server that sent you this page. The kernel developers did not need some fancy new tool. They used the plain C struct, very carefully, for decades.&lt;/p&gt;

&lt;p&gt;So I want to slow down here and get comfortable with structs first. Make sure you know when to use the dot and when to use the arrow. Know what it means for a struct to hold a pointer to another struct. Once that feels easy, a lot of C starts to make sense.&lt;/p&gt;

&lt;h2&gt;
  
  
  What comes next
&lt;/h2&gt;

&lt;p&gt;In the next few posts we will stay with the struct itself. We will look at how a struct sits in memory, why &lt;code&gt;sizeof&lt;/code&gt; sometimes gives a bigger number than you expect, and how structs go inside other structs. After that come unions, bitfields and function pointers inside structs, all with real examples from the Linux kernel.&lt;/p&gt;

&lt;p&gt;Once that ground is solid, we will start a separate series on linked lists, queues and trees.&lt;/p&gt;

&lt;p&gt;If you want to go through all of this step by step with exercises, our &lt;a href="https://www.jupiuslearning.com/courses/c-ds-a" rel="noopener noreferrer"&gt;C and Data Structures course&lt;/a&gt; at Jupius Learning covers it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://www.jupiuslearning.com/blog/the-humble-c-struct-that-still-runs-billions-of-machines/" rel="noopener noreferrer"&gt;Jupius Learning&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>c</category>
      <category>linux</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Why You Still Need the Basics When AI Writes the Code</title>
      <dc:creator>Vadher Nikunj</dc:creator>
      <pubDate>Mon, 09 Nov 2026 16:59:04 +0000</pubDate>
      <link>https://dev.to/vadher_nikunj_47e8c86d866/why-you-still-need-the-basics-when-ai-writes-the-code-2fed</link>
      <guid>https://dev.to/vadher_nikunj_47e8c86d866/why-you-still-need-the-basics-when-ai-writes-the-code-2fed</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was first published on &lt;a href="https://www.jupiuslearning.com/blog/why-you-still-need-the-basics-when-ai-writes-the-code/" rel="noopener noreferrer"&gt;Jupius Learning&lt;/a&gt;, where you can read it free with diagrams and practice exercises.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;AI can write code in seconds, but you still have to judge it. Here is why the basics matter more now, and how to learn them.&lt;/p&gt;

&lt;p&gt;Today you can type a sentence into an AI tool and get working code back in a few seconds. A login page, a sorting function, a whole small app. It feels like magic, and honestly it is very useful. I use it too.&lt;/p&gt;

&lt;p&gt;Getting code is easy now. Knowing if that code is any good is still hard. That is where the basics matter.&lt;/p&gt;

&lt;h2&gt;
  
  
  The code runs, so it must be right?
&lt;/h2&gt;

&lt;p&gt;Not really. Code can run and still be wrong. The real question is whether it does the right thing for your users, without being slow or unsafe.&lt;/p&gt;

&lt;p&gt;Say you ask for a function to find a user by email. The AI gives you a loop that checks every user one by one. It works fine with 50 users. With 500,000 users your app starts to crawl. If you know a little about data structures, you can spot the trade-off and consider a hash map or a database index. If you don't, you will just see a slow app and have no idea why.&lt;/p&gt;

&lt;p&gt;The AI did not make a mistake you can see. It made a choice you could not judge.&lt;/p&gt;

&lt;h2&gt;
  
  
  You are now the reviewer
&lt;/h2&gt;

&lt;p&gt;When AI writes the code, you still have to check it. Reading code you did not write can be hard. You need to understand its choices, not just its output.&lt;/p&gt;

&lt;p&gt;To review code you need to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What a variable, a loop and a function really do&lt;/li&gt;
&lt;li&gt;How data sits in memory and why that matters&lt;/li&gt;
&lt;li&gt;What a stack, a queue or a tree is good at&lt;/li&gt;
&lt;li&gt;How to read an error message instead of pasting it back and hoping&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without these, you can only say "it works" or "it doesn't work". With them, you can say why.&lt;/p&gt;

&lt;h2&gt;
  
  
  AI is a helping hand, and you decide how far it reaches
&lt;/h2&gt;

&lt;p&gt;Think of AI as a helping hand with a huge store of knowledge behind it. It has read more than any person ever could. But a hand does only what the person gives it to do.&lt;/p&gt;

&lt;p&gt;So you can only use AI as well as you understand the problem. If you don't know what to ask for, you ask for something small and vague, and you get something small and vague back. If you know the topic, you ask sharper questions, and you notice when the answer is off. The AI's knowledge is always big. Your knowledge is what limits how much of it you can actually use.&lt;/p&gt;

&lt;p&gt;Two people can open the same tool and get very different results. One says "make me a login". The other says "make a login with hashed passwords, a limit on wrong attempts and a session that expires". Same AI. The difference is what each person already knows.&lt;/p&gt;

&lt;p&gt;So if you want more from AI, the way to get it is to grow your own knowledge.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk09tf491v165i9ynu9z6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk09tf491v165i9ynu9z6.png" alt="Comparison of two prompts to the same AI: a short vague prompt gives a basic result, a detailed prompt gives a safer one" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AI is confident even when it is wrong
&lt;/h2&gt;

&lt;p&gt;This can surprise beginners. An AI tool can sound confident when it is wrong. It can invent a function that does not exist. It can use an old method that was removed years ago. It can leave a security hole without warning you.&lt;/p&gt;

&lt;p&gt;You still need to check the answer. The more you understand, the more likely you are to spot a problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Debugging is where basics pay off
&lt;/h2&gt;

&lt;p&gt;Sooner or later the code will break, and it will break in a way the AI cannot fix with one more prompt. Maybe the bug is in how two parts talk to each other. Maybe the data is not what you think it is.&lt;/p&gt;

&lt;p&gt;This is when people who know the fundamentals stay calm. They add a print line, check the value, trace the flow and find the problem. People who skipped the basics paste the error again, get a new guess, paste again, and go in circles for an hour.&lt;/p&gt;

&lt;h2&gt;
  
  
  Interviews still test the basics
&lt;/h2&gt;

&lt;p&gt;If you are learning to get a job, this matters a lot. Companies still ask you to explain how a hash table works, or when you would pick a queue over a stack. Some interviews even ask you to code without any AI tool. They want to know that you can think, not only that you can prompt.&lt;/p&gt;

&lt;h2&gt;
  
  
  So how should you use AI?
&lt;/h2&gt;

&lt;p&gt;I am not saying avoid it. That would be silly. Here is what works better:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Learn the basics first, even if it feels slow.&lt;/li&gt;
&lt;li&gt;Try a problem yourself before you ask the AI.&lt;/li&gt;
&lt;li&gt;When the AI gives you code, read every line. Ask it to explain the parts you don't get.&lt;/li&gt;
&lt;li&gt;Change something small and see what breaks. That is how you really learn.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Think of AI like a calculator. A calculator did not make maths useless. It just means you can skip the boring sums. But if you don't understand maths, you won't even know when the calculator gave you a silly answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;p&gt;AI has made writing code cheap. It has made understanding code more valuable. The people who do well in the next few years will not be the ones who type the best prompts. They will be the ones who know enough to tell good code from bad code.&lt;/p&gt;

&lt;p&gt;If you want guided practice, you can explore the &lt;a href="https://www.jupiuslearning.com/courses/c-ds-a" rel="noopener noreferrer"&gt;C and data structures course at Jupius Learning&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://www.jupiuslearning.com/blog/why-you-still-need-the-basics-when-ai-writes-the-code/" rel="noopener noreferrer"&gt;Jupius Learning&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>beginners</category>
      <category>programming</category>
      <category>career</category>
    </item>
    <item>
      <title>Linked Lists Explained with Real Examples from the Linux Kernel</title>
      <dc:creator>Vadher Nikunj</dc:creator>
      <pubDate>Fri, 06 Nov 2026 14:28:05 +0000</pubDate>
      <link>https://dev.to/vadher_nikunj_47e8c86d866/linked-lists-explained-with-real-examples-from-the-linux-kernel-l8h</link>
      <guid>https://dev.to/vadher_nikunj_47e8c86d866/linked-lists-explained-with-real-examples-from-the-linux-kernel-l8h</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was first published on &lt;a href="https://www.jupiuslearning.com/blog/linked-lists-explained-with-real-examples-from-the-linux-kernel/" rel="noopener noreferrer"&gt;Jupius Learning&lt;/a&gt;, where you can read it free with diagrams and practice exercises.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F93jw0xlxwkmyvnolu323.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F93jw0xlxwkmyvnolu323.png" alt="Linked Lists: four data and next nodes joined by arrows, ending at NULL" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the last post on queues, linked lists kept showing up. Wait queues were built from one, and the runqueue was 140 of them. This time we look at the linked list properly. It is one of the most used structures in the Linux kernel.&lt;/p&gt;

&lt;p&gt;Let's write a small one in C first, then see how the kernel does it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a linked list?
&lt;/h2&gt;

&lt;p&gt;Think of a treasure hunt. Each clue tells you where the next one is. You can't jump to clue five. You start at the first and follow the chain.&lt;/p&gt;

&lt;p&gt;A linked list works the same way. It is a chain of nodes. Each node holds some data and a pointer to the next node. The last one points to &lt;code&gt;NULL&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are two common kinds:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In a &lt;strong&gt;singly linked list&lt;/strong&gt;, each node points only to the next one&lt;/li&gt;
&lt;li&gt;In a &lt;strong&gt;doubly linked list&lt;/strong&gt;, each node points to the next one and the one before it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The good part is that adding or removing a node is cheap once you are at the right spot. You change a pointer or two and nothing moves. In an array, adding in the middle means shifting everything after it.&lt;/p&gt;

&lt;p&gt;The bad part is search. There is no index, so to find 42 you walk from the start and check each node.&lt;/p&gt;

&lt;h2&gt;
  
  
  A tiny linked list in C
&lt;/h2&gt;

&lt;p&gt;Here is a small singly linked list of integers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="cm"&gt;/* NULL means the list is empty */&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;push_front&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="cm"&gt;/* out of memory, so we stop */&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="cm"&gt;/* new node points to the old first node */&lt;/span&gt;
    &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="cm"&gt;/* and becomes the new first node */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;delete_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;node&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;pp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pp&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;pp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="cm"&gt;/* walk until we find x */&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gone&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="cm"&gt;/* skip over the node */&lt;/span&gt;
        &lt;span class="nb"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gone&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;print_list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%d -&amp;gt; "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"NULL\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding at the front takes the same short time no matter how long the list is. We make one node, change two pointers, and never look at the rest. That is why it is O(1).&lt;/p&gt;

&lt;p&gt;Deleting by value has to walk first, so it gets slower as the list grows. The pointer to a pointer, &lt;code&gt;pp&lt;/code&gt;, lets the same code remove the first node or a middle one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Doubly linked lists and the kernel's list_head
&lt;/h2&gt;

&lt;p&gt;A doubly linked list adds a &lt;code&gt;prev&lt;/code&gt; pointer to each node. It costs a bit more memory, but you can walk both ways and remove a node without hunting for the one before it.&lt;/p&gt;

&lt;p&gt;The Linux kernel uses doubly linked lists everywhere, but it does not write a new list for each kind of data. It has one generic struct called &lt;code&gt;list_head&lt;/code&gt; with only two fields, &lt;code&gt;next&lt;/code&gt; and &lt;code&gt;prev&lt;/code&gt;. No data at all.&lt;/p&gt;

&lt;p&gt;The trick is that you put a &lt;code&gt;list_head&lt;/code&gt; inside your own struct. The list does not hold your data. Your data holds the list.&lt;/p&gt;

&lt;p&gt;A few things follow from this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The lists are circular. An empty list is just a head whose &lt;code&gt;next&lt;/code&gt; and &lt;code&gt;prev&lt;/code&gt; both point back to itself.&lt;/li&gt;
&lt;li&gt;The pointers point to other &lt;code&gt;list_head&lt;/code&gt; fields, not to the start of the bigger struct. So when you are at a node, you need a way to get back to the struct around it.&lt;/li&gt;
&lt;li&gt;The kernel helper &lt;code&gt;list_entry&lt;/code&gt; does that. It knows how far the &lt;code&gt;list_head&lt;/code&gt; field sits from the start of the struct and subtracts that distance from the pointer. This idea is usually called "container of".&lt;/li&gt;
&lt;li&gt;Because every list looks the same, the kernel needs only one set of helpers to add, delete, and walk. Every subsystem reuses them instead of writing its own.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7ueji65i8zl2h9ydf6ul.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7ueji65i8zl2h9ydf6ul.png" alt="Circular doubly linked list with list_head inside task_struct and list_entry" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the Linux kernel uses linked lists
&lt;/h2&gt;

&lt;p&gt;The examples below are from the Linux 2.6 kernel. Newer kernels have changed some details, but the ideas are still there.&lt;/p&gt;

&lt;h3&gt;
  
  
  The process list
&lt;/h3&gt;

&lt;p&gt;Every process is linked into one big list through a &lt;code&gt;list_head&lt;/code&gt; field called &lt;code&gt;tasks&lt;/code&gt; in its descriptor. The head is &lt;code&gt;init_task&lt;/code&gt;, the descriptor of the very first process.&lt;/p&gt;

&lt;p&gt;To visit every process, the kernel has a macro called &lt;code&gt;for_each_process&lt;/code&gt;. It starts at &lt;code&gt;init_task&lt;/code&gt;, follows &lt;code&gt;next&lt;/code&gt;, and stops when it comes back around. That stop rule works because the list is circular.&lt;/p&gt;

&lt;h3&gt;
  
  
  The runqueue
&lt;/h3&gt;

&lt;p&gt;In Linux 2.6 each CPU has 140 lists, one per priority, and each is a plain &lt;code&gt;list_head&lt;/code&gt; list. When a process becomes ready, &lt;code&gt;enqueue_task()&lt;/code&gt; adds it to the tail of the list for its priority. To pick the next process, the scheduler finds the first non-empty list and takes the node at its front, using &lt;code&gt;list_entry&lt;/code&gt; to get back the process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wait queues
&lt;/h3&gt;

&lt;p&gt;Wait queues are also built on &lt;code&gt;list_head&lt;/code&gt;. Each sleeper is a small entry in the list, and the head carries a lock. Exclusive sleepers go at the tail so the kernel can wake just one. Adding at the tail is cheap because the head's &lt;code&gt;prev&lt;/code&gt; points straight at the last entry.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hash tables and hlist
&lt;/h3&gt;

&lt;p&gt;Hash tables have many buckets, and most are empty or short. A two-pointer head for each would waste memory. So the kernel has a lighter &lt;code&gt;hlist&lt;/code&gt;. Its head, &lt;code&gt;hlist_head&lt;/code&gt;, is a single pointer, and the list is not circular. You lose quick access to the tail, which a bucket rarely needs. The PID lookup table uses it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Everyday uses
&lt;/h2&gt;

&lt;p&gt;You meet linked lists outside the kernel too:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A music playlist where "next" and "previous" move you along a chain&lt;/li&gt;
&lt;li&gt;Undo history in an editor, where each change points back to the one before&lt;/li&gt;
&lt;li&gt;Browser back and forward history, which behaves like a doubly linked chain&lt;/li&gt;
&lt;li&gt;Free block lists in memory allocators, where each free block points to the next free one&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  List or array?
&lt;/h2&gt;

&lt;p&gt;Use an array when you read by position a lot, like "give me item 500". An array gets there in one step, and its items sit together in memory, which makes walking fast on real hardware.&lt;/p&gt;

&lt;p&gt;Use a linked list when you add and remove items often, especially in the middle or at the front, and you already have a pointer to the spot. A list grows one node at a time, so you never guess a size up front. The price is extra memory for pointers and slow search.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it yourself
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Add a &lt;code&gt;push_back()&lt;/code&gt; function to the code above. Then keep a &lt;code&gt;tail&lt;/code&gt; pointer so it runs in O(1).&lt;/li&gt;
&lt;li&gt;Write a function that reverses the list in place, by changing pointers only.&lt;/li&gt;
&lt;li&gt;Change the list to a doubly linked one, and write a delete that takes a node pointer instead of a value.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What comes next
&lt;/h2&gt;

&lt;p&gt;Next in the series is the hash table. The &lt;code&gt;hlist&lt;/code&gt; we just saw will make more sense once we look at how buckets work.&lt;/p&gt;

&lt;p&gt;If you want to get better at this, write a linked list yourself, break it on purpose, draw the boxes and arrows on paper, and step through it in a debugger. That sticks more than reading alone. Our &lt;a href="https://www.jupiuslearning.com/courses/c-ds-a/" rel="noopener noreferrer"&gt;C and data structures course at Jupius Learning&lt;/a&gt; walks through the same structures one step at a time if you want guided practice.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>c</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to Learn Zephyr RTOS on Real Hardware: A 24 Week Plan</title>
      <dc:creator>Vadher Nikunj</dc:creator>
      <pubDate>Wed, 04 Nov 2026 05:54:48 +0000</pubDate>
      <link>https://dev.to/vadher_nikunj_47e8c86d866/how-to-learn-zephyr-rtos-on-real-hardware-a-24-week-plan-1bc2</link>
      <guid>https://dev.to/vadher_nikunj_47e8c86d866/how-to-learn-zephyr-rtos-on-real-hardware-a-24-week-plan-1bc2</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was first published on &lt;a href="https://www.jupiuslearning.com/blog/learning-zephyr-rtos-on-real-hardware-how-our-24-week-course-works/" rel="noopener noreferrer"&gt;Jupius Learning&lt;/a&gt;, where you can read it free with diagrams and practice exercises.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the last post we looked at what Zephyr is and wrote a small program with two threads and a message queue. A common next question is: how do I actually learn this properly? Zephyr is big. There are hundreds of boards, a build tool, Devicetree, Kconfig and a long list of subsystems. It is easy to jump around and end up knowing a little of everything and not much of anything.&lt;/p&gt;

&lt;p&gt;So in this post we share a plan for learning Zephyr on a real board over about 24 weeks. You can follow it on your own with any board Zephyr supports. We use the ESP32-S3-DevKitC-1 with Zephyr 4.1 and the &lt;code&gt;west&lt;/code&gt; tool.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to know in C first
&lt;/h2&gt;

&lt;p&gt;Zephyr code is full of pointers, structs and macros. If those still feel shaky, fix that first. You will not get far reading Zephyr samples otherwise.&lt;/p&gt;

&lt;p&gt;Before you start, you should be able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pass a pointer to a struct into a function and change it there&lt;/li&gt;
&lt;li&gt;read a macro and work out what it expands to&lt;/li&gt;
&lt;li&gt;use &lt;code&gt;static&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;volatile&lt;/code&gt; and say why each one is there&lt;/li&gt;
&lt;li&gt;split code across &lt;code&gt;.c&lt;/code&gt; and &lt;code&gt;.h&lt;/code&gt; files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some earlier embedded work, like blinking an LED with a vendor SDK, helps. It is not a must.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 1: the build system and kernel objects
&lt;/h2&gt;

&lt;p&gt;Start with the tools. Install Zephyr and &lt;code&gt;west&lt;/code&gt;, build a sample, flash it, and print Hello World over the serial port. This sounds small, but many people lose days here later because they skipped it. Learn what &lt;code&gt;west build&lt;/code&gt; and &lt;code&gt;west flash&lt;/code&gt; do, and where the build output goes.&lt;/p&gt;

&lt;p&gt;Then go through the kernel objects one at a time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;threads and sleep&lt;/li&gt;
&lt;li&gt;semaphores&lt;/li&gt;
&lt;li&gt;mutexes and condition variables&lt;/li&gt;
&lt;li&gt;message queues&lt;/li&gt;
&lt;li&gt;software timers and work queues&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For each one, write a tiny program that only uses that object. Make two threads fight over a shared counter, then fix it with a mutex. Make one thread wait on a semaphore that another gives. These small tests teach you more than reading the API docs.&lt;/p&gt;

&lt;p&gt;In our plan this stage covers Units 1 to 8, roughly the first third.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 2: memory, Devicetree and real hardware
&lt;/h2&gt;

&lt;p&gt;Next comes memory. Learn how Zephyr gives you heaps and fixed blocks, and why many embedded programs avoid &lt;code&gt;malloc&lt;/code&gt; at run time.&lt;/p&gt;

&lt;p&gt;Then spend real time on Devicetree and Kconfig. This is where most beginners get stuck. Devicetree describes the hardware: which pins, which buses, which sensors. Kconfig turns features on and off. Learn to write an overlay file for your board and use aliases so your code does not hard code pin numbers.&lt;/p&gt;

&lt;p&gt;Once that clicks, drive real parts: GPIO, PWM, an I2C sensor, and flash storage (NVS) for something simple like a boot counter that survives a reset. In our plan this is Units 9 to 15.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbzxoo7u0phnf2of9v0vf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbzxoo7u0phnf2of9v0vf.png" alt="Timeline of the 24 week Zephyr plan in three stages, Units 1-8, 9-15 and 16-24" width="800" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 3: services, networking and updates
&lt;/h2&gt;

&lt;p&gt;The last stage is about the things real firmware needs around the core logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;logging modules, so you can turn debug output up or down per part of the code&lt;/li&gt;
&lt;li&gt;the Zephyr shell, so you can type commands to your board over serial&lt;/li&gt;
&lt;li&gt;zbus, for passing data between parts of your program&lt;/li&gt;
&lt;li&gt;Wi-Fi as a station, and a small web page from a soft access point&lt;/li&gt;
&lt;li&gt;over the air updates (OTA)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We also drive a stepper motor in this stage, because it forces you to care about timing.&lt;/p&gt;

&lt;p&gt;End with a capstone. Do not add a new topic. Take three things you already built and make them run together in one firmware. This is harder than it sounds. You have to bring things up in the right order and stop one part from breaking another. That is very close to real product work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a real board beats a simulator
&lt;/h2&gt;

&lt;p&gt;Simulators and copy paste tutorials are fine for a first look. But they hide the problems you will meet on the job: wrong wiring, a sensor at the wrong I2C address, a stack that is too small, a thread that never wakes up.&lt;/p&gt;

&lt;p&gt;A simple habit helps a lot. For every lab, make your firmware print one short, fixed line over serial when the task really worked. For example &lt;code&gt;SEM:OK&lt;/code&gt; for a semaphore lab, &lt;code&gt;DTS:OK&lt;/code&gt; for a Devicetree lab, or &lt;code&gt;OTA:OK&lt;/code&gt; after an update. If the line does not show up, the lab is not done. No guessing, no "it probably works". This also makes it easy to check your work with a script later.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to check your own progress
&lt;/h2&gt;

&lt;p&gt;Write down a few outcomes for yourself, one per area. For example: "I can use kernel objects to share data safely between threads" or "I can add a new I2C sensor using only an overlay and a driver". Then, at the end of each stage, ask if you can do each one from a blank project without looking at old code. If not, go back before moving on.&lt;/p&gt;

&lt;p&gt;Short quizzes after each topic help too. They show gaps fast.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;skipping C basics and fighting syntax instead of learning Zephyr&lt;/li&gt;
&lt;li&gt;jumping to Wi-Fi or OTA before understanding threads and queues&lt;/li&gt;
&lt;li&gt;copying a Devicetree overlay without knowing what each line means&lt;/li&gt;
&lt;li&gt;printing from many threads with no logging setup, and then not being able to read the output&lt;/li&gt;
&lt;li&gt;testing features one by one but never together&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The plan we use
&lt;/h2&gt;

&lt;p&gt;This is the plan we follow in our 24 week course, Zephyr RTOS on ESP32-S3. If you want to see the full list of units and outcomes, it is on the &lt;a href="https://www.jupiuslearning.com/courses/zep-rtos-a/" rel="noopener noreferrer"&gt;course page&lt;/a&gt;. Either way, the order above works well on your own too.&lt;/p&gt;

</description>
      <category>zephyr</category>
      <category>rtos</category>
      <category>esp32</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Zephyr RTOS Explained: A Simple Start on the ESP32-S3</title>
      <dc:creator>Vadher Nikunj</dc:creator>
      <pubDate>Wed, 04 Nov 2026 05:43:16 +0000</pubDate>
      <link>https://dev.to/vadher_nikunj_47e8c86d866/zephyr-rtos-explained-a-simple-start-on-the-esp32-s3-26dn</link>
      <guid>https://dev.to/vadher_nikunj_47e8c86d866/zephyr-rtos-explained-a-simple-start-on-the-esp32-s3-26dn</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was first published on &lt;a href="https://www.jupiuslearning.com/blog/zephyr-rtos-explained-a-simple-start-on-the-esp32-s3/" rel="noopener noreferrer"&gt;Jupius Learning&lt;/a&gt;, where you can read it free with diagrams and practice exercises.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So far in this series we have looked at stacks and queues inside the Linux kernel. This time we move to a much smaller computer: a microcontroller. And we look at Zephyr, a real-time operating system (RTOS) that runs on boards like the ESP32-S3.&lt;/p&gt;

&lt;p&gt;If you have only written Arduino sketches so far, this post is for you. We will end with a small program where two threads talk to each other.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an RTOS?
&lt;/h2&gt;

&lt;p&gt;Most people start embedded work with an Arduino style program. There is a &lt;code&gt;setup()&lt;/code&gt; that runs once and a &lt;code&gt;loop()&lt;/code&gt; that runs again and again. Inside the loop you read a sensor, blink an LED, check a button, and print something. One thing after another, forever.&lt;/p&gt;

&lt;p&gt;That works fine for small jobs. The trouble starts when the jobs grow. Say one task needs to read a sensor every 10 ms, another has to send data over Wi-Fi, and a third is waiting for a button press. If the Wi-Fi part takes 200 ms, the sensor reads are late. The loop fills up with timing checks and flags.&lt;/p&gt;

&lt;p&gt;An RTOS takes a different approach. Think of an office with one desk and several workers. Only one worker can sit at the desk at a time. A manager keeps a timetable and decides who sits next. When a worker is waiting for a phone call, they get up and let someone else use the desk. When an urgent job comes in, the manager lets that worker sit down first.&lt;/p&gt;

&lt;p&gt;The desk is the CPU. The workers are threads. The manager is the scheduler.&lt;/p&gt;

&lt;p&gt;"Real-time" does not mean fast. It means the timing is predictable. A high priority thread will run when it is supposed to, not "whenever the loop gets around to it".&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Zephyr?
&lt;/h2&gt;

&lt;p&gt;Zephyr is an open source RTOS run by the Linux Foundation. It supports hundreds of boards from many chip makers, including the ESP32 family, STM32, Nordic and others.&lt;/p&gt;

&lt;p&gt;Here is why I think people pick it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is free and open source, with a big community behind it&lt;/li&gt;
&lt;li&gt;Your code is not tied to one chip maker&lt;/li&gt;
&lt;li&gt;Hardware is described in Devicetree files, so moving to another board is easier&lt;/li&gt;
&lt;li&gt;It comes with drivers, logging, a shell, and networking already built in&lt;/li&gt;
&lt;li&gt;One tool, &lt;code&gt;west&lt;/code&gt;, gets the code, builds and flashes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The cost is a steeper start than Arduino. Once the ideas click, bigger projects get much easier to manage.&lt;/p&gt;

&lt;h2&gt;
  
  
  The building blocks
&lt;/h2&gt;

&lt;p&gt;Zephyr gives you a small set of kernel objects. These are the ones you will use most.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Threads.&lt;/strong&gt; A thread is a function with its own stack and a priority.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sleep.&lt;/strong&gt; When a thread calls &lt;code&gt;k_msleep(100)&lt;/code&gt;, it gives up the CPU for 100 ms. This is the big change from Arduino's &lt;code&gt;delay()&lt;/code&gt;, which just blocks everything.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Semaphores.&lt;/strong&gt; A semaphore is a counter used for signals. One thread gives it, another thread waits on it. A common use is an interrupt saying "data is ready" to a thread that is sleeping until then.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mutexes.&lt;/strong&gt; A mutex is a lock. If two threads share a buffer or a UART, a mutex makes sure only one of them uses it at a time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Message queues.&lt;/strong&gt; A message queue (&lt;code&gt;k_msgq&lt;/code&gt;) passes fixed size messages from one thread to another. It is the same FIFO idea from the last post, but safe to use between threads.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Timers.&lt;/strong&gt; A timer calls a function after a delay, once or again and again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Workqueues.&lt;/strong&gt; A workqueue lets you hand a job to a background thread. This is useful when an interrupt needs some slow work done but should not do it itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  A small example
&lt;/h2&gt;

&lt;p&gt;Here is a program with two threads. The producer makes a number every 500 ms and puts it in a message queue. The consumer waits for numbers and prints them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;zephyr/kernel.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;zephyr/sys/printk.h&amp;gt;&lt;/span&gt;

&lt;span class="cm"&gt;/* a queue that holds up to 10 ints */&lt;/span&gt;
&lt;span class="no"&gt;K_MSGQ_DEFINE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num_q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;k_msgq_put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;num_q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;K_NO_WAIT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"queue full, dropped %d\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nf"&gt;k_msleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/* sleep here until a number arrives */&lt;/span&gt;
        &lt;span class="nf"&gt;k_msgq_get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;num_q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;K_FOREVER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nf"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"got %d\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* name, stack size, entry, 3 args, priority, options, start delay */&lt;/span&gt;
&lt;span class="no"&gt;K_THREAD_DEFINE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prod_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;K_THREAD_DEFINE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cons_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few things to notice. There is no &lt;code&gt;main()&lt;/code&gt; loop doing the work. &lt;code&gt;K_THREAD_DEFINE&lt;/code&gt; creates each thread when the system starts. &lt;code&gt;K_MSGQ_DEFINE&lt;/code&gt; sets up the queue with room for 10 messages, each the size of an &lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The producer uses &lt;code&gt;K_NO_WAIT&lt;/code&gt;, so if the queue is ever full it does not get stuck. It just prints a note and moves on. The consumer uses &lt;code&gt;K_FOREVER&lt;/code&gt;, so it sleeps until there is something to read. While it sleeps, it uses no CPU at all.&lt;/p&gt;

&lt;p&gt;Neither thread needs to know when the other runs. That is the whole point of an RTOS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbu3mvmznmdymfqanh7rl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbu3mvmznmdymfqanh7rl.png" alt="Diagram of a producer thread putting numbers into a k_msgq message queue and a consumer thread taking them out" width="800" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Devicetree in a few words
&lt;/h2&gt;

&lt;p&gt;On Arduino you often write something like &lt;code&gt;pinMode(5, OUTPUT)&lt;/code&gt; right in your code. If you move to another board where the LED is on pin 12, you have to change the code.&lt;/p&gt;

&lt;p&gt;Zephyr puts hardware details in a separate file called a Devicetree. It says things like "this board has an LED on this pin" or "there is a sensor on this I2C bus at this address". Your C code asks for "the LED" by name, and the build fills in the real pin. To change the wiring, you change a small overlay file, not your program. A second file type, Kconfig, turns features like logging or the shell on and off.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you can build
&lt;/h2&gt;

&lt;p&gt;Once you know these pieces, the ESP32-S3 can do quite a lot under Zephyr. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Blinking LEDs and dimming them with PWM&lt;/li&gt;
&lt;li&gt;Reading a sensor over I2C&lt;/li&gt;
&lt;li&gt;Saving a value, like a boot count, in flash with NVS so it survives a reset&lt;/li&gt;
&lt;li&gt;A command shell over UART where you type commands to your board&lt;/li&gt;
&lt;li&gt;Driving a stepper motor through an A4988 or TMC2209 driver&lt;/li&gt;
&lt;li&gt;Joining a Wi-Fi network and downloading a firmware update over the air (OTA)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it yourself
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Change the producer to sleep for 100 ms and the consumer to sleep 1 second after each print. Watch the "queue full" message show up, and think about why.&lt;/li&gt;
&lt;li&gt;Add a second producer thread that sends negative numbers. Does the consumer still get every message in order of arrival?&lt;/li&gt;
&lt;li&gt;Replace the int with a small struct holding a number and a timestamp from &lt;code&gt;k_uptime_get()&lt;/code&gt;, and print both in the consumer.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Where to go from here
&lt;/h2&gt;

&lt;p&gt;If you want to go further, we have a 24 week course, &lt;a href="https://www.jupiuslearning.com/courses/zep-rtos-a/" rel="noopener noreferrer"&gt;Zephyr RTOS on ESP32-S3&lt;/a&gt;. It starts with setting up Zephyr 4.1 and &lt;code&gt;west&lt;/code&gt; on the ESP32-S3-DevKitC-1, works through threads, semaphores, message queues, timers and workqueues, and then moves on to Devicetree, GPIO, I2C, the shell, motor control, Wi-Fi and OTA, ending in a capstone project.&lt;/p&gt;

&lt;p&gt;One thing to know first: Zephyr code is plain C, with lots of pointers and structs. Strong C basics come first. If you have done some embedded work before, that helps too, but it is not a must.&lt;/p&gt;

</description>
      <category>zephyr</category>
      <category>rtos</category>
      <category>esp32</category>
      <category>embedded</category>
    </item>
    <item>
      <title>Queues Explained with Real Examples from the Linux Kernel</title>
      <dc:creator>Vadher Nikunj</dc:creator>
      <pubDate>Mon, 02 Nov 2026 05:42:39 +0000</pubDate>
      <link>https://dev.to/vadher_nikunj_47e8c86d866/queues-explained-with-real-examples-from-the-linux-kernel-2750</link>
      <guid>https://dev.to/vadher_nikunj_47e8c86d866/queues-explained-with-real-examples-from-the-linux-kernel-2750</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was first published on &lt;a href="https://www.jupiuslearning.com/blog/queues-explained-with-real-examples-from-the-linux-kernel/" rel="noopener noreferrer"&gt;Jupius Learning&lt;/a&gt;, where you can read it free with diagrams and practice exercises.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the last post we looked at the stack, where the last thing in is the first thing out. This time we look at its opposite, the queue. It is just as simple, and the Linux kernel leans on it all the time.&lt;/p&gt;

&lt;p&gt;Let's start with the idea, write a small one in C, and then see where the kernel uses it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a queue?
&lt;/h2&gt;

&lt;p&gt;Think of the line at a ticket counter. A new person joins at the back. The person at the front gets served and leaves. Nobody jumps ahead. The first person who came is the first person who gets a ticket.&lt;/p&gt;

&lt;p&gt;That rule is called first in, first out, or FIFO. A queue has two main actions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Enqueue&lt;/strong&gt; adds a new item at the back&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dequeue&lt;/strong&gt; takes the item at the front&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most queues also let you check if they are empty or full.&lt;/p&gt;

&lt;p&gt;The big difference from a stack is fairness. A queue always serves the item that has waited the longest.&lt;/p&gt;

&lt;h2&gt;
  
  
  A tiny queue in C
&lt;/h2&gt;

&lt;p&gt;The easy way to build a queue is with an array and two positions. One position, &lt;code&gt;head&lt;/code&gt;, marks the front. The other, &lt;code&gt;tail&lt;/code&gt;, marks where the next item goes. But there is a catch. If you only ever move forward, you soon reach the end of the array, even though the front part is now empty.&lt;/p&gt;

&lt;p&gt;The fix is to wrap around. When &lt;code&gt;tail&lt;/code&gt; or &lt;code&gt;head&lt;/code&gt; reaches the end, it goes back to 0. This is called a circular buffer, or ring buffer. Picture the array bent into a circle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define MAX 8&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="cm"&gt;/* where the next item is taken from */&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="cm"&gt;/* where the next item goes in */&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="cm"&gt;/* how many items are waiting */&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="cm"&gt;/* full, so we stop */&lt;/span&gt;
    &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="no"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="cm"&gt;/* wrap around to the start */&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;dequeue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="cm"&gt;/* empty, nothing to give */&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="no"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;% MAX&lt;/code&gt; part does the wrap. If &lt;code&gt;tail&lt;/code&gt; is 7 and &lt;code&gt;MAX&lt;/code&gt; is 8, then &lt;code&gt;(7 + 1) % 8&lt;/code&gt; is 0, so the next item goes back at the start. The &lt;code&gt;count&lt;/code&gt; tells us if the queue is full or empty, because &lt;code&gt;head == tail&lt;/code&gt; can mean either one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0m0d2vur5ujn3op1q2zz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0m0d2vur5ujn3op1q2zz.png" alt="Diagram of a circular queue with 8 slots, head at slot 2 and tail at slot 5" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Like the stack, both actions take the same short time no matter how many items are waiting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the Linux kernel uses queues
&lt;/h2&gt;

&lt;p&gt;The examples below are from the Linux 2.6 kernel. Newer kernels have changed some details, but the ideas are still there.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wait queues for sleeping processes
&lt;/h3&gt;

&lt;p&gt;A process often has to wait for something. It may be waiting for a disk read to finish, or for a resource to be free. There is no point keeping the CPU busy while it waits. So the kernel puts it to sleep on a &lt;strong&gt;wait queue&lt;/strong&gt; and runs something else.&lt;/p&gt;

&lt;p&gt;Each wait queue belongs to one kind of event. When the event happens, the kernel wakes the processes sleeping on it. Inside, a wait queue is a doubly linked list. Each item points to a sleeping process. The head of the list also has a lock, because interrupt handlers and normal kernel code can both change it.&lt;/p&gt;

&lt;p&gt;There is a nice detail here. Sometimes many processes wait for a resource that only one of them can use. Waking all of them would be a waste, since only one would win and the rest would go back to sleep. This is called the "thundering herd" problem. To avoid it, such processes are marked as exclusive and added at the back of the list, and the kernel wakes only one of them at a time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Read/write semaphores in strict FIFO order
&lt;/h3&gt;

&lt;p&gt;A semaphore is a kind of lock. When a process can't get it, the process sleeps on the semaphore's wait queue. For read/write semaphores, the kernel handles the waiting processes in strict FIFO order. Each new waiter joins at the back. When the lock is released, the kernel looks at the process at the front first. That is a ticket counter line, written in kernel code.&lt;/p&gt;

&lt;h3&gt;
  
  
  The runqueue
&lt;/h3&gt;

&lt;p&gt;Processes that are ready to run sit on the &lt;strong&gt;runqueue&lt;/strong&gt;. Older versions kept one big list and had to scan all of it to pick the best process. Linux 2.6 split it into 140 lists, one for each priority level, and each CPU got its own set.&lt;/p&gt;

&lt;p&gt;When a process becomes ready, the function &lt;code&gt;enqueue_task()&lt;/code&gt; adds it to the tail of the list for its priority. The kernel also has &lt;code&gt;dequeue_task()&lt;/code&gt; to take it out. So within one priority level, processes line up and take turns. For round robin real-time processes, the one that gets the CPU is moved to the end of its list, so others of the same priority get a fair share.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pipes as a circular buffer
&lt;/h3&gt;

&lt;p&gt;When you type &lt;code&gt;ls | grep txt&lt;/code&gt;, the two programs talk through a pipe. From Linux 2.6.11, each pipe uses 16 buffers, and the kernel treats them as one circular buffer. Writers keep adding data. Readers keep taking it out from the front. To find the first empty buffer, the kernel adds the current buffer index and the number of full buffers, modulo 16. That is the same &lt;code&gt;% MAX&lt;/code&gt; trick from our code above.&lt;/p&gt;

&lt;h2&gt;
  
  
  Queue or stack?
&lt;/h2&gt;

&lt;p&gt;How do you pick? Ask one question: who should be served next?&lt;/p&gt;

&lt;p&gt;If the newest item should go first, use a stack. Function calls and undo work that way.&lt;/p&gt;

&lt;p&gt;If the oldest item should go first, use a queue. This is what you want when things arrive over time and each one should get its turn, like print jobs or network packets. It is also the base of breadth first search in graphs, where you visit nodes level by level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it yourself
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Add a &lt;code&gt;peek()&lt;/code&gt; function to the code above that returns the front item without removing it.&lt;/li&gt;
&lt;li&gt;Change the queue to use a linked list instead of an array, so it has no fixed size.&lt;/li&gt;
&lt;li&gt;Build a queue using two stacks. It is a common interview question.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What comes next
&lt;/h2&gt;

&lt;p&gt;Next in the series is the linked list, which we have already met twice here, inside wait queues and the runqueue. If you want to write these structures yourself and see them run, our C and data structures course at &lt;a href="https://www.jupiuslearning.com/courses/c-ds-a/" rel="noopener noreferrer"&gt;/courses/c-ds-a/&lt;/a&gt; walks through them step by step.&lt;/p&gt;




&lt;p&gt;I first published this on the &lt;a href="https://www.jupiuslearning.com/blog/queues-explained-with-real-examples-from-the-linux-kernel/" rel="noopener noreferrer"&gt;Jupius Learning blog&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>datastructures</category>
      <category>c</category>
      <category>linux</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Stacks Explained with Real Examples from the Linux Kernel</title>
      <dc:creator>Vadher Nikunj</dc:creator>
      <pubDate>Mon, 02 Nov 2026 05:31:02 +0000</pubDate>
      <link>https://dev.to/vadher_nikunj_47e8c86d866/stacks-explained-with-real-examples-from-the-linux-kernel-o2n</link>
      <guid>https://dev.to/vadher_nikunj_47e8c86d866/stacks-explained-with-real-examples-from-the-linux-kernel-o2n</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was first published on &lt;a href="https://www.jupiuslearning.com/blog/stacks-explained-with-real-examples-from-the-linux-kernel/" rel="noopener noreferrer"&gt;Jupius Learning&lt;/a&gt;, where you can read it free with diagrams and practice exercises.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A stack is one of the simplest data structures there is. You can learn the idea in one minute. But it is also one of the most useful, and it sits inside every program you have ever run, including the Linux kernel.&lt;/p&gt;

&lt;p&gt;Let's start with the idea, then look at where the kernel uses it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a stack?
&lt;/h2&gt;

&lt;p&gt;Think of a pile of plates. You put a new plate on top. When you need one, you take the top plate. You never pull one from the middle. The last plate you put on is the first one you take off.&lt;/p&gt;

&lt;p&gt;That rule has a name: last in, first out, or LIFO. A stack has only two main actions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Push&lt;/strong&gt; puts a new item on top&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pop&lt;/strong&gt; takes the top item off&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most stacks also let you peek at the top item without removing it, and check if the stack is empty.&lt;/p&gt;

&lt;h2&gt;
  
  
  A tiny stack in C
&lt;/h2&gt;

&lt;p&gt;Here is a stack of integers using a plain array. It is small, but it shows the whole idea.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define MAX 100&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="cm"&gt;/* -1 means the stack is empty */&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;MAX&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="cm"&gt;/* full, so we stop */&lt;/span&gt;
    &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="cm"&gt;/* empty, nothing to give */&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both actions are quick. They do not depend on how many items are in the stack. That is why stacks are used in places where speed matters a lot.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the Linux kernel uses stacks
&lt;/h2&gt;

&lt;p&gt;Every process on Linux gets its own stack inside the kernel. It is separate from the stack your program uses in normal mode. In the Linux 2.6 days on x86, this kernel stack lived in a fixed area of 8 KB, which is two memory pages. The small data block that describes the process sat at the start of that area, and the stack started at the far end and grew backwards toward it.&lt;/p&gt;

&lt;p&gt;Notice the detail here. The stack does not grow forward. It starts at the high end and moves down as you push. A register called &lt;code&gt;esp&lt;/code&gt; always points to the current top. When the stack is empty, it points just past the end of the area. This is the same thing as &lt;code&gt;top = -1&lt;/code&gt; in our small example, only done at machine level.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fj3k9m2rzbdncdba0bztc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fj3k9m2rzbdncdba0bztc.png" alt="Diagram of a Linux kernel stack growing downward toward the process info block, with esp at the top" width="800" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Function calls
&lt;/h3&gt;

&lt;p&gt;When one function calls another, the CPU pushes the return address and the local variables on the stack. When the function ends, they are popped, and the program goes back to exactly where it left off. If function A calls B, and B calls C, then C must finish first, then B, then A. That is LIFO again. You never have to think about it, because the stack does it for you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interrupts
&lt;/h3&gt;

&lt;p&gt;Now the more interesting case. When a device such as a keyboard or a network card needs attention, the CPU stops what it is doing and runs an interrupt handler. The first thing the kernel does is save the CPU registers on the kernel stack. The last thing it does is restore them, so the interrupted work can carry on as if nothing happened.&lt;/p&gt;

&lt;p&gt;Interrupts can also be nested. One handler can be interrupted by another one, and that one by another. How does the kernel remember where to go back to? It just pushes each one on the stack. The newest handler finishes first, then it returns to the one before it, and so on, until the CPU is back to the original work. A stack is the natural fit, because the last thing that started is always the first thing that must finish.&lt;/p&gt;

&lt;h3&gt;
  
  
  Switching between processes
&lt;/h3&gt;

&lt;p&gt;When the kernel stops one process and starts another, it has to save the state of the first one so it can be continued later. Part of that state is stored in the process's own kernel stack. Because each process has its own stack, the kernel can switch to another process by changing the stack pointer. In the code, the switch is really a swap from one stack to another.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the stack is small
&lt;/h2&gt;

&lt;p&gt;You may wonder why the kernel keeps this stack so small. It is because kernel code is careful. It does not use deep chains of function calls or big local arrays. A stack that runs out of space is a serious bug, since the data next to it can be overwritten. The kernel also had a setting for even smaller 4 KB stacks, and in that case it used a few extra stacks just for interrupts, so that deeply nested handlers would not run it out of room.&lt;/p&gt;

&lt;p&gt;The lesson for your own code is the same. A stack has a limit, and if you go too deep, for example with a function that calls itself forever, you get the famous stack overflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  More places you already use stacks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The undo button in your editor&lt;/li&gt;
&lt;li&gt;The back button in your browser&lt;/li&gt;
&lt;li&gt;Checking that brackets match in code, like &lt;code&gt;{ ( ) }&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Evaluating expressions like &lt;code&gt;3 + 4 * 2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Depth first search in graphs and trees&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it yourself
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Write a function that uses a stack to check if a string like &lt;code&gt;{[()]}&lt;/code&gt; has matching brackets.&lt;/li&gt;
&lt;li&gt;Reverse a string using only push and pop.&lt;/li&gt;
&lt;li&gt;What happens in the code above if you call &lt;code&gt;pop()&lt;/code&gt; on an empty stack? How would you handle it better?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you can do these three, you understand stacks well enough for most interviews.&lt;/p&gt;

&lt;h2&gt;
  
  
  What comes next
&lt;/h2&gt;

&lt;p&gt;Next in the series is the queue. It is the stack's opposite, first in and first out, and the kernel uses it a lot too. If you want to practise as you read, our C and data structures course covers stacks, queues and the rest hands-on.&lt;/p&gt;




&lt;p&gt;I first published this on the &lt;a href="https://www.jupiuslearning.com/blog/stacks-explained-with-real-examples-from-the-linux-kernel/" rel="noopener noreferrer"&gt;Jupius Learning blog&lt;/a&gt;. If you want to practice, the &lt;a href="https://www.jupiuslearning.com/courses/c-ds-a/" rel="noopener noreferrer"&gt;C, data structures and algorithms course&lt;/a&gt; is a good place to start.&lt;/p&gt;

</description>
      <category>datastructures</category>
      <category>c</category>
      <category>linux</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
