<?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: Vivek P</title>
    <description>The latest articles on DEV Community by Vivek P (@vivx_developer).</description>
    <link>https://dev.to/vivx_developer</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%2F1194804%2F7d1babd4-4f24-4d7d-ba9c-0d2f6d1cc8c1.jpeg</url>
      <title>DEV Community: Vivek P</title>
      <link>https://dev.to/vivx_developer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vivx_developer"/>
    <language>en</language>
    <item>
      <title>CPU Registers</title>
      <dc:creator>Vivek P</dc:creator>
      <pubDate>Sat, 19 Oct 2024 19:42:08 +0000</pubDate>
      <link>https://dev.to/vivx_developer/cpu-registers-eff</link>
      <guid>https://dev.to/vivx_developer/cpu-registers-eff</guid>
      <description>&lt;h1&gt;
  
  
  CPU Registers
&lt;/h1&gt;

&lt;p&gt;CPU registers are small storage locations within the CPU used to hold data temporarily for processing. They are crucial for the CPU's operation, enabling quick access to data and efficient execution of instructions. Registers are much faster than other memory types (like RAM), and they play a central role in the CPU's architecture. Below are the key types of CPU registers:&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Registers
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;General-Purpose Registers (GPRs)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;These registers are used for various general operations such as arithmetic, logic, and data transfer. Common examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AX, BX, CX, DX&lt;/strong&gt; (in x86 architecture):

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AX&lt;/strong&gt; (Accumulator Register): Often used for arithmetic and logic operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BX&lt;/strong&gt; (Base Register): Used as a base pointer for memory addressing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CX&lt;/strong&gt; (Count Register): Typically used for loop control in repetitive operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DX&lt;/strong&gt; (Data Register): Used in I/O operations and some arithmetic functions.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Segment Registers&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Segment registers store segment addresses for accessing memory segments. They are used for different types of memory segments in a program:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CS&lt;/strong&gt; (Code Segment): Points to the segment containing the code being executed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DS&lt;/strong&gt; (Data Segment): Points to the segment containing the data used by the program.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SS&lt;/strong&gt; (Stack Segment): Points to the segment containing the stack.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ES, FS, GS&lt;/strong&gt;: Additional segment registers for specific data segments.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Special-Purpose Registers&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;These registers have specific functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Instruction Pointer (IP)&lt;/strong&gt;: Holds the address of the next instruction to be executed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flags Register (EFLAGS/RFLAGS)&lt;/strong&gt;: Contains flags that indicate the state of the CPU (e.g., zero flag, carry flag, overflow flag).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stack Pointer (SP)&lt;/strong&gt;: Points to the top of the stack, used for stack operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Base Pointer (BP)&lt;/strong&gt;: Used as a reference point for accessing stack data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;Floating-Point Registers (FPRs)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Used in processors with Floating Point Units (FPUs) for performing arithmetic operations on floating-point numbers. In x86, these are often referred to as &lt;strong&gt;ST0, ST1, ... ST7&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. &lt;strong&gt;Control Registers&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;These registers control various aspects of the CPU's operation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CR0&lt;/strong&gt;: Controls the operating mode of the CPU (e.g., enabling/disabling protected mode).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CR2&lt;/strong&gt;: Contains the Page Fault Linear Address.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CR3&lt;/strong&gt;: Holds the address of the page directory (used in paging).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CR4&lt;/strong&gt;: Controls advanced features such as Virtual-8086 mode, protected-mode virtual interrupts, and others.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. &lt;strong&gt;Debug Registers&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;These registers (DR0–DR7) are used to manage hardware breakpoints for debugging purposes.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. &lt;strong&gt;Model-Specific Registers (MSRs)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;These are special registers specific to a particular CPU model, used for configuring or reading specific CPU features.&lt;/p&gt;

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

&lt;p&gt;CPU registers are essential for the CPU's efficient operation, providing quick access to data and control information. Understanding registers is fundamental for low-level programming and optimizing code for performance.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>IPC Mechanisms in Linux</title>
      <dc:creator>Vivek P</dc:creator>
      <pubDate>Fri, 11 Oct 2024 07:30:31 +0000</pubDate>
      <link>https://dev.to/vivx_developer/ipc-mechanisms-in-linux-2oie</link>
      <guid>https://dev.to/vivx_developer/ipc-mechanisms-in-linux-2oie</guid>
      <description>&lt;p&gt;In Linux, several Inter-Process Communication (IPC) mechanisms enable processes to communicate and share data. These mechanisms are critical for coordination between processes running in user space. Here are some of the most commonly used IPC mechanisms &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;IPC Mechanism&lt;/th&gt;
