<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>C on Lloyd Rochester's Geek Blog</title><link>https://lloydroc.github.io/categories/c/</link><description>Recent content in C on Lloyd Rochester's Geek Blog</description><generator>Hugo</generator><language>en</language><copyright>Copyright © 2025, Lloyd Rochester.</copyright><lastBuildDate>Sat, 08 Feb 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://lloydroc.github.io/categories/c/index.xml" rel="self" type="application/rss+xml"/><item><title>DNS Proxy in C</title><link>https://lloydroc.github.io/post/c/dns-proxy/</link><pubDate>Sat, 30 Dec 2023 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/dns-proxy/</guid><description>&lt;h1 id="hahahugoshortcode17s0hbhb">DNS Proxy in C&lt;/h1>
&lt;p>This post provides a simple example of a DNS proxy. A client would query a DNS record from our program as if it was a server. This query will be passed on to a DNS server and eventuall back to the client.&lt;/p>
&lt;h2 id="proxy-server-paradigm">Proxy Server Paradigm&lt;/h2>
&lt;p>A proxy server is an intermediary server. Instead of a client requesting direct to a server the request goes to a proxy server that will handle the request on behalf of the client.&lt;/p></description></item><item><title>Time Conversion in C</title><link>https://lloydroc.github.io/post/c/c-time-conversion/</link><pubDate>Tue, 27 Dec 2022 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/c-time-conversion/</guid><description>&lt;h1 id="hahahugoshortcode11s0hbhb">Time Conversion in C&lt;/h1>
&lt;p>In the Unix Operating System we have multiple ways that time can be represented: as a epoch, broken down and as a string. Conversion between these three representation can be cumbersom. This blog post outlines the ways time is represented in the C programming language and provides a reference for conversions. Many other languages will follow very closely or exactly to what is done in C.&lt;/p></description></item><item><title>Understanding Process Execution</title><link>https://lloydroc.github.io/post/c/execute-process/</link><pubDate>Tue, 29 Nov 2022 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/execute-process/</guid><description>&lt;h1 id="hahahugoshortcode20s0hbhb">Understanding Process Execution&lt;/h1>
&lt;p>In this post we will dive in to command execution from inside a program. The ability to execute commands from inside a program to obtain their output and exit code is very common for automation and testing use cases. Unfortunately, due to how Operating Systems work it is not a simple process to obtain the output and exit code from a running program.&lt;/p>
&lt;figure>&lt;img src="https://lloydroc.github.io/assets/png/remote-process-execution.png">&lt;figcaption>
 &lt;h4>Remote Process Execution&lt;/h4>
 &lt;/figcaption>
&lt;/figure>

