<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by megha mohan on Medium]]></title>
        <description><![CDATA[Stories by megha mohan on Medium]]></description>
        <link>https://medium.com/@meghamohan?source=rss-7b5307b626c3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*rrGTxInJ2vQQqoKj3DPqwQ.jpeg</url>
            <title>Stories by megha mohan on Medium</title>
            <link>https://medium.com/@meghamohan?source=rss-7b5307b626c3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 06 Nov 2026 07:49:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@meghamohan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[How the Internet comes together!]]></title>
            <link>https://medium.com/@meghamohan/how-the-internet-comes-together-b959b3d5ba4?source=rss-7b5307b626c3------2</link>
            <guid isPermaLink="false">https://medium.com/p/b959b3d5ba4</guid>
            <category><![CDATA[servers]]></category>
            <category><![CDATA[internet]]></category>
            <category><![CDATA[dns]]></category>
            <category><![CDATA[holberton-school]]></category>
            <dc:creator><![CDATA[megha mohan]]></dc:creator>
            <pubDate>Wed, 09 Aug 2017 03:14:27 GMT</pubDate>
            <atom:updated>2017-08-13T22:42:26.451Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*D0j2gVn9hfX20iHj28cRNQ.jpeg" /></figure><p>For the Tech Interviews the age old question that anyone could expect is “What happens when you type <a href="http://www.google.com">www.google.com</a> ?”. It is always expected from any Software Engineer to understand in depth about the working of the Internet, the way in which computers contact each other and do things such as exchange email or display web pages. In this article I am going to try my best to explain my understanding about the slightly modified question, <strong>What happens when you type “holbertonschool.com” in your browser and press enter?</strong></p><p><strong>Step1</strong>: Type “holbertonschool.com” and press enter</p><p><strong>Step2</strong>: The typed URL is parsed and the browser checks if the domain is in its cache or not. If not it proceeds to make a system call (gethostbyname) for a lookup in the localhost file. If not found, it goes on to check in the Router cache for the IP.</p><p><strong>Step3</strong>: We still dont have the IP and so the next step is for your browser to send a UDP request to the DNS server configured in the OS’s Internet settings for the connection being used, passing the domain name. This DNS server typically belongs either to your company or to your local Internet Service Provider. DNS resolver is queried for the IP now. Resolver goes through a process called Recursion to find the IP corresponding to the domain name. Resolver starts by querying one of the root DNS servers. There are 13 root servers from A-M in over 380 locations. All these servers are copies of one master server run by IANA(Internet Assigned Numbers Authority). The Root servers hold locations of all TLD(Top Level Domain) servers such as .com, .de, .io etc. The root server may not know the IP address for ‘holbertonschool.com’ but it knows that .com server might know it, so it returns the location of the .com server. So the root responds with a list of .com servers.</p><p><strong>Step 4</strong>: Now the Resolver queries the .com servers. Once it gets the IP for ‘holbertonschool.com’, Browser will establish an SSL connection over HTTPS ( Hyper Text Transmission Protocol Secure, which is the secure version of HTTP) to ensure that the communication between server and client is private and encrypted. Similar to TCP Handshake(which will be explained soon), SSL also establishes a connection with the client through SSL handshake, which can be easily understood from the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/440/1*KyLRCXA3u4KrPJbI5DFNfw.png" /></figure><p><strong>Step 5:</strong> One thing to note is that its not just SSL certificate that ensures security for the data transmission. There is another component called Firewall which prevents hackers from accessing your system. (More explanation about Firewall can be found below. )</p><p>Now the browser has to serve the web page. For that it sends HTTP request to the Server using GET. Browser passes some meta information in the form of headers to the server along with the URL “holbertonschool.com”. HTTP requests made form browsers are handled by a special Software running on the server known as Web Server. example Apache, Nginx, IIS.</p><p><strong>Step 6</strong>: The Web server then passes the information to a Request Handler ( a program written to handle Web Server. example PHP, ASP.NET ). If the page to be served is Static, it is generated and an HTML response is created. This is then sent back to the browser. The serving of Webpage is done such that initially the HTML skeleton is received, followed by the static components like images, styling, javascript files etc. (Thats the reason why we sometimes face delay in loading of images even after the web page has been loaded!). These static files are also cached in the web browser so that they do not have to be transmitted the next time and can be served directly from cache.</p><p>If the information to be displayed is dynamic(Javascript, Ruby, PHP), the Application Server assists in pulling up the data from the codebase, translate them to an understandable format by compiling and then sends back to the client. If the data is to be retrieved from Database, Application Server again acts as a mediator, establishes a connection to the <strong>Database</strong>, pulls up the data and sends it back to the client.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oIivL9XjGrK9LVor7bJYDw.jpeg" /></figure><p>The above steps are just a high level explanation of what is actually happening behind the scenes in a matter of milli seconds when you enter holbertonschool.com. There are numerous terminologies that you should be well aware of while understanding the above steps. I shall explain them in detail.</p><ul><li><strong>DNS request</strong></li></ul><p>Domain Name System (DNS) can be explained as the heart of how the Internet works. When someone wants to open a webpage, he types in the URL and the DNS resolves and gives back the IP address of the webpage for the browser to be served. In other words, the DNS acts as a post office which accepts the address written in plain English(holbertonschool.com) and converts them to a series of numbers or IP (123.43.23.121), marks the location of a computer on the internet, similar to the house number and street of where you live. For more details on how DNS plays role here, go back and read <strong>Step2</strong>.</p><ul><li><strong>TCP/IP</strong></li></ul><p>TCP(Transmission Control Protocol) breaks down and reassembles the data packets whereas Internet Protocol(IP) is responsible for ensuring that the packets are sent to the right destination. At first, for establishing any TCP/IP connection, the client(PC) sends a SYN packet. Web server(for eg, google.com) sends SYN-ACK packet back. Client then answers with an ACK packet thus concluding a 3 way TCP connection establishment.(This is known as TCP Handshake).</p><p>TCP/IP is used is because the internet is a Packet switched network, where information sent is broken into small packets, sent over many different routes at the same time and then reassembled by the receiving end. Fascinating isn’t it? Well, each of these packets should be fewer than 1500 character length due to a lot of reasons, one of which is hardware limitation. So you can imagine the number of packets being sent for a single web page request, the packets being transported through different routes and reassembled to display the webpage! To ensure that no errors have occurred during the transmission and that all packets have been received, as packets are created, TCP also calculates and adds to the header a ‘checksum’ which is a number that it uses on the receiving end Checksum is based on the precise amount of data in the packet.</p><p>For computers to use TCP/IP they need special software known as Sockets. Read <strong>Step</strong> <strong>3</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O6xcvDO-35bKVX47nh2UoA.jpeg" /></figure><ul><li><strong>Firewall</strong></li></ul><p>Everytime a computer is connected to a network it faces potential danger by hackers. Firewall comes as a savior which enable anyone to access the internet but protecting from crackers, hackers and others on the internet from gaining access to the corporate or personal network causing damage. Firewalls are hardware and software combinations that are built using routers, servers and a variety of hardwares.</p><ul><li><strong>HTTPS/SSL</strong></li></ul><p>HTTPS(Hypertext Transfer Protocol Secure) is HTTP over SSL, where the SSL connection is established first, and then normal HTTP data is exchanged over this SSL connection. HTTPS is in other words the protocol for secure communication and it prevents hackers from information that visitors send or receive over the Internet.</p><p>To learn more about it, go back to <strong>Step 4.</strong></p><ul><li><strong>Load-balancer</strong></li></ul><p>Servers should be designed to accept concurrent requests from a large audience and serve response to them without crashing it. To serve the large number of requests, usually the content is distributed across multiple servers. A <strong>load balancer</strong> is placed in front of the server to redirect the incoming traffic so that the load on each server is reduced, hence the name! It helps in high availability, reduces load on the server and helps in serving the response faster. There are a lot of algorithms to connect load balancer and server. The load balancing algorithm determines which active servers on the backend can be used. A few of the commonly used algorithms are Round Robin, Least connections, Source.</p><ul><li><strong>Web server</strong></li></ul><p>A web server<strong> </strong>stores and delivers the content for a website to clients that request it. The most common type of client is a web browser program, which requests data from your website when a user clicks on a link or downloads a document on a page displayed in the browser.</p><p>A web server communicates with a web browser using the <a href="https://www.nginx.com/resources/glossary/http/">Hypertext Transfer Protocol</a> (HTTP). The content of most web pages is encoded in Hypertext Markup Language (HTML). The content can be static (for example, text and images) or dynamic .</p><ul><li><strong>Application server</strong></li></ul><p>Application Server consists of Web server connectors, Runtime libraries, Database connectors etc which are needed to deploy, configure and manage code and connect these components together on a server.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b959b3d5ba4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mutable vs Immutable Objects in Python]]></title>
            <link>https://medium.com/@meghamohan/mutable-and-immutable-side-of-python-c2145cf72747?source=rss-7b5307b626c3------2</link>
            <guid isPermaLink="false">https://medium.com/p/c2145cf72747</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[holberton-school]]></category>
            <category><![CDATA[oop]]></category>
            <category><![CDATA[object-oriented]]></category>
            <dc:creator><![CDATA[megha mohan]]></dc:creator>
            <pubDate>Thu, 25 May 2017 06:06:31 GMT</pubDate>
            <atom:updated>2017-05-31T05:36:09.282Z</atom:updated>
            <content:encoded><![CDATA[<p>Everything in Python is an object. And what every newcomer to Python should quickly learn is that all objects in Python can be either <strong>mutable</strong> or <strong>immutable</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oErPCXv1PFcuuizXqGEEbw.png" /></figure><p>Lets dive deeper into the details of it… Since everything in Python is an Object, every variable holds an object instance. When an object is initiated, it is assigned a unique object id. Its type is defined at runtime and once set can never change, however its state can be changed if it is mutable. Simple put, a <strong>mutable</strong> object can be changed after it is created, and an <strong>immutable</strong> object can’t.</p><blockquote><em>Objects of built-in types like (int, float, bool, str, tuple, unicode) are immutable. Objects of built-in types like (list, set, dict) are mutable. Custom classes are generally mutable. To simulate immutability in a class, one should override attribute setting and deletion to raise exceptions.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*uFlTNY4W3czywyU18zxl8w.png" /></figure><p>Now comes the question, how do we find out if our variable is a mutable or immutable object. For this we should understand what ‘ID’ and ‘TYPE’ functions are for.</p><h3><strong>ID and TYPE</strong></h3><p>The built-in function <strong>id</strong>() returns the identity of an object as an integer. This integer usually corresponds to the object’s location in memory, although this is specific to the Python implementation and the platform being used. The <strong>is</strong> operator compares the identity of two objects.</p><p>The built-in function type() returns the type of an object. Lets look at a simple example</p><pre>&#39;&#39;&#39; Example 1 &#39;&#39;&#39;<br>&gt;&gt;&gt; x = &quot;Holberton&quot;<br>&gt;&gt;&gt; y = &quot;Holberton&quot;<br>&gt;&gt;&gt; id(x)<br>140135852055856<br>&gt;&gt;&gt; id(y)<br>140135852055856<br>&gt;&gt;&gt; print(x is y) &#39;&#39;&#39;comparing the types&#39;&#39;&#39;<br>True</pre><pre>&#39;&#39;&#39; Example 2 &#39;&#39;&#39;<br>&gt;&gt;&gt; a = 50<br>&gt;&gt;&gt; type(a)<br>&lt;class: ‘int’&gt;<br>&gt;&gt;&gt; b = &quot;Holberton&quot;<br>&gt;&gt;&gt; type(b)<br>&lt;class: &#39;string&#39;&gt;</pre><p>We have now seen how to compare two simple string variables to find out the types and id’s .So using these two functions, we can check to see how different types of objects are associated with variables and how objects can be changed .</p><h3><strong>Mutable and Immutable Objects</strong></h3><p>So as we discussed earlier, a mutable object can change its state or contents and immutable objects cannot.</p><blockquote><strong>Mutable objects</strong>:</blockquote><blockquote>list, dict, set, byte array</blockquote><blockquote><strong>Immutable objects:</strong></blockquote><blockquote>int, float, complex, string, tuple, frozen set [note: immutable version of set], bytes</blockquote><p>A practical example to find out the mutability of object types</p><pre>x = 10</pre><pre>x = y</pre><p>We are creating an object of type int. identifiers x and y points to the same object.</p><pre>id(x) == id(y)</pre><pre>id(y) == id(10)</pre><p>if we do a simple operation.</p><pre>x = x + 1</pre><p>Now</p><pre>id(x) != id(y)</pre><pre>id(x) != id(10)</pre><p>The object in which x was tagged is changed. object 10 was never modified. <strong>Immutable objects doesn’t allow modification after creation</strong></p><p>In the case of <strong>mutable objects</strong></p><pre>m = list([1, 2, 3])</pre><pre>n = m</pre><p>We are creating an object of type list. identifiers m and m tagged to the same list object, which is a collection of 3 immutable int objects.</p><pre>id(m) == id(n)</pre><p>Now poping an item from list object does change the object,</p><pre>m.pop()</pre><p>object id will not be changed</p><pre>id(m) == id(n)</pre><p>m and n will be pointing to the same list object after the modification. The list object will now contain [1, 2].</p><p>So what have we seen so far from the above examples?</p><ul><li>Python handles mutable and immutable objects differently.</li><li>Immutable are quicker to access than mutable objects.</li><li>Mutable objects are great to use when you need to change the size of the object, example list, dict etc.. Immutables are used when you need to ensure that the object you made will always stay the same.</li><li>Immutable objects are fundamentally expensive to “change”, because doing so involves creating a copy. Changing mutable objects is cheap.</li></ul><h3><strong>Exceptions in immutability..</strong></h3><p>Not all of the immutable objects are actually immutable. Confused? Let me explain.</p><p>As discussed earlier, Python containers liked tuples are immutable. That means value of a tuple can&#39;t be changed after it is created. But the &quot;value&quot; of a tuple is infact a sequence of names with unchangeable bindings to objects. The key thing to note is that the <em>bindings</em> are unchangeable, not the objects they are bound to.</p><p>Let us consider a tuple <strong>t = (‘holberton’, [1, 2, 3])</strong></p><p>The above tuple <strong>t</strong> contains elements of different data types, the first one is an immutable string and the second one is a mutable list.The tuple itself isn’t mutable . i.e. it doesn’t have any methods for changing its contents. Likewise, the string is immutable because strings don’t have any mutating methods. But the list object does have mutating methods, so it can be changed. This is a subtle point, but nonetheless important: the “value” of an immutable object <em>can’t</em> change, but it’s constituent objects <em>can</em>.</p><h3><strong>How objects are passed to Functions</strong></h3><p>Its important for us to know difference between mutable and immutable types and how they are treated when passed onto functions .Memory efficiency is highly affected when the proper objects are used.</p><p>For example if a mutable object is called by reference in a function, it can change the original variable itself. Hence to avoid this, the original variable needs to be copied to another variable. Immutable objects can be called by reference because its value cannot be changed anyways.</p><pre>def updateList(list1):<br>    list1 += [10]</pre><pre>n = [5, 6]<br>print(id(n))                  # 140312184155336</pre><pre>updateList(n)<br>print(n)                      # [5, 6, 10]<br>print(id(n))                  # 140312184155336</pre><p>As we can see from the above example, we have called the list via <strong>call by reference</strong>, so the changes are made to the original list itself.</p><p>Lets take a look at another example:</p><pre>def updateNumber(n):<br>    print(id(n))<br>    n += 10</pre><pre>b = 5<br>print(id(b))                   # 10055680<br>updateNumber(b)                # 10055680<br>print(b)                       # 5</pre><p>In the above example the same object is passed to the function, but the variables value doesn’t change even though the object is identical. This is called <strong>pass</strong> <strong>by value</strong>. So what is exactly happening here? When the value is called by the function, only the value of the variable is passed, not the object itself. So the variable referencing the object is not changed, but the object itself is being changed but within the function scope only. Hence the change is not reflected.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2145cf72747" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Everything you need to know about Libraries in C]]></title>
            <link>https://medium.com/@meghamohan/everything-you-need-to-know-about-libraries-in-c-e8ad6138cbb4?source=rss-7b5307b626c3------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8ad6138cbb4</guid>
            <category><![CDATA[c-program]]></category>
            <category><![CDATA[holberton-school]]></category>
            <category><![CDATA[compilation]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[gcc]]></category>
            <dc:creator><![CDATA[megha mohan]]></dc:creator>
            <pubDate>Mon, 15 May 2017 06:08:49 GMT</pubDate>
            <atom:updated>2017-05-15T06:08:49.441Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*oSrmUlg7Q9WE34FMEPZkLw.jpeg" /></figure><p>In my last blog post I have discussed about the usage of <strong>Static Libraries</strong> in C ,when and how to use it. If you haven’t already read about Static Libraries, check it out: <a href="https://medium.com/@meghamohan/all-about-static-libraries-in-c-cea57990c495">All about Static Libraries in C</a> . I have briefly mentioned towards the end about the advantages of Shared Library/Dynamic Library over Static Library. As it was a topic that requires a separate blogpost for itself, here we are, so let us dive a little deeper about Dynamic Libraries and about its advantages over Static Library here.</p><p>So to recap, what is a Library and why and how is it used? And what is Static and Dynamic Library?</p><blockquote>A library is a collection of items that you can call from your program. You can save much time by reusing work someone else has already done and be more confident that it has fewer bugs (since probably many other people use the libraries too, and you benefit from having them finding and fixing bugs). A library is exactly like an executable, except instead of running directly, the library functions are invoked with parameters from your executable.</blockquote><p>You would be familiar about the compilation of a C file. If not check out my blogpost <a href="https://medium.com/@meghamohan/everything-you-want-to-know-about-gcc-fa5805452f96">here</a>. The role of Libraries comes in the final step of compilation which is called ‘<strong>Linking</strong>’. One of the main tasks for linker is to make code of library functions (like printf, strlen, etc.) available to your program. Let us understand the difference between both Static and Dynamic Library and how it is being linked.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/1*pGyDVISobv8hSgJLOOeQ7w.jpeg" /></figure><h4>Reviewing the Linking of Static Library</h4><p>This is the most straight forward way of using a library function linking the object files from the library directly into your final executable. Hence the name ‘Static’, because the library will remain unchanged unless the program is recompiled. The final product is a simple executable with no external dependencies.</p><p>As a quick refresher, let me list the steps of creating a Static Library below. But if you would like to know in full detail about how to create a Static Library, refer to my <a href="https://medium.com/@meghamohan/all-about-static-libraries-in-c-cea57990c495">blogpost</a> .</p><p>Step 1: Compile your Library code(.c files) into an object code</p><pre>$ gcc -c *.c</pre><p>Step 2: Create a Static Library from all “.o” files using command ar</p><pre>$ ar -rc libholberton.a *.o</pre><p>This tells ar to create an archive (option c) and to insert the objects, replacing older files where needed (option r)</p><p>Step 3: Next step is to create index for the library using ranlib command. ranlib<em> makes a header in the library with the symbols of the object file contents.This helps the compiler to quickly reference symbols.</em></p><p>$ ranlib libholberton.</p><p>Step 4: We have now created a static library libholberton.a. Now let us use the static library by invoking it as part of the compilation and linking process when creating a program executable. Incase of gcc we use following flags to create static library</p><pre>gcc main.c -L. -lholberton -o main</pre><ul><li><strong>L : </strong>specifies the path to the library .We can use -L. inorder to point to the current directory and -L/home/tmp to point to the /home/tmp directory.</li></ul><p>Now run the executable program ‘main’</p><pre>$./main</pre><p>So here are the <strong>advantages</strong> of using Static Libraries.</p><ul><li>Static libraries are not required at runtime, so you do not need to include them when you distribute your executable.</li><li>At compile time, linking to a static library is generally faster than linking to individual source files.</li></ul><p>Static linking is very straight forward, but has a number of <strong>drawbacks</strong>.The two main disadvantages are..</p><ul><li>If the library code is updated (say, to fix a bug) you have to recompile your program into a new executable.</li><li>Every program in the system that uses that library contains a copy in its executable. This is very inefficient (and a pain if you find a bug and have to recompile, as per point one).</li></ul><h4>Reviewing the Linking of Dynamic Library</h4><blockquote>Simply put, A shared library/ Dynamic Library is a library that is loaded dynamically at runtime for each application that requires it. Dynamic Linking doesn’t require the code to be copied, it is done by just placing name of the library in the binary file. The actual linking happens when the program is run, when both the binary file and the library are in memory.</blockquote><p>Dynamic libraries have advantages and disadvantages as well.</p><ul><li>They load only a single copy of the library file in memory when you run a program, so a lot of memory is saved when you start running multiple programs using that library.</li><li>The other benefit is that if any update is made to the functions inside a Library, your programs don’t need to be recompiled ,whereas incase of Static Library you had to recompile or else the update wont be reflected. This type of runtime dynamic linking allows for easier library maintenance.</li></ul><p>A major disadvantage of Dynamic Library is that they take more time to load and get the program running because the linking occurs at runtime instead of during compilation. Since there will be no dynamic querying of symbols in static libraries, many softwares still use Static Library for better speed.</p><h4><strong>How to create Dynamic Library</strong></h4><p>Now that we know the difference between a Static and Dynamic Library, let us learn in detail about how to create a Dynamic Library and make the executable run.</p><p>The steps of compilation of Dynamic Library is almost similar to that of Static Library but with a fe extra steps.</p><p><strong>Step 1</strong>: Compile all the C files in your current directory using the below command</p><pre>gcc -Wall -Werror <a href="http://www.adp-gmbh.ch/cpp/gcc/gcc_options.html#opt_c">-c</a> <a href="http://www.adp-gmbh.ch/cpp/gcc/gcc_options.html#opt_f_cap_pic">-fPIC</a> *.c -o dynTest.o</pre><p>As you can see, the command is very similar to that of Static Library, extra option here is -fPIC. This makes position independent code (PIC), which is a requirement for dynamic library. We first compile our c code but do not link them, by giving the -c option. The -Wall and -Werror options are just to enable warnings and convert them into errors.</p><p><strong>Step 2</strong>: Now we have an object code dynTest.o in our current directory. We need to convert this into a dynamic(shared) object file which can be linked with other objects to form an executable. This is accomplished using the following command.</p><pre>$ gcc -shared -o dynLib.so dynTest.o</pre><p>The -sharedoption creates a shared object(.so) which can be linked with other objects to form an executable.The -ooption allows us to choose the output filename(Here dynLib.so). The .so suffix stands for Shared Object, and this is convention for Dynamic Library names.</p><p><strong>Step 3:</strong> This creates a Dynamic Library file called dynTest.so in your current directory. Next step is for us to understand how to use this Dynamic Library that we have created. Since Dynamic Library is linked during runtime, we obviously need to make this file available during runtime. The dynamic linker searches standard paths available in the <strong>LD_LIBRARY_PATH</strong> and also searches in system cache (for details explore the command <strong><em>ldconfig</em></strong>) for dynamic libraries. So our next step is to add our working directory to the LD_LIBRARY_PATH environment variable so that the linker can find our library file. The below command is used for this.</p><pre>$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/my_path/</pre><p>If you do not want to export your current path to <strong>LD_LIBRARY_PATH</strong> for the dynamic library to work, there is another way to make it work. Move the library to <strong>/usr/local/lib</strong> because ‘/usr/local/lib’ is already a path specified in the ‘LD_LIBRARY_PATH’ environment variable. Now we need to run <strong>ldconfig</strong> on the directory you moved it to. This will add our library to a cache which is searched through when a program looks for a shared library:</p><pre>$ ldconfig /usr/local/lib</pre><p>Now we are all set! Our dynamic library is ready for use. We can compile and run our program myfile.c now using our dynamic library dynTest.so.</p><pre>$ gcc -Wall -Werror -o myfile.c dynTest.so</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8ad6138cbb4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[All about Static Libraries in C]]></title>
            <link>https://medium.com/@meghamohan/all-about-static-libraries-in-c-cea57990c495?source=rss-7b5307b626c3------2</link>
            <guid isPermaLink="false">https://medium.com/p/cea57990c495</guid>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[static-library]]></category>
            <category><![CDATA[holberton-school]]></category>
            <dc:creator><![CDATA[megha mohan]]></dc:creator>
            <pubDate>Mon, 06 Mar 2017 03:00:13 GMT</pubDate>
            <atom:updated>2017-03-07T21:48:24.815Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*D3kv9uxMEEImg6zEXycvZw.jpeg" /></figure><p>Simply put, a library is a collection of items that you can call from your program. It obviously has a lot of advantages, not least of which is that you can save much time by reusing work someone else has already done and be more confident that it has fewer bugs (since probably many other people use the libraries too, and you benefit from having them finding and fixing bugs). A library is exactly like an executable, except instead of running directly, the library functions are invoked with parameters from your executable.</p><p><strong>So what is a Static library??</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/615/1*J5fqmdDTjrmZaZO7Ctd6sg.png" /></figure><p>The most straight forward way of using a library function is to have the object files from the library linked directly into your final executable, just as with those you have compiled yourself. When linked like this the library is called a <em>static</em> library, because the library will remain unchanged unless the program is recompiled. This is the most straight forward way of using a library as the final result is a simple executable with no dependencies.</p><p>There is other type of library called <em>Dynamic</em><strong><em> </em></strong><em>Library</em>( or Shared Library) ,but more on that later, for now we will discuss about Static Library in detail.</p><blockquote>Static libraries are object files that are later combined with another object to form a final executable.</blockquote><p>By convention they have the prefix lib and the suffix .a — for example, libholberton.a</p><h3>Creating a Static Library file</h3><p>To create a static library using GCC we need to compile our library code into an object file so we tell GCC to do this using -c</p><pre>$ gcc -c *.c</pre><p>Here in the above command , all the .c extension files( C files) in the current working directory have been converted in to their respective object files. Once we have object file(s), we use the <strong>GNU ar</strong> command to create our final library/archive</p><blockquote>The <strong>archiver</strong>, also known simply as <strong>ar</strong>, is a <a href="https://en.wikipedia.org/wiki/Unix">Unix</a> utility that maintains groups of files as a single <a href="https://en.wikipedia.org/wiki/Archive_file">archive file</a>.</blockquote><pre>$ ar -rc libholberton.a *.o</pre><p>This tells ar to create an archive (option c) and to insert the objects, replacing older files where needed (option r) .</p><blockquote>Whenever files are added to a library, including the initial creation of the library , the library needs to be <em>indexed</em>, which is done with the command ranlib. ranlib makes a header in the library with the symbols of the object file contents.This helps the compiler to quickly reference symbols. A large library may have thousands of symbols meaning an index can significantly speed up finding references.</blockquote><p>$ ranlib libholberton.a</p><p>This step may or may not be necessary depending on your computer system or your archiver(not necessary with ar).</p><p>If we want to see the contents of our library, we can use the ar option -t.</p><p>ar -t libholberton.a</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/805/1*Lb3hKd4n-fdDHir7yNZnnA.png" /></figure><p>We can also see the symbols in our library, using the command nm, which lists each symbol’s symbol value, symbol type, and symbol name from object files.</p><p>nm lib_test.a</p><p>We have now created a static library libholberton.a and now let us use the static library by invoking it as part of the compilation and linking process when creating a program executable. Incase of gcc we use following flags to create static library</p><ul><li><strong>-l&lt;<em>libraryname without lib prefix and extension&gt;</em></strong></li><li><strong>-L : </strong>specifies the path to the library .We can use -L. inorder to point to the current directory and -L/home/tmp to point to the /home/tmp directory.</li></ul><pre>gcc main.c -L. -lholberton -o main</pre><p>Now run the executable program ‘main’</p><pre>$./main</pre><p>Executables generated using static libraries are no different than executables generated from individual source or object files. Static libraries are not required at runtime, so you do not need to include them when you distribute your executable. At compile time, linking to a static library is generally faster than linking to individual source files.</p><p>Static linking is very straight forward, but has a number of drawbacks.</p><p>The two main disadvantages are..</p><ul><li>If the library code is updated (say, to fix a bug) you have to recompile your program into a new executable.</li><li>Every program in the system that uses that library contains a copy in its executable. This is very inefficient (and a pain if you find a bug and have to recompile, as per point one).</li></ul><p>It is in these cases that we find Dynamic or Shared libraries better. Shared libraries are an elegant way around the problems posed by a static library. A shared library is a library that is loaded dynamically at runtime for each application that requires it. But more on this in another post.</p><p>Till then,Happy learning!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cea57990c495" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Everything you want to know about GCC]]></title>
            <link>https://medium.com/@meghamohan/everything-you-want-to-know-about-gcc-fa5805452f96?source=rss-7b5307b626c3------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa5805452f96</guid>
            <category><![CDATA[gcc]]></category>
            <category><![CDATA[compilation]]></category>
            <category><![CDATA[holberton-school]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[megha mohan]]></dc:creator>
            <pubDate>Sat, 11 Feb 2017 20:45:09 GMT</pubDate>
            <atom:updated>2017-02-15T03:31:35.646Z</atom:updated>
            <content:encoded><![CDATA[<p>As a newbie to the world of programming languages and computers, we should be aware that computers cannot ‘understand’ any of the programming languages that we are working with. It can interpret only machine languages ( zero’s and one’s). Compiler is what that comes to the rescue here. It ’translates’ the programming languages to machine language. Or to put it in another way, it converts our source code to executable instruction file for computers.</p><blockquote>GCC stands for “GNU Compiler Collection”. GCC is an integrated distribution of compilers for several major programming languages. These languages currently include C, C++, Objective-C, Objective-C++, Java, Fortran, and Ada.</blockquote><p>Lets go through the compilation process of a simple C program and understand whats happening behind the scenes.</p><p>First of all, write a simple C program to print “Hello World!”. I open the vim editor(which is in-built in Linux), write the program and save it as “HelloWorld.c”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/273/1*K9zy_mI7oys6ujMS2csmjA.png" /></figure><p>We now have a file named “HelloWorld.c” which is written in C language. The goal is to make the system interpret the C code and convert it into a machine language that the system understands. There are numerous processes through which the code undergoes to achieve the final output.</p><h3><strong>Preprocessing</strong></h3><p>This steps does the following : Removal of Comments, Expansion of Macros, Expansion of the included files.</p><p>The lines in our code that begin with the “<strong>#</strong>” character are preprocessor directives. In our “HelloWorld.c” program the first preprocessor directive (#include &lt;stdio.h&gt;) requests a standard header file, stdio.h, to be included into our source file. If you use macros in your program , this is the stage where it gets substituted by the corresponding value. In our program, #define PRINTTHIS “Hello World\n” is the macro and all occurrences of PRINTTHIS will get substituted with the corresponding value, here by the string “Hello World\n”</p><p>So in preprocessor stage those included header files and defined macros are expanded and merged within the source file to produce a transitory source file.</p><p>By using gcc’s “<strong>-E</strong>” flag we can directly do the pre-processing operation.</p><pre>[bash]$ gcc -E HelloWorld.c -o HelloWorldOutput</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*B5BiFi7hBkz5N_ucmgV7ZQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*jEruHfkaViSXnAbgFdjcSQ.png" /><figcaption>Preprocessed File (here, the output of HelloWorldOutput)</figcaption></figure><p>Note from the above output of the preprocessed file that since our program has requested the stdio.h header be included into our source which in turn, requested a whole bunch of other header files.</p><h3><strong>Compilation</strong></h3><p>The next step is to take the Preprocessed file as input, compile it and produce an intermediate compiled output. The output file for this stage produces Assembly code which is machine dependent.</p><p>By using “<strong>-S</strong>” flag with gcc we can convert the preprocessed C source code into assembly language without creating an object file:</p><pre>[bash]$ gcc -S HelloWorld.i -o HelloWorld.s</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*Yp0GLggBf8ca-W2hMj4HTw.png" /><figcaption>Compiled File</figcaption></figure><p>Though I am not much into assembly level programming but a quick look concludes that this assembly level output is in some form of instructions which the assembler can understand and convert it into machine level language.</p><h3><strong>Assembly</strong></h3><p>As we all know ,machines can understand only binary language, so now we require an <strong>ASSEMBLER</strong> that converts assembly code in “HelloWorld.c” file into binary code.</p><blockquote>ASSEMBLER was one of the first software tools developed after the invention of the digital computer.</blockquote><p>If there are any calls to external functions in the assembly code, the Assembler leaves the addresses of the external functions undefined, to be filled in later by the Linker.</p><p>The Assembler can be invoked as shown below. By using “<strong>-c</strong>” flag in gcc we can convert the assembly code into machine level code:</p><pre>[bash]$ gcc -c HelloWorld.c -o HelloWorld.o</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/706/1*O0m0hTV9kRjL4a0bdT-wFw.png" /><figcaption>HelloWorld.o</figcaption></figure><p>The only thing we can explain by looking at the HelloWorld.o file is about the string <strong>ELF</strong> in the first line. ELF stands for executable and linkable format.</p><blockquote>An object file and an executable file come in several formats such as ELF (Executable and Linking Format) and COFF (Common Object-File Format). For example, ELF is used on Linux systems, while COFF is used on Windows systems.</blockquote><p>This is a relatively new format for machine level object files and executable that are produced by gcc. Prior to this, a format known as a.out was used. ELF is said to be more sophisticated format than a.out (We might dig deeper into the ELF format in some other future article).</p><blockquote>If you compile your code without specifying the name of the output file, the output file produced has name ‘a.out’ but the format now have changed to ELF. It is just that the default executable file name remains the same.</blockquote><h3><strong>Linking</strong></h3><p>This is the final phase in which all the linking of function calls with their definitions are done. Linker knows where all these functions are implemented (Assembler has left the address of all the external functions to be called). Till this stage GCC doesn&#39;t know about the function like printf() .The Assembler would have left the address of the functions to be called and Linker does the final process of filling in these addresses with the actual definitions. The linker also does a few additional tasks for us. It combines our program with some standard routines that are needed to make our program run. So the final executable size is way more than the input file!</p><p>The entire linking process is handled by gcc and invoked as follows:</p><pre>[bash]$ gcc -o Output HelloWorld.c</pre><p>The above command runs the file “HelloWorld.c” and produces the final executable file “Output”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/487/1*Tu2zU6xXeZleU2BieJnZLQ.png" /><figcaption>listing all the files using ls -l</figcaption></figure><p>As you can see , ‘Output’ file is by default an executable file with permissions -rwxrwxr-x ,this just means that it has executable permission for all the users(owner,group and others). If you run this executable file by simply typing ‘./Output’ you get the final output of our Program !</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*pAdPyXMuE6bHRx3QjPmkQw.png" /><figcaption>Output of the executable file</figcaption></figure><p>So now we know how a C program gets converted to an executable . We will dive a little deeper into C programming in the coming articles. Till then, Happy Learning! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa5805452f96" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hard Link and Symbolic Link??]]></title>
            <link>https://medium.com/@meghamohan/hard-link-and-symbolic-link-3cad74e5b5dc?source=rss-7b5307b626c3------2</link>
            <guid isPermaLink="false">https://medium.com/p/3cad74e5b5dc</guid>
            <category><![CDATA[hard-link]]></category>
            <category><![CDATA[symlink]]></category>
            <category><![CDATA[unix]]></category>
            <category><![CDATA[holberton-school]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[megha mohan]]></dc:creator>
            <pubDate>Tue, 07 Feb 2017 18:42:23 GMT</pubDate>
            <atom:updated>2017-02-09T00:11:09.823Z</atom:updated>
            <content:encoded><![CDATA[<p>Before getting into the term Symbolic link and Hard link ,lets understand the term ‘inode’ . A Unix file is “stored” in two different parts of the disk — the data blocks and the inodes. The data blocks contain the “contents” of the file. But the <strong>information</strong> <strong>about the file</strong> is stored elsewhere — in the <strong>inode</strong>. Basically, inode is a file structure on a <a href="http://www.linux.org/article/view/intro-to-inodes/1#">file system</a>. More easily, it is a “database” of all file information except the file contents and the file name.Both the inodes and data blocks are stored in a “filesystem” which is how a disk partition is organized.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/759/1*1pFq8FM1s0LLd2EBzINA5w.png" /></figure><p>Coming to Symbolic Links and Hard Links..</p><p>Symbolic link (Symlinks/Soft links) are links between files. It is nothing but a shortcut of a file(in windows terms).</p><ul><li>You can delete the soft links without affecting the actual file or directory it is pointing to. The reason is because the <strong>inode</strong> of the linked file is different from that of the <strong>inode</strong> of the symbolic link. But if you delete the source file of the symlink ,symlink of that file no longer works or it becomes “dangling link” which points to nonexistent file .</li><li>Soft link can span across filesystem.</li><li>Soft links can link both files and directories.</li><li>A symbolic link shows the character <strong><em>l </em></strong>(file type corresponding to symbolic link) before the permissions for user, group and other users<em> </em>and displays an arrow followed by another file name, meaning it’s a link to another file (<em>see image below</em>).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/559/1*HZndjcDVGvJFUUP18SSMYQ.png" /></figure><p>To create Symlink ,</p><pre>ln -s &lt;source&gt; &lt;linkname&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*u7HhTpzlyUlcK_fNPwKVqQ.jpeg" /></figure><p><strong>Hard link</strong> is the exact replica of the actual file it is pointing to .Both the hard link and the linked file shares the same inode .</p><ul><li>If the source file is deleted ,the hard link still works and you will be able to access the file until the number of hard links to file isn’t 0(zero).</li><li>Hard links cannot span across filesystem . Why? Hard links are sharing the same inode — they point to the hardware location, instead of directory tree. This means that if you were to try to make hard link between ext3 and ext4, then you’d have some problems — these filesystems are different, so they can’t share the inode.</li><li>Hard links can link only files,not directories. The reason hard-linking directories is not allowed is a little technical. Essentially, they break the file system structure.</li><li>If the source file of hardline is removed ,the link still works. With hard links, there is no concept of original file and links , both files are equal.</li><li>Hard links are especially useful in performing “copy on write” applications. They allow you to keep a backup copy of a directory structure, while only using space for the files that change between two versions.</li><li>A hard link appears as an ordinary file (meaning you won’t see an <em>l</em>, but a “<strong>-</strong>” that stands for ordinary file, and no arrow will be displayed).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/0*ftfHLuaF4L63vtCm." /></figure><p>To create Hard link,</p><pre>ln &lt;source&gt; &lt;linkname&gt;</pre><p>So in conclusion, both are pointers to a file. The difference is the kind of pointer. Symlink points to another file by name whereas Hard link points by inode number.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3cad74e5b5dc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Going in depth : What happens when you type in ls *.c]]></title>
            <link>https://medium.com/@meghamohan/going-in-depth-what-happens-when-you-type-in-ls-c-663c5a88937b?source=rss-7b5307b626c3------2</link>
            <guid isPermaLink="false">https://medium.com/p/663c5a88937b</guid>
            <category><![CDATA[holberton-school]]></category>
            <category><![CDATA[terminal]]></category>
            <category><![CDATA[command-line]]></category>
            <dc:creator><![CDATA[megha mohan]]></dc:creator>
            <pubDate>Tue, 07 Feb 2017 17:49:07 GMT</pubDate>
            <atom:updated>2017-02-12T06:39:36.927Z</atom:updated>
            <content:encoded><![CDATA[<p>The first time anyone uses the terminal on Linux or Mac , “<strong>ls</strong>” would be most likely the first command that would have used. “<strong>ls</strong>” gets us started with just listing the files and directories present in the current directory , but it has a lot of advanced options that are useful on a regular workflow. Lets look at the output of ls command .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*3foEViTDFLxfGXn_LsWxLA.png" /><figcaption>output of ls command</figcaption></figure><p>As you can see ,it <strong>lists all the files and directories in the current directory that you are on</strong>. By default the list is sorted alphabetically . You have the option to sort it however we want using different options along with it. The command ‘man ls’ shows us the manual page of ls within the terminal. This lists all the different options that can be used in conjunction with ls for more advanced uses.</p><p>Coming to the command ‘ls *.c’ , the second part of the command is called a ‘Wildcard’ .Don’t get muddled with the term ,Wildcard makes our lives much easier as it has variety of uses like listing specific type of files/directories. The asterisk (*) here matches any character zero or more times. Meaning , “Hel*” matches anything beginning with “Hel” which means “Hello”, “Hello world” are all matched.Wildcards can be used with many different commands, not just with ls.</p><p>Coming back again to the command we are talking about “ls *.c” , the wildcard used here , “*.c” which means it matches all files that end with “.c” extension .The part where asterisk is mentioned can take any character zero or more times. As we know ,files that end with a “.c” extension are C files , so the output of the command simply lists all C files which are in the current directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*fjehbIUjhrBcO7kEskBqBA.png" /><figcaption>output of ls *.c</figcaption></figure><p>The C files listed after “ls *.c” are :</p><p>sampleCfile1.c</p><p>sampleCfile2.c</p><h3><strong>Now, that we understood whats happening on a high level, lets dig a little more into the details of whats happening above..</strong></h3><ol><li><strong><em>Feeding the command to the bash!</em></strong> : When the command <strong>ls</strong> is entered, the keyboard driver recognizes that characters have been typed and passes them to the shell. The string is passed as one single string .This is being split into tokens by removing the white spaces. So now, our command has two tokens, ‘ls’ and ‘*.c’.This is stored in an array of strings. This whole process is termed as <strong><em>Tokenization</em></strong>.</li><li>Now that we have the tokenized array , we have to search if each token has an alias defined .If an alias is found it is saved as a token after removing the spaces like before and again its checked for aliases. Ususally alias is stored in the following locations :~/.bashrc, ~/.bash_profile, /etc/bashrc, /etc/profile.</li><li>Next step is to check if each token is a built in function or not. If the command is built in, the shell executes the command directly, without invoking another program. ‘cd’ is a built in command. ‘ls’ is not a built in , so now system needs to find the executable for ‘ls’.</li><li><strong><em>Interpreting and finding the executable for the command :</em></strong> Next is for the bash to interpret the command. The first search for the command ‘ls’ is performed at <strong>$PATH</strong>. $PATH is an environmental variable which stores the path locations of all the common executable programs. This search is performed by calling a series of functions like find_user_command() ,find_user_command_in_path ,find_in_path_element. Each location specified in the PATH variable is searched for the executable corresponding to the command ‘<strong>ls</strong>’. BASH calls the function stat() to check for the existence of this executable in each of the paths.</li><li><strong><em>Pulling the executable to memory : </em></strong>Finally when it finds the file at the location ‘/usr/bin/ls’ , BASH performs execve() command to execute the file. There is a bunch of other things that needs to be done before the binary /usr/bin/ls is executed — the program has to be read from the disk, it’s binary format needs to be found and the appropriate handling code needs to be invoked which will read the binary into memory.</li><li><strong><em>And finally executing the command :</em></strong> So finally…. the program is in memory and is ready to execute when it gets a chance. So, the next questions is ..how does ls command read the directories and files from disk ? For this, a list of functions are executed internally to achieve the final output.The ls utility uses a function to read the directory contents, which in turn invokes a system call to read the list of files in the directory by consulting the underlying filesystem’s inode entries. Depending on which filesystem the path specified to ls is formatted, the function used to read the directory contents will vary.So once all the entries have been retrieved , the system call returns .The final steps of the shell would be to print the prompt again. The prompt is saved as environment variable <strong>PS1</strong>.The list of files is then returned to the prompt .</li></ol><p>Happy Learning!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=663c5a88937b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>