&lt;th&gt;Key Features&lt;/th&gt;
&lt;th&gt;Speed&lt;/th&gt;
&lt;th&gt;Use Cases&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Pipes&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Unidirectional, suitable for parent-child processes.&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Simple communication between related processes.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Message Queues&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Queue messages, supports priority messages.&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Asynchronous messaging between processes.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Shared Memory&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Fastest IPC, shared memory region accessible by multiple processes.&lt;/td&gt;
&lt;td&gt;Fast&lt;/td&gt;
&lt;td&gt;High-speed data sharing between processes.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Semaphores&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Synchronization of processes to avoid race conditions.&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Controlling access to shared resources.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Signals&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Asynchronous notification.&lt;/td&gt;
&lt;td&gt;Fast&lt;/td&gt;
&lt;td&gt;Event notification, process control.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Sockets&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Network communication (or local via UNIX domain).&lt;/td&gt;
&lt;td&gt;Medium/Slow&lt;/td&gt;
&lt;td&gt;Network and local client-server communication.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Mmap&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Memory-mapped files or devices, shared memory via file mapping.&lt;/td&gt;
&lt;td&gt;Fast&lt;/td&gt;
&lt;td&gt;Shared memory via file mappings.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;D-Bus&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;High-level, inter-application communication (used in desktop environments).&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Application-level IPC (e.g., GNOME, KDE).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Eventfd/Signalfd&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;File descriptor-based event and signal handling.&lt;/td&gt;
&lt;td&gt;Fast&lt;/td&gt;
&lt;td&gt;Handling events and signals in &lt;code&gt;poll&lt;/code&gt; or &lt;code&gt;epoll&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;POSIX Message Queues&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;FIFO messaging with priorities, used in real-time applications.&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Real-time message passing.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Files&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Slow but simple, persistent data sharing through files.&lt;/td&gt;
&lt;td&gt;Slow&lt;/td&gt;
&lt;td&gt;Data persistence, logging, simple IPC.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>learning</category>
      <category>linux</category>
      <category>code</category>
    </item>
    <item>
      <title>Linux Developer Interview Questions.</title>
      <dc:creator>Vivek P</dc:creator>
      <pubDate>Wed, 09 Oct 2024 16:16:01 +0000</pubDate>
      <link>https://dev.to/vivx_developer/linux-developer-interview-questions-1ipa</link>
      <guid>https://dev.to/vivx_developer/linux-developer-interview-questions-1ipa</guid>
      <description>&lt;h1&gt;
  
  
  Linux System Developer Interview Questions and Answers
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Basic Linux Knowledge
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Q: What happens when you type 'ls -l' in a terminal? Explain the entire process.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; When you type 'ls -l' and press Enter, the following process occurs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The shell (e.g., bash) reads the input and parses it.&lt;/li&gt;
&lt;li&gt;The shell identifies 'ls' as an external command and '-l' as an argument.&lt;/li&gt;
&lt;li&gt;The shell fork()s to create a child process.&lt;/li&gt;
&lt;li&gt;The child process uses execve() to replace itself with the 'ls' program.&lt;/li&gt;
&lt;li&gt;The 'ls' program:

&lt;ul&gt;
&lt;li&gt;Parses command-line arguments&lt;/li&gt;
&lt;li&gt;Opens the current directory (or specified directory)&lt;/li&gt;
&lt;li&gt;Reads directory entries using getdents() system call&lt;/li&gt;
&lt;li&gt;For each file:

&lt;ul&gt;
&lt;li&gt;Calls stat() to get file information&lt;/li&gt;
&lt;li&gt;Formats the output (permissions, owner, size, date, name)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Writes the formatted output to stdout&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The parent shell waits for the 'ls' command to complete&lt;/li&gt;
&lt;li&gt;The shell displays a new prompt&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Q: What is the Linux kernel?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;A:&lt;/strong&gt; The Linux kernel is the core component of Linux operating systems. It's a free and open-source, monolithic, modular Unix-like operating system kernel. It manages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;System hardware resources&lt;/li&gt;
&lt;li&gt;Process scheduling&lt;/li&gt;
&lt;li&gt;File systems&lt;/li&gt;
&lt;li&gt;Device drivers&lt;/li&gt;
&lt;li&gt;System calls&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Key characteristics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Written primarily in C&lt;/li&gt;
&lt;li&gt;Created by Linus Torvalds in 1991&lt;/li&gt;
&lt;li&gt;Released under GNU General Public License v2&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Q: Explain the boot process of a Linux system.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; The Linux boot process consists of the following stages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;BIOS/UEFI Stage&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Power-on self-test (POST)&lt;/li&gt;
&lt;li&gt;Identifies boot device&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Bootloader Stage (e.g., GRUB)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Loads kernel image into memory&lt;/li&gt;
&lt;li&gt;Passes control to kernel with initial RAM disk (initrd)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Kernel Stage&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initializes hardware and memory&lt;/li&gt;
&lt;li&gt;Mounts root filesystem&lt;/li&gt;
&lt;li&gt;Starts init process (PID 1)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Init Stage&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SystemD or traditional SysV init&lt;/li&gt;
&lt;li&gt;Starts system services&lt;/li&gt;
&lt;li&gt;Brings up network interfaces&lt;/li&gt;
&lt;li&gt;Mounts additional filesystems&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Runlevel/Target Stage&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reaches the specified runlevel or target&lt;/li&gt;
&lt;li&gt;System is ready for use&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Q: What is the difference between a soft link and a hard link?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hard Links:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Share the same inode number as the original file&lt;/li&gt;
&lt;li&gt;Can't cross filesystem boundaries&lt;/li&gt;
&lt;li&gt;Can't link to directories (usually)&lt;/li&gt;
&lt;li&gt;File content is only deleted when all hard links are deleted&lt;/li&gt;
&lt;li&gt;Same file size as the original file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example creating a hard link:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ln &lt;/span&gt;original.txt hardlink.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Soft Links (Symbolic Links):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Contain a path to the original file&lt;/li&gt;
&lt;li&gt;Can cross filesystem boundaries&lt;/li&gt;
&lt;li&gt;Can link to directories&lt;/li&gt;
&lt;li&gt;Can become dangling if original file is deleted&lt;/li&gt;
&lt;li&gt;Very small in size (just contains the path)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example creating a soft link:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ln&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; original.txt softlink.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Q: Explain the difference between user space and kernel space.&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; &lt;br&gt;
&lt;strong&gt;Kernel Space:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Highest privileged level (Ring 0)&lt;/li&gt;
&lt;li&gt;Full access to hardware&lt;/li&gt;
&lt;li&gt;Executes kernel code and device drivers&lt;/li&gt;
&lt;li&gt;Cannot be accessed directly by user applications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;User Space:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lower privilege level (Ring 3)&lt;/li&gt;
&lt;li&gt;Limited access to hardware&lt;/li&gt;
&lt;li&gt;Executes user applications&lt;/li&gt;
&lt;li&gt;Must use system calls to access kernel services&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example of crossing the boundary:&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;// User space code&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;fd&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="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;O_RDONLY&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// System call to kernel space&lt;/span&gt;

&lt;span class="c1"&gt;// Kernel space implementation&lt;/span&gt;
&lt;span class="n"&gt;SYSCALL_DEFINE3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;__user&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filename&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="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;umode_t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Kernel code here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Q: What are system calls? List and explain five common ones.&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; System calls are interfaces between user space programs and the kernel.&lt;/p&gt;

&lt;p&gt;Five common system calls:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;fork()&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Creates a new process by duplicating the calling process&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;read()&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;ssize_t&lt;/span&gt; &lt;span class="n"&gt;bytes&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="n"&gt;fd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Reads data from a file descriptor&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;write()&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;ssize_t&lt;/span&gt; &lt;span class="n"&gt;bytes&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="n"&gt;fd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Writes data to a file descriptor&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;open()&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;fd&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="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;O_RDONLY&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Opens a file or creates it if it doesn't exist&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;close()&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Closes a file descriptor&lt;/p&gt;
&lt;h3&gt;
  
  
  Q: Explain the Linux process scheduling algorithm.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;A:&lt;/strong&gt; Linux uses the Completely Fair Scheduler (CFS):&lt;/p&gt;

&lt;p&gt;Key concepts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Virtual Runtime (vruntime)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tracks process execution time&lt;/li&gt;
&lt;li&gt;Normalized by process priority&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Red-Black Tree&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Processes sorted by vruntime&lt;/li&gt;
&lt;li&gt;O(log n) insertion and removal&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example of how priority affects scheduling:&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="n"&gt;sched_param&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sched_priority&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;51&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Range: 1-99 for real-time&lt;/span&gt;

&lt;span class="n"&gt;sched_setscheduler&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="n"&gt;SCHED_FIFO&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;param&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Scheduler classes (in order of priority):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Stop scheduler (internal use)&lt;/li&gt;
&lt;li&gt;Deadline scheduler&lt;/li&gt;
&lt;li&gt;Real-time scheduler&lt;/li&gt;
&lt;li&gt;CFS scheduler&lt;/li&gt;
&lt;li&gt;Idle scheduler&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Q: What is a page fault? Explain different types.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;A:&lt;/strong&gt; A page fault occurs when a program tries to access memory that is mapped in the virtual address space but not loaded in physical memory.&lt;/p&gt;