&lt;p>Take for an example a program that from within we execute a command and capture the output in real time, as well as, capture the exit code once the program ends.&lt;/p></description></item><item><title>Understanding memory reallocation with realloc</title><link>https://lloydroc.github.io/post/c/understanding-realloc/</link><pubDate>Wed, 03 Aug 2022 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/understanding-realloc/</guid><description>&lt;h1 id="hahahugoshortcode36s0hbhb">Understanding memory reallocation with realloc&lt;/h1>
&lt;p>Let&amp;rsquo;s better understand how memory is allocated, then reallocated on the heap. When we allocate blocks of memory they are stacked on top of the heap with increasing memory addresses. What happens when we reallocate a memory block to make it larger when it&amp;rsquo;s isn&amp;rsquo;t the last item on the heap?&lt;/p>
&lt;h1 id="table-of-contents">Table of Contents&lt;/h1>
&lt;nav id="TableOfContents">&lt;/nav>
&lt;h1 id="heap-example">Heap Example&lt;/h1>
&lt;p>Let&amp;rsquo;s create an example using &lt;a href="https://man7.org/linux/man-pages/man3/malloc.3.html">malloc(3)&lt;/a> and &lt;a href="https://man7.org/linux/man-pages/man3/realloc.3p.html">realloc(3p)&lt;/a> that gives good insight. Here is how our example will play out:&lt;/p></description></item><item><title>Stack Protection with a Canary</title><link>https://lloydroc.github.io/post/c/stack-protection-canary/</link><pubDate>Fri, 11 Jun 2021 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/stack-protection-canary/</guid><description>&lt;h1 id="hahahugoshortcode41s0hbhb">Stack Protection with a Canary&lt;/h1>
&lt;p>Buffer overflows can be detected by inserting a &lt;em>canary&lt;/em> into a function. These canaries are inserted when a function&amp;rsquo;s stack frame is created. Before a function&amp;rsquo;s return we check for the canary on the stack frame. If the canary isn&amp;rsquo;t found then buffer overflow or &lt;em>stack smashing&lt;/em> is detected.&lt;/p>
&lt;h1 id="example">Example&lt;/h1>
&lt;p>Let&amp;rsquo;s first create a deliberate buffer overflow. Then we can see how it behaves with and without stack protection.&lt;/p></description></item><item><title>Finding Libriaries and Headers for C Programs in Unix</title><link>https://lloydroc.github.io/post/c/finding-c-headers-and-libraries/</link><pubDate>Thu, 27 May 2021 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/finding-c-headers-and-libraries/</guid><description>&lt;p>Need to list the installed libraries on a Unix system? Want to use a library in C and don&amp;rsquo;t know the linker flags to use? Need to find where header files are located for a package? This post answers these questions.&lt;/p>
&lt;h1 id="an-example-from-the-beginning">An example from the beginning&lt;/h1>
&lt;p>Let&amp;rsquo;s say I want to write a C program that uses &lt;code>libxml2&lt;/code>. We know we&amp;rsquo;re going to need &lt;code>#include&lt;/code> directives for the &lt;code>libxml2&lt;/code> package to refer to the header files. When we compile the program we need to have a linker flag with &lt;code>-l&lt;/code> to link in it&amp;rsquo;s shared library. Keep this example in mind as you read this post. Here are the steps to arrive with what we need:&lt;/p></description></item><item><title>Implementing a Timeout for a read in C</title><link>https://lloydroc.github.io/post/c/timeout-on-read/</link><pubDate>Mon, 12 Apr 2021 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/timeout-on-read/</guid><description>&lt;h1 id="hahahugoshortcode35s0hbhb">Implementing a Timeout for a read in C&lt;/h1>
&lt;p>Need to wait a specified amount of time for a read call to return before giving up and continuing on?&lt;/p>
&lt;p>In Unix when we &lt;code>read&lt;/code> from a file we can have a non-blocking read where if the file is not ready the &lt;code>read&lt;/code> call will pass through. We can also specify a blocking read where the &lt;code>read&lt;/code> will wait forever. What if we want &lt;code>read&lt;/code> to wait a specified amount of time not zero or infinity?&lt;/p></description></item><item><title>Timestamps in C</title><link>https://lloydroc.github.io/post/c/c-timestamp-epoch/</link><pubDate>Wed, 02 Dec 2020 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/c-timestamp-epoch/</guid><description>&lt;h1 id="hahahugoshortcode14s0hbhb">Timestamps in C&lt;/h1>
&lt;p>In this post I&amp;rsquo;ll provide some ways to create, convert and print timestamps using C. We&amp;rsquo;ll first create a Unix epoch which corresponds to seconds since January 1st 1970 at 00:00:00 UTC. We will also represent the epoch in milliseconds, as a double, and finally convert to an ISO 8601 Timestamp. We&amp;rsquo;ll conclude with the challenge of using fractional seconds.&lt;/p>
&lt;h1 id="table-of-contents">Table of Contents&lt;/h1>
&lt;nav id="TableOfContents">&lt;/nav>
&lt;h1 id="epoch-using-clock_gettime-from-timeh">Epoch using &lt;code>clock_gettime&lt;/code> from &lt;code>time.h&lt;/code>&lt;/h1>
&lt;p>Get the epoch in seconds and nanoseconds using &lt;code>clock_gettime&lt;/code>. This is POSIX.1-2001. We will use &lt;code>CLOCK_REALTIME&lt;/code> since all implementations will support this per &lt;code>man 3 clock_gettime&lt;/code>.&lt;/p></description></item><item><title>Disassembly of Recursion in C</title><link>https://lloydroc.github.io/post/c/arm-assembly-recursion/</link><pubDate>Fri, 25 Sep 2020 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/arm-assembly-recursion/</guid><description>&lt;h1 id="hahahugoshortcode19s0hbhb">Disassembly of Recursion in C&lt;/h1>
&lt;p>Let&amp;rsquo;s disassemble a recursive function in C to ARM assembly. We can use the textbook example of a recursive factorial function. We&amp;rsquo;ll play around with optimization levels and touch on Tail Recursion or Tail Calls at the end of the blog post.&lt;/p>
&lt;h2 id="c-code">C Code&lt;/h2>
&lt;p>Below is the C code we&amp;rsquo;ll use to disassemble. This code is a text book function that implements a factorial using recursion.&lt;/p></description></item><item><title>Systemd: Timers</title><link>https://lloydroc.github.io/post/unix/systemd_timers/</link><pubDate>Tue, 30 Jun 2020 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/unix/systemd_timers/</guid><description>&lt;h1 id="hahahugoshortcode98s0hbhb">Systemd: Timers&lt;/h1>
&lt;p>In the fourth post on how to create a service in &lt;code>systemd&lt;/code> we will create a timer that will run every minute sending messages to our example &lt;code>foo&lt;/code> service over a socket. We&amp;rsquo;re essentially creating a &lt;code>cron&lt;/code> job socket client.&lt;/p>
&lt;p>Previous posts:&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://lloydroc.github.io/post/autotools/systemd-service-daemon-autotools/">Project Creation&lt;/a> of an autotools project and start/stop a daemon service.&lt;/li>
&lt;li>&lt;a href="https://lloydroc.github.io/post/unix/systemd_journal/">Journalling/Syslog&lt;/a> to enable logging, notify of state changes and accept reloads.&lt;/li>
&lt;li>&lt;a href="https://lloydroc.github.io/post/unix/systemd_sockets/">Socket&lt;/a> creation of a Unix Domain Socket for other processes to send messages to our service through remote procedure calls.&lt;/li>
&lt;/ul>
&lt;h1 id="what-the-example-will-do">What the example will do?&lt;/h1>
&lt;p>We already have a Unix Domain Socket created by &lt;code>systemd&lt;/code> that is listening on &lt;code>/var/log/foo.socket&lt;/code>. We also have a service called &lt;code>foo.service&lt;/code> that is listening to this socket . When messages are received on the socket the service will log them to the journal and syslog.&lt;/p></description></item><item><title>Systemd: A Service and a Socket</title><link>https://lloydroc.github.io/post/unix/systemd_sockets/</link><pubDate>Fri, 26 Jun 2020 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/unix/systemd_sockets/</guid><description>&lt;h1 id="hahahugoshortcode97s0hbhb">Systemd: A Service and a Socket&lt;/h1>
&lt;p>This is the third post on how to create a &lt;em>service&lt;/em> in &lt;code>systemd&lt;/code>. See the &lt;a href="https://lloydroc.github.io/post/autotools/systemd-service-daemon-autotools/">first post&lt;/a> to create a autotools project and start/stop a daemon service. Or the &lt;a href="https://lloydroc.github.io/post/unix/systemd_journal/">second post&lt;/a> to enable logging, notify of state changes and accept reloads. In this post we&amp;rsquo;ll create a Unix Domain Socket so that other processes can send messages to our service through remote procedure calls.&lt;/p>
&lt;p>The socket we will use in this post is a Unix Socket with domain &lt;code>AF_UNIX&lt;/code> of type &lt;code>SOCK_DGRAM&lt;/code>. This type of socket is well suited for example purposes and is also ideal for remote procedure call applications. It is limited to communication on the same host, not over the internet. Unix Domain sockets are always reliable and are guaranteed to be delivered in order without duplication. Since this example uses a datagram socket we can receive the full message on each reception. Typically, the size of a datagram socket is under 2048 bytes see &lt;a href="https://www.man7.org/linux/man-pages/man7/socket.7.html">socket(7)&lt;/a> and look for the &lt;code>SO_SNDBUF&lt;/code> option for more details on maximum datagram size.&lt;/p></description></item><item><title>Status, Reloading and Journalling in Systemd</title><link>https://lloydroc.github.io/post/unix/systemd_journal/</link><pubDate>Tue, 23 Jun 2020 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/unix/systemd_journal/</guid><description>&lt;h1 id="hahahugoshortcode96s0hbhb">Status, Reloading and Journalling in Systemd&lt;/h1>
&lt;p>This is the second post on how to create a &lt;em>service&lt;/em> in &lt;code>systemd&lt;/code>. In the &lt;a href="https://lloydroc.github.io/post/autotools/systemd-service-daemon-autotools/">first post&lt;/a> we created an &lt;em>autotools&lt;/em> project and were able to start, stop and get basic status on the service. In this post we&amp;rsquo;ll build on the &lt;em>autotools&lt;/em> project to add logging to &lt;code>syslog&lt;/code> and &lt;code>journald&lt;/code>. We will also give an example on how we could reload the service with a &lt;code>systemctl reload foo&lt;/code>. We will also use the &lt;a href="https://www.freedesktop.org/software/systemd/man/sd_notify.html#">sd_notify(3)&lt;/a> API to notify the service manager the status of what the service is doing.&lt;/p></description></item><item><title>Viterbi Example</title><link>https://lloydroc.github.io/post/c/viterbi-example/</link><pubDate>Fri, 01 May 2020 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/viterbi-example/</guid><description>&lt;h1 id="hahahugoshortcode44s0hbhb">Viterbi Example&lt;/h1>
&lt;p>Here we go! In this post I&amp;rsquo;m going to go through how Viterbi Decoders work. There is a lot to cover since we need to touch on Convolutional Coding, State Machines, Trellis Diagrams, Hamming Distances and Trellis Diagrams. I&amp;rsquo;ll illustrate this with the absolute simplest example possible and walk you through every step of the way. I highly recommending taking out a pencil and paper for this.&lt;/p>
&lt;h1 id="what-is-viterbi-decoding">What is Viterbi Decoding?&lt;/h1>
&lt;p>Viterbi Decoding allows us to detect and correct errors that were transmitted through a noisy channel.&lt;/p></description></item><item><title>I/O Multiplexing in Unix</title><link>https://lloydroc.github.io/post/unix/synchronous-io-multiplexing/</link><pubDate>Sun, 26 Apr 2020 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/unix/synchronous-io-multiplexing/</guid><description>&lt;h1 id="hahahugoshortcode93s0hbhb">I/O Multiplexing in Unix&lt;/h1>
&lt;p>In Unix a process performs I/O on a single file descriptor at a time. When we talk about I/O on a file think &lt;code>read()&lt;/code> and &lt;code>write()&lt;/code> calls which are blocking. To get around blocking on a I/O call there are a number of ways. You can imagine how blocking on a single I/O call could bring the performance of a program to it&amp;rsquo;s knees. In this post we&amp;rsquo;ll quickly compare non-blocking I/O Models. Namely, &lt;code>select()&lt;/code>, &lt;code>poll()&lt;/code>, Signal Driven I/O, and &lt;code>epoll()&lt;/code>.&lt;/p></description></item><item><title>A CLI Tool to Recommend Text</title><link>https://lloydroc.github.io/post/c/recommend-command-line/</link><pubDate>Tue, 14 Apr 2020 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/recommend-command-line/</guid><description>&lt;h1 id="hahahugoshortcode29s0hbhb">A CLI Tool to Recommend Text&lt;/h1>
&lt;p>Computers are fast at comparing one string to another, however, when we need to find which string is &lt;em>closest&lt;/em> to other strings things become more challenging. In this post we&amp;rsquo;ll create an command line utility that will recommend a string by comparing it to a list of strings. We&amp;rsquo;ll recommend the string that is the &lt;em>closest&lt;/em> one to our input string.&lt;/p>
&lt;p>Spell checkers and search engines have similar algorithms to this to recommend and match queries for search results.&lt;/p></description></item><item><title>The Stack of Frames in C with ARM Assembly Example</title><link>https://lloydroc.github.io/post/c/stack-of-frames-arm/</link><pubDate>Fri, 03 Apr 2020 08:34:05 -0600</pubDate><guid>https://lloydroc.github.io/post/c/stack-of-frames-arm/</guid><description>&lt;p>Function calls in the C programming language make heavy use of the stack, also called the call stack. When functions are called they create the so-called stack of frames. Each function call creates a frame, and these frames are allocated on the stack. This stack frame is used to allocate memory for local variables and intermediate values. The stack frame also contains the previous frame pointer and program counter&amp;rsquo;s value to execute from once the frame is popped off the stack. We will disassemble C function calls to understand the stack of frames in ARM assembly.&lt;/p></description></item><item><title>Converting C Conditionals to ARM Assembly</title><link>https://lloydroc.github.io/post/c/arm-assembly-conditionals/</link><pubDate>Fri, 27 Mar 2020 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/arm-assembly-conditionals/</guid><description>&lt;h1 id="hahahugoshortcode12s0hbhb">Converting C Conditionals to ARM Assembly&lt;/h1>
&lt;p>In this post we can explore how to the &lt;code>gcc&lt;/code> compiler converts to C to ARM assembly language. We&amp;rsquo;ll specifically look at &lt;em>conditional logic&lt;/em>. We&amp;rsquo;ll look at examples of &lt;code>if&lt;/code> statements, &lt;code>if&lt;/code> statements with &lt;code>else if&lt;/code> and &lt;code>else&lt;/code>, then end with a &lt;code>switch&lt;/code> statement. To convert C to ARM assembly we will use the &lt;code>-S&lt;/code> flag on GCC. All these examples were done on a Raspberry Pi running Raspbian.&lt;/p></description></item><item><title>Delay Lines in C</title><link>https://lloydroc.github.io/post/c/delay-line/</link><pubDate>Mon, 23 Mar 2020 08:43:05 +0000</pubDate><guid>https://lloydroc.github.io/post/c/delay-line/</guid><description>&lt;h1 id="hahahugoshortcode16s0hbhb">Delay Lines in C&lt;/h1>
&lt;p>There are many scenarios where a delay line is needed in signal processing. Scenarios ranging from Digital Filters, Convolution, Reverberation, Echo and others. Delay lines have a fixed length of &lt;strong>N&lt;/strong> samples. When delay lines take in a new sample they discard the oldest sample. In this post we discuss how delay lines work and implement a delay line in the C programming language.&lt;/p>
&lt;h1 id="a-theoretical-delay-line">A Theoretical Delay Line&lt;/h1>
&lt;p>For our purposes we will store primitive data types in an array. We&amp;rsquo;ll spare for this example both non-primitive data types, as well as, non-contiguous memory. The primitive data type in this array would be any of the following types: &lt;strong>int&lt;/strong>, &lt;strong>char&lt;/strong>, &lt;strong>complex&lt;/strong>, &lt;strong>double&lt;/strong>, or &lt;strong>float&lt;/strong>. The memory for the delay line is monotonically increasing and continuous, meaning all memory addresses for samples are next to one another. Essentially, we&amp;rsquo;ll have a &lt;code>int delay[N]&lt;/code> to declare our delay line. The delay line will store &lt;strong>N&lt;/strong> samples. With each new sample we&amp;rsquo;ll throw out the oldest sample. Below is a figure to depict the state of the delay line on each new sample received.&lt;/p></description></item><item><title>UART Between Raspberry Pi and Arduino</title><link>https://lloydroc.github.io/post/hardware/uart-rpi-and-arduino/</link><pubDate>Wed, 18 Mar 2020 14:02:54 +0000</pubDate><guid>https://lloydroc.github.io/post/hardware/uart-rpi-and-arduino/</guid><description>&lt;p>Using a 2-wire serial connection we can communicate from a Raspberry Pi (RPi) to an Arduino. For the Raspberry Pi we&amp;rsquo;ll use the built in UART accessed through a &lt;code>tty&lt;/code>, and the Arduino we&amp;rsquo;ll use the &lt;a href="https://www.arduino.cc/reference/en/language/functions/communication/serial/">Serial&lt;/a> Communication Library.&lt;/p>
&lt;h1 id="what-this-example-does">What this Example Does?&lt;/h1>
&lt;p>This example will use a RPi and Arduino to send a fixed message from the RPi to the Arduino. The Arduino will then echo what it receives back the RPi. The hardware used was a RPi B+ and the Arduino Due. The code is all in C/C++.&lt;/p></description></item><item><title>Daemon Example in C</title><link>https://lloydroc.github.io/post/c/unix-daemon-example/</link><pubDate>Thu, 05 Mar 2020 08:24:36 +0000</pubDate><guid>https://lloydroc.github.io/post/c/unix-daemon-example/</guid><description>&lt;h1 id="hahahugoshortcode37s0hbhb">Daemon Example in C&lt;/h1>
&lt;p>In this post we&amp;rsquo;ll look at daemon creation and demonstrate how to programmatically create daemons. We&amp;rsquo;ll go into the SysV recommendation of &amp;ldquo;double forking&amp;rdquo; for daemon creation.&lt;/p>
&lt;h1 id="table-of-contents">Table of Contents&lt;/h1>
&lt;nav id="TableOfContents">
 &lt;ul>
 &lt;li>&lt;a href="#creating-a-daemon-programmatically">Creating a daemon programmatically&lt;/a>&lt;/li>
 &lt;li>&lt;a href="#creating-a-daemon-with-systemd">Creating a daemon with Systemd&lt;/a>&lt;/li>
 &lt;li>&lt;a href="#using-glibcs-daemon-function-to-create-a-daemon">Using glibc&amp;rsquo;s daemon function to create a daemon&lt;/a>
 &lt;ul>
 &lt;li>&lt;a href="#analysis-of-the-daemon">Analysis of the Daemon&lt;/a>&lt;/li>
 &lt;/ul>
 &lt;/li>
 &lt;li>&lt;a href="#manually-creating-a-daemon-in-c-using-a-double-fork">Manually Creating a Daemon in C using a Double Fork&lt;/a>&lt;/li>
 &lt;li>&lt;a href="#c-code-double-fork-daemon-example">C Code &amp;ldquo;Double Fork&amp;rdquo; Daemon Example&lt;/a>
 &lt;ul>
 &lt;li>&lt;a href="#double-fork-steps">Double Fork Steps&lt;/a>&lt;/li>
 &lt;li>&lt;a href="#process-ids-from-a-double-fork">Process IDs from a Double Fork&lt;/a>&lt;/li>
 &lt;li>&lt;a href="#c-code-to-use-the-daemon-example">C Code to use the Daemon Example&lt;/a>&lt;/li>
 &lt;/ul>
 &lt;/li>
 &lt;li>&lt;a href="#header-file-for-daemon-example">Header file for Daemon Example&lt;/a>
 &lt;ul>
 &lt;li>&lt;a href="#header-file-for-daemon-example-1">Header file for Daemon Example&lt;/a>&lt;/li>
 &lt;/ul>
 &lt;/li>
 &lt;li>&lt;a href="#running-the-example">Running the example&lt;/a>
 &lt;ul>
 &lt;li>&lt;a href="#analyzing-the-daemon-process">Analyzing the Daemon Process&lt;/a>&lt;/li>
 &lt;/ul>
 &lt;/li>
 &lt;li>&lt;a href="#known-issues">Known Issues&lt;/a>&lt;/li>
 &lt;li>&lt;a href="#the-linux-programming-interface-book">The Linux Programming Interface Book&lt;/a>&lt;/li>
 &lt;li>&lt;a href="#where-to-go-from-here">Where to go from here?&lt;/a>&lt;/li>
 &lt;/ul>
