<?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: Zobair Najdaoui</title>
    <description>The latest articles on DEV Community by Zobair Najdaoui (@ilorez).</description>
    <link>https://dev.to/ilorez</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1994128%2F90759a2c-a8c9-4fb6-b1d7-81c2448b9aa7.jpg</url>
      <title>DEV Community: Zobair Najdaoui</title>
      <link>https://dev.to/ilorez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ilorez"/>
    <language>en</language>
    <item>
      <title>Understanding the exit status Bits</title>
      <dc:creator>Zobair Najdaoui</dc:creator>
      <pubDate>Thu, 22 May 2025 18:14:08 +0000</pubDate>
      <link>https://dev.to/ilorez/understanding-the-exit-status-bits-22j1</link>
      <guid>https://dev.to/ilorez/understanding-the-exit-status-bits-22j1</guid>
      <description>&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.amazonaws.com%2Fuploads%2Farticles%2Frrotw29rz60cz3z1q08t.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.amazonaws.com%2Fuploads%2Farticles%2Frrotw29rz60cz3z1q08t.png" alt="Image description" width="624" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🔢 Understanding the &lt;code&gt;status&lt;/code&gt; Bits
&lt;/h2&gt;

&lt;p&gt;This status is a 16-bit integer returned by the kernel to describe &lt;strong&gt;how&lt;/strong&gt; a child process changed state. It can represent:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Normal termination&lt;/strong&gt; (via &lt;code&gt;exit()&lt;/code&gt; or &lt;code&gt;return&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Killed by a signal&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stopped by a signal&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Continued (after being stopped)&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  🧱 Bit Layout for Each Case
&lt;/h3&gt;

&lt;p&gt;Let's read this from &lt;strong&gt;least significant bit (bit 0)&lt;/strong&gt; to &lt;strong&gt;most (bit 15)&lt;/strong&gt;:&lt;/p&gt;




&lt;h3&gt;
  
  
  ✅ 1. &lt;strong&gt;Normal Termination&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bits 0–7:    Always zero
Bits 8–15:   Exit status (0–255)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the value looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0x00SS  (where SS is the exit status byte)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Detected by &lt;code&gt;WIFEXITED(status)&lt;/code&gt; macro.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WIFSIGNALED(status)&lt;/code&gt; → &lt;strong&gt;false&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ☠️ 2. &lt;strong&gt;Killed by Signal&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bits 0–6:   Signal number (non-zero)
Bit 7:      Core dump flag
Bits 8–15:  Unused (0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example: if the process was killed by &lt;code&gt;SIGSEGV (11)&lt;/code&gt; and dumped core:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Signal = 11  → bits 0-6 = 0001011
Core   = 1   → bit 7 = 1

status = 00000000 10001011  → 0x008B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;WIFSIGNALED(status)&lt;/code&gt; checks that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bits 0–6 ≠ 0 (so signal present)&lt;/li&gt;
&lt;li&gt;Bits 8–15 = 0 (not a stopped or continued signal)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;If so → returns &lt;strong&gt;true&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;WTERMSIG(status)&lt;/code&gt; → reads bits 0–6 → 11&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WCOREDUMP(status)&lt;/code&gt; → reads bit 7&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ✋ 3. &lt;strong&gt;Stopped by Signal&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bits 0–7:   0x7F (decimal 127)
Bits 8–15: Stop signal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Looks like: &lt;code&gt;0xSS7F&lt;/code&gt; where &lt;code&gt;SS&lt;/code&gt; = signal&lt;/li&gt;
&lt;li&gt;Detected by &lt;code&gt;WIFSTOPPED(status)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WIFSIGNALED(status)&lt;/code&gt; → &lt;strong&gt;false&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ▶️ 4. &lt;strong&gt;Continued&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;status = 0xFFFF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Used with &lt;code&gt;waitpid(..., WCONTINUED)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Detected by &lt;code&gt;WIFCONTINUED(status)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WIFSIGNALED(status)&lt;/code&gt; → &lt;strong&gt;false&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✅ Summary
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Status Type&lt;/th&gt;
&lt;th&gt;Bit Pattern Summary&lt;/th&gt;
&lt;th&gt;Detected by&lt;/th&gt;
&lt;th&gt;Related Macros&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Normal exit&lt;/td&gt;
&lt;td&gt;bits 0–7 = 0, bits 8–15 = exit code&lt;/td&gt;
&lt;td&gt;&lt;code&gt;WIFEXITED(status)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;WEXITSTATUS(status)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Killed by signal&lt;/td&gt;
&lt;td&gt;bits 0–6 = signal, bit 7 = core dump flag&lt;/td&gt;
&lt;td&gt;&lt;code&gt;WIFSIGNALED(status)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;WTERMSIG(status)&lt;/code&gt;, &lt;code&gt;WCOREDUMP(status)&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stopped&lt;/td&gt;
&lt;td&gt;bits 0–7 = 0x7F, bits 8–15 = stop signal&lt;/td&gt;
&lt;td&gt;&lt;code&gt;WIFSTOPPED(status)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;WSTOPSIG(status)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Continued&lt;/td&gt;
&lt;td&gt;status == 0xFFFF&lt;/td&gt;
&lt;td&gt;&lt;code&gt;WIFCONTINUED(status)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>programming</category>
      <category>c</category>
      <category>cpp</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Break down WIFSIGNALED</title>
      <dc:creator>Zobair Najdaoui</dc:creator>
      <pubDate>Thu, 22 May 2025 18:04:06 +0000</pubDate>
      <link>https://dev.to/ilorez/break-down-wifsignaled-4dld</link>
      <guid>https://dev.to/ilorez/break-down-wifsignaled-4dld</guid>
      <description>&lt;h3&gt;
  
  
  🧠 Context: What is &lt;code&gt;status&lt;/code&gt;?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;status&lt;/code&gt; is an &lt;code&gt;int&lt;/code&gt; passed by reference to &lt;code&gt;wait()&lt;/code&gt; / &lt;code&gt;waitpid()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When a child process terminates, the kernel writes information about &lt;em&gt;how&lt;/em&gt; it terminated into &lt;code&gt;status&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You then use macros like &lt;code&gt;WIFSIGNALED(status)&lt;/code&gt; to interpret that information.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🔍 1. &lt;code&gt;WIFSIGNALED(status)&lt;/code&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Was the child process terminated by an uncaught signal?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;This macro checks if the child died &lt;strong&gt;because of a signal&lt;/strong&gt;, not because it called &lt;code&gt;exit()&lt;/code&gt; or returned from &lt;code&gt;main()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Returns &lt;strong&gt;non-zero (true)&lt;/strong&gt; if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The child process ended &lt;strong&gt;due to a signal&lt;/strong&gt;, &lt;em&gt;not&lt;/em&gt; normal exit.&lt;/li&gt;
&lt;li&gt;The signal &lt;strong&gt;was not caught or ignored&lt;/strong&gt;, meaning it went unhandled.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;💡 &lt;strong&gt;Example&lt;/strong&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&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;sys/wait.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;signal.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&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="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="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fork&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;pid&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="c1"&gt;// Child process: cause segmentation fault&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;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// SIGSEGV&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;status&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;waitpid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&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;status&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;WIFSIGNALED&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Child was killed by signal %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;WTERMSIG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&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;WCOREDUMP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Core dump was generated&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🛑 2. &lt;code&gt;WTERMSIG(status)&lt;/code&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Which signal caused the termination?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;WIFSIGNALED(status)&lt;/code&gt; is true, you can use &lt;code&gt;WTERMSIG(status)&lt;/code&gt; to get the &lt;strong&gt;signal number&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;SIGSEGV&lt;/code&gt; = 11 (Segmentation fault)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SIGKILL&lt;/code&gt; = 9&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SIGINT&lt;/code&gt; = 2 (CTRL+C)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;These signal numbers are defined in &lt;code&gt;&amp;lt;signal.h&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  💾 3. &lt;code&gt;WCOREDUMP(status)&lt;/code&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Did the process create a core dump?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Some systems generate a &lt;strong&gt;core dump file&lt;/strong&gt; when a process crashes due to a signal.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WCOREDUMP(status)&lt;/code&gt; checks whether that happened.&lt;/li&gt;
&lt;li&gt;It’s &lt;strong&gt;not part of the official POSIX (SUSv3) standard&lt;/strong&gt;, but it’s available on many systems like Linux, BSD, etc.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🧩 How does this work under the hood?
&lt;/h3&gt;

&lt;p&gt;Internally, the &lt;code&gt;status&lt;/code&gt; integer returned by &lt;code&gt;wait()&lt;/code&gt; contains bit fields that encode:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Whether the process exited normally or by signal&lt;/li&gt;
&lt;li&gt;What signal or exit code&lt;/li&gt;
&lt;li&gt;Whether a core dump occurred&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Macros like &lt;code&gt;WIFSIGNALED&lt;/code&gt; just check specific bits in that integer.&lt;/p&gt;




&lt;h3&gt;
  
  
  🧪 Summary Table
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Macro&lt;/th&gt;
&lt;th&gt;What it tells you&lt;/th&gt;
&lt;th&gt;Only valid when&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;WIFSIGNALED(s)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Process was killed by an uncaught signal&lt;/td&gt;
&lt;td&gt;Always&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;WTERMSIG(s)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The signal number that killed it&lt;/td&gt;
&lt;td&gt;When &lt;code&gt;WIFSIGNALED(s)&lt;/code&gt; true&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;WCOREDUMP(s)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;If a core dump was produced&lt;/td&gt;
&lt;td&gt;When &lt;code&gt;WIFSIGNALED(s)&lt;/code&gt; true (and supported)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>c</category>
      <category>cpp</category>
      <category>linux</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
