<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>System Programming on Cheat Sheet</title><link>/tags/system-programming/</link><description>Recent content in System Programming on Cheat Sheet</description><generator>Hugo</generator><language>en</language><atom:link href="/tags/system-programming/index.xml" rel="self" type="application/rss+xml"/><item><title>File I/O</title><link>/linux/system-programming/file-io/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/linux/system-programming/file-io/</guid><description>&lt;p&gt;Based on &lt;em&gt;The Linux Programming Interface&lt;/em&gt; Chapter 4 (File I/O: The Universal I/O Model) and Chapter 5 (File I/O: Further Details).
See &lt;a href="../../programming/go/std/io/"&gt;Go io package&lt;/a&gt; for equivalent abstractions (&lt;code&gt;io.Reader&lt;/code&gt;, &lt;code&gt;io.Writer&lt;/code&gt;, &lt;code&gt;io.Seeker&lt;/code&gt;) in Go.&lt;/p&gt;
&lt;h2 id="core-function-signatures"&gt;Core Function Signatures&lt;a class="anchor" href="#core-function-signatures"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;All I/O operations on regular files, devices, pipes, sockets, and terminals use these core system calls.&lt;/p&gt;
&lt;h3 id="1-open"&gt;1. open&lt;a class="anchor" href="#1-open"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Opens an existing file or creates a new file.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#57606a"&gt;#include&lt;/span&gt; &lt;span style="color:#57606a"&gt;&amp;lt;fcntl.h&amp;gt;&lt;/span&gt;&lt;span style="color:#57606a"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;int&lt;/span&gt; &lt;span style="color:#6639ba"&gt;open&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#cf222e"&gt;const&lt;/span&gt; &lt;span style="color:#cf222e"&gt;char&lt;/span&gt; &lt;span style="color:#0550ae"&gt;*&lt;/span&gt;pathname&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; &lt;span style="color:#cf222e"&gt;int&lt;/span&gt; flags&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; &lt;span style="color:#1f2328"&gt;...&lt;/span&gt; &lt;span style="color:#57606a"&gt;/* mode_t mode */&lt;/span&gt;&lt;span style="color:#1f2328"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Return:&lt;/strong&gt; New file descriptor (non-negative integer) on success; &lt;code&gt;-1&lt;/code&gt; on error (&lt;code&gt;errno&lt;/code&gt; set)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parameters:&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;pathname&amp;gt;&lt;/code&gt;: Path to the target file&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;flags&amp;gt;&lt;/code&gt;: Bitwise OR of access mode and status flags&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;mode&amp;gt;&lt;/code&gt;: Permissions for newly created file (&lt;code&gt;0644&lt;/code&gt;, &lt;code&gt;0600&lt;/code&gt;, etc.), required only when &lt;code&gt;O_CREAT&lt;/code&gt; is specified&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="access-modes-specify-exactly-one"&gt;Access Modes (Specify exactly one)&lt;a class="anchor" href="#access-modes-specify-exactly-one"&gt;#&lt;/a&gt;&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;O_RDONLY&lt;/code&gt;: Open for read-only&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_WRONLY&lt;/code&gt;: Open for write-only&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_RDWR&lt;/code&gt;: Open for reading and writing&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="common-creation-and-status-flags"&gt;Common Creation and Status Flags&lt;a class="anchor" href="#common-creation-and-status-flags"&gt;#&lt;/a&gt;&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;O_CREAT&lt;/code&gt;: Create file if it does not exist&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_EXCL&lt;/code&gt;: Used with &lt;code&gt;O_CREAT&lt;/code&gt;; fail atomically if file already exists (&lt;code&gt;EEXIST&lt;/code&gt;). Prevents Time-of-Check to Time-of-Use (TOCTOU) race conditions when creating lock files (&lt;code&gt;app.lock&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_TRUNC&lt;/code&gt;: Truncate existing file length to zero&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_APPEND&lt;/code&gt;: Atomically updates file offset to end of file before each write. Prevents race conditions between multiple processes appending to the same file (eliminates &lt;code&gt;lseek(SEEK_END)&lt;/code&gt; race conditions)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_NONBLOCK&lt;/code&gt;: Open in non-blocking mode (useful for FIFOs, pipes, and sockets)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_CLOEXEC&lt;/code&gt;: Enable close-on-exec flag atomically. Prevents file descriptor leakage across &lt;code&gt;exec&lt;/code&gt; calls to child programs (stored in the process-private FD table)&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id="2-read"&gt;2. read&lt;a class="anchor" href="#2-read"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Reads data from an open file descriptor into a memory buffer.&lt;/p&gt;</description></item><item><title>Process</title><link>/linux/system-programming/process/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/linux/system-programming/process/</guid><description>&lt;p&gt;Based on &lt;em&gt;The Linux Programming Interface&lt;/em&gt; Chapter 6 (Processes), Chapter 24 (Process Creation), Chapter 25 (Process Termination), Chapter 26 (Monitoring Child Processes), and Chapter 27 (Program Execution).&lt;/p&gt;
&lt;p&gt;The Linux process lifecycle is governed by the combination of four core system calls:&lt;/p&gt;
&lt;span class="book-katex" &gt;\( 
\mathbf{fork()} \longrightarrow \mathbf{execve()} \longrightarrow \mathbf{exit()} / \mathbf{\_exit()} \longrightarrow \mathbf{waitpid()}
 \)&lt;/span&gt;