&lt;/nav>
&lt;h1 id="what-are-daemons">What are Daemons?&lt;/h1>
&lt;p>Daemons are long running processes that run in the background with no controlling terminal - tty. Use cases for daemons are when the program needs to be available at all times and managed by the scheduler. Popular examples are nginx, postfix, httpd, sshd, cron, inetd. Many of these end in &amp;ldquo;d&amp;rdquo;, which is a convention for the name of a daemon. Daemons typically do not have the ability to write to &lt;code>stdout&lt;/code>, or &lt;code>stderr&lt;/code>, and have no means to connect to &lt;code>stdin&lt;/code> because they have no controlling terminal. Having no controlling terminal is a big deal in daemons. If they could connect to a controlling terminal they could be used in nefarious ways. We guarantee the daemon cannot take on a controlling terminal by doing the so-called &amp;ldquo;double fork&amp;rdquo;. Usually, output from the daemon is done by writing to log files. These logs are typically written in the in &lt;code>/var/log&lt;/code> directory. Input to a daemon, when necessary, is typically through sockets and signals. Let&amp;rsquo;s write an example daemon in C that runs on Unix.&lt;/p></description></item><item><title>Unix UDP Example in C</title><link>https://lloydroc.github.io/post/c/unix-udp-example/</link><pubDate>Thu, 27 Feb 2020 17:14:45 +0000</pubDate><guid>https://lloydroc.github.io/post/c/unix-udp-example/</guid><description>&lt;h1 id="hahahugoshortcode39s0hbhb">Unix UDP Example in C&lt;/h1>
&lt;p>In this post we&amp;rsquo;ll create an example client and server that communicate over UDP in C using Unix as the Operating System. UDP is a connection-less, unreliable datagram packet service. The term connection-less means there is no handshake between the client and server before information is exchanged. The term &amp;ldquo;fire and forget&amp;rdquo; applies here since the client sends and beforehand has no negotiation with the server. The term unreliable means there is no guarantee of delivery, ordering or duplicate protection. UDP is best for time-sensitive applications where it is preferable to discard packets rather than waiting for delayed ones.&lt;/p></description></item><item><title>Listing files in a Directory using C</title><link>https://lloydroc.github.io/post/c/list-directory/</link><pubDate>Mon, 24 Feb 2020 17:40:14 +0000</pubDate><guid>https://lloydroc.github.io/post/c/list-directory/</guid><description>&lt;h1 id="hahahugoshortcode25s0hbhb">Listing files in a Directory using C&lt;/h1>
&lt;p>Use the glibc &lt;code>scandir&lt;/code> function from &lt;code>&amp;lt;dirent.h&amp;gt;&lt;/code> to list all the files in a directory using the C programming language. The &lt;code>scandir&lt;/code> function allows for sorting, comparing and filtering the names of files in the directory. There are default options to list alphabetically through the &lt;code>alphasort()&lt;/code> glibc standard function. We&amp;rsquo;ll make an example in C that shows how to list all the files in a directory and give the name and type of each file in the directory.&lt;/p></description></item><item><title>Regular Expressions in C</title><link>https://lloydroc.github.io/post/c/regex_pcre/</link><pubDate>Sat, 01 Feb 2020 13:29:10 +0000</pubDate><guid>https://lloydroc.github.io/post/c/regex_pcre/</guid><description>&lt;p>In this blog post we will construe some simple examples of regular expressions in C, also known as a &lt;code>regex&lt;/code>. We will use the popular libraries PCRE and PCRE2. If you don&amp;rsquo;t know what a regex is or have never used them, then you can close this tab right now! Learning regular expressions in in C is probably the wrong way to go. Start with something more easy like Python, Perl, or anything else because doing them in C is difficult. If you know regular expressions from other languages, learning them in C will strengthen your understanding of the concept. This example is meant to be easy to understand, simple and useful.&lt;/p></description></item><item><title>Why static declarations are confusing in C</title><link>https://lloydroc.github.io/post/c/static-variables/</link><pubDate>Thu, 22 Aug 2019 18:32:31 +0000</pubDate><guid>https://lloydroc.github.io/post/c/static-variables/</guid><description>&lt;p>The &lt;code>static&lt;/code> variable in C is unnecessarily confusing because it is used for both the memory management model and for linkage. Since these two concerns are mutually exclusive another keyword would have really cleared things up in the linkage case.&lt;/p>
&lt;h3 id="a-single-keyword-for-memory-management-and-linkage">A single keyword for memory management and linkage?&lt;/h3>
&lt;p>As you may know in C there are 3 methods of memory management models: automatic, static and manual. Automatic variables are what most programming languages use. Manual is memory allocated with &lt;code>malloc()&lt;/code> and de-allocated with &lt;code>free()&lt;/code>. It&amp;rsquo;s manual meaning you control allocation and de-allocation of memory. It&amp;rsquo;s static memory management we&amp;rsquo;re focused on in this post.&lt;/p></description></item><item><title>Implementing FIR Filters in C</title><link>https://lloydroc.github.io/post/c/fir-filter/</link><pubDate>Mon, 19 Aug 2019 16:00:21 +0000</pubDate><guid>https://lloydroc.github.io/post/c/fir-filter/</guid><description>&lt;h1 id="hahahugoshortcode23s0hbhb">Implementing FIR Filters in C&lt;/h1>
&lt;p>Implementing FIR filters in C is much easier if we make use of the static variables declared in our functions. In this blog post we will create some simple example FIR filters, and get their impulse response. To understand this post you will have to have the basic theory of FIR filtering down.&lt;/p>
&lt;h3 id="fir-filter-implementation">FIR Filter Implementation&lt;/h3>
&lt;p>There are a lot of ways to implement a FIR filter in C. The method provided makes the implementation simple because we can simply put the last time sample into the filter and the filtered result will come out. The filter function itself will handle the delay line of the time samples. It&amp;rsquo;s a bit abstract so let&amp;rsquo;s take an example of just creating a delay line of samples. Then later we&amp;rsquo;ll add in the FIR filter coefficients.&lt;/p></description></item><item><title>pthread barrier example</title><link>https://lloydroc.github.io/post/c/pthread-barrier-example/</link><pubDate>Mon, 12 Aug 2019 14:30:06 +0000</pubDate><guid>https://lloydroc.github.io/post/c/pthread-barrier-example/</guid><description>&lt;p>A Barrier in computing is a synchronization method where a group of threads cannot proceed until a condition allows the blocked threads to proceed. In this post we will construct an example of a Barrier that relates to men and women at the dinner table using pthreads. We will men waiting to get their food, but the men will be blocked until the woman has eaten, thus, trigger the start state of the Barrier. Ladies First!&lt;/p></description></item><item><title>Example FFT in C</title><link>https://lloydroc.github.io/post/c/example-fft/</link><pubDate>Thu, 08 Aug 2019 00:00:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/example-fft/</guid><description>&lt;h1 id="hahahugoshortcode22s0hbhb">Example FFT in C&lt;/h1>
&lt;p>In this post we&amp;rsquo;ll provide the simplest possible Fast Fourier Transform (FFT) example in C. After understanding this example it can be adapted to modify for performance or computer architecture.&lt;/p>
&lt;h1 id="table-of-contents">Table of Contents&lt;/h1>
&lt;nav id="TableOfContents">
 &lt;ul>
 &lt;li>&lt;a href="#fft-example-usage">FFT Example Usage&lt;/a>&lt;/li>
 &lt;li>&lt;a href="#c-header-of-the-fft">C Header of the FFT&lt;/a>
 &lt;ul>
 &lt;li>&lt;a href="#rearranging-the-input">Rearranging the Input&lt;/a>&lt;/li>
 &lt;li>&lt;a href="#c-header-to-use-the-fft">C Header to use the FFT&lt;/a>&lt;/li>
 &lt;/ul>
 &lt;/li>
 &lt;li>&lt;a href="#c-implementation-of-the-fft">C Implementation of the FFT&lt;/a>&lt;/li>
 &lt;li>&lt;a href="#test-cases-for-the-fft">Test Cases for the FFT&lt;/a>&lt;/li>
 &lt;/ul>