&lt;p&gt;Types of page faults:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Minor Page Fault&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Page is in memory but not marked in MMU&lt;/li&gt;
&lt;li&gt;No disk I/O required&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Major Page Fault&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Page must be loaded from disk&lt;/li&gt;
&lt;li&gt;Requires disk I/O&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Invalid Page Fault&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access to invalid memory address&lt;/li&gt;
&lt;li&gt;Results in segmentation fault&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example of handling page faults in 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="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;__do_page_fault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&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="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;addr&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="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;task_struct&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;tsk&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="n"&gt;vm_area_struct&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;vma&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;fault&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;vma&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;find_vma&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;addr&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;vma&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;VM_FAULT_BADMAP&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;fault&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;handle_mm_fault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vma&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fault&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;
  
  
  Q: What are the different types of process states in Linux?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Linux processes can be in the following states:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Running (R)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Currently executing on a CPU or waiting to be executed&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Sleeping&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Interruptible Sleep (S)&lt;/strong&gt;: Waiting for an event, can be interrupted&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Uninterruptible Sleep (D)&lt;/strong&gt;: Usually I/O, can't be interrupted&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Stopped (T)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Process has been stopped, usually by user signal (SIGSTOP)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Zombie (Z)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Process has completed but parent hasn't read its exit status&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Dead (X)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Process is being terminated&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example command to see process states:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ps aux | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $8}'&lt;/span&gt; | &lt;span class="nb"&gt;sort&lt;/span&gt; | &lt;span class="nb"&gt;uniq&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  System Programming
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Q: What is the difference between a process and a thread?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Processes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Have separate memory spaces&lt;/li&gt;
&lt;li&gt;Have their own file descriptors, program counter, stack&lt;/li&gt;
&lt;li&gt;Communication between processes requires IPC mechanisms&lt;/li&gt;
&lt;li&gt;Higher overhead for creation and context switching&lt;/li&gt;
&lt;li&gt;More isolated and secure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Threads:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Share the same memory space within a process&lt;/li&gt;
&lt;li&gt;Share file descriptors, code, and data segments&lt;/li&gt;
&lt;li&gt;Can communicate through shared memory&lt;/li&gt;
&lt;li&gt;Lower overhead for creation and context switching&lt;/li&gt;
&lt;li&gt;Less isolated, potential for race conditions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example of creating a process vs a thread:&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;// Process creation&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="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&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="c1"&gt;// Parent process&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Thread creation&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;pthread.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&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;thread_function&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="c1"&gt;// Thread code&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;NULL&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="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&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="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;thread_function&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Q: What are signals in Linux? How do you handle signals in a program?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Signals are software interrupts used for inter-process communication. They can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sent by the kernel to processes&lt;/li&gt;
&lt;li&gt;Sent by processes to other processes&lt;/li&gt;
&lt;li&gt;Sent by processes to themselves&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Common signals:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SIGTERM (15): Termination request&lt;/li&gt;
&lt;li&gt;SIGKILL (9): Immediate termination&lt;/li&gt;
&lt;li&gt;SIGINT (2): Interactive attention (Ctrl+C)&lt;/li&gt;
&lt;li&gt;SIGSEGV (11): Segmentation violation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example of signal handling:&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;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;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;signal_handler&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;signum&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;"Caught 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;signum&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="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Register signal handler&lt;/span&gt;
    &lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SIGINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signal_handler&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="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Running...&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;sleep&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;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;h2&gt;
  
  
  Debugging &amp;amp; Performance
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Q: How would you profile a Linux application for performance optimization?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Several tools and techniques can be used:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;perf&lt;/strong&gt; - Linux profiling tool
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;perf record ./myapp
perf report
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;gprof&lt;/strong&gt; - GNU profiler
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc &lt;span class="nt"&gt;-pg&lt;/span&gt; program.c &lt;span class="nt"&gt;-o&lt;/span&gt; program
./program
gprof program gmon.out &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; analysis.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Valgrind&lt;/strong&gt; - Memory profiler
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;valgrind &lt;span class="nt"&gt;--tool&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;callgrind ./myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;strace&lt;/strong&gt; - Trace system calls
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;strace &lt;span class="nt"&gt;-c&lt;/span&gt; ./myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key areas to look for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CPU usage (user vs system time)&lt;/li&gt;
&lt;li&gt;Memory allocation/deallocation patterns&lt;/li&gt;
&lt;li&gt;I/O operations&lt;/li&gt;
&lt;li&gt;Cache misses&lt;/li&gt;
&lt;li&gt;System call usage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example of using perf to find hotspots:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;perf record &lt;span class="nt"&gt;-g&lt;/span&gt; ./myapp
perf report &lt;span class="nt"&gt;--stdio&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Coding Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Q: Write a program to create a daemon process.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&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;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;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;sys/types.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;sys/stat.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;syslog.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="c1"&gt;// Fork off the parent process&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;&amp;lt;&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;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EXIT_FAILURE&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;pid&lt;/span&gt; &lt;span class="o"&gt;&amp;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;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EXIT_SUCCESS&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Parent exits&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Create new session&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;setsid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EXIT_FAILURE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Fork again (recommended)&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;&amp;lt;&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;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EXIT_FAILURE&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;pid&lt;/span&gt; &lt;span class="o"&gt;&amp;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;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EXIT_SUCCESS&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Set file permissions&lt;/span&gt;
    &lt;span class="n"&gt;umask&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="c1"&gt;// Change working directory&lt;/span&gt;
    &lt;span class="n"&gt;chdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Close all open file descriptors&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;int&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;sysconf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_SC_OPEN_MAX&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;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;x&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="n"&gt;close&lt;/span&gt;&lt;span class="p"&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="c1"&gt;// Open logs&lt;/span&gt;
    &lt;span class="n"&gt;openlog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"mydaemon"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;LOG_PID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;LOG_DAEMON&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Daemon-specific initialization&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="n"&gt;syslog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LOG_NOTICE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Daemon is running"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;closelog&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;EXIT_SUCCESS&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;Key points about daemons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Double forking ensures the process isn't a session leader&lt;/li&gt;
