<?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 Flavio Orlando on Medium]]></title>
        <description><![CDATA[Stories by Flavio Orlando on Medium]]></description>
        <link>https://medium.com/@2109?source=rss-7fa4f56ebe40------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1hqo53hvMBuLqgK1.jpg</url>
            <title>Stories by Flavio Orlando on Medium</title>
            <link>https://medium.com/@2109?source=rss-7fa4f56ebe40------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 27 May 2026 14:26:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@2109/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[What (actually) happens when you type a web address on your browser and hit enter…]]></title>
            <link>https://2109.medium.com/what-actually-happens-when-you-type-a-web-address-on-your-browser-and-hit-enter-55263dd202a5?source=rss-7fa4f56ebe40------2</link>
            <guid isPermaLink="false">https://medium.com/p/55263dd202a5</guid>
            <dc:creator><![CDATA[Flavio Orlando]]></dc:creator>
            <pubDate>Thu, 22 Apr 2021 17:53:42 GMT</pubDate>
            <atom:updated>2021-04-24T23:03:30.785Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/615/1*uENYj6AtYyM80zqQyLIg9Q.png" /></figure><p>Of course that the simplest answer to the question on the title of this article is: “you get to see a web site?”. But on this case let’s try to go deeper into the concept and find out the details within the entire process that happens between you type your web site address (and in this article and for exemplifying purposes we will be using <a href="https://holbertonschool.com)and">https://holbertonschool.com</a>), hit the “Enter” key and you finally get to see the web site of your choice.</p><h4>www.holber… what?</h4><p>Holberton School is a software learning institution based in San Francisco and I am writing this blog as a requirement for their curricula. Here´s a screenshot of the main page from Microsoft Edge:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n4EcWNN0GSxHXwRvdtQE4A.png" /></figure><p>Web browsers such as Edge, Chrome or Firefox take text strings (because at the end of the day www.holbertonschool.comor any other web site name is a text string) and directs you to the web site that corresponds with that string.</p><p>Now, in order to match the text string it receives with a corresponding web site (with the web site IP address in fact, more about that later), what the browser will do is to parse the text string and divide it in smaller chunks it can identify and relate to something else. The break up goes like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*bY8TJapFuhjNOKPlSK7SVg.png" /></figure><p>In the case of the complete addresswww.holbertonschool.com<a href="http://www.holbertonschool.com,">,</a> the different components are broken down in this way:</p><p>https— This is the <strong>PROTOCOL</strong>, spcifically HTTPS (HyperText Transfer Protocol Secure). This is the data transfer method used between the client and server.</p><p><a href="http://www.holbertonschool.com">www.holbertonschool.com</a>- the <strong>DOMAIN </strong>name that matches the web site specific IP address.</p><p>The <strong>PORT </strong>is the specific port of the server where the request will be sent. As in the example we are not actually metioning an specific port, HTTPS will use the default assigned one which is port 443.</p><p><strong>PATH AND FILE NAME </strong>of the<strong> </strong>file we are requesting within the server. As it is also being left blank in the example this means that we are querying the server at its root.</p><p>We could, of course, provide the browser with the exact IP (Internet Protocol) address of the server we want to access, but human brains are notoriusly bad at remembering large combinations of numbers, while we can easily remember text strings as long as they match actual words from the human-used languages.</p><p>DNS (Domain Name System) is what we use to make the text strings we use as domain names, to match specific IP addresses on the server side.</p><p>So, again, when we type <a href="http://www.holbertonschool.com">www.holbertonschool.com</a> and press Enter, we are telling our browser that we want to access the file located at the root directory of the server that hosts <a href="http://www.holbertonschool.com">www.holbertonschool.com</a>. Moreover, we are also indicating the browser to contact the server using HTTPS protocol through port number 443.</p><p>A remarkable feature of browsers, is that they have a cache memntlyreory that allows them to “remember” the information they have previously processed. This cache memory stores recently called domain numbers, along with the matching IP addres for each one.</p><p>If the browser already has the domain name stored in its own cache, it will bring up the IP address with no further steps, but if the domain name is not then teh browser will search for it within the main Operating System cache. If the domain name is not here neither, then something called the DNS resolution process must start.</p><h4>DNS resolution process.</h4><p>Having failed to match the received domain name in both, its own cache and the operating system’s cache, the browser then sends the domain name off to the nearest resolver server, which most of the times will be your ISP or Internet Service Provider, to be resolved into its IP through the Domain Name System.</p><p>Now, DNS is a complex process on itself. If you´d like to learn more about it, I highly recommend the extremely effective yet funny explanation on <a href="https://howdns.works/">this link.</a></p><p>The resolver contacts both the top-level domain server (.com, in our case) and domain registrar before successfully matching the domain name into its corresponding IP address. At the end of the process, the browser finally knows the specific IP address that corresponds to our web site <a href="http://www.holbertonschool.com.">www.holbertonschool.com.</a></p><h4>TCP/IP.</h4><p>Having figured out the IP address of the web site, the browser will start communicating with the server. This communication will most likely be based on TCP/IP, which stands for Transmission Control Protocol/Internet Protocol, as this protocol is the standard in use on applications where instant delivery (such as streaming) <strong>IS NOT</strong> required. This is because TCP guarantees delivery regardless of it taking longer than instant protocols such as UDP (User Datagram Package).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*dgl67lrlr3CTYjl-0aJb9Q.png" /></figure><h4>SSL</h4><p>Now that the browser has established communication with the IP address, the first thing it does is sending a message containing its Transport Layer Security (TLS) version. TLS is an encryption method used to keep the privacy and reliability of the trasferred data. While nowadays TLS is the new standard, the original and traditionally used method was SSL which stands for Secure Socket Layer. Even though TLS is being used now, we keep using the “S” from SSL to complete the HTTPS acronym.</p><p>After receiving the TLS version from the browser, the server will choose its preferred TLS algorithm and method and respond with a security certificate that includes the server TLS public key. The browser then uses this public key to encrypt a pre-master key that is sent back to the server.</p><p>When the server receives the browser´s pre-master encrypted key, it will try to decrypt it using its own private key. By being able to perform this decryption, the server can then assume that the communication being received is a valid, reliable one, certified by its own public key that was first sent. The browser and the server have now set up a <em>trusted </em>connection.</p><p>This security process is known as the TLS handshake. Browsers show a lock icon on the address bar as indication of having established a secure connection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/417/1*pXtCU1qPLIFhyZxt5pOzkQ.png" /></figure><h4>HTTPS.</h4><p>Breaking down the acronym, the first four letters: HTTP stands for HyperText Transfer Protocol, and it is the standard protocol for Internet communications. HTTP defines how computers interact with each other.</p><p>Following up with the TLS handshake process, after completion of it, the browser sends a HTTP request message to the server. This message must follow a pre-defined format.:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*MMNA81eRniRD4WepZ5Xtcw.png" /></figure><p>The first line will define what kind of request message we are talking about. On this case this is a GET message, which means it retrieves web content from a server to a client.</p><p>In the header section the browser can specify details of the request, such as if the connection to the server should be terminated immediately or not, or whether the server should store cookies (information that would remain on the browser´s cache even after the session is terminated).</p><p>The request body is optional, and mostly irrelevant to request messages.</p><h4>LOAD BALANCER.</h4><p>Web sites, in particular those that have a heavy traffic, divide their work load between more than just one single physical server. With over half of the world population accessing the Internet and with some hughe web sites that have a semi-monopoly of some internet functions, it is clear that the <strong><em>load </em></strong>must be <strong><em>balanced </em></strong>between multiple servers.</p><p>A load balancer is an intermediary software which, well… balances the incoming requests and directs them according to a load balancing algorithm, in the way that the designers of the back end have decided that their servers will respond in the most efficient way. It can be installed on the same server that hosts the requested web content or on a server of its own.</p><p>There are many load balancing algorithms being the most commonly used the one called <em>round-robin </em>which sends requests to servers in turn according to a queue.</p><p>In real world, web sites are configured with multiple load balancers that are able to back up each other in the event of one of them failing. This is done in order to avoid having what is known as single point of failure, as if there is only one load balancer, the entire system would collapse if it fails.</p><h4><strong>FIREWALL</strong>.</h4><p>Before the GET request is finally received by the server, the message goes through one last security check: the <a href="https://kinsta.com/blog/what-is-a-firewall/">firewall</a>.</p><p>Firewalls are hardware, software, or an implementation of both that filter all traffic coming into and out of a server. TLS is effective for preventing data from being intercepted mid-transmission. Yet, it assumes that received data is coming from a trusted source. Firewalls make no such assumptions, and utilize a combination of packet filters, application gateways, circuit-level gateways and proxy servers to make certain that a packet does not contain viruses or malicious hardware.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*groQvYesU8Pw4TFUdh5zRA.png" /></figure><h4>HOST SERVER.</h4><p>The final destination of the GET request. Host servers are web stack consisting on multiple parts. Let´s break them down:</p><p><strong>Operating system: </strong>the operating system on which the host server runs. According to <a href="https://www.zdnet.com/article/can-the-internet-exist-without-linux/">this article</a>, at this moment (April 2021) 96.3% of the top 1 million web servers in the world run on some distribution of Linux.</p><p><strong>HTTP Web Server: </strong>This is the software that handles HTTP request/response messages and ultimately delivers the static web page. The most popular and widely used are Apache and Nginx.</p><p><strong>The database server: </strong>This is the database software, typically SQL-based, that stores information such as user accounts. A typical website will be configured with multiple database servers, with one configured as a “primary” database having exclusive write privileges whose changes are echoed out to “replicant” databases only having read privileges. This setup is referred to as a “primary-replica” model. MySQL is one of the most typically used database software.</p><p><strong>Application Server: </strong>Application servers are network computers that store and run an application for client computers. Application servers, whatever their function, occupy a large chunk of computing territory between database servers and the end user. Most broadly, this is called “middleware” which tells us something about what application servers do. First and foremost, application servers connect database information (usually coming from a database server) and our browser. PHP and Python are two high-level languages supported by web servers that can handle dynamic content.</p><p>It is extremely common that a typical setup would make use of popular open source solutions to build their web stacks. The combination of the most widely used open source solutions for each component: Linux, Apache, MySQL and PHP/Phyton, has given name to what is known as the <a href="https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-ubuntu-18-04">LAMP model.</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1005/1*-TUU6LNsV_m7l-kCBI66JQ.png" /></figure><h4>Recap:</h4><ul><li>A GET request is received by the web server. The web server pulls up the file configured at the given location (in our example, the HTML file configured at the root (/) of the machine).</li><li>If the file contains dynamic content, the application server is run (in a LAMP model the corresponding Python or PHP scripts are run). The result of these scripts is inserted into the web page.</li><li>If the dynamic content involves stored data, the Python scripts queries from the database server.</li><li>The web server delivers the web page.</li></ul><p>After pulling up the HTML file configured at the root of www.holbertonschool.com, the host server sent it back to the web browser in a HTTP response message formatted as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*RXKYLx_GlKjV-yyDQMc7PQ.png" /></figure><p>“200 OK” in the first line indicates the status code of the request (a succesful request in this case). Other common status codes include 301 (page redirection) and 404 (page not found).</p><p>In the response header, the host server states information about the delivered page such as its type (HTML, in our case) and size.</p><p>Finally, in the response message body, the host server delivers the actual, entire HTML code itself. Now, utilizing its HTML and CSS engines to parse the code, break it down into its Document Object Model, and render the page. Any JavaScript scripts written in the file are run. Finally the browser displays the Holberton School website home page.</p><h4>References:</h4><ul><li><a href="https://howdns.works/">How DNS works. What is DNS? Learn how step by step.</a></li><li><a href="https://www.zdnet.com/article/can-the-internet-exist-without-linux/">​Can the Internet exist without Linux? | ZDNet</a></li><li><a href="https://kinsta.com/blog/what-is-a-firewall/">What Is a Firewall? A Starting Guide to Firewalls and Whether You Need One</a></li><li><a href="https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-ubuntu-18-04">How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 18.04 | DigitalOcean</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55263dd202a5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[IoT — The Internet of Things.]]></title>
            <link>https://2109.medium.com/iot-the-internet-of-things-42a8f276f5ec?source=rss-7fa4f56ebe40------2</link>
            <guid isPermaLink="false">https://medium.com/p/42a8f276f5ec</guid>
            <category><![CDATA[technology-trends]]></category>
            <category><![CDATA[internet-of-things]]></category>
            <dc:creator><![CDATA[Flavio Orlando]]></dc:creator>
            <pubDate>Sun, 04 Apr 2021 20:28:18 GMT</pubDate>
            <atom:updated>2021-04-04T20:45:16.472Z</atom:updated>
            <content:encoded><![CDATA[<h3>IoT — The Internet of Things.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*dFaHsa11EHNSfD1rml4yFw.png" /></figure><p>Historians recognize four different industrial revolutions along history, or at least four different stages in a larger process identified as “the” Industrial Revolution.</p><p>The first three are related to the massive use of coal along with steam power, gas and fossil fuels and electronics and nuclear power.</p><p>A fourth Industrial Revolution seems to be happening at this very moment empowered by what has been called IoT (Internet of Things). Let’s work out this concept in further detail.</p><p>You can learn more about the concept of the fourth industrial revolution from <a href="https://www.weforum.org/agenda/2016/01/the-fourth-industrial-revolution-what-it-means-and-how-to-respond/">this article in the World Economics Forum website.</a></p><h4>So, IoT?</h4><p>The Internet of Things is about the entire constellation of devices that can connect to The Internet. And every day we figure out ways in which being connected the Internet can optimize the functionality of devices that were traditionally non-internet related, such as a washing machine, a refrigerator, a car or medical care devices.</p><p>By being able to connect to The Internet, these devices have the possibility to enhance their capabilities as they can share and receive information from similar devices and control centers from virtually anywhere in the planet.</p><h4>CCII — Consumer, Commercial, Industrial, Infrastructure.</h4><p>These are the categories in which the IoT devices are normally classified in.</p><h4>Consumer:</h4><p>The range of these products is endless and grows by the minute, but some remarkable examples are home assistants such as Google Nest or Amazon Alexa. These devices are able to interpret voice commands in order to interact (through IoT of course) with a variety of other systems such as a music playlist from a music streaming service or the IoT connected light bulbs in your home if you want to dim your lights.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FcTUffAQp7qk%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DcTUffAQp7qk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FcTUffAQp7qk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/28dbb93834ff9f7bafb4dc31ec2cab54/href">https://medium.com/media/28dbb93834ff9f7bafb4dc31ec2cab54/href</a></iframe><p>Another rather recent addition to IoT are cars. In Latin America, the newest line of Brazilian-produced Chevrolet cars include the <a href="https://www.onstar.com/us/en/home/">On-Star system</a> which monitors in real time the current condition of the car including fluid levels, tire conditions and engine temperature. It also allows the user to request emergency assistant in the event of an accident and can schedule service and repair instances as well as order spare parts for the shop to receive them in advance of the scheduled service date.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*CHZAi_by3MLcWj5bwIZy8Q.png" /><figcaption>Chevrolet’s Onstar system console.</figcaption></figure><p>Some other worth mentioning examples are wearable devices such as the Apple watch or the much more economic Xiaomi Band that can monitor body functions such as your heart rate, blood pressure and even the quality of your sleep. Nowadays we even have <a href="https://www.thespruce.com/best-smart-washers-and-dryers-4158885">“smart” washing machines</a>, which can download washing programs for specific types of clothing based on the world-wide experience of every user in the World. They can also be programmed from anywhere in the planet by accessing a smart phone app or a web-site. You could start your washing machine in one specific program when you are on vacations in the other side of the World.</p><h4>Commercial:</h4><p>Public services benefit from IoT by automatizing tasks that were traditionally performed by human operators or analogic systems that were unable to recognize a change in the conditions in which the service needs to be performed.</p><p>Think on an automatized toll collection system. The booth can recognize the specific car that comes in, either by reading the license plate or a special tag label on the car windshield and connects to a pre-set payment system to collect the fee and then raises the barrier. This optimizes the traffic flow making it way more agile than if the fee was collected by a human operator through cash payment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*bK9l-XIGBLC4cEZhJrOk7w.png" /></figure><p>Going back to consumer wearable IoT products such as smart watches that monitor the user’s health, there is the notion of applying the data collected from these devices to feed commercial-oriented medical databases. This application, called the internet of Medical Things, has the potential to revolutionize health care by integrating IoT devices into medical practices. While the information collected by the wearable devices is limited in its applicability, if the same information could be transferred over to a the user medical records accessible by the health care provider, then the scope of the potential uses for the same information expands exponentially.</p><p>While consumer IoT devices aim to improve our everyday life, the commercial IoT is becoming increasingly important for public welfare.</p><h4>Industrial:</h4><p>The objective of Industrial IoT is to improve the productivity, efficiency and reliability of the industrial process, resulting in economic benefits. By connecting the operational technology, such as the robots that manufacture cars in a factory assembly line, with machine learning and data analytics through IoT, all the industrial supply chain can be optimized as well as the manufacturing process itself, resulting in an improved cost-effective equation. Using industrial IoT platforms, companies connect, monitor, analyze, and act on data in new ways.</p><p>One example of successful industrial IoT application is the experience of the oil giant Royal Dutch Shell. <a href="https://blog.tsl.io/5-big-iot-success-stories-for-business-leaders">The company installed smart monitor sensors on some of their oil extraction fields located in West Africa.</a> These fields were on particular hard-to-access locations with very limited local infrastructure. By being able to perform remote monitor, the company was able to dramatically cut the investment in site visits for maintenance.</p><p>An investment on the IoT technology of $87.000 produced an estimated return of over 1 million dollars.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*oeQsGkEHbMVMoIVIz337iQ.png" /><figcaption>source: <a href="https://blog.tsl.io/5-big-iot-success-stories-for-business-leaders">https://blog.tsl.io/5-big-iot-success-stories-for-business-leaders</a></figcaption></figure><p>The industrial applications of IoT is what has inspired the historians to define the so called Fourth Industrial Revolution, or Industry 4.0 as it is often referred to. The term Industrie 4.0 is also used as the term originated in Germany. The potential for increased productivity and correlational revenue is appalling, being estimated in several trillion dollars of increased benefits within the next decade alone.</p><p>To learn more about Industry 4.0 you might want to check <a href="https://www.i-scoop.eu/industry-4-0/#:~:text=Industry%204.0%20is%20the%20current,called%20a%20%E2%80%9Csmart%20factory%E2%80%9D.">this article from the specialized European media site i-scoop.</a></p><h4>Infrastructure:</h4><p>Considering how IoT can be applied to optimize contained scale projects, if you consider it, there is no reason why it cannot be applied in a larger scale to optimize the administration of an entire city or a country power grid.</p><p>This is achieved by using environmental monitoring systems to monitor the quality of air and water, integrating traffic control systems that optimizes traffic flow adapting to the different times of the day and level sensors that monitor the sewage system heights and open and close valves accordingly, in an “smart” optimized way to better adapt to the system capabilities.</p><h4>Understanding IoT:</h4><p>IoT or the act of connecting to The Internet through devices that did not originate as internet-enabled requires three different technologies:</p><p>- Wireless data protocols.</p><p>- Hardware.</p><p>- IoT platforms.</p><h4>Wireless Data Protocols:</h4><p>Most internet-enabled devices connect to the internet through wireless transmission of data, mainly, but not limited to, through WIFI or Bluetooth.</p><h4>Hardware:</h4><p>It is the part of the devices that allow the interaction, either with a user or the internet itself. The hardware may consist of sensors, microphones or network connection unit.</p><h4>IoT platforms:</h4><p>IoT platforms manage the data thet IoT devices share with each other. IoT platforms are software solutions that organize the flow of information within specific networks of IoT devices. Some of the most popular IoT platforms today are <a href="https://azure.microsoft.com/es-mx/services/iot-hub/?&amp;ef_id=CjwKCAjwx6WDBhBQEiwA_dP8re6Y3WMdwiSEQXSJTfqnPFANzNhuZeXqBiW8ryL56p0hQl_g6jsw8BoCWeEQAvD_BwE:G:s&amp;OCID=AID2101387_SEM_CjwKCAjwx6WDBhBQEiwA_dP8re6Y3WMdwiSEQXSJTfqnPFANzNhuZeXqBiW8ryL56p0hQl_g6jsw8BoCWeEQAvD_BwE:G:s&amp;dclid=CJTotZKo5e8CFSM0uQYdFS0M1Q">Microsoft Azure IoT</a>, <a href="https://cloud.google.com/solutions/iot?utm_source=google&amp;utm_medium=cpc&amp;utm_campaign=latam-LATAM-all-es-dr-BKWS-all-all-trial-b-dr-1009897-LUAC0009276&amp;utm_content=text-ad-none-any-DEV_c-CRE_424444091021-ADGP_Hybrid%20%7C%20BKWS%20-%20MIX%20%7C%20Txt%20~%20General_IOT-KWID_43700042945306230-kwd-667091836054&amp;utm_term=KW_%2Bgoogle%20%2Biot-ST_%2BGoogle%20%2BIOT&amp;gclid=CjwKCAjwx6WDBhBQEiwA_dP8rcrj9G6OBrJa16lkc8avhD8lriuBMn_KP0tqrtKqvT_twMzS90h10hoCJPYQAvD_BwE&amp;gclsrc=aw.ds">Google Cloud IoT solutions</a> or <a href="https://aws.amazon.com/iot/">Amazon’s AWS IoT</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IOLc2cnxDGSYXy4N9Ib6lg.png" /><figcaption>Source: <a href="https://android-developers.googleblog.com/2018/06/android-things-client-library-for.html">https://android-developers.googleblog.com/2018/06/android-things-client-library-for.html</a></figcaption></figure><p>Considering the appalling amount of interconnected devices available today, the amount of data being shared is beyond huge. This fact is actually the fundamental issue of Big Data, or the limitations of technology to manage incredibly large and complex databases. The response to the challenge of Big Data has been the implementation of cloud computing, or the delivery of computing services over the internet.</p><h4>Ethical concerns about IoT:</h4><p>While IoT is making our lives more comfortable, easy and fun and our businesses more efficient and profitable there are valid concerns that must be considered, particularly those referred to privacy and security.</p><p>The number of IoT devices is increasing exponentially raging from mobile phones to light bulbs and they all collect and share data which ultimately refers to how each of us live our personal lives. From what web pages you visit and how much time do you spend on each one, all the way to what kind of food you keep on your refrigerator or what kind of workout you enjoy the most. Even your general health condition or well or bad you are sleeping the last few nights. All of this is being shared through your IoT devices.</p><p>This is no minor issue. Companies that gather large amount of personal information must make a responsible and ethical use of it, while also making sure that the information they gather is safe and not available for third parties to use it for different reasons than those accepted when it was collected.</p><p><a href="https://www.nytimes.com/2018/03/17/us/politics/cambridge-analytica-trump-campaign.html">A shocking example of lack of security was seen in the 2016 US Elections when the British company Cambridge Analytics was able to harvest data from millions of Facebook users. This data was then used by Donald Trump’s campaign to identify, target and influence potential voters.</a></p><p>When you think about the amount of sensitive personal information that we share through our IoT devices it becomes clear that this kind of misuse of users’ data are simply not acceptable to happen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*l_phXZFf0tO6Iyzx6Gw42A.png" /><figcaption>An email shared between employees of Cambridge Analytics describing traits that could be predicted. Source: <a href="https://www.nytimes.com/2018/03/17/us/politics/cambridge-analytica-trump-campaign.html">https://www.nytimes.com/2018/03/17/us/politics/cambridge-analytica-trump-campaign.html</a></figcaption></figure><p>While these are valid and very much real concerns, it must also been considered that IoT has been around only for the last decade, so there is a lot yet to do in terms of regulation and control. Ultimately, the unstoppable success of IoT lies in the fact of how it has delivered the intended objectives of this technology. IoT is making our lives more comfortable, easy and fun and we have more than justifiable reasons to believe that the growing rate of IoT will only continue. There are good reasons to be optimistic about the responsible future use of this technology.</p><h4>References:</h4><ul><li><a href="https://www.weforum.org/agenda/2016/01/the-fourth-industrial-revolution-what-it-means-and-how-to-respond/">The Fourth Industrial Revolution: what it means and how to respond</a></li><li><a href="https://store.google.com/us/product/nest_audio?hl=en-US">Nest Audio</a></li><li><a href="https://blog.tsl.io/5-big-iot-success-stories-for-business-leaders">5 Big IoT Initiative Successes for Business Leaders</a></li><li><a href="https://www.i-scoop.eu/industry-4-0/">Industry 4.0 and the fourth industrial revolution explained</a></li><li><a href="https://android-developers.googleblog.com/2018/06/android-things-client-library-for.html">Android Things client library for Google Cloud IoT Core</a></li><li><a href="https://www.nytimes.com/2018/03/17/us/politics/cambridge-analytica-trump-campaign.html">How Trump Consultants Exploited the Facebook Data of Millions (Published 2018)</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42a8f276f5ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A dream of recursion.]]></title>
            <link>https://2109.medium.com/a-dream-of-recursion-93165f506ce3?source=rss-7fa4f56ebe40------2</link>
            <guid isPermaLink="false">https://medium.com/p/93165f506ce3</guid>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[heap]]></category>
            <category><![CDATA[stack]]></category>
            <category><![CDATA[heap-memory]]></category>
            <category><![CDATA[recursion]]></category>
            <dc:creator><![CDATA[Flavio Orlando]]></dc:creator>
            <pubDate>Tue, 02 Feb 2021 17:59:33 GMT</pubDate>
            <atom:updated>2021-02-02T17:59:33.425Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/417/0*JUia170HWL6opfKY.gif" /></figure><p>In order to unfold the concept of recursion in an approachable way, let’s start by clarifying two concepts that need to be really clear before moving any forward: stack and heap.</p><h4>What is stack?</h4><p>Stack is a special area of the computer memory which stores temporary variables created by a function. Is stack, variables are declared, stored and initialized during runtime. It is a temporary storage memory. When the task is complete, the memory of the variable will be erased.</p><h4>What is heap?</h4><p>The heap is the actual memory space available for the computer’s CPU to use. It is restricted only by the actual physical limits of the computer you are working on.</p><h4>Ok, and then, what is recursion?</h4><p>Let’s make use of our good old mate <a href="https://en.wikipedia.org/wiki/Recursion_(computer_science)#:~:text=In%20computer%20science%2C%20recursion%20is,instances%20of%20the%20same%20problem.&amp;text=Most%20computer%20programming%20languages%20support,from%20within%20its%20own%20code.">Wikipedia</a>:</p><p>“In computer science, recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem. Such problems can generally be solved by iteration, but this needs to identify and index the smaller instances at programming time. <strong>Recursion solves such recursive problems by using functions that call themselves from within their own code</strong>.”</p><p>And that is the key, a <em>recursive function is a function that calls itself.</em></p><p>In order to further explain the concept, let’s use an example of a recursive function that calculates a number (x) taken to the power of a second variable (y).</p><pre>float _pow_recursion(float x, float y)<br>{<br>    if (y == 0)<br>        return (1);<br>    if (y &lt; 0)<br>        return (_pow_recursion(x, y + 1) / x);<br><br>    return (_pow_recursion(x, y - 1) * x);<br>}</pre><p>For the purpose of this explanation, we will be calling the functions using 2 and 3 as x and y.</p><pre>_pow_recursion(2, 3).</pre><p>Let’s imagine four little girls playing throw and catch in a single row. Each girl will pass the ball to the next child down the row. Once the ball reaches the last girl, she passes it back to the girl from whom she received the ball. The ball then reverses all the way back to the beginning of the row up to the first girl again (hey, nobody said it was a fun game).</p><p>Forcing an analogy, we can think of the garden (or whatever surface these girls are using for their game) as the heap. This is the entire space available for the game to happen.</p><p>Again, forcing an analogy, when we call _pow_recursion(2, 3) the row of girls can be pictured like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*18KEozmOr2wVcglgjS47-A.png" /></figure><p>The ball starts with the girl at the far left of the row and represents the variables passed over to the function: x = 2 and y = 3.</p><p>That first girl represents the instantiation of a new process with a stack storing the values of x and y.</p><p>Each time a girl passes the ball to the next girl at her right, we will imagine that we are calling again to the function, meaning, the instantiation of a new stack on the heap. Once a girl has received the ball and passed it over to the next, only then we could say that a process has finished and its corresponding stack has been cleared.</p><p>Now, this are <em>complying </em>girls. They have been instructed to carry on with the game until they are instructed otherwise and they mean to carry on their orders. As we wouldn’t really like to see them faint in exhaustion we better pass them an specific situation that, when met, it will mean to stop keep passing the ball again and again. As we are actually talking about a recursive function, this condition that would end the process is what we call <em>the base case. </em>Whenever the base case scenario presents itself, the recursive function will stop calling itself over. In the _pow_recursion function we are working with, the base case is represented by the first conditional (if):</p><pre>if (y == 0)<br> return (1)</pre><p>On our scenario the base case represents the point at which the ball has reached the end of the row, all the way down to the last girl, and is backtracked.</p><p>Back to the girls playing in the garden, the starting point of the ball is in the hands of the girl at the far left. At this point the ball represents the variables ‘x’ and ‘y’ with the values 2 and 3 respectively. Which means that the base case scenario is not being met as y is not 0 and the ball is not at the end of the row. Therefore, the ball passes to the next girl.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9HakP17-RNOUly-4ucSKAw.png" /></figure><p>Again, passing the ball to the next girl at the right is an analogy to calling the function again. Even before the first girl has thrown the ball, she has specified the variables it will pass. In our function, this corresponds to the next conditional (the second &#39;if’):</p><pre>if (y &lt; 0)<br>    return (_pow_recursion(x, y + 1) / x);return (_pow_recursion(x, y - 1) * x);</pre><p>When the ball reaches the second girl, the function calls itself again, but, this new call will receive different parameters than previously. This is actually essential as, otherwise, if the parameters wouldn’t change, a base case would never be met and our girls would pass the ball over and over for all eternity.</p><p>When the ball is still at the hands of the first girl, ‘y’ is equal to 3. As 3 is greater than 0, the girl subtracts 1 from ‘y’ and passes the ball to the next girl. This calls the function again but in this case with the updated value of ‘y’ equals to 2, therefore: _pow_recursion(2, 2).</p><p>The process repeats itself for the second girl. As ‘y’ is still different than 0 and the ball has not yet reached the end of the row, the second girls substracts 1 to ‘y’ and passes over the ball to the third girl who will then call _pow_recursion(2, 1).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p0Lt8T0KCTRYsrLd0jivHg.png" /></figure><p>The ball reaches the third girl. The same process for player 3 is instantiated with a stack with the variables ‘x’ = 2 and &#39;y’ = 1. Again &#39;y’ is not 0, so the third girl substract 1 from it and pass the ball to the right</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*liBMpkqQ3H5mmJbY1H0iqA.png" /></figure><p>When the ball reaches the fourth girl, the base case is met (finally). The ball is at the end of the row and ‘y’ is now equal to 0.</p><p>Let’s remember:</p><pre>if (y == 0)<br> return (1);</pre><p>Having reached the end of the row, the fourth girl sends the ball back from where it came, now representing the return value 1. A fourth process started when the fourth girl received the ball, but immediately terminates and clears after returning the baseball back the other way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GOXEdYf88n9JQslzuJjb_Q.png" /></figure><p>The ball, now representing 1, is received for a second time by the third girl. Let’s remember the expression within which the third girl 3 threw the ball the first time:</p><pre>return (_pow_recursion(x, y — 1) * x);</pre><p>Once she receives 1 from the previous girl, the third girl is ready to terminate. She multiplies the received value by its known x (still accessible within her stack) and keep the game going, throwing the ball back with a return value of 2. The third girl has now thrown the ball in both directions, so her process terminates and stack clears.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0_brfL1yhb7QKpHAXULjGQ.png" /></figure><p>The second girl receives the ball for the second time and goes through the same process. She multiples the 2 by their known x (2), and send it to the left again, now with the return value 4. The second girl stack is now cleared.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n6C9zgAR6wOqZ5whOeTrdw.png" /></figure><p>Finally, the ball gets back to the hands of the first girl. It now has gone the entire length of the game row forth and back. The first girl receives the value 4, multiplies it by its known x (2), and returns the final result 8. The first girl’s stack is cleared, and the entire process is over. The girls can now play something else (hopefully funnier), and the garden where they were playing (heap) can be used for something else.</p><h4>Wrapping up:</h4><p>While the example we used here was quite simple, the core concept could be applied to much more complicated functions and would still be valid. A recursive function WILL continue to call itself until a base case is met. This lead us to the evident conclusion that we should be really careful about memory usage when implementing recursive functions, as if the base case is not met, the function will keep keep calling itself causing what is known as stack overflow (which is also the name of probably the favorite website for all programmers in the world). When this happens, our computer, which ultimately has physical boundaries regarding the amount of information it can process, will eventually crash and the process will terminate without returning the expected result.</p><p>For further clarity here’s the flowchart to the function we used in this article:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*MQLdiZLgL8pvoXxe8qGjFw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93165f506ce3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Everything is an Object (in Python).]]></title>
            <link>https://2109.medium.com/everything-is-an-object-in-python-4f67045dc15?source=rss-7fa4f56ebe40------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f67045dc15</guid>
            <dc:creator><![CDATA[Flavio Orlando]]></dc:creator>
            <pubDate>Tue, 12 Jan 2021 19:18:48 GMT</pubDate>
            <atom:updated>2021-01-17T18:29:13.414Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/668/1*WY2HAYdsPY6UDGs2l7txAQ.png" /><figcaption>The concept of ‘mutability’ (hence: mutants) will actually be relevant for this article.</figcaption></figure><p>Moving forward into learning software engineering, will inevitable put you against the concept of Object Oriented Programming, or OOP for short.</p><p>From Wikipedia:</p><p><em>“Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).”</em></p><p>Referring specifically about Python, it is an extremely versatile programming language and that versatility comes from its object-oriented nature. As the title says, everything in Python is considered an object. <em>Everything</em>.</p><p>But what does this actually mean for real life purposes? That is what I will be trying to cover next. How objects are actually used to represent values in Python and what actually objects <em>are</em> in Python?</p><p>Let’s try to find out.</p><h4>Types:</h4><p>An object in Python is, well, an object, a thing. While this doesn’t seem too accurate it is actually true because remember: everything is an object in Python! In a little bit more technical explanation, we could say that an object represents a value that can be referred to by a variable. In the example below, the variable “a” refers to an object representing the value “1”.</p><pre>&gt;&gt;&gt; a = 1</pre><p>In Python, all objects have a specific <em>type </em>which can be checked by using the built-in method called, well, type. Going back to the previous example, the type of “a” as it refers to the value “1” is the numeric type “int”:</p><pre>```&gt;&gt;&gt; a = 1</pre><pre>&gt;&gt;&gt; type(a)</pre><pre>&lt;class ‘int’&gt;</pre><p>By changing the value of “a” to another type of data, such as a string, the return of the type entry will obviously change to reflect the new type:</p><pre>&gt;&gt;&gt; a = “pepe”</pre><pre>&gt;&gt;&gt; type(a)</pre><pre>&lt;class ‘str’&gt;</pre><p>In this case the return is the sequence type “str”.</p><h4>Identity:</h4><p>Each object exists within the computer memory somewhere. Therefore, each object exists within a physical memory address. In order to see the memory address of an object, the built-in method “id()” can be used.</p><pre>&gt;&gt;&gt; a = 1</pre><pre>&gt;&gt;&gt; id(a)</pre><pre>2149256685872</pre><p>The memory address is shown as a unique integer value which is considered as the <em>identity </em>of the object as long as the object exists.</p><p>While any physical memory address is, of course, unique, it is worth mentioning that, in Python, it is possible for two different variables to share the same identity. Comparing memory locations of two different objects can be done by using the “is” operator.</p><p>“is” will return “True” if the two compared variables have the same identity and “False” otherwise.</p><pre>&gt;&gt;&gt; a = 12</pre><pre>&gt;&gt;&gt; b = 21</pre><pre>&gt;&gt;&gt; a is b</pre><pre>False</pre><p>In this example “a” refers to the physical address of an “int” object (12), while “b” refers to a different memory location of another “int” object (21), therefore, both variables have different identities and the return result is “False”. The same statement could be seen the other way around:</p><pre>&gt;&gt;&gt; a = 12</pre><pre>&gt;&gt;&gt; b = 21</pre><pre>&gt;&gt;&gt; a is b</pre><pre>False</pre><pre>&gt;&gt;&gt; a is not b</pre><pre>True</pre><p>While it could be assumed that the operator “==” does exactly the same as “is”, they are not the same as while “is” compares the physical address of two variables, “==” compares their values.</p><p>This is also explainable by referencing the difference between assignment and referencing. Assigning a value to a variable is not the same than referencing a variable to another variable which is at the same time assigned to a value.</p><h4>Immutable objects.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*Ry8jLY5Z0avGSf4ANApIyA.png" /><figcaption>An example of an immutable object (everything is an object).</figcaption></figure><p>Whenever an instance of an object is created, Python checks if such instance already exists in memory. If it does, the previously existing instance will be re-used. This is much easier to appreciate by using an example:</p><pre>&gt;&gt;&gt; a = 10</pre><pre>&gt;&gt;&gt; b = 10</pre><pre>&gt;&gt;&gt; a is b</pre><pre>True</pre><pre>&gt;&gt;&gt; id(a)</pre><pre>1469277170256</pre><pre>&gt;&gt;&gt; id(b)</pre><pre>1469277170256</pre><p>“a” is being assigned to an instance of the “int” object 10. Instead of creating a new instance when assigning “b”, Python checks if a previous instance already exists and then “b” is assigned to it. That is why the “id” operator returns exactly the same identity for both “a” and “b”. This is pretty much the same than assigning “a” to “b”.</p><pre>&gt;&gt;&gt; a = 13</pre><pre>&gt;&gt;&gt; b = a</pre><pre>&gt;&gt;&gt; a is b</pre><pre>True</pre><pre>&gt;&gt;&gt; id(a)</pre><pre>1573613824688</pre><pre>&gt;&gt;&gt; id(b)</pre><pre>1573613824688</pre><p>This process of <em>object instantiation </em>in which an instance is “recycled” is only applicable when we are talking about <strong>immutable objects</strong>.</p><p>An immutable object as the name suggests, cannot be changed without changing the identity of the object itself.</p><p>All numeric object types (such as int or float) are immutable types in Python. The same goes for strings.</p><p>As explained before, immutable objects can be used by more than one variable to refer to them. Take this string example:</p><pre>&gt;&gt;&gt; a = &quot;Pepe&quot;</pre><pre>&gt;&gt;&gt; b = &quot;Pepe&quot;</pre><pre>&gt;&gt;&gt; a is b</pre><pre>True</pre><pre>&gt;&gt;&gt;</pre><p>Both “a” anb “b” refer to the same “str” object “Pepe”. This is called <strong>aliasing. </strong>Whenever two or more variables refer to the same object it can be said that the object is <em>aliased</em>.</p><p>The value of immutable type objects cannot change without changing the object identity. Because of that, whenever we change the value that a variable refers to, we are actually changing the object of reference of that variable for a new one.</p><pre>```&gt;&gt;&gt; a = 15</pre><pre>&gt;&gt;&gt; id(a)</pre><pre>1573613824752</pre><pre>&gt;&gt;&gt; a = 15 + 1</pre><pre>&gt;&gt;&gt; id(a)</pre><pre>1573613824784</pre><p>Python keeps an internal counter on how many references an object has. Once the counter goes to zero — meaning that no reference is made to the object — the garbage collector in Python removes the object , thus freeing up the memory.</p><h4>Mutable objects:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*8X2fX0pUvvGtrnmwrfaICw.png" /><figcaption>Example of mutable object (EVERYTHING is an object!)</figcaption></figure><p>Mutability, also as the name suggests, implies that mutable objects can change over time while keeping the same identity than originally.</p><p>An example of a mutable type in Python are lists:</p><pre>&gt;&gt;&gt; list_1 = [1, 2, 3]</pre><pre>&gt;&gt;&gt; list_2 = [1, 2, 3]</pre><pre>&gt;&gt;&gt; list_1 is list_2</pre><pre>False</pre><pre>&gt;&gt;&gt; list_1 == list_2</pre><pre>True</pre><pre>&gt;&gt;&gt; id(list_1)</pre><pre>1573654126208</pre><pre>&gt;&gt;&gt; id(list_2)</pre><pre>1573654942080</pre><p>On this example we can see in a pretty clear fashion that while list_1 and list_2 have exactly the same value, they are in fact not the same instance (remember that the “==” operator compares values while “is” compares identities). You can see that both lists have different identity values.</p><p>Unlike immutable types, when creating an instance for a mutable object type, Python will not do a memory search looking for similar values but will simply create the new instance with a fresh identity.</p><p>A mutable object can afterwards, be altered, as many times as you wish, which will not change the object’s identity, as this is not tied to its value.</p><p>By using the “append()” method we can modify an existing list. See below:</p><pre>&gt;&gt;&gt; list = [1, 2, 3]</pre><pre>&gt;&gt;&gt; id(list)</pre><pre>1573654931968</pre><pre>&gt;&gt;&gt; list.append(4)</pre><pre>&gt;&gt;&gt; print(list)</pre><pre>[1, 2, 3, 4]</pre><pre>&gt;&gt;&gt; id(list)</pre><pre>1573654931968</pre><p>You can see that, even though the list was altered in order to append “4” to it, the identity value remains the same than before the change. This is the main point here. <strong>Changing the value of a mutable object will NOT change its identity.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/823/1*Kr2QEsP4YW8R0kEucsBH8A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*JKHEUGZP3cSpHVs-7RPJ6A.png" /></figure><h4>A word about Tuples.</h4><p>The immutable/mutable distinction in which changing the object’s value will change it identity or not is consistent for all object types but one: tuples.</p><p>Tuples are similar to lists, but unlike those, they are immutable objects and you cannot modify the values within a tuple… unless one of the values contained within a tuple is itself a list, which is in fact a mutable object. Something like this:</p><pre>&gt;&gt;&gt; tuple = (1, [2, 3])</pre><p>Again, we could use the “append()” method to modify the “[2, 3]” list within the tuple.</p><pre>&gt;&gt;&gt; tuple = (1, [2, 3])</pre><pre>&gt;&gt;&gt; id(tuple)</pre><pre>1573654891712</pre><pre>&gt;&gt;&gt; tuple[1].append(4)</pre><pre>&gt;&gt;&gt; print(tuple)</pre><pre>(1, [2, 3, 4])</pre><pre>&gt;&gt;&gt; id(tuple)</pre><pre>1573654891712</pre><p>See that while the list did was modified, the identity of the tuple was not, as it should because remember, it is an immutable object.</p><p>Tuples are instantiated as mutable objects. This is done to consider the possibility that values contained in a tuple could change.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*j240RzXspIgCkPthQY71Xg.png" /></figure><h4>Why should I care?</h4><p>Exactly. What difference does it make the mutable/immutable nature of an object? Quite a lot actually.</p><p>Take this function, for instance. It increments a number by 1:</p><pre>&gt;&gt;&gt; def increment(n):</pre><pre>&gt;&gt;&gt; ... n += 1</pre><p>If we pass an integer to the function, that “int” value would keep its identity value regardless of being used by the function or not, because “int” objects are immutable — the reference to 1 passed to n in “increment” becomes a new object within the scope of the function, while the variable x retains the identity of 1 beyond it:</p><pre>&gt;&gt;&gt; x = 1</pre><pre>&gt;&gt;&gt; n = x</pre><pre>&gt;&gt;&gt; n += 1</pre><pre>&gt;&gt;&gt; x</pre><pre>1</pre><pre>&gt;&gt;&gt; n</pre><pre>2</pre><p>This means that <strong>changes made to immutable objects within functions do not register beyond the scope of that function while changes made to mutable objects within functions DO register beyond the scope of the function.</strong></p><p>Whenever you are writing a Python program, it is important to keep in mind the mutability properties of the objects that the variables passed to your functions will refer to.</p><h4>Finally:</h4><p>As repeated several times along this article, whenever there’s an immutable objects instantiation, Python searches for previously existing objects with the same values before creating new instances. This explain this already seen example:</p><pre>&gt;&gt;&gt; x = 256</pre><pre>&gt;&gt;&gt; y = 256</pre><pre>&gt;&gt;&gt; x is y</pre><pre>True</pre><p>So, what about this?:</p><pre>&gt;&gt;&gt; x = 257</pre><pre>&gt;&gt;&gt; y = 257</pre><pre>&gt;&gt;&gt; x is y</pre><pre>False</pre><pre>&gt;&gt;&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/248/1*b4qXBX_ge6fNrOa0XtURUw.png" /><figcaption>Wait, what???</figcaption></figure><p>What on Earth is going on here???</p><p>This behavior has to do with a specific characteristic of the CPython virtual machine. CPython is the reference implementation of Python. Written in C and Python, CPython is the default and most widely used implementation of the language.</p><p>Python is actually checking for pre-existing objects, but it is just searching within a PRE-DEFINED range of objects.</p><p>The idea is that certain values are more repeatedly used in programming. By being able to keep in cache these values, CPython reduces significantly the amount of time and memory consumed.</p><p>For integers, the range expands from -5 to 256, included, which explains the system reaction when 257 was used.</p><p>The pre-definition of these ranges occurs through an assignment of these values to the macros NSMALLPOSINTS and NSMALLNEGINTS.</p><h4>RECAP:</h4><p>- Everything is an object in Python.</p><p>- Immutable objects cannot change their values without changing their identity.</p><p>- When instantiating an immutable object, Python first checks if an object of the same value already exists in memory before instantiating a new instance.</p><p>- Mutable objects can change their values without modifying their identity.</p><p>- Mutable objects are created with no previous memory check for similar values and have a unique identity.</p><p>- Tuples are the only object type that, while immutable, can potentially modify their values.</p><p>- Changes made to immutable objects within functions do not register beyond the scope of the function.</p><p>- Changes made to mutable objects within functions do register beyond the scope of the function.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f67045dc15" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Class and instance attributes]]></title>
            <link>https://2109.medium.com/class-and-instance-attributes-9b2e4a62e465?source=rss-7fa4f56ebe40------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b2e4a62e465</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python-class]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Flavio Orlando]]></dc:creator>
            <pubDate>Mon, 11 Jan 2021 21:50:07 GMT</pubDate>
            <atom:updated>2021-01-11T21:50:07.132Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/991/1*u6xRyoP8zQcaQDbwvneRMA.png" /></figure><h4>What is a class?</h4><p>A class is a code template for creating objects along with related data and functions. Objects have variables and behavior associated with them. In Python a class is created by the keyword class.</p><p>An object is created using the constructor of the class. This object will then be called the instance of the class. In Python we create instances in the following manner</p><pre>Instance = class(arguments)</pre><h4>Attributes and Methods in class:</h4><p>A class by itself has no purpose unless there is some functionality associated to it. These functionalities are defined by setting <em>attributes</em>, which act as containers for data and functions related to those attributes. Functions within classes are called methods.</p><h4>Instance Attribute</h4><p>An instance attribute is a Python variable that belongs to one specific object. It is only accessible when working from within the object itself and it is defined inside the constructor function of a class.</p><p>For example, <em>__init__(self,..).</em></p><h4>Class Attribute</h4><p>A class attribute is a Python variable that belongs to a class instead than to an specific object. Class attributes are shared between all objects within the same class and are defined outside the constructor function __init__(self,…), of the class.</p><h4>Differences Between Class and Instance Attributes</h4><p>As mentioned before, class attributes are common to all instances within the class, thus, changing the value of a class attribute will affect every instance of the class that have that attribute. The attribute of an instance on the other hand is unique to that instance.</p><h4><strong>What are the advantages and drawbacks of each of them</strong></h4><p>While class attributes are more flexible to declare and call, the downside is that they are available to the entire class. That means that after being called and given different values, it’s final value when called as a return value may be unexpected, as their may be side effects that you were not counting on.</p><p>With instance attributes, since they are only available in the scope of the object that created it, it is encapsulated (protected) but the use of the instance attribute is far less flexible than the class attribute.</p><h3>__dict__</h3><p>A __dict__ is a dictionary containing the module’s symbol table, or in other words, used to store an object’s attributes. It is created by the execution engine along with the class it belongs to.</p><p>How does Python deal with the object and class attributes using the __dict__ ? Basically it contains all the attributes which describe the object in question and it can be used to alter or read the attributes.</p><p>The __dict__ documentation says the following:</p><p><strong>A dictionary or other mapping object used to store an object’s (writable) attributes.</strong></p><p>In fact, if you print out__dict__ for the following example:</p><pre>#!/usr/bin/python3<br>class Person:<br>    &quot;&quot;&quot;Defines Person class&quot;&quot;&quot;</pre><pre>#Class Variable<br>    class_variable = &quot;Class variable content&quot;</pre><pre>def __init__(self, name, last_name):<br>        &quot;&quot;&quot;Constructor method __init__&quot;&quot;&quot;</pre><pre># Instance properties<br>        self.name = name<br>        self.last_name = last_name</pre><pre>#str method definition<br>    def __str__(self):<br>        &quot;&quot;&quot;Defines string to return&quot;&quot;&quot;</pre><pre>str = self.name + self.last_name<br>        return(str)</pre><pre>#instance creation<br>person1 = Person (&quot;Kurt&quot;, &quot;Cobain&quot;)</pre><pre>#dir print<br>print(&quot;Person __dict__:&quot;, Person.__dict__)<br>print(&quot;&quot;)<br>print(&quot;person1 __dict__:&quot;, person1.__dict__)</pre><p>You will be able to actually see how the variables are stored in the dictionary for both class and instance</p><pre>Person __dict__: {&#39;__module__&#39;: &#39;__main__&#39;, &#39;__doc__&#39;: &#39;Defines Person class&#39;, &#39;class_variable&#39;: &#39;Class variable content&#39;, &#39;__init__&#39;: &lt;function Person.__init__ at 0x0000028833B58820&gt;, &#39;__str__&#39;: &lt;function Person.__str__ at 0x0000028833B588B0&gt;, &#39;__dict__&#39;: &lt;attribute &#39;__dict__&#39; of &#39;Person&#39; objects&gt;, &#39;__weakref__&#39;: &lt;attribute &#39;__weakref__&#39; of &#39;Person&#39; objects&gt;}</pre><pre>person1 __dict__: {&#39;name&#39;: &#39;Kurt&#39;, &#39;last_name&#39;: &#39;Cobain&#39;}</pre><h3>Creating Classes and Instance Pythonic and non-Pythonic</h3><p>A non-Pythonic way would be like this:</p><pre>class Square:<br>    def __init__(self, size=0):     <br>        if not isinstance(size, int):     <br>            raise TypeError(&quot;size must be an integer&quot;) <br>     <br>        if size &lt; 0:           <br>            raise ValueError(&quot;size must be &gt;= 0&quot;)     <br> <br>        self.__size = size * size    def area(self):    <br>       return self.__sizet__width()</pre><p>The disadvantage comes when we need to modify the code. The pythonic way of doing it would be to use getter and setter property methods.</p><pre>class Square:<br>    def __init__(self, size=0):        <br>        self.size = size    @property    <br>       def size(self):    <br>           return self.__size         @size.setter  <br>       def size(self, value):       <br>           if not isinstance(value, int):        <br>               raise TypeError(&quot;size must be an integer&quot;)    <br>           if value &lt; 0:          <br>               raise ValueError(&quot;size must be &gt;= 0&quot;)   <br>           self.__size = value        def area(self):      <br>        return self.__size * self.__size</pre><p>The advantage of using property allows us to attach code to the self.size attribute and any code assigned the value of size will be called with size in def size.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b2e4a62e465" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Static vs Dynamic libraries.]]></title>
            <link>https://2109.medium.com/static-vs-dynamic-libraries-42f20fdd81e7?source=rss-7fa4f56ebe40------2</link>
            <guid isPermaLink="false">https://medium.com/p/42f20fdd81e7</guid>
            <dc:creator><![CDATA[Flavio Orlando]]></dc:creator>
            <pubDate>Mon, 14 Dec 2020 17:15:09 GMT</pubDate>
            <atom:updated>2020-12-14T17:15:09.112Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/267/1*nvwUlFmv01OgR0PDk07Qsw.png" /></figure><p>The more code we write, the more we realize that some parts or chunks of code are repeatedly used in many programs. Something as repetitive like a mathematic operation can be re-used over and over in virtually every program we write.</p><p>A library allows us to have those repetitive functions outside the specific programs and be able to utilize them whenever we need. There are huge advantages associated to that, being the most evident not having to re-write the code over an over again every time we need to use the same function.</p><p><strong>Libraries are already-compiled and usage-ready functions to be used by any program we write.</strong></p><p>In order to insert our code in a library, it must be organized in the following way:</p><ul><li>One or more .c source files containing the code for our functions.</li><li>One or more .h header files containing the types (typedefs, structs) and prototypes of the functions we want to have available for utilization.</li></ul><pre>library1.c</pre><pre>int sum (int a, int b)<br>{<br> return a+b;<br>}<br>int sub (int a, int b)<br>{<br> return a-b;<br>}</pre><pre>library1.h</pre><pre>#ifndef _LIBRARY_1_H<br>#define _LIBRARY_1_H<br>int sum (int a, int b);<br>int sub (int a, int b);<br>#endif</pre><p><strong>Static and dynamic.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*7KPRGu8rInoQTap-7MpoOA.png" /><figcaption>The Uruguay’s Congress library. Totally unrelated to this article subject but it looks cool.</figcaption></figure><p>In Linux we can create two types of libraries: statics and dynamics.</p><p>An <em>static library</em> is “copied” into our program when we compile it. Once we have the executable file for our program, the library is no longer used by it. In fact, we could even delete it and the program would continue to work properly as it has copied everything it requires from the library. By the way, the program would only copy what it requires from the library. If the library contains two functions but only one is called by the program, that would be the only one copied over.</p><p>A <em>dynamic library</em> is NOT copied into the program. Whenever we are running our executable, each time the code requires something from the library it will go and get it from there. If the library is deleted, the program will return an error.</p><p>What are the advantages and disadvantages of each type of library?</p><ul><li>A program compiled with static libraries is larger, as a copy of everything required from the library is created. For static libraries memory footprints are also larger.</li><li>A program compiled with static libraries can even be moved over to a different computer without the need to move the libraries along with it. In static libraries, once everything is bundled into your application, you don’t have to worry that the client will have the right library (and version) available on their system.</li><li>A program compiled with static libraries is (at least on paper) faster on its execution. When it calls a function from the library, it is actually on its own code. Therefore, there is no need to access the library file to retrieve information from it.</li><li>One drawback of static libraries is, for any change in the static libraries, you have to recompile the main program every time.</li><li>If we change or even remove a static library, already created executables are not affected. However, if we change a dynamic library, executable will be affected. This could be an advantage, if we have amended the library in order to correct an error (as the error will be fixed in all executables).</li><li>Dynamic libraries will conserve memory since each program can access the library without creating an individual copy of the functions.</li></ul><p>Ultimately, deciding what type of library will be used is a negotiation between the advantages and inconveniences of each one, affected by the purpose of the software we are designing.</p><p>For smaller, simpler programs, static libraries are often used. Dynamic libraries are commonly used for larger programs or system libraries.</p><p>In Unix, static libraries are normally named <strong>lib</strong>name<strong>.a</strong> and dynamic libraries are named <strong>lib</strong>name<strong>.so</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*U73axV2NCTSr8O-l_TqmyA.png" /></figure><h4>Creating static libraries using GNU.</h4><p>Let’s assume we have a (very) simple C program called lib_mylib.c:</p><pre>#include &lt;stdio.h&gt; <br>void fun(void) <br>{ <br>  printf(&quot;fun() called from a static library&quot;); <br>}</pre><p>We also have a header .h file with the function prototype on it:</p><pre>#ifndef LIB_MYLIB_H<br>#def LIB_MYLIB_H</pre><pre><strong>void</strong> fun(<strong>void</strong>);</pre><pre>#endif</pre><p>In order to create a library (regardless of it being static or dynamic), we must first obtain the .o files from our .c files. The .o or object code files contain the compiled content of the corresponding .c file.</p><p>So, in order to create the .o file from the lib_mylib.c program, we must do the following entry:</p><pre>gcc -c lib_mylib.c -o lib_mylib.o</pre><p>In order to create the static library we must use the command <em>ar </em>(ar as in <em>archive</em>). This step is to bundle multiple object files in one static library:</p><pre>ar rcs lib_mylib.a lib_mylib.o</pre><p>From this point on the library can be called from another program. In order to test this, let’s create a program called <em>driver </em>that uses the newly created static library:</p><pre>#include &quot;lib_mylib.h&quot;</pre><pre><strong>void</strong> main()</pre><pre>{<br>fun();<br>}</pre><p>Let’s then compile it:</p><pre>gcc -c driver.c -o driver.o</pre><p>We must link the compiled <em>driver </em>program to the static library. Note that -L. is used to tell that the static library is in current folder.</p><pre>gcc -o driver driver.o -L. -l_mylib</pre><p>In the above command it is worth noting that the source code, <em>driver </em>in this case, needs to be listed before the -l flag. The expression, -l combined with _mylib tells the compiler to look for an archive called <em>lib_mylib.a. </em>This is why is so important to use the standard format for naming the libraries.</p><p>Finally, we can execute the driver program:</p><pre>./driver <br>fun() called from a static library</pre><h4>Creating dynamic libraries using GNU.</h4><p>The process starts in a similar way than before, by creating the .o files out of the source .c files. That is done quick and easy with the following command:</p><pre>gcc *.c -c -fPIC</pre><p>The -fPIC flag is an instruction for the code to be position-independent. This means that the generated machine code is not dependent on being located at a specific address in order to work. The -c flag ensures that each .o file is not linked yet.</p><p>Next step is to enter this command:</p><pre>gcc *.o -shared -o libname.so</pre><p>The -shared flag specifies that you intend to create a dynamic library.</p><p>Finally we must export the path for libraries for programs to know where to look for the specific library. This is done by using this command:</p><pre>export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH</pre><p>Let’s go a little deeper on this last step.</p><p>In Linux, the environment variable LD_LIBRARY_PATH is set of directories where libraries should be searched for first. It tells the dynamic link loader where to search for the dynamic shared libraries an application was linked against. The directories specified in LD_LIBRARY_PATH get searched before the standard locations.</p><p><strong>A few useful tools when working with libraries:</strong></p><p>The nm command will list symbols from object files, or when used when -D (“D” as in “dynamic”), it will basically show you the content of the library:</p><pre>$ nm -D libholberton.so<br>0000000000000b1c T _abs<br>0000000000000b4f T _atoi<br>0000000000202048 B __bss_start<br>                 w __cxa_finalize<br>0000000000202048 D _edata<br>0000000000202050 B _end<br>0000000000000bf4 T _fini<br>                 w __gmon_start__<br>0000000000000910 T _init<br>0000000000000b13 T _isalpha<br>0000000000000b2e T _isdigit<br>0000000000000b0a T _islower<br>0000000000000b25 T _isupper<br>                 w _ITM_deregisterTMCloneTable<br>                 w _ITM_registerTMCloneTable<br>                 w _Jv_RegisterClasses<br>0000000000000a75 T main<br>0000000000000ba9 T _memcpy<br>0000000000000b97 T _memset<br>                 U printf<br>0000000000000ae0 T _putchar<br>0000000000000b37 T _puts<br>0000000000000b59 T _strcat<br>0000000000000bba T _strchr<br>0000000000000b89 T _strcmp<br>0000000000000b41 T _strcpy<br>0000000000000a9f T _strlen<br>0000000000000b67 T _strncat<br>0000000000000b78 T _strncpy<br>0000000000000bd7 T _strpbrk<br>0000000000000bc9 T _strspn<br>0000000000000be5 T _strstr<br>                 U write</pre><p>The <em>ldd </em>command will list the shared libraries required by each program or shared library specified on the command line. You can do this simply by typing ldd and the name of your executable file:</p><pre>$ ldd len<br>        linux-vdso.so.1 =&gt;  (0x00007ffd391b8000)<br>        libholberton.so =&gt; /home/vagrant/holbertonschool-low_level_programming/0x18-dynamic_libraries/libholberton.so (0x00007f91da758000)<br>        libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f91da388000)<br>        /lib64/ld-linux-x86-64.so.2 (0x00007f91da960000)</pre><p>Finally <em>ldconfig </em>is used to create the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories (/lib and /usr/lib).</p><p><em>ldconfig </em>checks the header and filenames of the libraries it encounters when determining which versions should have their links updated.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42f20fdd81e7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What (really) happens at ls -l?]]></title>
            <link>https://2109.medium.com/what-really-happens-at-ls-l-489c9956bcba?source=rss-7fa4f56ebe40------2</link>
            <guid isPermaLink="false">https://medium.com/p/489c9956bcba</guid>
            <category><![CDATA[basic-linux-commands]]></category>
            <category><![CDATA[command-execution]]></category>
            <category><![CDATA[linux-kernel]]></category>
            <dc:creator><![CDATA[Flavio Orlando]]></dc:creator>
            <pubDate>Fri, 20 Nov 2020 20:04:21 GMT</pubDate>
            <atom:updated>2020-11-24T18:18:05.498Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*yQDOMrzIiPhwsAmbEy5wyQ.png" /><figcaption>We normally only get to see the tip of the iceberg.</figcaption></figure><p>“ls” it is probably the first and most often used command you will learn whenever you start working on a Linux based shell.</p><p>In the simplest possible way to say it, the “ls” command is used to list the contents of a directory. When used with no extra arguments within the command line (meaning when you type <em>only</em> “ls” and hit enter), the system response will display a list with whatever files (and sub-directories) exist at that moment within the <strong>current directory</strong> you are positioned at.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eh_z1vu_BmxYB6d9wTn4YA.png" /><figcaption>Image 01: On this particular system response, sub-directories are highlighted in blue, while “README.MD” is a file.</figcaption></figure><p>However, as mentioned before, it is possible to customize the “ls” command response by adding arguments to the command line. That’s it, by typing “ls” along with something else before hitting enter. Some (and just some)of the most common arguments that can be passed along with “ls” are shown on the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/961/1*T8BrZSmaXHFX-ERRthDmPQ.png" /></figure><p>As the purpose of each single argument that could be used along with “ls” escapes the scope of this article, we will be merely focusing on the instance when “-l” is added to the “ls” command line. The system response to this command + argument (ls -l) concatenation, will be to list the files in the working directory in “long format”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*66OiChI4OMTcTXG2A4KAyw.png" /><figcaption>Image 02: The content of the same directory shown on previous image, now on long display.</figcaption></figure><p>As seen on the image above, the long display view, displays not only the information on different alignment, but also displays way <em>more </em>information about the directory content, than the regular, no arguments, view.</p><p>This is a breakdown of the information shown in the long format response:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/753/1*RDLOhgFUzDsA57bOySxeIg.png" /><figcaption>Image 03. Breakdown of the long format response.</figcaption></figure><ul><li><strong>File Name -</strong>The name of the file or directory.</li><li><strong>Modification Time -</strong>The last time the file was modified. If the last modification occurred more than six months in the past, the date and year are displayed. Otherwise, the time of day is shown.</li><li><strong>Size -</strong>The size of the file in bytes.</li><li><strong>Group -</strong>The name of the group that has file permissions in addition to the file’s owner.</li><li><strong>Owner -</strong>The name of the user who owns the file.</li><li><strong>File Permissions -</strong>A representation of the file’s access permissions. The first character is the type of file. A “-” indicates a regular (ordinary) file. A “d” indicates a directory. The second set of three characters represent the read, write, and execution rights of the file’s owner. The next three represent the rights of the file’s group, and the final three represent the rights granted to everybody else. File permissions is a complex topic that would deserve an article of its own.</li></ul><p>So far so good? Great, but the original question stands. <strong>What REALLY happens when you type “ls -l” and hit enter?</strong></p><p>It is a fair question, because so far we have only covered what we <em>get to</em> <em>see on the screen </em>as system response, and that is only half of the answer. There is a whole process waiting to be explained, running on the background.</p><p>Once you type the command and hit enter, the system will recognize that you have entered <em>something, </em>which needs interpretation. Have you entered “ls -l” or have you typed “asdasdjkgjiii -156465” which, of course wouldn’t mean anything that the Shell can interpret?</p><p>In order to “realize” this, your system will start by creating a single string of characters from your text entry. This is done by using the <strong>getline() </strong>function. Shell is written mostly in the C programming language. The <strong>getline()</strong> function is a standard C function that “reads” the command line and creates the resulting string. This string will be then separated using prefixed delimiters, which in the case of “ls -l” is going to be the blank space between “ls” and “-l”. Each resulting individual element recognized after removing the delimiters is now known as a <strong>token. </strong>All tokens will then be placed in an array of strings. This whole process is known as <strong>tokenization</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*YYlurYtqebDPykQ8ES3v1w.png" /></figure><h4><strong>Alias?</strong></h4><p>Each token will have to be checked for previously assigned <em>aliases</em>. An alias is a shortcut to a longer command created by the user and not built-in into the shell. It would make a lot of sense not to assign an alias to “ls” as the alias will have priority over the original purpose of the function.</p><p>Once checked that “ls” is not an alias, next, the computer checks if tokens are built-in functions or not. A built-in command is a Linux/Unix command which is built into the shell interpreter itself. These commands will always be available in the RAM memory, so that accessing them is bit faster than compared to external commands which are stored on the hard drive.</p><p>If the command is a built-in, the shell runs the command directly, without using another program. For example, “cd” is a built-in, however, “ls” is not, so the system needs to find the specific executable file for it.</p><p>Once the built-in option has been discarded out, the Shell looks for an executable “ls” file in the <strong>“$PATH”</strong> variable. “$PATH” is a variable that stores a list of directories that Shell looks through whenever a command is entered. Each path in the “$PATH” variable is searched for the executable that corresponds with the command “<strong>ls”</strong>. The system will then, invoke the function <strong>stat()</strong> to check if there is a matching executable in each path.</p><p>When the “ls” executable file is located (normally within usr/bin/ls), the Shell invokes the <strong>execve()</strong> command to run or <em>execute </em>it.</p><p>With the <strong>execve()</strong> function the shell will know the command and arguments required for the execution, as well as where to find them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/539/1*TDceQ9WWb6-QXocU_-XzvA.png" /></figure><p>This example will execute the “ls” command found in “/usr/bin/ls”, with the “-l” argument.</p><p>The function gets executed within a child process by using the <strong>fork()</strong> function. <strong>fork()</strong> will create a “child” process and return the output to <strong>STDOUT . </strong>The “child” process is created by duplicating the calling process. This means that at the time the fork happens, both processes (calling or parent and child) are identical but have different PIDs (process id, a number that identifies each process within the system).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/218/1*IOV7gCfXIael86SAM-ZuHQ.png" /></figure><p>While this occurs, the parent process <strong>waits </strong>for execution to be completed and the child process is terminated, after which memory is cleared, and the parent process takes over again and waits for the next input from the user.</p><p>We can summarize the entire process like this:</p><ol><li>Shell interprets the command.</li><li>Shell creates a child process and executes the command on the child process.</li></ol><pre>fork()<br>execve()</pre><p>3. Shell waits for the child process’s completion.</p><pre>wait()</pre><p>So, that is pretty much it. Hope it helps somebody out there. Please let any of the authors know if you have any question.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=489c9956bcba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How integers are stored in memory using two’s complement.]]></title>
            <link>https://2109.medium.com/how-integers-are-stored-in-memory-using-twos-complement-30ad89a0d801?source=rss-7fa4f56ebe40------2</link>
            <guid isPermaLink="false">https://medium.com/p/30ad89a0d801</guid>
            <dc:creator><![CDATA[Flavio Orlando]]></dc:creator>
            <pubDate>Fri, 06 Nov 2020 16:41:53 GMT</pubDate>
            <atom:updated>2020-11-06T16:41:53.863Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/523/1*iE9telIGI0_ln2yfyYPe_A.png" /></figure><h4>But, what’s an integer?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*8PqSHOVnAmQLGZmyZHP_FQ.png" /></figure><p>According to Wikipedia “<em>An </em><strong><em>integer</em></strong><em> (from the Latin integer meaning “whole”) is colloquially defined as a number that can be written without a fractional component.</em>”</p><p>That means that numbers such as “21”, “189” or “-1824” are integers, while “8.5” , “13 1/2” or “√4” are not. The number “0” (zero) is also an integer.</p><p>As we know, computers, in order to understand and store data (such as integer numbers) have to encode such data as <em>binary numbers</em>, which are numbers expressed in the base-2 numeral system. This system is known as base-2 because it only uses two symbols (typically 0 and 1), instead of the 10 symbols used by the decimal system.</p><p>A binary number is a sequence of <em>bits. </em>The specific position of the 0’s and 1’s within the sequence of bits creates the representation of the number’s value.</p><h4>Bit what?</h4><p>Now, a <strong><em>bit </em></strong>is the most basic unit of the computer memory. Going back to Wikipedia: “<em>The bit is a basic unit of information in computing and digital communications. The name is a portmanteau of binary digit. The bit represents a logical state with one of two possible values. These values are most commonly represented as either “1”or”0&quot;, but other representations such as true/false, yes/no, +/−, or on/off are common.</em>”</p><p>Physically, there are several ways to store a bit, but ultimately, it depends on the physical device capability to differentiate between two states. Whatever the physical implementation, the important thing to know about a bit is that, like a switch, it can only take one of two values: it is either “on” or “off”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*00ldBlLLJZUuaRAf3ZGeGQ.png" /></figure><p>Eight bits placed together are called a <strong><em>byte. </em></strong>In a 32-bit or 64-bit computer, an integer value is stored in a collection of 4 bytes, or 32 bits.</p><p>So, if we consider the integer value “50”, the computer will store it in memory within a collection of bits of value 0 or 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*3AF9D8WKh_fSu5dP4ZEgGA.png" /></figure><p>The image of the Windows calculator above is actually filtering out all the leading 0’s in the binary value. The actual value stored would be something like this:</p><h3><strong>00000000 00000000 00000000 00110010</strong></h3><p>But what happens when representing <strong><em>negative </em></strong>integers such as -50?</p><p>Well, negative and positive are the two possible states of an integer value. In binary, each state can be represented by what is called the Most Significant Bit (MSB). The MSB will be the value of the first bit to the left of the binary number. If the integer number is positive, the MSB will have a value of 0, but if the number is negative, the bit is 1.</p><p>So -50 will be:</p><h3>10000000 00000000 00000000 00110010</h3><p>The problem is that using MSB to represent negative numbers has some important disadvantages, starting with the fact that reserving one bit to represent the negative/positive value of the number reduces the amount of available numbers by one half.</p><p>At the same time, using MSB would allow us to represent a value that actually doesn’t exist on decimal numeration such as “-0”. Like this:</p><h3><strong>10000000 00000000 00000000 00000000</strong></h3><h4><strong>Enters one’s complement:</strong></h4><p>A possible work around to the reduction of the range of numbers is to utilize the method called one’s complement.</p><p>This method represents negative numbers by inverting the values of bits of the number magnitude. In plain English this means to flip all the bit values within the binary number to its opposite. A 1 is placed whenever there was a 0 and vice versa. The signed binary number obtained from the inversion is called complement.</p><p>So, again, 50 and -50:</p><p><strong>00000000 00000000 00000000 00110010<br>11111111 11111111 11111111 11001101</strong></p><p>While this significantly increases the amount of available numbers, it still has the issue of allowing the representation of a negative 0, which again, doesn’t actually exists:</p><p><strong>11111111 11111111 11111111 11111111 (-0???).</strong></p><h4><strong>What about two’s complement then?</strong></h4><p>Wikipedia (again): “<em>Compared to other systems for representing signed numbers (e.g., ones’ complement), two’s complement has the advantage that the fundamental arithmetic operations of addition, subtraction, and multiplication are identical to those for unsigned binary numbers (as long as the inputs are represented in the same number of bits as the output, and any overflow beyond those bits is discarded from the result). This property makes the system simpler to implement, especially for higher-precision arithmetic. </em><strong><em>Unlike ones’ complement systems, two’s complement has no representation for negative zero, and thus does not suffer from its associated difficulties.</em></strong>”</p><h4>But how?</h4><p>In a nutshell:</p><p>1- Find the binary equivalent for any given decimal.<br>2- Find the one’s complement of the binary by inverting 0&#39;s and 1’s.<br>3- Add 1 to the one’s complement.</p><p>Let’s start again with the integer 50 in binary:</p><p>00000000 00000000 00000000 00110010</p><p>Now let’s calculate -50 with the one’s complement method:</p><p>11111111 11111111 11111111 11001101</p><p>Finally we are going to add one (+1 in binary, of course) to -50.</p><p>11111111 11111111 11111111 11001101 +</p><p>00000000 00000000 00000000 00000001 (this is the binary representation of the decimal integer “1”).</p><p>Result:</p><p><strong>1</strong>1111111 11111111 11111111 11001101</p><p>The fact that the first bit on the left is a “1” indicates that the number is negative.</p><p>The range of integers obtained from using the two’s complement method is the same as the obtained from the one’s complement, but by using the two’s method a negative zero value will not be represented, as “1” it is always added to the complement of zero.</p><p>Decimal “0” — 00000000 00000000 00000000 00000000</p><p>“-0” (not a thing) — 11111111 11111111 11111111 11111111</p><p>addition of 1 -00000000 00000000 00000000 00000001</p><p>Result: 00000000 00000000 00000000 00000000 (actually 0!).</p><p>So, that are the reasons why computers use two’s complements to deal with negative values.</p><p>Hope you enjoyed!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=30ad89a0d801" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Worst abuse of the C preprocessor (IOCCC winner, 1986)]]></title>
            <link>https://2109.medium.com/worst-abuse-of-the-c-preprocessor-ioccc-winner-1986-494d05001b3c?source=rss-7fa4f56ebe40------2</link>
            <guid isPermaLink="false">https://medium.com/p/494d05001b3c</guid>
            <dc:creator><![CDATA[Flavio Orlando]]></dc:creator>
            <pubDate>Mon, 19 Oct 2020 16:27:06 GMT</pubDate>
            <atom:updated>2020-10-19T16:27:06.682Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*icstgIz0Ml6T2hqHvJCGlA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/644/1*yFG7t9xrh2L2HC1Cl4KXIA.png" /></figure><p>IOCCC (The International Obfuscated C Code Contest) is an annual programming contest for C <em>“obfuscated”</em> code.</p><p>Obfuscated code is machine code that is deliberately written to be hardly understood by humans. It can be used for different purposes but it is mainly done for security reasons, by making it obscure to avoid tampering, hide implicit values or conceal the logic used.</p><p>In <strong><em>1986</em></strong> the award for the category <em>“worst abuse of the C preprocessor” </em>was given to <strong>Jim Hague for </strong>writing the following piece of code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*dO_lEh0Y-cxUr1akpJD3VQ.png" /></figure><p>Apart from the particular formatting, what jumps to the eye is the number of “unnecessary” macros and the repetitive use of DIT and DAT variations.</p><p>If we compile the code at this point we see many warnings. Among them, two for the implicit declaration of __DIT and _DAH. After that step, we can run the code, and as we provide sequences of ascii characters, it returns sequences of “.” and _.</p><p>$ ./a.out hello, world</p><p>.... . .-.. .-.. --- --..-- .-- --- .-. .-.. -..</p><p>This is actually Morse code (the one used in telegraph communications back in XIX century). When decoded it reverses back to HELLO, WORLD.</p><p>Let’s first try to perform the pre-processor job and replace the macros by their values. After a bit of reformatting, this is what we have:</p><pre>char _DAH_[]=”ETIANMSURWDKGOHVFaLaPJBXCYZQb54a3d2f16g7c8a90l?e’b.s;i,d:”;<br>main()<br>{<br>char *_DIT, *DAH_, *DIT_, *_DIT_, *malloc (), *gets();<br>for (_DIT = malloc(81), DIT_=_DIT++; _DIT == gets(_DIT); __DIT(‘n’))<br>   for (DAH_=_DIT; *DAH_; __DIT(*_DIT_ ? _DAH(*DIT_ ) : ‘?’),__DIT(‘ ‘),DAH_++) <br>     for (*DIT_ = 2, _DIT_ = _DAH_; *_DIT_ &amp;&amp; (*_DIT_ != (*DAH_ &gt;= ‘a’ ? *DAH_&amp;223 : *DAH_ )); (*DIT_ )++,_DIT_++)<br>         *DIT_+= (*_DIT_&gt;=’a’ ? *_DIT_ — ‘a’ : 0);<br>}<br>_DAH(DIT_)<br>{ <br>__DIT(DIT_&gt; 3 ? _DAH(DIT_&gt;&gt;1) : ‘’);<br>return DIT_ &amp; 1 ? ‘-’ : ‘.’;<br>} <br>__DIT(DIT_) char DIT_;<br>{<br>(void) write (1,&amp;DIT_,1);<br>}</pre><p>We see the three functions we expected: main, _DAH, and __DIT. We also see an external variable __DAH__ , a long string. __DIT is like the putchar function from the standard library, printing a char at a time.</p><p>And what about _DAH ?</p><p>As long as the argument is a number that takes more than 2 bits to write, it calls the function again, stripping the number from its last bit. The output will be part of the argument printed as — and . masking for 1 and 0 , i.e. the number in binary format, and it will return the second leftmost digit. As an example, if we call _DAH(5) , 5 being 101 in binary, it will call _DAH(2) . That is the base case. it prints (nothing) and return 10 &amp; 1 == 0 so . . Then it will print . and return 101 &amp; 1 == 1 so -. If we want to print that we have to call __DIT(_DAH(5)) which outputs .- which actually corresponds to 3 written in binary. _DAH(n) is a rather obfuscated function, which will not print/return n in binary but n — 2 .</p><h4>The main function</h4><pre>char code[]=”ETIANMSURWDKGOHVFaLaPJBXCYZQb54a3d2f16g7c8a90l?e’b.s;i,d:”;<br>main ( )<br>{<br>char *line, *letter, *value, *code_copy, *malloc ( ),* gets ( );<br>    for (line = malloc(81), value= line++; line == gets(line); putchar(‘n’))<br>     {<br>     for (letter = line; *letter; putchar(*code_copy ? _DAH(*value) : ‘?’), putchar(‘ ‘), letter++)<br>         {<br>         for (*value = 2, code_copy = code; *code_copy &amp;&amp; (*code_copy != (*letter &gt;= ‘a’ ? *letter &amp; 2<br>23: *letter)); (*value)++, code_copy++)<br>             {<br>              *value += (*code_copy &gt;=’a’ ? *code_copy — ‘a’: 0);<br>             }<br>         }<br>     }<br>}</pre><p><strong>The outer loop</strong>: Each time the user enters a new line, it creates a buffer, and reads a line from the standard input to the buffer. The function either returns the buffer it takes as an argument or NULL in this case. This loop also assigns an address to value which it will use in the inner loop. This loop will print a new line after completion of the two inner loops.</p><p><strong>The middle loop</strong>: it will loop through each letter in the string obtained above. As it moves from one letter to another, it will either print it using _DAH seen above or print a ? and add a space. As we looked at _DAH above, we used integers as arguments. It works fine as letters, ASCII characters like *value to be more precise, and are small integers in C.</p><p><strong>The inner loop</strong>: it sets the value of *value to 2, and look at the value of the letter at this point. ((*letter &gt;= &#39;a&#39;) ? *letter &amp; 223 : *letter) means if the letter is lower case, use its upper case version. The octal 233 or 10010011 serves as a mask to change the one bit that is different for a lowercase letter than an uppercase. Of course 223 is not the most obvious one 137 would more easily come to mind. Knowing this, we can realize this inner loop will iterate as long as the *letter variable does not have a match in the code string. As it iterates, it will increase *value by 1 and move on to the next letter in code . Interestingly, if at one point in this process, the letter in code is lowercase, the loop will increase *value by some special number.</p><p><strong>Going from a letter to its Morse code</strong>: globally, the inner loop starts with a *value = 2 and increase it by 1 each time the iteration moves on to the next letter in code until the letter in code is the letter in my line. It will then print *value using _DAH . Let’s see some examples:</p><p>*letter = &#39;E&#39; then *value = 2 and since E is the first letter in code it goes back to the middle loop and calls putchar(*code_copy ? _DAH(*value) : &#39;?&#39;)` . Value of *code_copy == &#39;E&#39; here so expression above becomes putchar(_DAH(2)) As seen above _DAH(2) returns/prints the binary value of 0as ‘.’ and ‘-’. The output of this expression will be . This is indeed the Morse code for E.</p><p>*letter = &#39;I&#39; then to start *value = 2 , since I is the third letter in code the inner loop will exit with *value == 4 . putchar(_DAH(4)) will print out .. , the Morse code for I.</p><p>We can observe a pattern, as the order chosen for the letters in code is such that the letters have Morse codes that are equal to the binary values of their index in the code string plus 2. Of course, this is not perfect. Remember the inner loop special cases ? If *code == &#39;a&#39; , the loop will skip that *value or said otherwise, this *value or index does not map any Morse code. If *code == &#39;b&#39; , the loop will skip that *value and shift by &#39;b&#39; — &#39;a&#39; == 1 , which means from the on, the Morse code for the letters in code will map the value of their index in the string plus 3. An so on, next comes a d which will shift that new mapping by &#39;d&#39; — &#39;a&#39; == 3 .</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=494d05001b3c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[EVERYTHING YOU ALWAYS WANTED TO KNOW ABOUT STATIC LIBRARIES BUT WERE AFRAID TO ASK.]]></title>
            <link>https://2109.medium.com/everything-you-always-wanted-to-know-about-static-libraries-but-were-afraid-to-ask-fb93c8562d87?source=rss-7fa4f56ebe40------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb93c8562d87</guid>
            <dc:creator><![CDATA[Flavio Orlando]]></dc:creator>
            <pubDate>Sat, 10 Oct 2020 00:45:35 GMT</pubDate>
            <atom:updated>2020-10-12T23:06:43.179Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*DwjykunMAaBVqWu5OVHNhA.png" /></figure><blockquote><em>(This article applies for UNIX systems only.)</em></blockquote><p><strong><em>What is a C static library?</em></strong></p><p>A C library is just a set of named functions.</p><p>Static libraries (also known as archives) have been around as long as C itself. Like a .zip file, they’re just a bag of object files — containing functions, of course — with a table of contents in front giving the address of each name. Static libraries are created from object files using a librarian utility of some kind. One such programs is ar (ar as in <em>archive</em>).</p><p>Functions in static libraries are joined to a program’s main module by a static linker at build time to produce an executable program. The executable incorporates the libraries’ object code into its own body, making it completely self-sufficient.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*kpzId_2441tRglQpYq4_cQ.png" /></figure><p><strong><em>Why using them?</em></strong></p><p>One major advantage of static libraries being preferred even now is speed. There will be no dynamic querying of symbols in static libraries. Many production line software use static libraries even today.</p><p>There are several advantages to statically linking libraries with an executable instead of dynamically linking them. The most significant advantage is that the application can be certain that all its libraries are present and that they are the correct version. This avoids dependency problems, known colloquially as DLL Hell or more generally dependency hell. Static linking can also allow the application to be contained in a single executable file, simplifying distribution and installation.</p><p>With static linking, it is enough to include those parts of the library that are directly and indirectly referenced by the target executable (or target library). With dynamic libraries, the entire library is loaded, as it is not known in advance which functions will be invoked by applications. Whether this advantage is significant in practice depends on the structure of the library.</p><p>In static linking, the size of the executable becomes greater than in dynamic linking, as the library code is stored within the executable rather than in separate files. But if library files are counted as part of the application then the total size will be similar, or even smaller if the compiler eliminates the unused symbols.</p><p><strong><em>How to create static libraries?</em></strong></p><p>To create a static library, we need to specify to the compiler, which is GCC in our case, that we want to compile all library codes (*.c) into object files (*.o) without linking. To do that we are going to use the command below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/619/1*2q5fmqBpYhM-Xi4wsE0Q4g.png" /></figure><p>Note that the “*.c” matches all files in the current working directory with the “.c” extension.</p><p>For example, let us take two c files, add.c, and mul.c and a header file that contains the prototypes of these functions. The picture below shows the output generated after using the command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*RQ0pbAkhANNHm2hv3C9YZw.png" /></figure><p>Once we have object file(s), we can now bundle all object files into one static library.<br>To create a static library or to add additional object files to an existing static library, we have to use the <a href="https://man7.org/linux/man-pages/man1/ar.1.html">ar</a> (archiver) program. We can use a command like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*NySqjVKdAXERxrTTjjj9SA.png" /></figure><p>This command creates a static library named “libname.a” and puts copies of the object files “add.o” and “mul.o” in it. The ‘c’ flag tells ar to create the library if it doesn’t already exist. The ‘r’ flag tells it to insert object files or replace existing object files in the library, with the new object files.</p><p>In order to actually use the newly created static library we can use the command below to create our final executable program:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/389/1*LALj7xexnJUcTJhIoAtyJg.png" /></figure><p>When using GCC we must use the following flags to create the static library:</p><ul><li>-L : specifies the path to the library .We can use -L in order to point to the current directory and -L/home/tmp to point to the /home/tmp directory.</li><li>-l : adds the library’s name to the list of object file names given to the linker.</li></ul><p>This will create a program using the object file “main.o”, and any symbols it requires from the “name” static library.</p><p>After a library archive is created, or modified, there is a need to index it. This index is later used by the compiler to speed up symbol-lookup inside the library, and to make sure that the order of the symbols in the library won’t matter during compilation. The command used to create or update the index is called &#39;ranlib&#39;.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*Kpj9U_y28F9Tb1-MYIWakQ.png" /></figure><p>Cheers!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb93c8562d87" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>