&lt;/nav>
&lt;h2 id="fft-example-usage">FFT Example Usage&lt;/h2>
&lt;p>In the example below we&amp;rsquo;ll perform an FFT on a complex (real + imaginary) array of 32 elements. After the FFT has completed it will write over the provided arrays as the FFT is done &lt;em>in place&lt;/em>. Note, we&amp;rsquo;re using floating point here and not fixed width. Thus, if you don&amp;rsquo;t have a math coprocessor it will be very slow.&lt;/p></description></item><item><title>A Useful Linked List</title><link>https://lloydroc.github.io/post/c/useful-linked-list/</link><pubDate>Fri, 26 Jul 2019 22:23:08 +0000</pubDate><guid>https://lloydroc.github.io/post/c/useful-linked-list/</guid><description>&lt;h1 id="hahahugoshortcode43s0hbhb">A Useful Linked List}&lt;/h1>
&lt;p>In this post we will create a useful Linked List in the C Programming Language. This is opposed to all the useless linked lists that I&amp;rsquo;ve seen on the internet. They are numerious, typically written by academics that haven&amp;rsquo;t much experience programming. It&amp;rsquo;s not the logic or syntax that is bad, but the API and functions they create that make the usage of the Linked List obtuse and awkward. This implementation of a Linked List in C is something meant to be used over and over again in projects.&lt;/p></description></item><item><title>Adding SQLite to a C Project</title><link>https://lloydroc.github.io/post/c/add-sqlite-project/</link><pubDate>Sat, 06 Jul 2019 10:43:41 +0000</pubDate><guid>https://lloydroc.github.io/post/c/add-sqlite-project/</guid><description>&lt;h1 id="hahahugoshortcode13s0hbhb">Adding SQLite to a C Project&lt;/h1>
&lt;p>Let&amp;rsquo;s add SQLite to your C project. It&amp;rsquo;s easy and straightforward. We&amp;rsquo;ll use the SQLite.org recommended way by adding the so-called &amp;ldquo;amalgamation&amp;rdquo; to our C project and compile from source. In the words of &lt;a href="https://sqlite.org/howtocompile.html">SQLite.org&lt;/a> &amp;ldquo;the use of the amalgamation is recommended for all applications.&amp;rdquo; In this blog post we&amp;rsquo;ll download, compile and use SQLite in our C project.&lt;/p>
&lt;h2 id="step-1---download-the-sqlite-amalgamation">Step 1 - Download the SQLite Amalgamation&lt;/h2>
&lt;p>In Step 1 we&amp;rsquo;ll download the amalgamation and look at the contents of the files. This is done by doing to the &lt;a href="https://www.sqlite.org/download.html">SQLite downloads&lt;/a> and downloading the zip file in the &amp;ldquo;C Source Code as an amalgamation&amp;rdquo; section. Let&amp;rsquo;s download the zip file and see what is inside it:&lt;/p></description></item><item><title>Passing Datagrams Between Python and C</title><link>https://lloydroc.github.io/post/c/unix-domain-socket-datagram/</link><pubDate>Tue, 18 Jun 2019 06:45:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/unix-domain-socket-datagram/</guid><description>&lt;p>Let&amp;rsquo;s discuss how to pass data from a C program to a Python Script and vice-versa. This subject involves IPC - Inter-Process Communications. In this blog post we&amp;rsquo;ll discuss the more exotic IPC mechanism of the &lt;strong>Unix domain datagram socket&lt;/strong>. Specifically, this is domain &lt;code>AF_UNIX&lt;/code> and type &lt;code>SOCK_DGRAM&lt;/code> type of socket. Before you consider this method let&amp;rsquo;s address some of it&amp;rsquo;s benefits and limitations of using Unix Domain Sockets for Interprocess Communications:&lt;/p></description></item><item><title>C Double Pointers</title><link>https://lloydroc.github.io/post/c/double-pointer/</link><pubDate>Mon, 17 Jun 2019 16:45:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/double-pointer/</guid><description>&lt;h1 id="hahahugoshortcode18s0hbhb">C Double Pointers&lt;/h1>
&lt;figure>&lt;img src="https://lloydroc.github.io/assets/svg/c-double-pointers.svg">&lt;figcaption>
 &lt;h4>double pointers in c&lt;/h4>
 &lt;/figcaption>