&lt;li&gt;Changing directory to / prevents locking mounted filesystems&lt;/li&gt;
&lt;li&gt;Closing file descriptors prevents resource leaks&lt;/li&gt;
&lt;li&gt;Using syslog for logging as stdout/stderr are closed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Q: What are signals in Linux? How do you handle them?&lt;br&gt;
&lt;strong&gt;A:&lt;/strong&gt; Signals are software interrupts used for inter-process communication.&lt;/p&gt;

&lt;p&gt;Common signals:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;SIGTERM (15) - Termination request&lt;/li&gt;
&lt;li&gt;SIGKILL (9) - Immediate termination&lt;/li&gt;
&lt;li&gt;SIGINT (2) - Interactive attention (Ctrl+C)&lt;/li&gt;
&lt;li&gt;SIGSEGV (11) - Segmentation violation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Signal handling example:&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;signal.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;signal_handler&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;signum&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;"Caught 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;signum&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;sigaction&lt;/span&gt; &lt;span class="n"&gt;sa&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;sa&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sa_handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;signal_handler&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;sigemptyset&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;sa&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sa_mask&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;sa&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sa_flags&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;sigaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SIGINT&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;sa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&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="n"&gt;sleep&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;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;Sending signals:&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;// Send signal to another process&lt;/span&gt;
&lt;span class="n"&gt;kill&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="n"&gt;SIGTERM&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Send signal to process group&lt;/span&gt;
&lt;span class="n"&gt;killpg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pgid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SIGTERM&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Q: What is a deadlock? How can you prevent it?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;A:&lt;/strong&gt; A deadlock occurs when two or more processes are waiting indefinitely for resources held by each other.&lt;/p&gt;

&lt;p&gt;Four conditions for deadlock:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Mutual Exclusion&lt;/li&gt;
&lt;li&gt;Hold and Wait&lt;/li&gt;
&lt;li&gt;No Preemption&lt;/li&gt;
&lt;li&gt;Circular Wait&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example of potential deadlock:&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="n"&gt;pthread_mutex_t&lt;/span&gt; &lt;span class="n"&gt;mutex1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PTHREAD_MUTEX_INITIALIZER&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;pthread_mutex_t&lt;/span&gt; &lt;span class="n"&gt;mutex2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PTHREAD_MUTEX_INITIALIZER&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="nf"&gt;thread1_function&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="n"&gt;pthread_mutex_lock&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;mutex1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;sleep&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="c1"&gt;// Increase chance of deadlock&lt;/span&gt;
    &lt;span class="n"&gt;pthread_mutex_lock&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;mutex2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// ... critical section ...&lt;/span&gt;
    &lt;span class="n"&gt;pthread_mutex_unlock&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;mutex2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;pthread_mutex_unlock&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;mutex1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&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;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;thread2_function&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="n"&gt;pthread_mutex_lock&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;mutex2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;sleep&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="n"&gt;pthread_mutex_lock&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;mutex1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// ... critical section ...&lt;/span&gt;
    &lt;span class="n"&gt;pthread_mutex_unlock&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;mutex1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;pthread_mutex_unlock&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;mutex2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;NULL&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;Prevention strategies:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lock ordering&lt;/li&gt;
&lt;li&gt;Lock timeout&lt;/li&gt;
&lt;li&gt;Deadlock detection&lt;/li&gt;
&lt;li&gt;Use lock-free algorithms&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Q: Explain the difference between threads and processes.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;A:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Processes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Separate address space&lt;/li&gt;
&lt;li&gt;Higher creation overhead&lt;/li&gt;
&lt;li&gt;More isolation&lt;/li&gt;
&lt;li&gt;IPC required for communication&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Threads:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Shared address space&lt;/li&gt;
&lt;li&gt;Lower creation overhead&lt;/li&gt;
&lt;li&gt;Less isolation&lt;/li&gt;
&lt;li&gt;Can communicate through shared memory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example of creating both:&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;// Process creation&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&lt;/span&gt;
    &lt;span class="n"&gt;exit&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="c1"&gt;// Thread creation&lt;/span&gt;
&lt;span class="n"&gt;pthread_t&lt;/span&gt; &lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&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="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;thread_function&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;pthread_join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Memory comparison:&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;// Process - separate memory&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;fork&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="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Only changes in child&lt;/span&gt;
        &lt;span class="n"&gt;exit&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="c1"&gt;// Parent's x is still 5&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Thread - shared memory&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;thread_func&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="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="o"&gt;=&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="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="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="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Changes for all threads&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;NULL&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;
  
  
  Q: What is a race condition? How can you prevent it?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;A:&lt;/strong&gt; A race condition occurs when multiple threads access shared data concurrently, and the outcome depends on the order of execution.&lt;/p&gt;

