<?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: Hayatu Sanusi</title>
    <description>The latest articles on DEV Community by Hayatu Sanusi (@hamicch).</description>
    <link>https://dev.to/hamicch</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%2F244604%2F901208f1-4c37-46a5-9dc6-9df63aa627e5.jpg</url>
      <title>DEV Community: Hayatu Sanusi</title>
      <link>https://dev.to/hamicch</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hamicch"/>
    <language>en</language>
    <item>
      <title>What happens when you type “ls -l *.c” in the shell?</title>
      <dc:creator>Hayatu Sanusi</dc:creator>
      <pubDate>Sun, 14 Nov 2021 10:45:39 +0000</pubDate>
      <link>https://dev.to/hamicch/what-happens-when-you-type-ls-l-c-in-the-shell-522e</link>
      <guid>https://dev.to/hamicch/what-happens-when-you-type-ls-l-c-in-the-shell-522e</guid>
      <description>&lt;p&gt;You have to understand what a &lt;em&gt;shell&lt;/em&gt; is in relation to the &lt;em&gt;kernel&lt;/em&gt; to understand what really happens when you run a command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsij3r8sg4vrocg6q92cl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsij3r8sg4vrocg6q92cl.png" alt="The shell as the interface between the kernel and the user, *RMIT University*"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Kernel&lt;/em&gt; is the most fundamental part of a computer operating system. It’s a program itself and it controls all other programs on the computer. It talks to hardware and software and is highly involved in resource management.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Shell&lt;/em&gt; is an application that executes programs called commands through an interactive user interface with an &lt;a href="https://en.wikipedia.org/wiki/Operating_system" rel="noopener noreferrer"&gt;operating system&lt;/a&gt;. It is the layer of programming that understands and executes the commands a user enters. In some systems, the shell is called a &lt;em&gt;command interpreter&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Use the syntax below to write a command with an argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;command [arg1] [arg2] .. [argn]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Now what is &lt;em&gt;ls&lt;/em&gt;?
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;ls&lt;/em&gt; is a shell command that lists files and directories within a directory. With the &lt;em&gt;-l&lt;/em&gt; option, &lt;em&gt;ls *.c&lt;/em&gt; will list out files and directories with a &lt;em&gt;.c&lt;/em&gt; extension in a long list format which includes their respective permissions, owners, and created date and time as seen in the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdvcr03g9v5r6iw1scebv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdvcr03g9v5r6iw1scebv.png" alt="Output after type ls *-l*.c* in the command line"&gt;&lt;/a&gt;&lt;/p&gt;
Shell project at www.alxafrica.com



&lt;h3&gt;
  
  
  What happens under the hood when you type “ls -l *.c” and “enter” in the shell:
&lt;/h3&gt;

&lt;p&gt;After inputting the command into the shell prompt, the shell reads the command, &lt;em&gt;ls -l&lt;/em&gt;.c* using &lt;em&gt;thegetLine()&lt;/em&gt; function’s STDIN data stream. From here, It stores the input into a buffer as a string.&lt;/p&gt;

&lt;p&gt;The buffer then reads from &lt;strong&gt;STDIN&lt;/strong&gt; to the given block size and writes each block to the standard output.&lt;/p&gt;

&lt;p&gt;Now the string is broken into tokens by removing whitespaces and then stored in an array of strings.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;shell&lt;/em&gt; checks if any of the tokens have an alias defined. If there is, it replaces the token with it’s value.&lt;/p&gt;

&lt;p&gt;If there isn’t, it checks if any of the tokens is a built-in function and if it’s not there either.&lt;/p&gt;

&lt;p&gt;The shell looks for a program file called ls in the shell’s environment where all the executable files are in the system, specifically in the $PATH variable. The $PATH variable is a list of directories the shell searches every time a command is entered. One of the environment variables, $PATH is parsed using the ‘=’ as a delimiter. Once the $PATH is identified, all the directories in $PATH are tokenized, each location specified using the ‘:’ delimiter, recursively searched by appending the command at the end of the path.&lt;/p&gt;

&lt;p&gt;Since it searches recursively, it will first search in the &lt;code&gt;pwd&lt;/code&gt; and then its parent and so on and so forth with all other commands.&lt;/p&gt;

&lt;p&gt;To execute ls, the following system calls are made: &lt;em&gt;fork(), execve() and wait().&lt;/em&gt;&lt;br&gt;
System calls are interactions we make with the kernel.&lt;/p&gt;

&lt;p&gt;To run a command in a shell, the &lt;em&gt;fork()&lt;/em&gt;  system call is made, this duplicates the shell by creating a child shell of the parent shell.&lt;/p&gt;

&lt;p&gt;Then the &lt;em&gt;execve()&lt;/em&gt; system call is made, which stops the old duplicate process (parent shell), loads up the new process(child shell which is &lt;em&gt;ls&lt;/em&gt; in this case) and start the new program which gives a brand new address with the program by replacing the current process's memory stack with the new data loaded from the &lt;em&gt;ls&lt;/em&gt; executable file.&lt;/p&gt;

&lt;p&gt;Meanwhile, using the &lt;em&gt;wait()&lt;/em&gt; system call, the parent process continues to keep track of it’s children in the background.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lastly
&lt;/h3&gt;

&lt;p&gt;Once the &lt;em&gt;ls -l&lt;/em&gt;.c* command is executed, the shell frees up memory, exits and prompts the user for input again.&lt;/p&gt;

&lt;p&gt;All these happen under a few milliseconds in most cases, depending your machine power.&lt;/p&gt;

&lt;p&gt;Did I misplace anything? let me know in the comments.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>computerscience</category>
      <category>ubuntu</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