&lt;/figure>

&lt;p>Updated December 19, 2022&lt;/p>
&lt;p>Examples and explanations of single pointers are used throughout many programming languages including C. This blog post explains and explores use cases for double pointers in the C programming language. Toward the end we&amp;rsquo;ll look at a &lt;code>glibc&lt;/code> example that uses a triple pointer.&lt;/p>
&lt;h1 id="use-cases">Use Cases&lt;/h1>
&lt;p>The primary use cases I&amp;rsquo;ve found are the following:&lt;/p>
&lt;ul>
&lt;li>Arrays of strings &lt;code>char *&lt;/code>. These strings are typically not fixed length. Thus, &lt;code>char *buf[N]&lt;/code> is a bit misleading since &lt;code>N&lt;/code> will vary.&lt;/li>
&lt;li>Functions that allocate memory for the caller. In this case the caller is expected to free the allocated memory after finished.&lt;/li>
&lt;/ul>
&lt;h1 id="mental-model">Mental Model&lt;/h1>
&lt;p>What is the difference between a &lt;code>char **foo&lt;/code> and &lt;code>char *foo[]&lt;/code>? Let&amp;rsquo;s take an example program:&lt;/p></description></item><item><title>Simple SQLite C API Example</title><link>https://lloydroc.github.io/post/c/sqlite-api/</link><pubDate>Sat, 02 Feb 2019 16:45:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/sqlite-api/</guid><description>&lt;p>When getting started on the SQLite C API they recommend 2 basic ways: &lt;code>sqlite3_exec()&lt;/code> and &lt;code>sqlite3_get_table()&lt;/code> both are convenience wrappers around the core API Calls. They give good examples for the &lt;code>sqlite3_exec()&lt;/code> call, and the &lt;code>sqlite3_get_table()&lt;/code> is deprecated in the documentation. Using &lt;code>sqlite3_exec()&lt;/code> employs a callback function which gives the advantage of speed and low memory usage - due to being able to process each row at a time. It however, does not allow for knowing when all the rows are returned, as well as, being synchronous.&lt;/p></description></item><item><title>Simple Convolution in C</title><link>https://lloydroc.github.io/post/c/convolution/</link><pubDate>Wed, 17 Oct 2018 06:10:00 +0000</pubDate><guid>https://lloydroc.github.io/post/c/convolution/</guid><description>&lt;h1 id="hahahugoshortcode15s0hbhb">Simple Convolution in C&lt;/h1>
&lt;p>Updated April 21, 2020&lt;/p>
&lt;p>In this blog post we&amp;rsquo;ll create a simple 1D convolution in C. We&amp;rsquo;ll show the classic example of convolving two squares to create a triangle. When convolution is performed it&amp;rsquo;s usually between two discrete signals, or time series. In this example we&amp;rsquo;ll use C arrays to represent each signal.&lt;/p>
&lt;p>When implementing convolution it&amp;rsquo;s important to know the length of convolution result, since the resulting array is bigger than the two input arrays. This can cause memory problems. Computing the length of the convolution result is actually a simple computation. If you have array &lt;strong>H&lt;/strong> convolved with array &lt;strong>X&lt;/strong>, where the lengths are 5 and 5 respectively, the resulting size of &lt;strong>Y=H*X&lt;/strong> (&lt;strong>H&lt;/strong> convolved with &lt;strong>X&lt;/strong> to make &lt;strong>Y&lt;/strong>) will be &lt;strong>Length(H) + Length(X) - 1&lt;/strong>. For this example the resulting length of two size 5 arrays will be 5+5-1 = 9. This convolution is typically done where &lt;strong>H&lt;/strong> is a digital filter and &lt;strong>X&lt;/strong> is a time series to be filtered. The output array &lt;strong>Y&lt;/strong> is the time series that results after filtering. For our example we have input arrays &lt;strong>H&lt;/strong> and &lt;strong>X&lt;/strong>. The output of array of our convolution will be called &lt;strong>Y&lt;/strong>.&lt;/p></description></item></channel></rss>