&lt;p&gt;Example of a race condition:&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;counter&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="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;increment&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="k"&gt;for&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;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="mi"&gt;1000000&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="n"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Race condition here&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pthread_t&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&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;t1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&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;t2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;pthread_join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;pthread_join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&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;"Counter: %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="c1"&gt;// Will be less than 2000000&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Prevention methods:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Mutex&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;pthread_mutex_t&lt;/span&gt; &lt;span class="n"&gt;mutex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PTHREAD_MUTEX_INITIALIZER&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="nf"&gt;safe_increment&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="k"&gt;for&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;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="mi"&gt;1000000&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="n"&gt;pthread_mutex_lock&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;mutex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;pthread_mutex_unlock&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;mutex&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="nb"&gt;NULL&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;ol&gt;
&lt;li&gt;
&lt;strong&gt;Atomic Operations&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&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;stdatomic.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="n"&gt;atomic_int&lt;/span&gt; &lt;span class="n"&gt;counter&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="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;atomic_increment&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="k"&gt;for&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;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="mi"&gt;1000000&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="n"&gt;atomic_fetch_add&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;counter&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;return&lt;/span&gt; &lt;span class="nb"&gt;NULL&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;



</description>
      <category>developer</category>
      <category>interview</category>
      <category>linux</category>
    </item>
    <item>
      <title>Simple x86 Bootloader Project</title>
      <dc:creator>Vivek P</dc:creator>
      <pubDate>Sun, 15 Sep 2024 13:30:57 +0000</pubDate>
      <link>https://dev.to/vivx_developer/simple-x86-bootloader-project-2p7n</link>
      <guid>https://dev.to/vivx_developer/simple-x86-bootloader-project-2p7n</guid>
      <description>&lt;h1&gt;
  
  
  Simple x86 Bootloader Project
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Prerequisites&lt;/li&gt;
&lt;li&gt;Bootloader Code&lt;/li&gt;
&lt;li&gt;Code Explanation&lt;/li&gt;
&lt;li&gt;Bootloader Flow&lt;/li&gt;
&lt;li&gt;Building the Bootloader&lt;/li&gt;
&lt;li&gt;Testing the Bootloader&lt;/li&gt;
&lt;li&gt;Troubleshooting&lt;/li&gt;
&lt;li&gt;Further Learning&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This project demonstrates how to create a simple bootloader using x86 assembly language. The bootloader, when executed, prints the message "Hello World I am vivek" to the screen. This serves as an educational tool to understand the basics of how a computer boots up and how to write low-level code that interacts directly with the hardware.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;To work with this project, you'll need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic understanding of assembly language concepts&lt;/li&gt;
&lt;li&gt;An x86 assembler (like NASM or GNU Assembler)&lt;/li&gt;
&lt;li&gt;A linker (like ld)&lt;/li&gt;
&lt;li&gt;A virtualization tool (like QEMU) or a physical machine to test the bootloader&lt;/li&gt;
&lt;li&gt;A text editor for writing code&lt;/li&gt;
&lt;li&gt;A bash-compatible shell (for Unix-like systems) or equivalent for Windows&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bootloader Code
&lt;/h2&gt;

&lt;p&gt;Save the following code in a file named &lt;code&gt;sample_bootable.s&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.code16
.global init

init:
   mov $string, %si
   mov $0xe, %ah

print:
   lodsb
   cmp $0, %al
   je end
   int $0x10
   jmp print

end:
   hlt

string: .asciz "Hello World I am vivek"

.fill 510-(.-init), 1, 0
.word 0xaa55
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Code Explanation
&lt;/h2&gt;

&lt;p&gt;Let's break down each part of the code:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;.code16&lt;/code&gt;: This directive tells the assembler to generate 16-bit code, which is what the BIOS expects when it first starts up.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;.global init&lt;/code&gt;: This makes the &lt;code&gt;init&lt;/code&gt; label globally visible, which is important for the linker.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;init:&lt;/code&gt;: This label marks the entry point of our program.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;mov $string, %si&lt;/code&gt;: This loads the address of our string into the SI (Source Index) register.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;mov $0xe, %ah&lt;/code&gt;: This sets the AH register to 0xe, which is the BIOS teletype output function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;print:&lt;/code&gt;: This label marks the start of our printing loop.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;lodsb&lt;/code&gt;: This loads a byte from the address in SI into AL and increments SI.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;cmp $0, %al&lt;/code&gt;: This compares the loaded byte with 0 (our string terminator).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;je end&lt;/code&gt;: If the byte is 0, we jump to the end label.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;int $0x10&lt;/code&gt;: This calls BIOS interrupt 0x10, which prints the character in AL.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;jmp print&lt;/code&gt;: This jumps back to the print label to process the next character.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;end: hlt&lt;/code&gt;: This halts the CPU when we're done printing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;string: .asciz "Hello World I am vivek"&lt;/code&gt;: This defines our null-terminated string.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;.fill 510-(.-init), 1, 0&lt;/code&gt;: This pads our bootloader to 510 bytes with zeros.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;.word 0xaa55&lt;/code&gt;: This is the boot signature that the BIOS looks for to determine if a sector is bootable.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Bootloader Flow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;The BIOS loads our bootloader into memory at address 0x7C00.&lt;/li&gt;
&lt;li&gt;Our code sets up the SI register to point to our string.&lt;/li&gt;
&lt;li&gt;We enter a loop that prints each character of the string:

&lt;ul&gt;
&lt;li&gt;Load a character&lt;/li&gt;
&lt;li&gt;Check if it's the end of the string (null terminator)&lt;/li&gt;
&lt;li&gt;If not, print it and continue&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;After printing the entire string, we halt the CPU.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Building the Bootloader
&lt;/h2&gt;

&lt;p&gt;To build the bootloader, we'll use a bash script that assembles the code, links it, and prepares it for execution. Save the following script as &lt;code&gt;build_bootloader.sh&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash &lt;/span&gt;
as sample_bootable.s &lt;span class="nt"&gt;-o&lt;/span&gt; sample_bootable.o
ld &lt;span class="nt"&gt;-o&lt;/span&gt; boot.bin &lt;span class="nt"&gt;--oformat&lt;/span&gt; binary &lt;span class="nt"&gt;-e&lt;/span&gt; init &lt;span class="nt"&gt;-Ttext&lt;/span&gt; 0x7c00 &lt;span class="nt"&gt;-o&lt;/span&gt; boot.bin sample_bootable.o
&lt;span class="nb"&gt;rm &lt;/span&gt;sample_bootable.o
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"execute command  :- qemu-system-x86_64 boot.bin "&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what each line does:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;as sample_bootable.s -o sample_bootable.o&lt;/code&gt;: Assembles our source file into an object file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ld -o boot.bin --oformat binary -e init -Ttext 0x7c00 -o boot.bin sample_bootable.o&lt;/code&gt;: Links the object file into a flat binary:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--oformat binary&lt;/code&gt;: Outputs a flat binary file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-e init&lt;/code&gt;: Sets the entry point to our &lt;code&gt;init&lt;/code&gt; label.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-Ttext 0x7c00&lt;/code&gt;: Sets the starting address to 0x7c00, where the BIOS loads the bootloader.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rm sample_bootable.o&lt;/code&gt;: Removes the intermediate object file.&lt;/li&gt;
&lt;li&gt;The echo command reminds you how to run the bootloader in QEMU.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Make the script executable 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;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x build_bootloader.sh
./build_bootloader.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create your &lt;code&gt;boot.bin&lt;/code&gt; file, which is your bootable image.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing the Bootloader
&lt;/h2&gt;

&lt;p&gt;You can test your bootloader using QEMU:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;qemu-system-x86_64 boot.bin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will start QEMU, load your bootloader, and you should see your message "Hello World I am vivek" printed on the screen.&lt;/p&gt;

&lt;h2&gt;
  
  
  Troubleshooting
&lt;/h2&gt;

&lt;p&gt;If you encounter issues:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Ensure your assembler and linker are correctly installed.&lt;/li&gt;
&lt;li&gt;Check that the file paths in the build script match your directory structure.&lt;/li&gt;
&lt;li&gt;Verify that QEMU is properly installed on your system.&lt;/li&gt;
&lt;li&gt;If the message doesn't appear, double-check your assembly code for typos.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Further Learning
&lt;/h2&gt;

&lt;p&gt;To expand your knowledge of bootloaders and low-level programming:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Modify the message or add more functionality to the bootloader.&lt;/li&gt;
&lt;li&gt;Learn about loading larger programs from disk.&lt;/li&gt;
&lt;li&gt;Study how real bootloaders transition from 16-bit real mode to 32-bit protected mode.&lt;/li&gt;
&lt;li&gt;Explore multiboot-compliant bootloaders for loading modern operating systems.&lt;/li&gt;
&lt;li&gt;Investigate UEFI (Unified Extensible Firmware Interface) bootloaders for newer systems.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congratulations! You've created a simple bootloader that prints a message to the screen. This project demonstrates the basics of how a computer starts up and how we can run our own code at the very beginning of the boot process. While this bootloader is simple, it forms the foundation for understanding more complex boot processes used in real-world systems.&lt;/p&gt;

&lt;p&gt;Remember, working with bootloaders means you're operating at a very low level. Always test in a virtual machine to avoid potentially damaging your computer's boot sector.&lt;/p&gt;

