{"id":613,"date":"2025-04-10T04:00:36","date_gmt":"2025-04-10T04:00:36","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=613"},"modified":"2025-04-10T04:00:36","modified_gmt":"2025-04-10T04:00:36","slug":"linux-syscall","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=613","title":{"rendered":"Linux Syscall Explained For Beginners"},"content":{"rendered":"<p>In this blog, we will look into <strong>Linux System Calls<\/strong>, frequently discussed in DevOps and SRE interviews, especially in top product companies.<\/p>\n<p>Not everyone in DevOps gets to work with system internals or troubleshoot performance issues daily.<\/p>\n<p>Even in my 12+ years of DevOps career, I spent 80% of the time in design and implementation and 20% in troubleshooting issues.<\/p>\n<p>It totally depends on the job nature and the projects you choose to work on.<\/p>\n<p>However, it is very important for DevOps\/SRE folks to have a strong understanding of system troubleshooting because we usually work with servers.<\/p>\n<p>Syscall is a foundational concept in this regard.<\/p>\n<p>When you appear for DevOps\/SRE interviews, you can expect questions related to syscalls<\/p>\n<p>To get started, we need to be clear on a few basics.<\/p>\n<h2 id=\"kernel-space\">Kernel Space<\/h2>\n<p>This is where the core of the operating system, the kernel, operates.<\/p>\n<p>As per Wikipedia,<\/p>\n<blockquote><p>A&nbsp;<strong>kernel<\/strong>&nbsp;is a&nbsp;computer program&nbsp;at the core of a&nbsp;computer&#8217;s&nbsp;operating system&nbsp;that constantly has complete control over everything in the system (Wikipedia).<\/p><\/blockquote>\n<p>The kernel controls everything:&nbsp;<strong>memory, processes, hardware, drivers, security, and more.<\/strong><\/p>\n<p>Also, it<strong>&nbsp;<\/strong>directly interacts with the CPU, RAM, disk, and other hardware and has unrestricted access to all system resources.<\/p>\n<h2 id=\"userspace\">Userspace<\/h2>\n<p>This is the environment where&nbsp;<strong>user facing applications run<\/strong>&nbsp;(Web servers, Chrome, Text editors, command utilities etc).<\/p>\n<p>It is like a&nbsp;<strong>restricted zone<\/strong>&nbsp;because it can\u2019t directly access hardware or manage system resources.<\/p>\n<p>That is why usually if an application crashes, it doesn\u2019t crash the whole OS.<\/p>\n<h2 id=\"syscall-system-calls\">Syscall (System Calls)<\/h2>\n<p>The programs in&nbsp;<strong>Userspace<\/strong>&nbsp;need&nbsp;<strong>Kernel Space<\/strong>&nbsp;to access system resources.<\/p>\n<p>This communication is handled via System Calls (Syscalls).<\/p>\n<p>In this communication the Kernel acts as a&nbsp;<strong>middleman<\/strong>, making sure userspace programs&nbsp;<strong>don\u2019t mess up the system.<\/strong><\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/blog.techiescamp.com\/content\/images\/2025\/02\/image-d971a6cae0a4c59d.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"924\" height=\"544\"><\/figure>\n<div class=\"kg-card kg-callout-card kg-callout-card-blue\">\n<div class=\"kg-callout-emoji\">\ud83d\udca1<\/div>\n<div class=\"kg-callout-text\">System calls are part of the&nbsp;<b><strong style=\"white-space: pre-wrap;\">kernel<\/strong><\/b>. They are implemented as functions in the&nbsp;<b><strong style=\"white-space: pre-wrap;\">kernel space<\/strong><\/b>, and user programs can access them using specific instructions or libraries (like the C library,&nbsp;<code spellcheck=\"false\" style=\"white-space: pre-wrap;\">glibc<\/code>).<\/div>\n<\/div>\n<h2 id=\"practical-example-the-ls-command\">Practical Example: The ls Command<\/h2>\n<p>To better understand the interaction between user space and kernel space, let&#8217;s consider a real-world example using the simple Linux&nbsp;<code>ls<\/code>&nbsp;command.<\/p>\n<p><code>ls<\/code>&nbsp;is a userspace utility that lists directory contents.<\/p>\n<p>Every time you type&nbsp;<code>ls<\/code>&nbsp;to list files, your system is secretly making&nbsp;<strong>dozens of syscalls<\/strong>&nbsp;to communicate with the Kernel.<\/p>\n<p>The following illustration gives you an idea of how all these interactions happen, from the user to the system hardware (disk) through the kernel.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/blog.techiescamp.com\/content\/images\/2025\/02\/image-ba2ae568e06ff293.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"920\" height=\"787\"><\/figure>\n<h2 id=\"types-of-system-calls\">Types of System Calls<\/h2>\n<p>Now, let&#8217;s look at the types of system calls.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-blue\">\n<div class=\"kg-callout-emoji\">\ud83d\udca1<\/div>\n<div class=\"kg-callout-text\">The system call functions given below are&nbsp;<b><strong style=\"white-space: pre-wrap;\">C library functions<\/strong><\/b>, but they are&nbsp;<b><strong style=\"white-space: pre-wrap;\">wrappers around system calls<\/strong><\/b>&nbsp;provided by the operating system<\/p>\n<p>These functions internally invoke&nbsp;<b><strong style=\"white-space: pre-wrap;\">Linux system calls<\/strong><\/b>&nbsp;to interact with the kernel.<\/div>\n<\/div>\n<h4 id=\"1-process-control\">1. Process Control<\/h4>\n<p>To&nbsp;manage processes (creation, termination, execution).<\/p>\n<ol>\n<li><code>fork()<\/code>: Create a new process.<\/li>\n<li><code>exec()<\/code>: Replace the current process with a new program.<\/li>\n<li><code>exit()<\/code>: Terminate a process.<\/li>\n<li><code>wait()<\/code>: Pause until a child process finishes.<\/li>\n<li><code>kill()<\/code>: Send signals to processes (e.g., terminate or pause).<\/li>\n<\/ol>\n<div class=\"kg-card kg-callout-card kg-callout-card-blue\">\n<div class=\"kg-callout-text\"><code spellcheck=\"false\" style=\"white-space: pre-wrap;\">fork()<\/code>&nbsp;vs&nbsp;<code spellcheck=\"false\" style=\"white-space: pre-wrap;\">exec()<\/code>&nbsp;system call is one of the popular interview questions.<\/div>\n<\/div>\n<h3 id=\"2-file-management\">2. File Management<\/h3>\n<p>To handle file operations (read, write, modify metadata).<\/p>\n<ul>\n<li><code>open()<\/code>: Open a file for reading\/writing.<\/li>\n<li><code>close()<\/code>: Release access to a file.<\/li>\n<li><code>read()<\/code>: Read data from a file.<\/li>\n<li><code>write()<\/code>: Write data to a file.<\/li>\n<li><code>unlink()<\/code>: Delete a file.<\/li>\n<li><code>chmod()<\/code>: Change file permissions.<\/li>\n<\/ul>\n<h3 id=\"3-device-management\">3. Device Management<\/h3>\n<p>To control hardware devices (treated as files in Unix-like systems).<\/p>\n<ul>\n<li><code>ioctl()<\/code>: Configure device-specific operations.<\/li>\n<li><code>read()<\/code>: Read data from a device.<\/li>\n<li><code>write()<\/code>: Write data to a device.<\/li>\n<\/ul>\n<h3 id=\"4-information-maintenance\">4. Information Maintenance<\/h3>\n<p>To get or set system\/process data.<\/p>\n<ul>\n<li><code>getpid()<\/code>: Retrieve the current process ID.<\/li>\n<li><code>time()<\/code>: Fetch system time.<\/li>\n<li><code>sysinfo()<\/code>: Check system resource usage.<\/li>\n<li><code>setpriority()<\/code>: Adjust process scheduling priority.<\/li>\n<\/ul>\n<h3 id=\"5-communication-ipc\">5. Communication (IPC)<\/h3>\n<p>To enable inter-process communication (IPC) or networking.<\/p>\n<ul>\n<li><code>pipe()<\/code>: Create a unidirectional data channel.<\/li>\n<li><code>shmget()<\/code>: Allocate shared memory.<\/li>\n<li><code>socket()<\/code>: Establish network communication.<\/li>\n<li><code>send()<\/code>: Transfer data over a network.<\/li>\n<li><code>recv()<\/code>: Receive data over a network.<\/li>\n<\/ul>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Using syscalls in day-to-day work is very rare unless you work with low-level system programming, kernel development, performance tuning, etc.<\/p>\n<p>However, understanding syscalls can be helpful to in roles like&nbsp;<strong>DevOps, SRE, and backend engineering<\/strong>&nbsp;when troubleshooting performance issues, debugging applications, etc.<\/p>\n<p>If you have any doubts about this blog, drop it on the comment!<\/p>\n<p>Want to Stay Ahead in DevOps &amp; Cloud? Join the Free Newsletter Below.<\/p>\n<p><!--kg-card-begin: html--><br \/>\n <iframe loading=\"lazy\" src=\"https:\/\/embeds.beehiiv.com\/2a495ef4-3de7-4600-8a0d-de5dc968b372\" data-test-id=\"beehiiv-embed\" width=\"100%\" height=\"320\" frameborder=\"0\" scrolling=\"no\" style=\"border-radius: 4px; border: 2px solid #e5e7eb; margin: 0; background-color: transparent;\"><\/iframe><br \/>\n<!--kg-card-end: html--><\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/linux-syscall\/\" target=\"_blank\" rel=\"noopener noreferrer\">Linux Syscall Explained For Beginners \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/linux-syscall\/<\/p>\n","protected":false},"author":1,"featured_media":614,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-613","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=613"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/613\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/614"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}