&lt;hr&gt;
&lt;h2 id="1-process-virtual-memory-layout-tlpi-ch-6"&gt;1. Process Virtual Memory Layout (TLPI Ch 6)&lt;a class="anchor" href="#1-process-virtual-memory-layout-tlpi-ch-6"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The kernel allocates an independent virtual address space for each process, structured from low to high addresses as follows:&lt;/p&gt;</description></item><item><title>Shared Libraries</title><link>/linux/system-programming/shared-libraries/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/linux/system-programming/shared-libraries/</guid><description>&lt;p&gt;Based on &lt;em&gt;The Linux Programming Interface&lt;/em&gt; Chapter 41 (Fundamentals of Shared Libraries) and Chapter 42 (Advanced Features of Shared Libraries).&lt;/p&gt;
&lt;h1 id="shared-libraries"&gt;Shared Libraries&lt;a class="anchor" href="#shared-libraries"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;hr&gt;
&lt;h2 id="1-header-vs-static-vs-shared-library-tlpi-ch-41"&gt;1. Header vs Static vs Shared Library (TLPI Ch 41)&lt;a class="anchor" href="#1-header-vs-static-vs-shared-library-tlpi-ch-41"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;C libraries (eg. &lt;code&gt;glibc&lt;/code&gt;) separate compile-time declarations from link-time and runtime binaries.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Artifact&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Extension &amp;amp; Example Path&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Role&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Phase&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Header&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;.h&lt;/code&gt; (&lt;code&gt;/usr/include/dlfcn.h&lt;/code&gt;)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Plain-text function signatures, types, and macros&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Compile time (&lt;code&gt;gcc -c&lt;/code&gt;)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Static Library&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;.a&lt;/code&gt; (&lt;code&gt;libc.a&lt;/code&gt;)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Archive of &lt;code&gt;.o&lt;/code&gt; object files copied directly into the executable&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Link time (&lt;code&gt;gcc -static&lt;/code&gt;)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Shared Library&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;.so&lt;/code&gt; (&lt;code&gt;/lib/x86_64-linux-gnu/libc.so.6&lt;/code&gt;)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Precompiled Position-Independent machine code loaded once into memory and shared across processes&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Runtime (&lt;code&gt;ld.so&lt;/code&gt; dynamic linker)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="building-and-inspecting-shared-libraries"&gt;Building and Inspecting Shared Libraries&lt;a class="anchor" href="#building-and-inspecting-shared-libraries"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ gcc -fPIC -shared -Wall -o libfoo.so foo.c
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ ldd &amp;lt;binary&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ nm -D libfoo.so&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-fPIC&lt;/code&gt;: Generate Position-Independent Code so the library can be mapped at any virtual address&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-shared&lt;/code&gt;: Produce a shared object (&lt;code&gt;.so&lt;/code&gt;) instead of an executable requiring &lt;code&gt;main&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ldd&lt;/code&gt;: List shared libraries required by a binary and their resolved paths&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nm -D&lt;/code&gt;: Inspect exported dynamic symbols in a &lt;code&gt;.so&lt;/code&gt; file&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="2-dynamic-loading-api-dlopen--dlsym-tlpi-ch-42"&gt;2. Dynamic Loading API: &lt;code&gt;dlopen&lt;/code&gt; &amp;amp; &lt;code&gt;dlsym&lt;/code&gt; (TLPI Ch 42)&lt;a class="anchor" href="#2-dynamic-loading-api-dlopen--dlsym-tlpi-ch-42"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;glibc&lt;/code&gt; API for loading shared libraries into memory and resolving symbol addresses dynamically &lt;strong&gt;at runtime&lt;/strong&gt; rather than at program startup (&lt;code&gt;man 3 dlopen&lt;/code&gt;, &lt;code&gt;man 3 dlsym&lt;/code&gt;). The &lt;strong&gt;&lt;code&gt;RTLD_&lt;/code&gt;&lt;/strong&gt; prefix stands for &lt;strong&gt;R&lt;/strong&gt;un-&lt;strong&gt;T&lt;/strong&gt;ime &lt;strong&gt;L&lt;/strong&gt;ink-&lt;strong&gt;D&lt;/strong&gt;ynamic.&lt;/p&gt;</description></item></channel></rss>