&lt;p&gt;Happy coding and exploring the fascinating world of low-level programming!&lt;/p&gt;

&lt;p&gt;github repo: &lt;a href="https://github.com/Vivx701/simple_bootable" rel="noopener noreferrer"&gt;https://github.com/Vivx701/simple_bootable&lt;/a&gt;&lt;/p&gt;

</description>
      <category>bootloader</category>
      <category>code</category>
    </item>
    <item>
      <title>Linux System Information Files Reference</title>
      <dc:creator>Vivek P</dc:creator>
      <pubDate>Sun, 08 Sep 2024 11:43:41 +0000</pubDate>
      <link>https://dev.to/vivx_developer/linux-system-information-files-reference-14b1</link>
      <guid>https://dev.to/vivx_developer/linux-system-information-files-reference-14b1</guid>
      <description>&lt;h1&gt;
  
  
  Linux System Information Files Reference
&lt;/h1&gt;

&lt;p&gt;This guide provides an overview of important system information files in Linux, typically found in the &lt;code&gt;/proc&lt;/code&gt; and &lt;code&gt;/sys&lt;/code&gt; filesystems. These files offer valuable insights into the system's current state and configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. /proc/ioports
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Shows registered I/O port regions and their usage by devices or drivers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example content:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0000-0cf7 : PCI Bus 0000:00
  0000-001f : dma1
  0020-0021 : pic1
  0040-0043 : timer0
  ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt; &lt;code&gt;cat /proc/ioports&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. /proc/interrupts
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Displays information about system interrupts, including which are in use and by which devices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example content:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;           CPU0       
  0:         36   IO-APIC-edge      timer
  1:          9   IO-APIC-edge      i8042
  ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt; &lt;code&gt;cat /proc/interrupts&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. /proc/meminfo
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Provides detailed information about system memory usage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example content:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MemTotal:        8174824 kB
MemFree:         3975548 kB
MemAvailable:    5631104 kB
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt; &lt;code&gt;cat /proc/meminfo&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. /proc/cpuinfo
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Contains detailed information about the system's CPU(s).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example content:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 142
model name      : Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt; &lt;code&gt;cat /proc/cpuinfo&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. /proc/devices
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Lists all character and block devices currently configured in the kernel.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example content:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  ...
Block devices:
  7 loop
  8 sd
 11 sr
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt; &lt;code&gt;cat /proc/devices&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  6. /proc/modules
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Lists all currently loaded kernel modules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example content:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;snd_hda_codec_hdmi 49152 1 - Live 0x0000000000000000
snd_hda_codec_realtek 106496 1 - Live 0x0000000000000000
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt; &lt;code&gt;cat /proc/modules&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  7. /proc/version
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Shows the Linux kernel version and additional system information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example content:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Linux version 5.4.0-42-generic (buildd@lcy01-amd64-027) (gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)) #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt; &lt;code&gt;cat /proc/version&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  8. /proc/partitions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Lists all partitions known to the system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example content:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;major minor  #blocks  name
   8        0  976762584 sda
   8        1     524288 sda1
   8        2  976236544 sda2
 ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt; &lt;code&gt;cat /proc/partitions&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  9. /proc/mounts
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Shows currently mounted filesystems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example content:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt; &lt;code&gt;cat /proc/mounts&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  10. /proc/net/dev
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Provides statistics about network interfaces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example content:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo: 2816326   34851    0    0    0     0          0         0  2816326   34851    0    0    0     0       0          0
  eth0: 1215332    2535    0    0    0     0          0        19   187816    1353    0    0    0     0       0          0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt; &lt;code&gt;cat /proc/net/dev&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  11. /sys/class/net
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Contains directories for each network interface, providing detailed information and controls.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; /sys/class/net                     &lt;span class="c"&gt;# List network interfaces&lt;/span&gt;
&lt;span class="nb"&gt;cat&lt;/span&gt; /sys/class/net/eth0/address       &lt;span class="c"&gt;# Show MAC address of eth0&lt;/span&gt;
&lt;span class="nb"&gt;cat&lt;/span&gt; /sys/class/net/eth0/statistics/rx_bytes  &lt;span class="c"&gt;# Show received bytes on eth0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  12. /proc/uptime
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Shows how long the system has been running.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example content:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;350735.47 1395914.78
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(The first number is total uptime in seconds, the second is idle time)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt; &lt;code&gt;cat /proc/uptime&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Notes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;These files are part of the procfs (process filesystem) and sysfs in Linux.&lt;/li&gt;
&lt;li&gt;They provide real-time information about the system's current state.&lt;/li&gt;
&lt;li&gt;The content of these files is generated on-the-fly when read.&lt;/li&gt;
&lt;li&gt;Some files may require root privileges to access, depending on system configuration.&lt;/li&gt;
&lt;li&gt;The /sys filesystem (sysfs) provides a more structured way to access kernel information and manipulate device parameters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Remember to use these files responsibly, especially in production environments, as reading them can impact system performance if done excessively.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
