<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-2321045628204084378</atom:id><lastBuildDate>Wed, 11 Mar 2026 10:41:09 +0000</lastBuildDate><category>Latest</category><category>How-to</category><category>Web</category><category>learn</category><category>programming</category><category>Google</category><category>Blogging</category><category>C</category><category>Brain</category><category>blogger</category><category>Entertainment</category><category>News</category><category>Computer</category><category>Information</category><category>html</category><category>tricks</category><category>linux</category><category>ubuntu</category><category>web language</category><category>google-search</category><category>sorting</category><category>choose</category><category>git</category><category>github</category><category>python</category><category>Psychology</category><category>Sports</category><category>css</category><category>delhi</category><category>hash</category><category>security</category><category>webmaster</category><category>Android</category><category>Memory</category><category>cache</category><category>cartoon</category><category>cordova</category><category>debugging</category><category>gdb</category><category>golang</category><category>phonegap</category><category>places</category><category>string</category><category>Animals</category><category>Diwali</category><category>List</category><category>architecture</category><category>colleges</category><category>commits</category><category>cpp</category><category>fix</category><category>framework</category><category>image processing</category><category>javascript</category><category>laptop</category><category>mvc</category><category>php</category><category>ssd</category><category>url-shortener</category><category>youtube</category><title>Information Well</title><description></description><link>http://fetch-info.blogspot.com/</link><managingEditor>noreply@blogger.com (Kapil Garg)</managingEditor><generator>Blogger</generator><openSearch:totalResults>98</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-2032735266810078980</guid><pubDate>Fri, 20 Sep 2024 15:59:00 +0000</pubDate><atom:updated>2024-09-20T08:59:40.547-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">golang</category><category domain="http://www.blogger.com/atom/ns#">How-to</category><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">programming</category><title>Basic CLI Application Using Golang</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9oqrjibgIZn0zhA1WkwDl2bM81xa5fbFSc2R6JAbI8LyBDiB_w1L9JzCr0-021YfD7GtyxFBfQt7sXIwcL3z82CAjP2F5-B32BhZCfFS0izvy9kc82OmR1-LorLkTsPUKZ7pAV8xPXrql8oZXUwmNgD37h8-cLOg9aF-0vnY-jCFKCIIEMDFggETKGj_Q/s1024/golang-cli-application.webp&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; width=&quot;400&quot; data-original-height=&quot;1024&quot; data-original-width=&quot;1024&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9oqrjibgIZn0zhA1WkwDl2bM81xa5fbFSc2R6JAbI8LyBDiB_w1L9JzCr0-021YfD7GtyxFBfQt7sXIwcL3z82CAjP2F5-B32BhZCfFS0izvy9kc82OmR1-LorLkTsPUKZ7pAV8xPXrql8oZXUwmNgD37h8-cLOg9aF-0vnY-jCFKCIIEMDFggETKGj_Q/s400/golang-cli-application.webp&quot;/&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h1&gt;Writing a Basic CLI Application Using Go&lt;/h1&gt;

    &lt;p&gt;Go is a great language for building command-line tools due to its simplicity and powerful standard library. In this article, we will go through how to create a simple CLI app in Go that accepts commands and flags from the user.&lt;/p&gt;

    &lt;h2&gt;Step 1: Install Go&lt;/h2&gt;
    &lt;p&gt;Before you start, ensure that Go is installed on your machine. You can download it from &lt;a href=&quot;https://golang.org/dl/&quot;&gt;here&lt;/a&gt;. Once installed, verify by running:&lt;/p&gt;
    &lt;pre class=&quot;prettyprint&quot;&gt;&lt;code&gt;go version&lt;/code&gt;&lt;/pre&gt;

    &lt;h2&gt;Step 2: Set Up Your Project&lt;/h2&gt;
    &lt;p&gt;Create a new folder for your project and navigate into it:&lt;/p&gt;
    &lt;pre class=&quot;prettyprint&quot;&gt;&lt;code&gt;mkdir cli-app
cd cli-app
&lt;/code&gt;&lt;/pre&gt;

    &lt;h2&gt;Step 3: Write Your CLI App&lt;/h2&gt;
    &lt;p&gt;In your project folder, create a file called &lt;code&gt;main.go&lt;/code&gt;. This is where we&#39;ll write our CLI code.&lt;/p&gt;
    &lt;pre class=&quot;prettyprint&quot;&gt;&lt;code&gt;
package main

import (
    &quot;flag&quot;
    &quot;fmt&quot;
    &quot;os&quot;
)

func main() {
    name := flag.String(&quot;name&quot;, &quot;World&quot;, &quot;a name to say hello to&quot;)
    flag.Parse()

    if len(os.Args) &lt; 2 {
        fmt.Println(&quot;Usage: go run main.go --name [your name]&quot;)
        os.Exit(1)
    }

    fmt.Printf(&quot;Hello, %s!\n&quot;, *name)
}
&lt;/code&gt;&lt;/pre&gt;

    &lt;h2&gt;Step 4: Run the Application&lt;/h2&gt;
    &lt;p&gt;You can now run your CLI app by using the following command:&lt;/p&gt;
    &lt;pre class=&quot;prettyprint&quot;&gt;&lt;code&gt;go run main.go --name=John&lt;/code&gt;&lt;/pre&gt;

    &lt;p&gt;This will output:&lt;/p&gt;
    &lt;pre class=&quot;prettyprint&quot;&gt;&lt;code&gt;Hello, John!&lt;/code&gt;&lt;/pre&gt;

    &lt;p&gt;If you omit the &lt;code&gt;--name&lt;/code&gt; flag, it will default to:&lt;/p&gt;
    &lt;pre class=&quot;prettyprint&quot;&gt;&lt;code&gt;Hello, World!&lt;/code&gt;&lt;/pre&gt;

    &lt;h2&gt;Step 5: Add More Functionality&lt;/h2&gt;
    &lt;p&gt;You can add more flags to extend your CLI tool. For example, to add a flag for age:&lt;/p&gt;
    &lt;pre class=&quot;prettyprint&quot;&gt;&lt;code&gt;age := flag.Int(&quot;age&quot;, 0, &quot;your age&quot;)&lt;/code&gt;&lt;/pre&gt;

    &lt;p&gt;Then use it in the output:&lt;/p&gt;
    &lt;pre class=&quot;prettyprint&quot;&gt;&lt;code&gt;fmt.Printf(&quot;Hello, %s! You are %d years old.\n&quot;, *name, *age)&lt;/code&gt;&lt;/pre&gt;

    &lt;p&gt;This simple guide demonstrates how easy it is to build a basic CLI application using Go. By using Go&#39;s &lt;code&gt;flag&lt;/code&gt; package, you can create commands and flags to handle user input efficiently. With more advanced packages, you can build even more powerful CLI tools.&lt;/p&gt;
</description><link>http://fetch-info.blogspot.com/2024/09/basic-cli-application-using-golang.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9oqrjibgIZn0zhA1WkwDl2bM81xa5fbFSc2R6JAbI8LyBDiB_w1L9JzCr0-021YfD7GtyxFBfQt7sXIwcL3z82CAjP2F5-B32BhZCfFS0izvy9kc82OmR1-LorLkTsPUKZ7pAV8xPXrql8oZXUwmNgD37h8-cLOg9aF-0vnY-jCFKCIIEMDFggETKGj_Q/s72-c/golang-cli-application.webp" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-35061287769082762</guid><pubDate>Sun, 08 Sep 2024 08:36:00 +0000</pubDate><atom:updated>2024-09-08T01:44:24.266-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">golang</category><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">programming</category><title>Why Interfaces are Awesome in Go</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaR5iD2T-CfUtw0GJkAg7gwIFZ_4HVfzvmJV2IV0gapMU6x6K6kF9IICeQAQZjbbvOJrBQFoyAC02N7ugStd75j8orWze4KWGmFKIqkzpPpozmKesBOgPW2YbrWu-zvPoC7RlNSohXmUPYzwrJ5uRA5rLVNJujhQpmunryjrKMrMBVRXLzQXfHfMlYtHYV/s1024/DALL%C2%B7E%202024-09-08%2014.03.34%20-%20A%20clean%20and%20modern%20illustration%20showing%20Go%20interfaces%20concept%20in%20programming.%20The%20image%20could%20feature%20symbolic%20elements%20like%20a%20flowchart%20with%20intercon.webp&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;golang interfaces&quot; border=&quot;0&quot; width=&quot;600&quot; data-original-height=&quot;1024&quot; data-original-width=&quot;1024&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaR5iD2T-CfUtw0GJkAg7gwIFZ_4HVfzvmJV2IV0gapMU6x6K6kF9IICeQAQZjbbvOJrBQFoyAC02N7ugStd75j8orWze4KWGmFKIqkzpPpozmKesBOgPW2YbrWu-zvPoC7RlNSohXmUPYzwrJ5uRA5rLVNJujhQpmunryjrKMrMBVRXLzQXfHfMlYtHYV/s600/DALL%C2%B7E%202024-09-08%2014.03.34%20-%20A%20clean%20and%20modern%20illustration%20showing%20Go%20interfaces%20concept%20in%20programming.%20The%20image%20could%20feature%20symbolic%20elements%20like%20a%20flowchart%20with%20intercon.webp&quot;/&gt;&lt;/a&gt;&lt;/div&gt;

&lt;h1&gt;Why Interfaces are Awesome in Go Language&lt;/h1&gt;

&lt;p&gt;Go, or Golang, is known for being a simple, yet powerful programming language. One of the core concepts that makes Go stand out is &lt;strong&gt;interfaces&lt;/strong&gt;. Interfaces in Go help you write flexible, reusable, and maintainable code. They allow different types to share common behaviors without worrying about the exact type behind the scenes.&lt;/p&gt;

&lt;p&gt;In this article, we&#39;ll dive into why interfaces are awesome, with easy-to-understand examples. We’ll also give some tips on how to make the best use of them in your Go programs.&lt;/p&gt;

&lt;h2&gt;What is an Interface?&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;interface&lt;/strong&gt; in Go defines a set of method signatures (rules), but it doesn&#39;t provide the implementation. Instead, any type that implements these methods can be said to &quot;satisfy&quot; the interface. This allows you to write code that can work with different types as long as they share certain behaviors.&lt;/p&gt;

&lt;h3&gt;Basic Example of an Interface&lt;/h3&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code&gt;package main

import &quot;fmt&quot;

// Define an interface with one method
type Speaker interface {
    Speak() string
}

// Define a type that implements the Speaker interface
type Dog struct{}

func (d Dog) Speak() string {
    return &quot;Woof!&quot;
}

// Another type that implements the Speaker interface
type Cat struct{}

func (c Cat) Speak() string {
    return &quot;Meow!&quot;
}

func main() {
    var animals []Speaker
    // Dog and Cat both implement the Speaker interface
    animals = append(animals, Dog{}, Cat{})
    for _, animal := range animals {
        fmt.Println(animal.Speak())
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this example, we define a &lt;code&gt;Speaker&lt;/code&gt; interface that requires a &lt;code&gt;Speak()&lt;/code&gt; method. Both &lt;code&gt;Dog&lt;/code&gt; and &lt;code&gt;Cat&lt;/code&gt; implement this interface by providing their own versions of the &lt;code&gt;Speak()&lt;/code&gt; method. When we loop through a slice of &lt;code&gt;Speaker&lt;/code&gt; types, the correct &lt;code&gt;Speak()&lt;/code&gt; method for each type is called.&lt;/p&gt;

&lt;p&gt;This flexibility is what makes interfaces so powerful. You can handle multiple types without needing to know their exact types, as long as they implement the required behavior.&lt;/p&gt;

&lt;h2&gt;Why are Interfaces Awesome?&lt;/h2&gt;

&lt;h3&gt;1. Encapsulation of Behavior&lt;/h3&gt;

&lt;p&gt;With interfaces, you can focus on behavior rather than specific types. In the example above, the &lt;code&gt;Speaker&lt;/code&gt; interface doesn&#39;t care whether it&#39;s a &lt;code&gt;Dog&lt;/code&gt;, &lt;code&gt;Cat&lt;/code&gt;, or some other type. It only cares that the type knows how to &lt;code&gt;Speak()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;2. Flexibility&lt;/h3&gt;

&lt;p&gt;Interfaces let you write flexible code. You can add new types that satisfy the interface without changing existing code. This makes it easier to extend and maintain your programs.&lt;/p&gt;

&lt;p&gt;For example, if you later decide to add a &lt;code&gt;Bird&lt;/code&gt; type, it can easily be added to the &lt;code&gt;animals&lt;/code&gt; list without changing anything else:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code&gt;type Bird struct{}

func (b Bird) Speak() string {
    return &quot;Chirp!&quot;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Just add &lt;code&gt;Bird{}&lt;/code&gt; to the slice of animals, and it works perfectly:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;animals = append(animals, Bird{})
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;3. Loose Coupling&lt;/h3&gt;

&lt;p&gt;Interfaces allow you to decouple different parts of your code. The code using the interface doesn&#39;t need to know the details of the types that implement it. This leads to more modular code, which is easier to test and reuse.&lt;/p&gt;

&lt;h2&gt;Making Best Use of Interfaces&lt;/h2&gt;

&lt;p&gt;Now that we’ve seen why interfaces are great, let’s talk about how to use them effectively.&lt;/p&gt;

&lt;h3&gt;1. Design for Behavior, Not Data&lt;/h3&gt;

&lt;p&gt;When designing interfaces, focus on what the type should do, not how it stores data. Instead of thinking, &quot;I need an interface for animals with legs,&quot; think, &quot;I need an interface for things that can &lt;code&gt;Speak()&lt;/code&gt;.&quot; This encourages flexible design and avoids overcomplicating your code.&lt;/p&gt;

&lt;h3&gt;2. Keep Interfaces Small&lt;/h3&gt;

&lt;p&gt;Go encourages small, focused interfaces. A good rule of thumb is to keep interfaces with just one or two methods. This makes them easier to implement and encourages composability.&lt;/p&gt;

&lt;p&gt;Here’s a good example of small interfaces:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code&gt;type Writer interface {
    Write(p []byte) (n int, err error)
}

type Reader interface {
    Read(p []byte) (n int, err error)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Both &lt;code&gt;Writer&lt;/code&gt; and &lt;code&gt;Reader&lt;/code&gt; interfaces have just one method, which makes them easy to implement. You can combine them in more complex interfaces when needed:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;type ReadWriter interface {
    Reader
    Writer
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;3. Use Empty Interface with Caution&lt;/h3&gt;

&lt;p&gt;Go has an &quot;empty&quot; interface, &lt;code&gt;interface{}&lt;/code&gt;, which can hold any type. This is sometimes useful, but it should be used sparingly. Overusing &lt;code&gt;interface{}&lt;/code&gt; can lead to code that’s hard to understand and error-prone, because you lose the benefits of type safety.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;func PrintAnything(i interface{}) {
    fmt.Println(i)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;While this works for any type, it&#39;s better to use specific interfaces or types whenever possible to keep your code clear and maintainable.&lt;/p&gt;

&lt;h3&gt;4. Use Type Assertions to Access Underlying Types&lt;/h3&gt;

&lt;p&gt;Sometimes, you might need to access the underlying type that implements an interface. You can do this using &lt;strong&gt;type assertions&lt;/strong&gt;. Here&#39;s an example:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&lt;code&gt;func SpeakLoudly(s Speaker) {
    fmt.Println(s.Speak())

    if dog, ok := s.(Dog); ok {
        fmt.Println(&quot;This is a dog, let&#39;s make it louder: WOOF!&quot;)
    }
}

func main() {
    SpeakLoudly(Dog{})
    SpeakLoudly(Cat{})
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this example, we use a type assertion (&lt;code&gt;s.(Dog)&lt;/code&gt;) to check if the &lt;code&gt;Speaker&lt;/code&gt; is of type &lt;code&gt;Dog&lt;/code&gt; and handle it differently.&lt;/p&gt;

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

&lt;p&gt;Interfaces are awesome in Go because they provide flexibility, loose coupling, and allow for clean, maintainable code. By focusing on behavior rather than concrete types, you can write code that works with a wide range of types, making it easier to extend and maintain over time.&lt;/p&gt;

&lt;p&gt;To make the best use of interfaces in Go:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Design interfaces around behavior.&lt;/li&gt;
    &lt;li&gt;Keep them small and focused.&lt;/li&gt;
    &lt;li&gt;Avoid overusing the empty interface.&lt;/li&gt;
    &lt;li&gt;Use type assertions wisely when necessary.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By following these practices, you can make the most out of Go’s powerful interface system and write cleaner, more efficient code.&lt;/p&gt;

&lt;p&gt;Happy coding! 🎉&lt;/p&gt;</description><link>http://fetch-info.blogspot.com/2024/09/why-interfaces-are-awesome-in-go.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaR5iD2T-CfUtw0GJkAg7gwIFZ_4HVfzvmJV2IV0gapMU6x6K6kF9IICeQAQZjbbvOJrBQFoyAC02N7ugStd75j8orWze4KWGmFKIqkzpPpozmKesBOgPW2YbrWu-zvPoC7RlNSohXmUPYzwrJ5uRA5rLVNJujhQpmunryjrKMrMBVRXLzQXfHfMlYtHYV/s72-c/DALL%C2%B7E%202024-09-08%2014.03.34%20-%20A%20clean%20and%20modern%20illustration%20showing%20Go%20interfaces%20concept%20in%20programming.%20The%20image%20could%20feature%20symbolic%20elements%20like%20a%20flowchart%20with%20intercon.webp" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-1493706666571856725</guid><pubDate>Thu, 05 Sep 2024 05:24:00 +0000</pubDate><atom:updated>2024-09-04T22:26:16.836-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">How-to</category><category domain="http://www.blogger.com/atom/ns#">Information</category><category domain="http://www.blogger.com/atom/ns#">Latest</category><category domain="http://www.blogger.com/atom/ns#">learn</category><title>How to Start Learning Go Language: A Beginner&#39;s Guide</title><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDyhwv7zT-8d6TGN34ZtX97dXsz98go2PGLJExe7udNAgI5MUJ3u9qsEC7axtCm6mBgsX2VUuctpGiyEVTot7ekJmluwyV4TEvgJ2dbNlyseIi_i0B9eiuSQBjuyyDPzhHX7S8FDJ1BM5Kv4zZhyphenhyphen4bLchfv-gS3Vk2tIXLTusxMw07afmTUZDyghox-e5h/s860/360-3600447_go-language-logo-png-transparent-png.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;380&quot; data-original-width=&quot;860&quot; height=&quot;282&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDyhwv7zT-8d6TGN34ZtX97dXsz98go2PGLJExe7udNAgI5MUJ3u9qsEC7axtCm6mBgsX2VUuctpGiyEVTot7ekJmluwyV4TEvgJ2dbNlyseIi_i0B9eiuSQBjuyyDPzhHX7S8FDJ1BM5Kv4zZhyphenhyphen4bLchfv-gS3Vk2tIXLTusxMw07afmTUZDyghox-e5h/w640-h282/360-3600447_go-language-logo-png-transparent-png.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;

    &lt;h1&gt;How to Start Learning Go Language: A Beginner&#39;s Guide&lt;/h1&gt;

    &lt;h2&gt;Introduction to Go Language&lt;/h2&gt;
    &lt;p&gt;Go, also called Golang, is a programming language developed by Google in 2009. It’s simple, fast, and great for building reliable software. If you’re interested in learning programming or want to try a new language, Go is a great choice. This article will help you understand how to start learning Go in a simple and easy way.&lt;/p&gt;

    &lt;h2&gt;Why Learn Go?&lt;/h2&gt;
    &lt;p&gt;Before we jump into how to learn Go, let’s look at why Go is popular:&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;strong&gt;Simple syntax:&lt;/strong&gt; Go is easier to learn compared to other programming languages like C++ or Java.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Fast performance:&lt;/strong&gt; Go runs quickly, making it great for web servers and large systems.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Great for beginners:&lt;/strong&gt; Go has built-in tools that make coding easier, especially for new programmers.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Growing demand:&lt;/strong&gt; More companies are using Go, so learning it can help in your career.&lt;/li&gt;
    &lt;/ul&gt;

    &lt;h2&gt;Steps to Start Learning Go&lt;/h2&gt;

    &lt;h3&gt;1. Set Up Go on Your Computer&lt;/h3&gt;
    &lt;p&gt;To learn Go, you need to install it on your computer. Follow these steps:&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;Visit the &lt;a href=&quot;https://golang.org/dl/&quot; target=&quot;_blank&quot;&gt;official Go website&lt;/a&gt; to download the Go installer.&lt;/li&gt;
        &lt;li&gt;After downloading, follow the instructions to install Go.&lt;/li&gt;
        &lt;li&gt;To make sure Go is installed, open your terminal or command prompt and type:&lt;/li&gt;
        &lt;pre&gt;&lt;code&gt;go version&lt;/code&gt;&lt;/pre&gt;
        &lt;li&gt;If it shows the version of Go, you&#39;re ready to start coding!&lt;/li&gt;
    &lt;/ul&gt;

    &lt;h3&gt;2. Understand the Basics of Go&lt;/h3&gt;
    &lt;p&gt;Now that you’ve installed Go, it’s time to understand the basics. Here are some key concepts you should start with:&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;strong&gt;Variables:&lt;/strong&gt; Learn how to store and use data in Go.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Functions:&lt;/strong&gt; Go uses functions to organize code into reusable blocks.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Control structures:&lt;/strong&gt; Learn how to use &lt;code&gt;if-else&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt; loops, and &lt;code&gt;switch&lt;/code&gt; statements.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;Go’s syntax:&lt;/strong&gt; Go uses simple syntax, so it’s easier for beginners to read and write.&lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;You can find a good introduction to Go&#39;s syntax in the &lt;a href=&quot;https://tour.golang.org/&quot; target=&quot;_blank&quot;&gt;Go Tour&lt;/a&gt;, an interactive tutorial from the Go team.&lt;/p&gt;

    &lt;h3&gt;3. Practice by Writing Small Programs&lt;/h3&gt;
    &lt;p&gt;One of the best ways to learn any programming language is by writing code. Start with simple programs, like:&lt;/p&gt;
    &lt;h4&gt;Printing “Hello, World!” in Go:&lt;/h4&gt;
    &lt;pre class=&quot;prettyprint&quot; style=&quot;white-space: pre !important&quot;&gt;
package main

import &quot;fmt&quot;

func main() {
    fmt.Println(&quot;Hello, World!&quot;)
}&lt;/pre&gt;

    &lt;h4&gt;Writing a program that adds two numbers:&lt;/h4&gt;
    &lt;pre class=&quot;prettyprint&quot; style=&quot;white-space: pre !important&quot;&gt;
package main

import &quot;fmt&quot;

func main() {
    var a int = 5
    var b int = 10
    fmt.Println(&quot;Sum:&quot;, a+b)
}&lt;/pre&gt;

    &lt;p&gt;Keep practicing with small projects to build confidence. For more practice, visit &lt;a href=&quot;https://gobyexample.com/&quot; target=&quot;_blank&quot;&gt;Go by Example&lt;/a&gt;, which has examples and explanations for many Go programs.&lt;/p&gt;

    &lt;h3&gt;4. Explore Go’s Standard Library&lt;/h3&gt;
    &lt;p&gt;Go has a rich &lt;strong&gt;standard library&lt;/strong&gt; that makes it easy to handle tasks like working with files, networking, and more. You can use Go’s documentation to explore different libraries and learn how to use them. Check out the &lt;a href=&quot;https://pkg.go.dev/std&quot; target=&quot;_blank&quot;&gt;Go Documentation&lt;/a&gt; to see all the built-in packages.&lt;/p&gt;

    &lt;h3&gt;5. Work on Real-World Projects&lt;/h3&gt;
    &lt;p&gt;Once you’ve got a grip on the basics, you can move on to building larger projects. Try to build simple applications like:&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;A to-do list app&lt;/li&gt;
        &lt;li&gt;A basic web server&lt;/li&gt;
        &lt;li&gt;A command-line tool&lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;These projects will help you understand Go&#39;s power and flexibility. You can also contribute to open-source Go projects on GitHub.&lt;/p&gt;

    &lt;h3&gt;6. Join Go Communities&lt;/h3&gt;
    &lt;p&gt;Learning from others can speed up your progress. Join Go communities where you can ask questions, share knowledge, and learn from other developers. Here are some good places to start:&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;a href=&quot;https://forum.golangbridge.org/&quot; target=&quot;_blank&quot;&gt;Go Forum&lt;/a&gt;: A great place to ask questions and interact with other Go learners.&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;https://www.reddit.com/r/golang/&quot; target=&quot;_blank&quot;&gt;Reddit Go Community&lt;/a&gt;: Useful discussions, tips, and news.&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;https://gophers.slack.com/&quot; target=&quot;_blank&quot;&gt;Go Slack Channel&lt;/a&gt;: Join the Gophers Slack and connect with Go developers.&lt;/li&gt;
    &lt;/ul&gt;

    &lt;h2&gt;Helpful Resources&lt;/h2&gt;
    &lt;p&gt;Here are some additional learning resources:&lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;a href=&quot;https://golang.org/doc/&quot; target=&quot;_blank&quot;&gt;Go Official Documentation&lt;/a&gt; – Complete documentation from the Go team.&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;https://tour.golang.org/&quot; target=&quot;_blank&quot;&gt;A Tour of Go&lt;/a&gt; – An interactive tutorial for beginners.&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;https://gobyexample.com/&quot; target=&quot;_blank&quot;&gt;Go by Example&lt;/a&gt; – Simple examples for learning Go’s syntax.&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;https://exercism.org/tracks/go&quot; target=&quot;_blank&quot;&gt;Exercism.io Go Track&lt;/a&gt; – Solve exercises and get feedback from mentors.&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;https://pkg.go.dev/std&quot; target=&quot;_blank&quot;&gt;Go’s Standard Library&lt;/a&gt; – Explore the built-in libraries in Go.&lt;/li&gt;
    &lt;/ul&gt;

    &lt;h2&gt;Final Thoughts&lt;/h2&gt;
    &lt;p&gt;Learning Go can be a fun and rewarding experience. With its simple syntax and powerful tools, Go is perfect for beginners and experienced programmers alike. Start slow, practice writing code, and use the resources mentioned above to build your skills. As you grow more comfortable with Go, try solving real-world problems and joining the Go community.&lt;/p&gt;

    &lt;p&gt;&lt;strong&gt;Happy coding!&lt;/strong&gt;&lt;/p&gt;</description><link>http://fetch-info.blogspot.com/2024/09/how-to-start-learning-go-language.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDyhwv7zT-8d6TGN34ZtX97dXsz98go2PGLJExe7udNAgI5MUJ3u9qsEC7axtCm6mBgsX2VUuctpGiyEVTot7ekJmluwyV4TEvgJ2dbNlyseIi_i0B9eiuSQBjuyyDPzhHX7S8FDJ1BM5Kv4zZhyphenhyphen4bLchfv-gS3Vk2tIXLTusxMw07afmTUZDyghox-e5h/s72-w640-h282-c/360-3600447_go-language-logo-png-transparent-png.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-2501788318962074468</guid><pubDate>Thu, 10 Jan 2019 14:59:00 +0000</pubDate><atom:updated>2019-01-10T06:59:07.675-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Computer</category><category domain="http://www.blogger.com/atom/ns#">laptop</category><category domain="http://www.blogger.com/atom/ns#">Latest</category><category domain="http://www.blogger.com/atom/ns#">ssd</category><title>The new generation storage: SSD</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgr_FkzOvHey3yKuFqATJI202GwKzZi690yi0KtgkrZa5RorO8GMnUFPpaNS99HXO5NSk3IYqKFzLlK63ZEFdCJqa6qpSu_cF9EQJv90FI13hCnHOAlMfummWkW2GxhI7WS4kGnFPYa-gn2/s1600/ssd.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgr_FkzOvHey3yKuFqATJI202GwKzZi690yi0KtgkrZa5RorO8GMnUFPpaNS99HXO5NSk3IYqKFzLlK63ZEFdCJqa6qpSu_cF9EQJv90FI13hCnHOAlMfummWkW2GxhI7WS4kGnFPYa-gn2/s320/ssd.jpg&quot; width=&quot;320&quot; height=&quot;222&quot; data-original-width=&quot;550&quot; data-original-height=&quot;382&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;
This time we&#39;ll be learning about something which has already been in the market for a long time but still many of us doesn&#39;t understand its importance or how it is significantly better than its counterparts. That new thing is Solid State Drive (SSD). SSD is just another storage media like your pendrive and harddisk but it is way faster than either of them. In this post, we will learn what really makes an SSD faster than other storage media and how it can change our life forever.
&lt;/p&gt;
&lt;div class=&quot;index&quot;&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;a href=&quot;#what-is-ssd&quot;&gt;What really is an SSD ?&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#how-ssd-is-better&quot;&gt;What makes SSD better ?&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#laptop-and-ssd&quot;&gt;Role of SSD in laptops&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&quot;what-is-ssd&quot;&gt;What really is an SSD ?&lt;/h2&gt;
&lt;p&gt;
SSD is a flash device just like pendrives, micro sd cards and embedded storage of our mobile phones. A flash device is basically made of transistors and semiconductors as compared to harddisk which has magnetic disk and moving parts. I may introduce some heavy terms here which may not make sense to you much but keep reading. So like i was saying, flash devices have billions of transistors which are used to store data. Multiple transistors are joined to form a kind of a cell. A cell either can be NAND cell or NOR cell based on the configuration. One cell can hold one of 2 states (there are cells which can hold one of many states but lets stick to basic cell) either one or zero. As we all know (alright, some of us know), that everything is binary when it comes to computers, a combination of cell can store any value. All you have to do is to turn some cells ON and turn some cells OFF to store your particular value. This is achieved by passing a current to those cells and thus changing their states.
&lt;/p&gt;


&lt;h2 id=&quot;how-ssd-is-better&quot;&gt;What makes SSD better ?&lt;/h2&gt;
&lt;p&gt;
As you know now that storing a value in a cell is just a matter of passing the current, one may wonder how much time does it take. Well, the answer is 0 seconds, 0 milliseconds, 0 microseconds. Yes, that happens in nanoseconds. That said, now lets talk about harddisk. Harddisk is a giant blunder of magnetic disks and moving arms. It served the purpose of storing data to a good time until people realized that computers can perform better if all the data required by applications is already in RAM because fetching it from harddisk is a slow process and not even the fastest processor can do any good with it. So the bottom line is, storage media is the bottleneck for high performance. The way harddisks operate is itself so optimized that there is no way to get something better out of it. If you ever open a harddisk (or see the diagram below), you will notice that it has one pin which needs to go to the location where the requested data is stored. This involves movement of the pin and is a microseconds job.
&lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuP7rP0FEO589iuBiUy7aohLgFq_4Mv5ko2PDFoEiJPoc5-g3uPzpykDpwplXaZXigj5peshhuJ_mM1uD368czAID-_qIv105v62wi5_HMD-OPmVJK3MAun3EFlZZqitwuaa6w2bHbZZqe/s1600/harddisk.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuP7rP0FEO589iuBiUy7aohLgFq_4Mv5ko2PDFoEiJPoc5-g3uPzpykDpwplXaZXigj5peshhuJ_mM1uD368czAID-_qIv105v62wi5_HMD-OPmVJK3MAun3EFlZZqitwuaa6w2bHbZZqe/s320/harddisk.jpg&quot; width=&quot;320&quot; height=&quot;251&quot; data-original-width=&quot;550&quot; data-original-height=&quot;432&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;
On the other hand, SSD just requires current to be passed to some specific cells which is a nanoseconds job. SSD itself has its own RAM and a simple processor which keep things moving. Thus SSD can serve better and can provide data at a much faster rate than traditional harddisk drives.
&lt;/p&gt;

&lt;h2 id=&quot;laptop-and-ssd&quot;&gt;Role of SSD in laptops&lt;/h2&gt;
&lt;p&gt;
Like i said earlier, SSDs&#39; write and read speed is pretty much higher than harddisks and applications which run on our laptop constantly require memory to write and read stuff. Even when you start an application, it boots up when processor reads the binary from the storage. So SSD can not even make your applications run smoothly but it can also improve applications startup time. I myself has experienced this on my intel-i3 laptop where my OS (ubuntu 18.04) is installed on one of samsung SSD. The boot up time has improved to 2-3 seconds and even android studio runs like a charm without GPU (But that has something to do with 8GB RAM also). So if you go to buy a laptop for your college work then i would suggest you to go for a laptop which has all basic parts like good enough processor(i5), may be GPU card, decent screen, which can come at a decent price of 40k. Just make sure it has additional RAM slot. Then spend additional 12k to buy more RAM and one 256GB internal SSD. Go to some laptop repair shop and ask him to replace your HDD with your brand new SSD and your CD player with the HDD he just removed. That way you can install your applications and OS (like windows 10) on your SSD and keep your other stuff like movies on HDD. Also ask the repairman to install that extra RAM to the additional RAM slow in your laptop. Just make sure you buy the RAM of the same frequency as the already installed one. At the end, you will have a powerful laptop in under 55k. Companies offer laptops with same configuration at a 15-20k higher price which is not at all required.
&lt;/p&gt;

&lt;p&gt;This was it for today&#39;s post fellas. If you need any help selecting the laptop or which SSD to buy, just drop a comment and i will reply faster than SSD serves data :D.&lt;/p&gt;
</description><link>http://fetch-info.blogspot.com/2019/01/the-new-generation-storage-ssd.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgr_FkzOvHey3yKuFqATJI202GwKzZi690yi0KtgkrZa5RorO8GMnUFPpaNS99HXO5NSk3IYqKFzLlK63ZEFdCJqa6qpSu_cF9EQJv90FI13hCnHOAlMfummWkW2GxhI7WS4kGnFPYa-gn2/s72-c/ssd.jpg" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-2063880453051580505</guid><pubDate>Thu, 22 Feb 2018 15:24:00 +0000</pubDate><atom:updated>2018-02-22T07:24:44.573-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">hash</category><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">programming</category><title>Hash Tables : Designing Hash Functions</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxqUnJCRkE74cyyK6ms4tw5YYSVZssbhaJivpKfn6UR-gO3Xk-mdNvFH0dvm3ynJ00D26HMh8PJ-fX3KoenUJhJEc7c-JLSORLqvFv5c1RaVeAAI70EM0yFKticgaB_nl21jpkKrJ8Jgjh/s1600/hash-lock.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxqUnJCRkE74cyyK6ms4tw5YYSVZssbhaJivpKfn6UR-gO3Xk-mdNvFH0dvm3ynJ00D26HMh8PJ-fX3KoenUJhJEc7c-JLSORLqvFv5c1RaVeAAI70EM0yFKticgaB_nl21jpkKrJ8Jgjh/s400/hash-lock.jpg&quot; width=&quot;400&quot; height=&quot;400&quot; data-original-width=&quot;310&quot; data-original-height=&quot;310&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;
    In the &lt;a href=&quot;https://fetch-info.blogspot.in/2018/02/hash-tables-handling-collision.html&quot;&gt;last post&lt;/a&gt;,
    we saw how collision in handled in a hash table. Now its time to explore some
    real world scenarios because come on, what is the use of learning all this if we
    can&#39;t apply it in real world softwares. As we learned that open addressing and
    chaining were both good in different scenarios only if we met a condition that
    the keys are uniformaly distributed. But its not the application job to pass
    distributed keys to hash tables, its our job to map any key to some random location.
    We do this using a hash function (or prehash function, whatever term you prefer).
    A hash function should map the keys as uniform as possible. So lets discuss some
    decent hash functions to some high end hash functions.
&lt;/p&gt;

&lt;div class=&quot;index&quot;&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2018/02/hash-tables-scratching-surface.html#hash-motivation&quot;&gt;What Problems Does Hashing Solve ?&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2018/02/hash-tables-scratching-surface.html#simple-hash&quot;&gt;Simple Implementation of Hash Table&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2018/02/hash-tables-handling-collision.html#collision-resolution&quot;&gt;Collision Resolution&lt;/a&gt;
            &lt;ul&gt;
                &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2018/02/hash-tables-handling-collision.html#chaining&quot;&gt;Chaining&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2018/02/hash-tables-handling-collision.html#open-addressing&quot;&gt;Open Addressing&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#hash-functions&quot;&gt;Desining Hash Functions&lt;/a&gt;
            &lt;ul&gt;
                &lt;li&gt;&lt;a href=&quot;#hash-properties&quot;&gt;Properties of Hash Functions&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#basic-hash-functions&quot;&gt;Basic Hash Functions&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#cryptographic-hash-functions&quot;&gt;Cryptographic Hash Functions&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#python-dictionary&quot;&gt;How Python Dictionaries Work&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#hash-table&quot;&gt;Hashing in Action&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;

&lt;h2 id=&quot;hash-functions&quot;&gt;Desining Hash Functions&lt;/h2&gt;
&lt;p&gt;
    Hash Functions do the job of mapping a key to some location in hash table.
    This is achieved by doing some fixed operations on the key and generating a number
    between 0 to the capacity of table. Hash functions highly determine the overall
    complexity of hash table for insert and search operations because as we saw in
    open addressing if a lot of keys are mapped to same location then it will shorty
    form a clusture there and then inserting a new key or searching an old key will
    not be a &lt;code&gt;O(1)&lt;/code&gt; operation anymore. So we want our hash function to
    have some properties. Lets see what they are.
&lt;/p&gt;
&lt;h3 id=&quot;hash-properties&quot;&gt;Properties of Hash Functions&lt;/h3&gt;
&lt;p&gt;
    Desirable properties of a hash function are:
    &lt;ul&gt;
        &lt;li&gt;
            &lt;b&gt;Uniform Distribution:&lt;/b&gt; Each key should be equally likely hashed
            to any slot of the table independent of where other keys hash. This means
            that hashing of a key should not be related to hash table size or placement
            of other keys.
        &lt;/li&gt;
        &lt;li&gt;
            &lt;b&gt;Time Independent: &lt;/b&gt; Hash values of keys should be changed over time.
            This implies to the fact that if i hash my key now and if i hash the same
            key one year later, the resulting hash values in both the cases should be
            same.
        &lt;/li&gt;
    &lt;/ul&gt;
&lt;/p&gt;
&lt;h3 id=&quot;basic-hash-functions&quot;&gt;Basic Hash Functions&lt;/h3&gt;
&lt;p&gt;
    As we saw what we want in our hash functions, we are ready to write some. We are
    making one assumption that the key is always integer. Later we&#39;ll see how to
    convert any object to an integer atleast in c/c++. Some of the hash functions which
    are used commonly are described below.
    &lt;ul&gt;
        &lt;li&gt;&lt;b&gt;Division Method: &lt;/b&gt; In this method, we take modulo of key with table size.
            This will generate a number between 0 and size of table. This may create a problem
            if keys and size of table have common factors, then only some of table space will
            be utilized.
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1EIzfE2mB0n_n6XR0kFmVRmkBKHRRv-t2Pvk0X2eMkyw9J_SVxWasammG7pme5Oc7yFV0XVAGIDQ44fa35g6e84GHGmYsjR5H15wo6GCCW-HjJePksPu1yKAJ1p4jibmWuIg_DShIWc9o/s1600/division-hashing.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1EIzfE2mB0n_n6XR0kFmVRmkBKHRRv-t2Pvk0X2eMkyw9J_SVxWasammG7pme5Oc7yFV0XVAGIDQ44fa35g6e84GHGmYsjR5H15wo6GCCW-HjJePksPu1yKAJ1p4jibmWuIg_DShIWc9o/s320/division-hashing.png&quot; width=&quot;320&quot; height=&quot;200&quot; data-original-width=&quot;640&quot; data-original-height=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
            &lt;b&gt;Multiplication Method: &lt;/b&gt; Opposite of the division method is multiplication method.
            In this method we multiply the key with some FIXED number and take out a bunch of bits.
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjL6NAvfEcnSKERRcO4vrNuH3DfHTchXDZ2-0whgQnPIRnM2_WyJr_0jJBDEqx79izJNcscqGzXrE72wyFoU4SjZs15hj3e_mWYte9CkxprdMouhyp-EHHXO_GEXcJozDf2-tvsgDNN4-iF/s1600/multiply-hashing.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjL6NAvfEcnSKERRcO4vrNuH3DfHTchXDZ2-0whgQnPIRnM2_WyJr_0jJBDEqx79izJNcscqGzXrE72wyFoU4SjZs15hj3e_mWYte9CkxprdMouhyp-EHHXO_GEXcJozDf2-tvsgDNN4-iF/s320/multiply-hashing.png&quot; width=&quot;320&quot; height=&quot;200&quot; data-original-width=&quot;640&quot; data-original-height=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
            &lt;b&gt;Universal Hashing: &lt;/b&gt; In this method, we compute a value by multiplying the key with
            prime number and then taking the mod with size of table. The worst case possibility of colliding
            two keys is &lt;code&gt;1/(size of table)&lt;/code&gt; which is ideal situation.
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7pLYj4Ctw7EzjIJEAwR4p67Zy_BD7LkExWnFgwqowFKVcn4SRnDPE9Iojm15VAFlTN8UgmfJuxtOne9hoOZQ67pTHHs3olFU03qAGFBpasuyJDkFWFC8IOAIDrmYbY5OgZ-xYJ1hHV6IB/s1600/universal-hashing.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7pLYj4Ctw7EzjIJEAwR4p67Zy_BD7LkExWnFgwqowFKVcn4SRnDPE9Iojm15VAFlTN8UgmfJuxtOne9hoOZQ67pTHHs3olFU03qAGFBpasuyJDkFWFC8IOAIDrmYbY5OgZ-xYJ1hHV6IB/s320/universal-hashing.png&quot; width=&quot;320&quot; height=&quot;200&quot; data-original-width=&quot;640&quot; data-original-height=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
&lt;/p&gt;
&lt;h3 id=&quot;cryptographic-hash-functions&quot;&gt;Cryptographic Hash Functions&lt;/h3&gt;
&lt;p&gt;
    Hash Functions are not only used to compute the location of a key in a table but also
    to convert any given key or should we say any data to a fixed length digest. This
    digest is unique for each key and thus it is made sure that no 2 keys have same
    digest. Such type of hash functions are used in cryptography to encrypt a message
    or a password to transfer it safely from one place to another. Cryptographic hash
    functions must have a property other than the above two and that is they should be
    &lt;b&gt;one way&lt;/b&gt;. This means that it must not be possible to recover the key from
    its digest. There are many widely used hash functions which are either used for
    encryption or just to compute checksum. Two most commonly used and famous hash functions are
    &lt;b&gt;MD&lt;/b&gt; and &lt;b&gt;SHA&lt;/b&gt;. We won&#39;t go in detail how these work because there are libraries to use these hash functions. Lets see what are those.
    &lt;ul&gt;
        &lt;li&gt;
            &lt;b&gt;MD (Message Digest): &lt;/b&gt; The MD family comprises of hash functions MD2, MD4, MD5
            and MD6. It was adopted as Internet Standard RFC 1321. It is a 128-bit hash function.
            MD5 was the most popular hash function for some years until in 2004, collisions were
            found in MD5. Since then it is not recommended to use but still people use it for
            less sophisticated applications where collisions are allowed.
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZYtUumiGMBnKZJDEpnuqtKerNTNtvt4VQMKVooB2of1u95F1sqLUxdbmAXLUhiAVRarUP_oQDGcbzvedqG4gbIRsZJGKqzLtJJNYvYKfvqL-fZoWP_Y6aZhcpmnE_CGTF2TmIXnsZshvu/s1600/MD5.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZYtUumiGMBnKZJDEpnuqtKerNTNtvt4VQMKVooB2of1u95F1sqLUxdbmAXLUhiAVRarUP_oQDGcbzvedqG4gbIRsZJGKqzLtJJNYvYKfvqL-fZoWP_Y6aZhcpmnE_CGTF2TmIXnsZshvu/s320/MD5.png&quot; width=&quot;291&quot; height=&quot;320&quot; data-original-width=&quot;546&quot; data-original-height=&quot;600&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
            &lt;b&gt;SHA (Secure Hash Function): &lt;/b&gt; Family of SHA comprise of four SHA algorithms;
            SHA-0, SHA-1, SHA-2, and SHA-3. SHA-1 is most widely used hash function of this
            family. It is also employed in SSL (Secure Socket Layer).
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgp0qjBzlm836Pug8Td2huOWXRy4-EG1p-F8pkflNw54SatdANPp8FRUg5a_RgJZAyR5HCRfmnzLJe8pNBUlg0Sd5Lu3_gabyVgeeZ7OY_s_saDxr4DvX0e2-Z2sTrR4p8qkMNo3Zav6_ew/s1600/sha1.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgp0qjBzlm836Pug8Td2huOWXRy4-EG1p-F8pkflNw54SatdANPp8FRUg5a_RgJZAyR5HCRfmnzLJe8pNBUlg0Sd5Lu3_gabyVgeeZ7OY_s_saDxr4DvX0e2-Z2sTrR4p8qkMNo3Zav6_ew/s320/sha1.png&quot; width=&quot;307&quot; height=&quot;320&quot; data-original-width=&quot;1200&quot; data-original-height=&quot;1249&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
&lt;/p&gt;
&lt;p&gt;
    This was it for this post. We&#39;ll see in the final post of this series how python
    manages it dictionaries and lists. Also share this article to your nerd friends
    to make them aware of where the world is heading. Keep Hashing
&lt;/p&gt;
</description><link>http://fetch-info.blogspot.com/2018/02/hash-tables-designing-hash-functions.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxqUnJCRkE74cyyK6ms4tw5YYSVZssbhaJivpKfn6UR-gO3Xk-mdNvFH0dvm3ynJ00D26HMh8PJ-fX3KoenUJhJEc7c-JLSORLqvFv5c1RaVeAAI70EM0yFKticgaB_nl21jpkKrJ8Jgjh/s72-c/hash-lock.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-5215109163242518854</guid><pubDate>Sun, 11 Feb 2018 06:11:00 +0000</pubDate><atom:updated>2018-02-22T07:26:04.201-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">hash</category><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Hash Tables : Handling Collision</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioFpvoO32ZDrEG353VXc9gh6c8iKHjzaD9urrOnJTIznfLEqvTkzPJPVH5EAQoHbf6s-VkF4nczsleVACzz51-DZwQC9xkL_SeOnM_fIEcLKPI3ty9ZZuefBrHn8uciM1Wo-SA_f02EV-6/s1600/collision.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioFpvoO32ZDrEG353VXc9gh6c8iKHjzaD9urrOnJTIznfLEqvTkzPJPVH5EAQoHbf6s-VkF4nczsleVACzz51-DZwQC9xkL_SeOnM_fIEcLKPI3ty9ZZuefBrHn8uciM1Wo-SA_f02EV-6/s400/collision.jpg&quot; width=&quot;400&quot; height=&quot;300&quot; data-original-width=&quot;640&quot; data-original-height=&quot;480&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;
We saw in the &lt;a href=&quot;https://fetch-info.blogspot.in/2018/02/hash-tables-scratching-surface.html&quot; target=&quot;_blank&quot;&gt;previous
post&lt;/a&gt; how a simple hash table can be constructed without much effort. But, and that is a big But,
that table was prone to collisions and could result in corrupted data. Of course we don&#39;t want
a data structure which can corrupt our data even if it is faster. So to avoid data corruption
we will study some collision resolution techniques and will see how to evenly distribute
a key over the table to minimize collision. Lets continue the journey.
&lt;/p&gt;
&lt;div class=&quot;index&quot;&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2018/02/hash-tables-scratching-surface.html#hash-motivation&quot;&gt;What Problems Does Hashing Solve ?&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2018/02/hash-tables-scratching-surface.html#simple-hash&quot;&gt;Simple Implementation of Hash Table&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#collision-resolution&quot;&gt;Collision Resolution&lt;/a&gt;
            &lt;ul&gt;
                &lt;li&gt;&lt;a href=&quot;#chaining&quot;&gt;Chaining&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#open-addressing&quot;&gt;Open Addressing&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2018/02/hash-tables-designing-hash-functions.html#basic-hash-functions#hash-functions&quot;&gt;Designing Hash Functions&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#python-dictionary&quot;&gt;How Python Dictionaries Work&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#hash-table&quot;&gt;Hashing in Action&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&quot;collision-resolution&quot;&gt;Collision Resolution&lt;/h2&gt;
&lt;p&gt;
    First of all, what is collision ? &lt;b&gt;Collision&lt;/b&gt; is a phenomenon which
    happens when 2 or more distinct keys are mapped to the same hash table entry.
    But we don&#39;t want that so using some technique, we relocate that key to some
    other empty location. The technique used to do that is called &lt;b&gt;collision
    resolution technique.&lt;/b&gt; There are 2 types of techniques in use. One is called
    &lt;b&gt;Chaining&lt;/b&gt; and Other is &lt;b&gt;Open Addressing&lt;/b&gt;. Open Addressing has
    further more types but the concept is basically the same. Lets start with
    chaining first and see how it can be useful.
&lt;/p&gt;
&lt;h3 id=&quot;chaining&quot;&gt;Chaining&lt;/h3&gt;
&lt;p&gt;As the name suggests, In chaining technique , we create a list of colliding keys
for every hash entry location. So our hash table is basically an array of pointers
which are pointing to the corresponding list. Whenever 2 distinct keys are mapped
to same location, we append them to that location&#39;s list. The below picture will
make it more clear.
&lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjluK844FxciIBXCU1LB3SAy41MGcI8K3i6u_1fXQG6Aj1l7UQUQyy2fdjEzxqNkyEQB0IzkGJ0pk4l2dK8J6AGall4iF3lRWh_m1McuXmJ0TqJBqjrvr885ab3twYLkKYLHwIOcZlNges6/s1600/hash_chain.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjluK844FxciIBXCU1LB3SAy41MGcI8K3i6u_1fXQG6Aj1l7UQUQyy2fdjEzxqNkyEQB0IzkGJ0pk4l2dK8J6AGall4iF3lRWh_m1McuXmJ0TqJBqjrvr885ab3twYLkKYLHwIOcZlNges6/s400/hash_chain.png&quot; width=&quot;400&quot; height=&quot;201&quot; data-original-width=&quot;721&quot; data-original-height=&quot;362&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Now the question comes about searching for a key in chained table. As you
can see in the above picture, we cannot find a key in one go. We&#39;ll first have
to find the entry location in hash table and then will have to traverse the whole
list associated with that location. Each list node has the original key and thus
we can compare our key with node&#39;s key to find the match.
&lt;/p&gt;

&lt;p&gt;Chaining may seem to solve the problem of collision and in reality it does
but there is an overhead of memory. Each new list node must have a pointer to
the next node. Also you may have observed in the above picture that a bad
hashing technique may try to place all the keys in one location, thus making search
a linear operation. Thus we can get amortized O(1) time for get, set and search
operations if our hashing technique evenly distributes the keys&lt;/p&gt;

&lt;h3 id=&quot;open-addressing&quot;&gt;Open Addressing&lt;/h3&gt;
&lt;p&gt;
    After seeing how the chaining works, its time to explore another collision
    resolution technique which utilizes the hash table to its full potential.
    As we saw in chaining, a lot of keys can be placed in one location and then
    many other locations remain empty and not fully utilized. Open addressing
    solves this problem. There are many types of open addressing techniques but
    in this post we will look at &lt;b&gt;Linear Open Addressing&lt;/b&gt;
&lt;/p&gt;
&lt;p&gt;
    In linear open addressing, we first find a key location inside hash table where
    it should sit using hash function. Then we check whether that location is empty or not. If its
    empty, place the key there otherwise, find the next empty location linearly
    in circular motion, i.e., go to the start to the table if there is no empty
    location till the end of the table. After finding an empty entry, place the
    key there. The following picture will help you understand this better.
&lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9X5s9P91wLqeWZaKHEAHQejUaatLe-KDEGa3fpXuHdes1rBT_B05Ay8pWel2z-5YGhGzltUjBn316QhhgwoMfPYmzxkW_MsO7ITOr3xDWKFLThV_fknVKHf7sNFmUR9an7YrkGpGW6nH2/s1600/linear-probing.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9X5s9P91wLqeWZaKHEAHQejUaatLe-KDEGa3fpXuHdes1rBT_B05Ay8pWel2z-5YGhGzltUjBn316QhhgwoMfPYmzxkW_MsO7ITOr3xDWKFLThV_fknVKHf7sNFmUR9an7YrkGpGW6nH2/s400/linear-probing.png&quot; width=&quot;400&quot; height=&quot;215&quot; data-original-width=&quot;378&quot; data-original-height=&quot;203&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;
    Now lets to come to search. Search operation is somewhat similar to insert
    operation. When we want to search a key in table, we first find its location
    using hash function. After that, we linearly go through all the keys and match
    with our key until we find it. If we encounter an empty entry during our search
    or if we are back to where we started, it implies that the key is not present.
    As you can imagine, linear probing may result in clustures if our hash
    function is not uniformly distributive enough. The other type of open
    addressing which tries to avoid clustures is &lt;b&gt;Quadratic Probing&lt;/b&gt;. In
    this we don&#39;t go in linear fashion instead we go in steps of power of 2.
    Like we first go 1 step to find next empty bucket, 2 steps if not found, 4 steps
    if no empty location found, 8 steps and so on.
&lt;/p&gt;

&lt;p&gt;
    As we saw in this post Open addressing solves memory problem but chaining is
    faster for high load factors [&lt;b&gt;load factor = (total number of keys)/(number of entries in table)&lt;/b&gt;].
    In real world cases, people prefer open addressing as Open addressing
    is usually faster than chained hashing when the load factor
    is low because you don&#39;t have to follow pointers between list nodes. If the
    load factor approaches 1, open addressing becomes very very slower. But usually,
    the table size is kept double the number of total keys in the table and thus
    open addressing achieves good performance. This was it for this post, in the
    next post, we&#39;ll see how python manages its dictionaries and how we ensure
    that table size is always double the number of entries. Stay tuned. Also share
    this article to your friends if this helped you in any way.
&lt;/p&gt;
</description><link>http://fetch-info.blogspot.com/2018/02/hash-tables-handling-collision.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioFpvoO32ZDrEG353VXc9gh6c8iKHjzaD9urrOnJTIznfLEqvTkzPJPVH5EAQoHbf6s-VkF4nczsleVACzz51-DZwQC9xkL_SeOnM_fIEcLKPI3ty9ZZuefBrHn8uciM1Wo-SA_f02EV-6/s72-c/collision.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-803881397608922918</guid><pubDate>Sat, 03 Feb 2018 11:58:00 +0000</pubDate><atom:updated>2018-02-22T07:26:20.335-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">cpp</category><category domain="http://www.blogger.com/atom/ns#">hash</category><category domain="http://www.blogger.com/atom/ns#">learn</category><title>Hash Tables : Scratching the Surface</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5iO0ye50B8vj9JqWnM2gTPZ2WlY89hheN7XFxSQOm_QIBjQzl1-oUti-IrpdGfauZk6tmHbZNb2PWIACXdVvFaqzBSOOrlse9XhQIxK3X6n9lcmabzS55G8s2poq42kRP2D4UAai9b4Dy/s1600/hash.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5iO0ye50B8vj9JqWnM2gTPZ2WlY89hheN7XFxSQOm_QIBjQzl1-oUti-IrpdGfauZk6tmHbZNb2PWIACXdVvFaqzBSOOrlse9XhQIxK3X6n9lcmabzS55G8s2poq42kRP2D4UAai9b4Dy/s400/hash.jpg&quot; width=&quot;400&quot; height=&quot;160&quot; data-original-width=&quot;1200&quot; data-original-height=&quot;480&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;
    Have you ever worked with key-value data stores where you can attach one
    value to a key and store it in some form to perform easy lookups ? If yes, then
    i guess you have already seen hash tables in action. Hash Tables are so common
    these days that you don&#39;t even realise when you start using them but not a
    lot of people are familiar with the internals of hash tables and how these
    can be used in real world problems like cryptography. This a series of articles
    all about hashing. So sit tight and enjoy the journey.
&lt;/p&gt;
&lt;div class=&quot;index&quot;&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;a href=&quot;#hash-motivation&quot;&gt;What Problems Does Hashing Solve ?&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#simple-hash&quot;&gt;Simple Implementation of Hash Table&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2018/02/hash-tables-handling-collision.html#collision-resolution&quot;&gt;Collision Resolution&lt;/a&gt;
            &lt;ul&gt;
                &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2018/02/hash-tables-handling-collision.html#chaining&quot;&gt;Chaining&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2018/02/hash-tables-handling-collision.html#open-addressing&quot;&gt;Open Addressing&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2018/02/hash-tables-designing-hash-functions.html#basic-hash-functions#hash-functions&quot;&gt;Desining Hash Functions&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#python-dictionary&quot;&gt;How Python Dictionaries Work&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;&quot;&gt;Hashing in Action&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&quot;hash-motivation&quot;&gt;What Problems Does Hashing Solve ?&lt;/h2&gt;
&lt;p&gt;
    &lt;i&gt;Hash Tables are the most popular data structure in computer science.&lt;/i&gt;
    We forget to worry about time complexities of finding our data in hash table
    as its basically constant. But it doesn&#39;t show how hashing solves real world
    problems. Following are some problems which are being solved by hashing only.
    &lt;ul&gt;
        &lt;li&gt;
            &lt;b&gt;Computer Cache&lt;/b&gt; is internally a hash table with keys being the
            tag bits (refer to &lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/wondering-cache.html&quot;
            target=&quot;_blank&quot;&gt;cache post&lt;/a&gt;)
        &lt;/li&gt;
        &lt;li&gt;
            &lt;b&gt;Python Dictionaries&lt;/b&gt; are implemented using hashing scheme only.
        &lt;/li&gt;
        &lt;li&gt;
            &lt;b&gt;Associative Arrays&lt;/b&gt; in any programming language such as php are
            maintained using hashing.
        &lt;/li&gt;
        &lt;li&gt;
            &lt;b&gt;Cryptographic Hashing&lt;/b&gt; uses hash functions to digest a variable
            length message to a fixed length string.
        &lt;/li&gt;
    &lt;/ul&gt;
    Apart from the above scenarios, you can use hash tables anywhere you may find
    appropriate. As hash tables store data in unordered fashion, we can&#39;t use it
    in sorting applications.
&lt;/p&gt;
&lt;h2 id=&quot;simple-hash&quot;&gt;Simple Implementation of Hash Table&lt;/h2&gt;
&lt;p&gt;
    We can consider a hash table as an array where each array entry holds the
    original key and value pair. If the key itself is a value then there is no
    need to store value separately. For any hash table to work, you need some way
    to map keys to array indices. This mapping is done by a hash function which
    can be anything you want it to be.
    Desirable properties of a hash function are:
    &lt;ul&gt;
        &lt;li&gt;
            Uniform Distribution of keys over hash table. We&#39;ll see shortly why
            is that needed.
        &lt;/li&gt;
        &lt;li&gt;
            Mapping of one key to one hash table entry must not change over time.
        &lt;/li&gt;
    &lt;/ul&gt;
    Considering the above properties in mind, we can build our hash function using
    many simple arithmetic techniques. One of them is &lt;code&gt;modulo&lt;/code&gt; operator.
    Assume we are managing integer keys and there is a value associated to each
    key. Thus we need our hash table to perform constant time lookup of integers.
    This can be achieved by using an integer array. Our &lt;b&gt;hash function&lt;/b&gt; which
    maps a key to one hash entry location performs the modulo operation on the key
    and returns the index in the array where the key should be placed. Following
    is the implementation of such a hash table in c++.
    &lt;pre class=&quot;prettyprint&quot;&gt;
#include &amp;lt;iostream&amp;gt;
class HashTable{
public:
    static const int CAPACITY = 10;
private:
    int* entries;
    bool* valid_map;
    int size;
public:
    Table(){
        entries = new int[CAPACITY];
        valid_map = new bool[CAPACITY];
        size = 0;
    }
    void insert(int key, value){
        int index = hash(key);
        entries[index] = value;
        valid_map[index] = true;
        size++;
    }
    void delete(int key){
        int index = hash(key);
        valid_map[index] = false;
        size--;
    }
    bool exists(int key){
        int index = hash(key);
        return valid_map[index];
    }
    int find(int key){
        int index = hash(key);
        if(valid_map[index] == true){
            return entries[index];
        }
        return -1;
    }
private:
    int hash(int key){
        return key%CAPACITY;
    }
};
    &lt;/pre&gt;
    As you see above, our hash table is capable of storing 10 keys. But there is
    one major problem with the above approach. There is no collision handling.
    If two keys map to same location then second key will overwrite the first one.
    We&#39;ll see how can we tackle Collisions in the next post and will look at how
    python handles hashing of objects.
&lt;/p&gt;
</description><link>http://fetch-info.blogspot.com/2018/02/hash-tables-scratching-surface.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5iO0ye50B8vj9JqWnM2gTPZ2WlY89hheN7XFxSQOm_QIBjQzl1-oUti-IrpdGfauZk6tmHbZNb2PWIACXdVvFaqzBSOOrlse9XhQIxK3X6n9lcmabzS55G8s2poq42kRP2D4UAai9b4Dy/s72-c/hash.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-4895097467400314602</guid><pubDate>Mon, 27 Mar 2017 19:05:00 +0000</pubDate><atom:updated>2017-03-27T13:08:10.882-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">cache</category><category domain="http://www.blogger.com/atom/ns#">Computer</category><category domain="http://www.blogger.com/atom/ns#">learn</category><title>Cache : Replacement, Mapping and Writing</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://cdn.wp.nginx.com/wp-content/uploads/2015/07/cache-stock-photo.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://cdn.wp.nginx.com/wp-content/uploads/2015/07/cache-stock-photo.jpg&quot; width=&quot;320&quot; height=&quot;273&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;
 In the &lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/wondering-cache.html&quot;&gt;previous article&lt;/a&gt;, we discussed about cache and its properties. We also learnt some concepts and terminology related to cache. If you haven&#39;t read that article, i will recommend you to read that article first. This article contains many terms which i&#39;ve discussed in that article. In this article, we will learn cache replacement strategies, cache mapping techniques and cache writing techniques. Below is the index of this complete cache tutorial.
&lt;/p&gt;
&lt;div class=&quot;index&quot;&gt;
 &lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/wondering-cache.html#cache-intro&quot;&gt;Cache Introduction&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/wondering-cache.html#cache-organisation&quot;&gt;Cache Organisation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/wondering-cache.html#memory-terms&quot;&gt;Understanding a few Concepts&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cache-replacement&quot;&gt;Cache Replacement Strategies&lt;/a&gt;
   &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;#random&quot;&gt;Random Replacement&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#lru&quot;&gt;Least Recently Used (LRU) Replacement&lt;/a&gt;&lt;/li&gt;
   &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cache-mapping&quot;&gt;Cache Mapping Techniques&lt;/a&gt;
   &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;#direct-map&quot;&gt;Direct Mapped Cache&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#set-associative&quot;&gt;Set Associative Mapped Cache&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#fully-associative&quot;&gt;Fully Associative Mapped Cache&lt;/a&gt;&lt;/li&gt;
   &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cache-writing&quot;&gt;The Tedious Task of Cache Writing&lt;/a&gt;
   &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;#cache-write-through&quot;&gt;Write Through&lt;/a&gt;
     &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#cache-write-around&quot;&gt;No Allocate (write around)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#cache-write-allocate&quot;&gt;Allocate&lt;/a&gt;&lt;/li&gt;
     &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#cache-write-back&quot;&gt;Write Back&lt;/a&gt;&lt;/li&gt;
   &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cache-simulator&quot;&gt;Basic Cache Simulator&lt;/a&gt;&lt;/li&gt;
 &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&quot;cache-replacement&quot;&gt;Cache Replacement Strategies&lt;/h2&gt;
&lt;p&gt;In the previous article, we looked at the process of searching data for an address in the cache but there was 1 step missing in that process. We didn&#39;t look at the scenario of cache miss. In case of a cache miss, we look at other invalid entry on that index page (note down that we are talking about an index page not the whole index table, recall the terminology from previous article if you want). We add the missing entry in cache index from main memory. But when there is no invalid entry in our cache index then we will have to replace some existing entry to make room for our new entry. Replacing an existing entry is a frequent task and thus plays an important role in determining cache efficiency. There are majorly 2 techniques for finding the entry which is to be replaced by the new one.&lt;/p&gt;
&lt;h3 id=&quot;random&quot;&gt;Random Replacement&lt;/h3&gt;
&lt;p&gt;
 Using this strategy, we find any random entry on that index page and replace it with the new one. This strategy has the advantage of being easy to implement but it may also increase cache misses if we are not lucky enough. A better random algorithm can ensure some improvement but still its random. 
&lt;/p&gt;
&lt;h3 id=&quot;lru&quot;&gt;Least Recently Used (LRU) Replacement&lt;/h3&gt;
&lt;p&gt;
 This is the most widely adopted technique and is somewhat more intuitive. In this strategy, we select the entry which has not been used for a while on the selected page. This means that the oldest used entry is replaced by the new one. This is intuitive because there is no worth of keeping an entry in the index which is lying stale. This strategy ensures better cache performance but it is also complex to implement mainly in hardware. But the algorithm used in strategy can be adopted in many different problem domains such as recommended product listing.
&lt;/p&gt;
&lt;h2 id=&quot;cache-mapping&quot;&gt;Cache Mapping Techniques&lt;/h2&gt;
&lt;p&gt;
 There are total 3 mapping techniques all of which have their own advantages and their disadvantages but all of them have many things in common like tag bits, line bits, word bits, byte select bits etc. The only thing which differs in these techniques is cache entries per index page. This also determines the total number of index pages or total number of cache lines. Correct Cache line is determined by line bits. Below is a list of formulas which you can derive easily if you understood the &lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/wondering-cache.html&quot;&gt;previous article&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
 Formula for total number of cache lines (index pages) :&lt;br/&gt; 
 &lt;code&gt;(cache_index_size)/(number_of_entries_per_page)&lt;/code&gt;&lt;br/&gt;&lt;br/&gt;
 Formula for total number of byte select bits :&lt;br/&gt;
 &lt;code&gt;log&lt;sub&gt;2&lt;/sub&gt;(word_size)&lt;/code&gt;&lt;br/&gt;&lt;br/&gt;
 Formula for total number of word bits :&lt;br/&gt;
 &lt;code&gt;log&lt;sub&gt;2&lt;/sub&gt;(number_of_words_per_block)&lt;/code&gt;&lt;br/&gt;&lt;br/&gt;
 Formula for total number of line bits :&lt;br/&gt;
 &lt;code&gt;log&lt;sub&gt;2&lt;/sub&gt;(number_of_cache_lines)&lt;/code&gt;&lt;br/&gt;&lt;br/&gt;
 Formula for total number of tag bits :&lt;br/&gt;
 &lt;code&gt;total_address_bits - number_line_bits - word_bits - byte_select_bits&lt;/code&gt;
&lt;/p&gt;
&lt;script async src=&quot;//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
&lt;!-- postbody --&gt;
&lt;ins class=&quot;adsbygoogle&quot;
     style=&quot;display:block&quot;
     data-ad-client=&quot;ca-pub-6499786486961559&quot;
     data-ad-slot=&quot;8358795822&quot;
     data-ad-format=&quot;auto&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h3 id=&quot;direct-map&quot;&gt;Direct Mapped Cache&lt;/h3&gt;
&lt;p&gt;
 In this technique, there is only 1 index entry per index page. This implies that we won&#39;t have to perform a linear search for our tag bits. Once we find the correct page , we examine its tag bits to check if this is the address we are looking for.
&lt;/p&gt;
&lt;h3 id=&quot;set-associative&quot;&gt;Set Associative Mapped Cache&lt;/h3&gt;
&lt;p&gt;
 In this technique, we explicitly define the number of entries in one index page. For eg, in a 16 item cache index, if we set number of entries per page = 2 then there will be total 8 index pages (cache lines = 16/2). Thus we will have 3 line bits. Once we find the correct page, there will be 2 entries. We will have to check both of them to determine whether we have a cache hit or a cache miss. If we denote the number of entries per line by &lt;b&gt;N&lt;/b&gt; then the technique is said to be &lt;b&gt;N-way set associative mapping technique&lt;/b&gt;. Our previous example has a &lt;b&gt;2-way set associative mapped cache.&lt;/b&gt;
&lt;/p&gt;
&lt;h4 id=&quot;fully-associative&quot;&gt;Fully Associative Mapped Cache&lt;/h4&gt;
&lt;p&gt;
 In this technique, there is only one index page which contains all the index entries. Thus we don&#39;t require line bits for this technique. This technique make most use of unoccupied entries but it also introduce inefficiency due to search needed to match each entry against given address.
&lt;/p&gt;

&lt;h2 id=&quot;cache-writing&quot;&gt;The Tedious Task of Cache Writing&lt;/h2&gt;
&lt;p&gt;
 Cache writing is the process of updating cache data after computations. Cache writing is somewhat a complex task as compared to reading because we need to update main memory as well to avoid data inconsistency.
&lt;/p&gt;
&lt;h3 id=&quot;cache-write-through&quot;&gt;Write Through&lt;/h3&gt;
&lt;p&gt;This is the technique in which we update the main memory along with cache memory on each update. This technique keeps the main memory always in sync with cache. So basically whenever we want to update some address data, we search it in the cache, update it in cache if there is a cache hit and update it in main memory too. But there are 2 variants of this technique based on the action when there is a cache miss.&lt;/p&gt;
&lt;ul&gt;
 &lt;li id=&quot;cache-write-around&quot;&gt;&lt;b&gt;No Allocate (write around)&lt;/b&gt; In case of cache miss, we simply update the main memory without writing anything to cache.&lt;/li&gt;
 &lt;li id=&quot;cache-write-allocate&quot;&gt;&lt;b&gt;Allocate&lt;/b&gt; In case of cache miss, we store index entry and data in cache and update the main memory as well.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;cache-write-back&quot;&gt;Write Back&lt;/h3&gt;
&lt;p&gt;In this technique, main memory is not updated until the cache entry needs to be replaced. To improve the efficiency further, we maintain a dirty bit associated with each cache index entry. This dirty bit represents whether the cache data has been modified or not. In case when the cache entry needs to be replaced, we update the main memory only if this dirty bit is set to 1 otherwise we just replace the entry.&lt;/p&gt;

&lt;p&gt;This was all about cache and its related concepts. We successfully summed up all this in 2 articles. This post is more theory and less pictures so i won&#39;t blame you if you found it too boring to read, but it also clears all the basic fundamentals about cache and will make you ready to implement your own cache simulator. If you think this article helped you in some way then do share it. Also if you have any doubts, feel free to comment it below.&lt;/p&gt;</description><link>http://fetch-info.blogspot.com/2017/03/cache-replacement-mapping-and-writing.html</link><author>noreply@blogger.com (Kapil Garg)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-7987351616671592772</guid><pubDate>Fri, 17 Mar 2017 12:21:00 +0000</pubDate><atom:updated>2017-03-27T12:07:46.965-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">architecture</category><category domain="http://www.blogger.com/atom/ns#">cache</category><category domain="http://www.blogger.com/atom/ns#">Computer</category><title>Wondering Cache</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a data-flickr-embed=&quot;true&quot;  href=&quot;https://www.flickr.com/photos/kapilgarg/33331320202/in/dateposted-public/&quot; title=&quot;cache-indexing&quot;&gt;&lt;img src=&quot;https://c1.staticflickr.com/4/3902/33331320202_33f25c317a_z.jpg&quot; width=&quot;640&quot; height=&quot;480&quot; alt=&quot;cache-indexing&quot;&gt;&lt;/a&gt;&lt;script async src=&quot;//embedr.flickr.com/assets/client-code.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
&lt;/div&gt;
&lt;p&gt;
 Some of you may have already heard about the computer cache in your curriculum or some of you may have not. But this article is intended for all of those who want to understand cache concepts. The next part of it consists of developing a cache simulator using C programming language. I believe that this article will give a clear insight of cache and how it works. So lets see what we will be learning in this series of articles. 
&lt;/p&gt;
&lt;div class=&quot;index&quot;&gt;
 &lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#cache-intro&quot;&gt;Cache Introduction&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cache-organisation&quot;&gt;Cache Organisation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#memory-terms&quot;&gt;Understanding a few Concepts&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/cache-replacement-mapping-and-writing.html#cache-replacement&quot;&gt;Cache Replacement Strategies&lt;/a&gt;
    &lt;ul&gt;
 &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/cache-replacement-mapping-and-writing.html#random&quot;&gt;Random Replacement&lt;/a&gt;&lt;/li&gt;
 &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/cache-replacement-mapping-and-writing.html#lru&quot;&gt;Least Recently Used (LRU) Replacement&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/cache-replacement-mapping-and-writing.html#cache-mapping&quot;&gt;Cache Mapping Techniques&lt;/a&gt;
   &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/cache-replacement-mapping-and-writing.html#direct-map&quot;&gt;Direct Mapped Cache&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/cache-replacement-mapping-and-writing.html#set-associative&quot;&gt;Set Associative Mapped Cache&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/cache-replacement-mapping-and-writing.html#fully-associative&quot;&gt;Fully Associative Mapped Cache&lt;/a&gt;&lt;/li&gt;
   &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/cache-replacement-mapping-and-writing.html#cache-writing&quot;&gt;The Tedious Task of Cache Writing&lt;/a&gt;
   &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/cache-replacement-mapping-and-writing.html#cache-write-through&quot;&gt;Write Through&lt;/a&gt;
     &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/cache-replacement-mapping-and-writing.html#cache-write-around&quot;&gt;No Allocate (write around)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/cache-replacement-mapping-and-writing.html#cache-write-allocate&quot;&gt;Allocate&lt;/a&gt;&lt;/li&gt;
     &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://fetch-info.blogspot.in/2017/03/cache-replacement-mapping-and-writing.html#cache-write-back&quot;&gt;Write Back&lt;/a&gt;&lt;/li&gt;
   &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cache-simulator&quot;&gt;Basic Cache Simulator&lt;/a&gt;&lt;/li&gt;
 &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&quot;intro&quot;&gt;Cache Introduction&lt;/h2&gt;
&lt;p&gt;
 The first thing we should know what are we talking about. Cache is a highly efficient (really fast) memory which can bring data to processor at a much more speed than our typical RAM. CPU cache memory operates between 10 to 100 times faster than RAM, requiring only a few nanoseconds to respond to the CPU request. Cache operates faster because it resides usually on the same chip as the processor. But it makes them expensive. Also caches are smaller in size (around 512KB to 3MB) whereas computer RAM can be upto 16GB. The reason why caches are smaller is because it makes the mapping techniques faster and also reduces cost for a normal computer. So cache behaves a temporary storage for RAM data. Computer processor looks for data in cache first and if it is not present, then it gets the data from RAM and stores it in cache for future reference.
&lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a data-flickr-embed=&quot;true&quot;  href=&quot;https://www.flickr.com/photos/kapilgarg/33446108506/in/dateposted-public/&quot; title=&quot;cache-basic-block-diagram&quot;&gt;&lt;img src=&quot;https://c1.staticflickr.com/4/3683/33446108506_f17438ebdc_z.jpg&quot; width=&quot;640&quot; height=&quot;480&quot; alt=&quot;cache-basic-block-diagram&quot;&gt;&lt;/a&gt;&lt;script async src=&quot;//embedr.flickr.com/assets/client-code.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;&lt;/div&gt;
&lt;h2 id=&quot;cache-organisation&quot;&gt;Cache Organisation&lt;/h2&gt;
&lt;p&gt;
 As we know now that cache is smaller than RAM, it cannot store all the RAM data. In RAM memory, a memory cell number serves the purpose of index for the memory which is also our physical address. For example, if we want to store &lt;code&gt;x&lt;/code&gt; on some address &lt;code&gt;32&lt;/code&gt; then this means that we just go to 32nd cell number into RAM and writes our data. So we don&#39;t need to consider an index table for RAM because RAM is all the memory we have got.
&lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a data-flickr-embed=&quot;true&quot;  href=&quot;https://www.flickr.com/photos/kapilgarg/33446108676/in/dateposted-public/&quot; title=&quot;ram-indexing&quot;&gt;&lt;img src=&quot;https://c1.staticflickr.com/3/2849/33446108676_b1427211bb_z.jpg&quot; width=&quot;640&quot; height=&quot;480&quot; alt=&quot;ram-indexing&quot;&gt;&lt;/a&gt;&lt;script async src=&quot;//embedr.flickr.com/assets/client-code.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;
 But the same address cannot work for cache memory because lets say our cache has only 16 cells. This means we need to create a seperate index table for our cache memory into which we will search for cache cell number which contains data for given physical address. For our earlier example, if our cache has only 16 memory cells and we want to search for data for address 32 then we will look into cache index and find which cache cell holds data for address 32. One way would be to have a 16-entry index table where each entry represents the cache cell number and contains the physical address for which the data is stored in that particular cell. The following picture will give you a better idea than my words (But read my words again after looking at the picture, you will understand what i am saying). 
&lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a data-flickr-embed=&quot;true&quot;  href=&quot;https://www.flickr.com/photos/kapilgarg/33331320202/in/dateposted-public/&quot; title=&quot;cache-indexing&quot;&gt;&lt;img src=&quot;https://c1.staticflickr.com/4/3902/33331320202_33f25c317a_z.jpg&quot; width=&quot;640&quot; height=&quot;480&quot; alt=&quot;cache-indexing&quot;&gt;&lt;/a&gt;&lt;script async src=&quot;//embedr.flickr.com/assets/client-code.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;
 So this is how it works.There are several ways to find a physical address in cache index. One of which would be to search all index entries one by one but that will make our cache inefficient. Thats why we use mapping techniques which maps RAM addresses to Cache addresses and make it efficient. The next article will make you understand what are the main mapping techniques are and how they work.
&lt;/p&gt;

&lt;h2 id=&quot;memory-terms&quot;&gt;Understanding a few Concepts&lt;/h2&gt;
&lt;p&gt;
 There are 3 mapping techniques all of which have their advantages and disadvantages. But all of them have some things in common. Each technique breaks a physical address into some components which we will discuss first. If you don&#39;t understand what these terms mean then its not a problem as the picture will make their meanings more clear. There are also some formulas related but you don&#39;t need to cram them. You will understand how these formulas are derived on your own.
&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;&lt;b&gt;Word:&lt;/b&gt; This is the unit of data which is transferred for each operation.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Word Length: &lt;/b&gt;This is the maximum number of bytes allowed to transfer in one operation. For eg., In a 32-bit CPU, word length is 4 bytes (32 bits/8 bits)&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Block: &lt;/b&gt;A group of words which is transferred between cache and main memory is called a block. This implies that whenever the requested data is missing from cache then a whole block of words is transferred from main memory to cache instead of just one word. The significance of transferring a whole block comes from the fact that most of the times data is stored sequentially by a program (stack memory) and thus transferring a whole block reduces the chances of next failed request. So all data transfer between cache and main memory happens in forms of blocks but data transfer between any memory and CPU happens in word.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Cache Hit: &lt;/b&gt;When the processor requests data from cache and finds it in the cache, then it is said to be a cache hit.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Cache Miss: &lt;/b&gt;Just the opposite of cache hit. When requested data is not in cache then it is a cache miss. Caches are designed to reduce cache misses and to increase cache hits. In case of a cache miss during data read, the required data is read from main memory after transfering a block of data to cache.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Byte Select Bits:&lt;/b&gt; These are the lower significance bits of an address which are used to select a specific byte of a word to select for operation. This is internal for a CPU. That is when a CPU asks for some data, whole word is transferred and which byte of that word is used for operation is out of our concern. For eg., In a 16-bit CPU, total 2 bytes (word length) are transferred for each request. This means only 1 bit (&lt;code&gt;log(word_length)&lt;/code&gt;) is used for byte select. If the address is of 16 bits (64KB RAM) then we look at only more significant 15 bits (&lt;code&gt;address bits - byte select bits&lt;/code&gt;) to look for data in RAM and transfer the 2 bytes regardless of what the last bit was. So if CPU asks for address XXX0010 or XXX0011 (XXX - first 12 bits) then we look for same location that is XXX001 (last bit ignored) and send both bytes in each case i.e. XXX0010 , XXX0011&lt;/li&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a data-flickr-embed=&quot;true&quot;  href=&quot;https://www.flickr.com/photos/kapilgarg/32675691343/in/dateposted-public/&quot; title=&quot;ram-data-transfer&quot;&gt;&lt;img src=&quot;https://c1.staticflickr.com/4/3742/32675691343_50c02a5864_z.jpg&quot; width=&quot;640&quot; height=&quot;480&quot; alt=&quot;ram-data-transfer&quot;&gt;&lt;/a&gt;&lt;script async src=&quot;//embedr.flickr.com/assets/client-code.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;&lt;/div&gt;
 &lt;li&gt;&lt;b&gt;Cache Line bits: &lt;/b&gt;Consider the cache index is index of some book and it contains several pages. So our index is written on several pages. Lines bits determine the page number on which to search for our physical address. This way we save our time by limiting our search to only one page instead of many. Remember that more entries on one page means that we will have to match each entry against given address. This means a linear search for the address is necessary after finding the page number of our memory address. Number of line bits depend on mapping techniques which we will cover in next article. Line bits are next bits after byte select bits which help us to limit our search for address in cache index&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Tag Bits: &lt;/b&gt; After finding the page number, we will have to search our address. But we do not need to store the whole address as index entries because we already know some part of it which are the line bits and word bits. So we store the remaining bits of the address (excluding byte select, word and line bits) which are called the tag bits. Hence an address is broken into 4 parts : tag bits, line bits, word bits, byte select bits.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Word Bits: &lt;/b&gt;As the data transfer between main memory and cache memory happens in blocks, some bits are needed to transfer the required word from cache to processor. These are the word bits. Cache index tells the address of memory block in cache memory. The correct word which needs to be transferred is decided by word bits.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Valid Bit: &lt;/b&gt;This bit is used to indicate whether the data stored in the cache is valid or not (1- valid, 0-invalid). We need this bit in our index so we can keep track of memory locations which contain valid data. If we need to clear the cache, we can just set valid bit of each entry to 0&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Dirty Bit: &lt;/b&gt;This bit is important in write back policy. When some data is loaded into cache and it is updated then it is marked as dirty. So that when the CPU needs to replace (which happens frequently) the data , it can save the updated data into RAM before replacing it.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a data-flickr-embed=&quot;true&quot;  href=&quot;https://www.flickr.com/photos/kapilgarg/33361363381/in/dateposted-public/&quot; title=&quot;cache-index-structure&quot;&gt;&lt;img src=&quot;https://c1.staticflickr.com/4/3818/33361363381_4ddd0b8b35_z.jpg&quot; width=&quot;640&quot; height=&quot;480&quot; alt=&quot;cache-index-structure&quot;&gt;&lt;/a&gt;&lt;script async src=&quot;//embedr.flickr.com/assets/client-code.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Now lets see how the whole process of reading data from cache takes place&lt;/p&gt;
&lt;ol&gt;
 &lt;li&gt;Cache index contains several pages depending on the mapping technique&lt;/li&gt;
 &lt;li&gt;Each entry of index contains tag bits, valid bit and dirty bit&lt;/li&gt;
 &lt;li&gt;Entry number represents the location of block in cache memory. &lt;/li&gt;
 &lt;li&gt;Processor sends a physical address to cache of which the data is required.&lt;/li&gt;
 &lt;li&gt;Physical address is broken into components in the following manner (Starting from lower bits)
  &lt;ul&gt;
   &lt;li&gt;Byte Select Bits = log(word_length)&lt;/li&gt;
   &lt;li&gt;Word Bits = log(number_of_words_per_block)&lt;/li&gt;
   &lt;li&gt;Line Bits = log(number_of_pages)&lt;/li&gt;
   &lt;li&gt;Tag Bits = remaining_bits_of_address&lt;/li&gt;
  &lt;/ul&gt;
 &lt;/li&gt;
 &lt;li&gt;Correct Index Page is found using line bits&lt;/li&gt;
 &lt;li&gt;Each entry of the page is matched with tag bits&lt;/li&gt;
 &lt;li&gt;Correct word is found by adding word bits to matched entry.&lt;/li&gt;
 &lt;li&gt;Found word is transferred to processor if it is valid (valid=1)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This was pretty much about the terms we need to understand before moving on. Make sure you understand these concepts because in the next article, these terms will be repeating a lot.&lt;/p&gt;</description><link>http://fetch-info.blogspot.com/2017/03/wondering-cache.html</link><author>noreply@blogger.com (Kapil Garg)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-5935391448627745490</guid><pubDate>Thu, 12 Jan 2017 15:27:00 +0000</pubDate><atom:updated>2017-01-12T07:29:42.357-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">image processing</category><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Image Search Engine Using Python</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiJoa0dSQ9nAVVhbyiWBOTK9B8pC8xfKdllRP7tdbQJaR1FSIHW5mpeWp4PK8QliG3c0b8fJFmMwxvxAdZ2t4yPGW0VMm9uo3JgqOrio1Bd4jZlIp8v-IrRXblIqYqCniiDoRd9RV085Y4/s1600/image-search-engine-python.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiJoa0dSQ9nAVVhbyiWBOTK9B8pC8xfKdllRP7tdbQJaR1FSIHW5mpeWp4PK8QliG3c0b8fJFmMwxvxAdZ2t4yPGW0VMm9uo3JgqOrio1Bd4jZlIp8v-IrRXblIqYqCniiDoRd9RV085Y4/s400/image-search-engine-python.png&quot; width=&quot;400&quot; height=&quot;300&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;
 Images provide a lot more information than audio or text. Image processing is the prime field of research for robotics as well as search engines. In this article we will explore the concept of finding similarity between digital images using python. Then we will use our program to find top 10 search results inside a dataset of images for a given picture. It won&#39;t be as good as google&#39;s search engine because of the technique we will be using to find similarity between images. But what we are going to make will be pretty cool. So lets start.
&lt;/p&gt;
&lt;div class=&quot;index&quot;&gt;
 &lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#tech&quot;&gt;Setting up the Environment&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#algo&quot;&gt;Our Algorithm&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#build&quot;&gt;How the code looks&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#gui&quot;&gt;Lets build the GUI&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#extra&quot;&gt;Additional Techniques&lt;/a&gt;&lt;/li&gt;
 &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&quot;tech&quot;&gt;Setting up the Environment&lt;/h2&gt;
&lt;p&gt;
 The code we are going to write requires a few tools which we need to install first. I will try to be as precise as i can and if you get stuck into installing some tool then you can drop a comment below and i will help you sort out the problem. So here are the tools and the steps to install those tools in ubuntu (16.04 but should work on any version). The steps are not independent so follow them accordingly.
&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;
  &lt;b&gt;Install virtualenv&lt;/b&gt;
  &lt;ul&gt;
   &lt;li&gt;&lt;code&gt;sudo pip install -g virtualenv&lt;/code&gt;&lt;/li&gt;
  &lt;/ul&gt;
 &lt;/li&gt;
 &lt;li&gt;
  &lt;b&gt;Create environment directory and activate virtual environment.&lt;/b&gt;
  &lt;ul&gt;
   &lt;li&gt;&lt;code&gt;mkdir env&lt;/code&gt;&lt;/li&gt;
   &lt;li&gt;&lt;code&gt;virtualenv env&lt;/code&gt;&lt;/li&gt;
   &lt;li&gt;&lt;code&gt;cd env&lt;/code&gt;&lt;/li&gt;
   &lt;li&gt;&lt;code&gt;source bin/activate&lt;/code&gt;&lt;/li&gt;
  &lt;/ul&gt;
 &lt;/li&gt;
 &lt;li&gt;
  &lt;b&gt;Install Numpy&lt;/b&gt;
  &lt;ul&gt;
   &lt;li&gt;&lt;code&gt;pip install numpy&lt;/code&gt;&lt;/li&gt;
  &lt;/ul&gt;
 &lt;/li&gt;
 &lt;li&gt;
  &lt;b&gt;Install Pillow (Python image library)&lt;/b&gt;
  &lt;ul&gt;
   &lt;li&gt;&lt;code&gt;pip install pillow&lt;/code&gt;&lt;/li&gt;
  &lt;/ul&gt;
 &lt;/li&gt;
 &lt;li&gt;
  &lt;b&gt;Install Tkinter (For GUI)&lt;/b&gt;
  &lt;ul&gt;
   &lt;li&gt;&lt;code&gt;sudo apt-get install python-tk&lt;/code&gt;&lt;/li&gt;
  &lt;/ul&gt;
 &lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;algo&quot;&gt;Our Algorithm&lt;/h2&gt;
&lt;p&gt;
 The approach we will be using includes finding euclidean distances between color histograms of images. This is a very basic approach and it will help us to search images using their colors and not using their features. So it is possible that with this approach the best search result for a zebra might be a yin-yang but that depends on the dataset actually. So this is not a bad approach at all. Lets see what the step by step process looks like :
&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;Create the color histogram of each image in the dataset&lt;/li&gt;
 &lt;li&gt;Create the color histogram of the image to be searched&lt;/li&gt;
 &lt;li&gt;Calculate the euclidean distance between the histogram of image to be searched and histograms of the images in the dataset.&lt;/li&gt;
 &lt;li&gt;Select the smallest 10 distances and those are the search results.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This might sound a little confusing because we don&#39;t know about a couple of things in the algorithm. Like color histogram and euclidean distance. Let us understand these things.&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;&lt;b&gt;What is color histogram&lt;/b&gt;:
  &lt;p&gt;Color histogram is spectrum of each possible color space of the image containing the number of pixels for a specific color component. Consider a simple RGB format image in which each pixel has one red component, one green and one blue component. So each pixel of the image can be represented by a tuple of 3 values (red, green, blue). Now the range of these values vary from 0 to 255 and thus forms a unique color by selecting any value for 3 components. Thus (0,0,0) represents white color and (255,255,255) represents black. The total possible values for each color component are 256 and thus we have a total of 768 possible color components (these are not total number of colors but only the number of the color components).&lt;/p&gt;
  &lt;p&gt;Now the color histogram for this image will be a table having 768 entries in which each entry contains the number of pixels containing that component&lt;/p&gt;
 &lt;/li&gt;
 &lt;li&gt;&lt;b&gt;What is Euclidean Distance&lt;/b&gt;
  &lt;p&gt;
   It is nothing special but the ordinary distance between 2 vectors. If you know about vectors then you should have known how the distance between 2 vectors is calculated. But in case you dont know then lets assume that we have 2 vectors with N-dimensions. So each vector has N components. The distance between these 2 vectors can be calculated as below.

  &lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjojHwWI1RF8sPpsyLsTjz59rR7LUCh4hcYMG1OHWGdm30lZVfOITVfLqkMfCap1ctbXlDMO3jNXqb5MX81Szw49PdSPH2z_U1Cpk4XtP-zQ1pCweLeTUD_-PtxLYS24T-oNZtYVccCAUWU/s1600/euclidean_distance.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjojHwWI1RF8sPpsyLsTjz59rR7LUCh4hcYMG1OHWGdm30lZVfOITVfLqkMfCap1ctbXlDMO3jNXqb5MX81Szw49PdSPH2z_U1Cpk4XtP-zQ1pCweLeTUD_-PtxLYS24T-oNZtYVccCAUWU/s400/euclidean_distance.jpg&quot; width=&quot;400&quot; height=&quot;49&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
 &lt;/li&gt;
&lt;/ul&gt;
&lt;script async src=&quot;//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
&lt;!-- postbody --&gt;
&lt;ins class=&quot;adsbygoogle&quot;
     style=&quot;display:block&quot;
     data-ad-client=&quot;ca-pub-6499786486961559&quot;
     data-ad-slot=&quot;8358795822&quot;
     data-ad-format=&quot;auto&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id=&quot;build&quot;&gt;How the code looks&lt;/h2&gt;
&lt;p&gt;So far we have set up our environment and have learnt about the alogorithm we are going to use. Now its time to write actual code. Create 2 files with names &lt;code&gt;hist.py&lt;/code&gt; and &lt;code&gt;show_images.py&lt;/code&gt;. Below is the code for the &lt;code&gt;hist.py&lt;/code&gt; file.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot; style=&quot;white-space: pre !important&quot;&gt;
from PIL import Image
from numpy import *
import os
import show_images

DATASETDIR = &#39;/path/to/your/dataset/directory/&#39;

def perform_search(filename):
 #create an image object using Image.open method for the given image
 im = Image.open(filename)

 #we can use histogram method of image object to automatically build our histogram
 #we then convert the histogram array to numpy array to perform calculations
 search_histo = array(im.histogram())

 #create an empty list to store distances
 dist = []

 #get all the images of the dataset directory
 files = os.listdir(DATASETDIR)

 #declare the structure of the data for your dist list
 #It is only to perform sorting using numpy
 dtype = [(&#39;name&#39;, &#39;S100&#39;), (&#39;distance&#39;, float)]

 #Now we calculate euclidean distance between our search_histo and all images histograms
 for file in files:
  imob = Image.open(os.path.join(DATASETDIR, file))
  histo = array(imob.histogram())
  
  #Euclidean Distance Calculation
  try:
   diff = histo - search_histo
   sq = square(diff)
   total = sum(sq)
   result = sqrt(total)
   dist.append((file, result))
  except ValueError:
   pass

 
 #convert our list to numpy array with given data type
 distance = array(dist, dtype=dtype)

 #sort the array in increasing order to get top 10 results
 sort_dist = sort(distance, order=&#39;distance&#39;)

 top10 = sort_dist[:11]
 
 #show the result images in a window
 show_images.show_images(top10[1:])
&lt;/pre&gt;

&lt;h2 id=&quot;gui&quot;&gt;Lets build the GUI&lt;/h2&gt;
&lt;p&gt;We have our backbone ready. Now we just need to show the images for which we require some GUI library. You can use pyqt or tkinter. But for this article we are going to use tkinter. If you have followed the first section then you already have tkinter installed into your system but if its not then i suggest you to install it first. Lets build our GUI program. The following code is for &lt;code&gt;show_images.py&lt;/code&gt;&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot; style=&quot;white-space: pre&quot;&gt;
from Tkinter import *
from PIL import Image, ImageTk
import os

DATASETDIR = &#39;/path/to/your/dataset/directory/&#39;

class MainFrame(Frame):
    def __init__(self, parent, *args, **kw):
  Frame.__init__(self, parent, *args, **kw)            

  # create a canvas object and a vertical scrollbar for scrolling it
  vscrollbar = Scrollbar(self, orient=VERTICAL)
  vscrollbar.pack(fill=Y, side=RIGHT, expand=False)

  canvas = Canvas(self, bd=0, highlightthickness=0,
                  yscrollcommand=vscrollbar.set)
  canvas.pack(side=LEFT, fill=BOTH, expand=True)
  vscrollbar.config(command=canvas.yview)

  # reset the view
  canvas.yview_moveto(0)

  # create a frame inside the canvas which will be scrolled with it
  self.interior = interior = Frame(canvas)
  interior_id = canvas.create_window(0, 0, window=interior,
                                     anchor=NW)

  # track changes to the canvas and frame width and sync them,
  # also updating the scrollbar
  def _configure_interior(event):
      # update the scrollbars to match the size of the inner frame
      size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
      canvas.config(scrollregion=&quot;0 0 %s %s&quot; % size)
      if interior.winfo_reqwidth() != canvas.winfo_width():
          # update the canvas&#39;s width to fit the inner frame
          canvas.config(width=interior.winfo_reqwidth())
  interior.bind(&#39;&amp;lt;Configure&amp;gt;&#39;, _configure_interior)

  def _configure_canvas(event):
      if interior.winfo_reqwidth() != canvas.winfo_width():
          # update the inner frame&#39;s width to fill the canvas
          canvas.itemconfigure(interior_id, width=canvas.winfo_width())
  canvas.bind(&#39;&amp;lt;Configure&amp;gt;&#39;, _configure_canvas)


def show_images(top10, root=None): 
 root = Tk()
 root.title(&quot;Similar Images&quot;)
 root.imageframe = MainFrame(root)
 root.imageframe.pack(fill=BOTH, expand=True)

 images = []
 imagetks = []
 imagepanels = []
 r=0

 size = 128, 128

 for image in top10:
  imagename = os.path.splitext(image[0])[0]
  img = Image.open(os.path.join(DATASETDIR, image[0]))
  img.thumbnail(size)
  images.append(img)
  imgtk = ImageTk.PhotoImage(images[-1])
  imagetks.append(imgtk)
  panel = Label(root.imageframe.interior, image=imagetks[-1])
  imagepanels.append(panel)
  imagepanels[-1].grid(row=r, column=0)
  r = r+1

 root.mainloop()
&lt;/pre&gt;
&lt;p&gt;I wish i could tell you why the code for GUI looks like so but then we will be going off topic. I will consider writing another article on making GUI applications with tkinter but till then you can use the above code as it is.  Make sure you have specified the dataset directory in your both program files. So now that we have our main program and our gui program, its time to test the program.&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;Run the python interpreter using the command &lt;code&gt;python&lt;/code&gt;&lt;/li&gt;
 &lt;li&gt;Now import our &lt;code&gt;hist&lt;/code&gt; module using statement &lt;code&gt;import hist&lt;/code&gt;&lt;/li&gt;
 &lt;li&gt;call &lt;code&gt;hist.perform_search&lt;/code&gt; with the full path to the image to be searched&lt;/li&gt;
 &lt;li&gt;You should see a nice window showing the search results.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;extra&quot;&gt;Additional Techniques&lt;/h2&gt;
&lt;p&gt;The technique we used above is pretty simple and might not provide results on the basis of other features of images like the shape, orientation and scaling. So you might tempt to use a better search technique. I know 2 other methods which involves using the following concepts.
 &lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Color_layout_descriptor&quot; title=&quot;Color Layout Descriptor&quot; target=&quot;_blank&quot;&gt;Color Layout Descriptor&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Bag-of-words_model_in_computer_vision&quot; target=&quot;_blank&quot; title=&quot;Visual Words&quot;&gt;Visual Words&lt;/a&gt;&lt;/li&gt;
 &lt;/ul&gt;
&lt;/p&gt;

&lt;p&gt;Thats it for now. If you encounter any problem or have a question then don&#39;t hesitate to drop a comment below. Your feedback is also valuable so tell us your thoughts about the article in the comments.&lt;/p&gt;</description><link>http://fetch-info.blogspot.com/2017/01/image-search-engine-using-python.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiJoa0dSQ9nAVVhbyiWBOTK9B8pC8xfKdllRP7tdbQJaR1FSIHW5mpeWp4PK8QliG3c0b8fJFmMwxvxAdZ2t4yPGW0VMm9uo3JgqOrio1Bd4jZlIp8v-IrRXblIqYqCniiDoRd9RV085Y4/s72-c/image-search-engine-python.png" height="72" width="72"/><thr:total>18</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-969268507813710624</guid><pubDate>Sun, 04 Sep 2016 14:26:00 +0000</pubDate><atom:updated>2016-09-05T01:09:16.645-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">php</category><category domain="http://www.blogger.com/atom/ns#">programming</category><category domain="http://www.blogger.com/atom/ns#">security</category><title>Authentication System : Basic Design</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYjS59VuqIWUyG6w9h-bP2T9qSUuSFrhVAefWT_n0s6hXCIFi1i9DABuqHQlR1Jb19d3BBAyoiQJ5jCXbb_53kCVN9iKHvCKeTfatnxO21NgLBr1kpvGpvTCcK6FXAUTiMu7yPUv3Zwbt-/s1600/auth.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYjS59VuqIWUyG6w9h-bP2T9qSUuSFrhVAefWT_n0s6hXCIFi1i9DABuqHQlR1Jb19d3BBAyoiQJ5jCXbb_53kCVN9iKHvCKeTfatnxO21NgLBr1kpvGpvTCcK6FXAUTiMu7yPUv3Zwbt-/s320/auth.jpg&quot; width=&quot;320&quot; height=&quot;240&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;Being a system administrator, it is our main concern to protect users&#39; data from theft and corruption. Regarding theft, we focus in building a sophisticated but secure authentication system which can fulfill all the basic needs we discussed in the &lt;a href=&quot;https://fetch-info.blogspot.in/2016/08/authentication-step-to-security.html&quot; target=&quot;_blank&quot;&gt;last article&lt;/a&gt;. In the previous 2 articles, we learned about authentication and authorization. Also we understood the complications in building a secure system which can prevent tresspassing. But we have not code anything yet to build something. So in this article we will be designing an authentication system which will provide the functionalities we want and which will help us understand how the tech giants like google have taken security to another level. We will be writing code in &lt;code&gt;php&lt;/code&gt; but the same applies to other languages also.&lt;/p&gt;
&lt;div class=&quot;index&quot;&gt;
 &lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#tech&quot;&gt;What we&#39;ll be using&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#blueprint&quot;&gt;Blueprint&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#build&quot;&gt;Building the system&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#run&quot;&gt;Running the system&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#extra-care&quot;&gt;Additional Care&lt;/a&gt;&lt;/li&gt;
 &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&quot;tech&quot;&gt;What we&#39;ll be using&lt;/h2&gt;
&lt;p&gt;Lets see what are the languages we will be using and database plus some extra libraries&lt;/p&gt;
&lt;table&gt;
 &lt;tr&gt;
  &lt;td&gt;&lt;b&gt;Languages&lt;/b&gt;&lt;/td&gt;
  &lt;td&gt;PHP, Javascript, HTML, CSS&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
  &lt;td&gt;&lt;b&gt;Database&lt;/b&gt;&lt;/td&gt;
  &lt;td&gt;MySQL&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
  &lt;td&gt;&lt;b&gt;Libraries &amp;amp; Frameworks (optional)&lt;/b&gt;&lt;/td&gt;
  &lt;td&gt;Fingerprintjs, jquery, bootstrap, validatejs&lt;/td&gt;
 &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;If you dont have php and mysql in your system then you should install them first before writing the code. You can download &lt;code&gt;xampp&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;blueprint&quot;&gt;Blueprint&lt;/h2&gt;
&lt;p&gt;Now lets see the database schema and the strategy we will use to build our system.&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;User (id [integer], email [varchar], password [varchar])&lt;/li&gt;
 &lt;li&gt;Session (session_id [varchar], token [varchar], user_id [integer] , expire_time [datetime])&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Our system will authenticate the user based on the token stored in his machine and will prevent intrusuions.&lt;/p&gt;
&lt;ol&gt;
 &lt;li&gt;To prevent sniffing, the application should be using SSL which encrypts the data sent over network between the client and the server. But we can still do a lot to protect our system even if some man in the middle steals our data.&lt;/li&gt;
 &lt;li&gt;Create the database tables according to the schema described above.&lt;/li&gt;
 &lt;li&gt;When user visits a page, check for the `session_id` and `login` cookie. Two cases are possible&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Case #1 :&lt;/b&gt; If cookies are not present, ask for user credentials.&lt;/li&gt;
 &lt;li&gt;Search for those credentials in the `User` table.&lt;/li&gt;
 &lt;li&gt;If the credentials are correct then generate 2 random strings. One being the `session_id` and other being the `token`.&lt;/li&gt;
 &lt;li&gt;Create a new entry in the `Session` table with the values (session_id, token, user_id, expire_time). The `expire_time` can be any time you wish to allow a session being idle. For example: expire_time = current_time + 24 hours.&lt;/li&gt;
 &lt;li&gt;Send the session_id and token along with the response as `session_id` and `login` cookies respectively.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Case #2:&lt;/b&gt;If `session_id` cookie is present, search for the `session_id` in the database table.&lt;/li&gt;
 &lt;li&gt;If match is not found or the session has expired, ask for user credentials.&lt;/li&gt;
 &lt;li&gt;If session is found and active, match the `login` cookie value with the matched session `token`. &lt;/li&gt;
 &lt;li&gt;If match is not found, report a theft and delete all session data related to that user.&lt;/li&gt;
 &lt;li&gt;If match is found then authenticate the user and update the token value to some other random string.&lt;/li&gt;
 &lt;li&gt;Increase the expire time with 24 hours plus current system time.&lt;/li&gt;
 &lt;li&gt;Send the newly generated token along with the response and update the `login` cookie.&lt;/li&gt;
 &lt;li&gt;Make sure the cookies you save are set to be HTTP only so that XSS attacks can be prevented.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now lets assume that someone steals the cookies while they were being sent to the server. Now he is able to impersonate the user But if he makes some request to the server with the stolen data then the access token will be changed and when the user tries to visit some page a theft is reported because the user has invalidated token and the user and theif both gets logged out of the system. So far so good.&lt;/p&gt;
&lt;p&gt;But the problem arises when the theft occurs during the last request of the user. Now the user has left the application without logging out and some malicious person has the user&#39;s last authentication token. Now he is fully capabale of using the system for as long as he wants. We will see what can we do to prevent this and also how can we implement &quot;remember me&quot; functionality in our system. But before that lets build a system with the above strategy in mind.&lt;/p&gt;
&lt;script async src=&quot;//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
&lt;!-- postbody --&gt;
&lt;ins class=&quot;adsbygoogle&quot;
     style=&quot;display:block&quot;
     data-ad-client=&quot;ca-pub-6499786486961559&quot;
     data-ad-slot=&quot;8358795822&quot;
     data-ad-format=&quot;auto&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id=&quot;build&quot;&gt;Building the System&lt;/h2&gt;
&lt;p&gt;The system we will be designing is a pretty basic one but i will tell you what more you can add to take it to next level. Below are the components of the system with outlined code. The whole source code can be viewed on github &lt;a href=&quot;https://github.com/kapilgarg1996/fetch-info-auth&quot; target=&quot;_blank&quot;&gt;fetch-info-auth&lt;/a&gt; repository&lt;/p&gt;
&lt;h4&gt;HTML login form : (A simple form with e-mail and password field only)&lt;/h4&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;
&amp;lt;form&amp;nbsp;action=&amp;quot;http://fetchinfo.com/fetch-info-auth/public/html/login.php&amp;quot;&amp;nbsp;method=&amp;quot;post&amp;quot;&amp;gt;
&lt;br&gt; &amp;lt;input&amp;nbsp;type=&amp;quot;text&amp;quot;&amp;nbsp;name=&amp;quot;email&amp;quot;&amp;nbsp;maxlength=&amp;quot;30&amp;quot;&amp;nbsp;placeholder=&amp;quot;username&amp;quot;&amp;gt;
&lt;br&gt; &amp;lt;input&amp;nbsp;type=&amp;quot;password&amp;quot;&amp;nbsp;name=&amp;quot;password&amp;quot;&amp;nbsp;maxlength=&amp;quot;30&amp;quot;&amp;nbsp;placeholder=&amp;quot;password&amp;quot;&amp;gt;
&lt;br&gt; &amp;lt;input&amp;nbsp;type=&amp;quot;submit&amp;quot;&amp;nbsp;value=&amp;quot;Submit&amp;quot;&amp;gt;
&lt;br&gt;&amp;lt;/form&amp;gt;
&lt;/pre&gt;

&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSKm7a2JfS9rmqRMJloVopw-b9zvHcwP0GzfGzZddsHwGa6WIG3BpA3WuqE257kNAlvbldZJ7U88eS_dgh9S60KC58vhaTGXAGHqAViMTIi_Z95yh0gf-N10PnZQDh9hGOHYy341MrjyZi/s1600/fetch-info-auth-login.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSKm7a2JfS9rmqRMJloVopw-b9zvHcwP0GzfGzZddsHwGa6WIG3BpA3WuqE257kNAlvbldZJ7U88eS_dgh9S60KC58vhaTGXAGHqAViMTIi_Z95yh0gf-N10PnZQDh9hGOHYy341MrjyZi/s400/fetch-info-auth-login.png&quot; width=&quot;400&quot; height=&quot;135&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;h4&gt;Main Authentication Script&lt;/h4&gt;
&lt;p&gt;This script first calls another script to check whether the user has an authenticated cookie or not. If cookie is not verified then it checks whether the user has supplied his credentials or not. If credentials are provided then it verifies them and sends appropriate data.&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;
    function&amp;nbsp;auth(){
&lt;br&gt;  $result&amp;nbsp;=&amp;nbsp;authCookie()&amp;nbsp;;
&lt;br&gt;  if(is_integer($result)){
&lt;br&gt;   if(isset($_POST[&#39;email&#39;])){
&lt;br&gt;    $email&amp;nbsp;=&amp;nbsp;$_POST[&#39;email&#39;]&amp;nbsp;;
&lt;br&gt;    $pass&amp;nbsp;=&amp;nbsp;$_POST[&#39;password&#39;]&amp;nbsp;;
&lt;br&gt;    $conn&amp;nbsp;=&amp;nbsp;connect()&amp;nbsp;;
&lt;br&gt;    $query&amp;nbsp;=&amp;nbsp;&amp;quot;Select&amp;nbsp;*&amp;nbsp;from&amp;nbsp;user&amp;nbsp;where&amp;nbsp;email=&#39;&amp;quot;.$email.&amp;quot;&#39;&amp;nbsp;and&amp;nbsp;password=&#39;&amp;quot;.$pass.&amp;quot;&#39;&amp;quot;&amp;nbsp;;
&lt;br&gt;    $result&amp;nbsp;=&amp;nbsp;mysqli_query($conn,&amp;nbsp;$query)&amp;nbsp;;
&lt;br&gt;    if(mysqli_num_rows($result)==1){
&lt;br&gt;     
&lt;br&gt;     $uid&amp;nbsp;=&amp;nbsp;mysqli_fetch_row($result)[0]&amp;nbsp;;
&lt;br&gt;     $new_token&amp;nbsp;=&amp;nbsp;generateRandomString(100)&amp;nbsp;;
&lt;br&gt;     $new_time&amp;nbsp;=&amp;nbsp;date(&#39;Y-m-d&amp;nbsp;H:i:s&#39;,&amp;nbsp;strtotime(date(&#39;Y-m-d&amp;nbsp;H:i:s&#39;))+86400)&amp;nbsp;;
&lt;br&gt;     $new_session&amp;nbsp;=&amp;nbsp;generateRandomString(100)&amp;nbsp;;
&lt;br&gt;
&lt;br&gt;     
&lt;br&gt;     $query&amp;nbsp;=&amp;nbsp;&amp;quot;insert&amp;nbsp;into&amp;nbsp;session&amp;nbsp;values(&#39;&amp;quot;.$new_session.&amp;quot;&#39;,&#39;&amp;quot;.$new_token.&amp;quot;&#39;,&amp;quot;.$uid.&amp;quot;,&#39;&amp;quot;.$new_time.&amp;quot;&#39;)&amp;quot;&amp;nbsp;;
&lt;br&gt;     
&lt;br&gt;      if(&amp;nbsp;mysqli_query($conn,&amp;nbsp;$query)){
&lt;br&gt;      return&amp;nbsp;[$new_session,&amp;nbsp;$new_token]&amp;nbsp;;
&lt;br&gt;     }
&lt;br&gt;     return&amp;nbsp;globals(&amp;quot;UNAUTHENTICATED&amp;quot;)&amp;nbsp;;
&lt;br&gt;    }
&lt;br&gt;    return&amp;nbsp;globals(&amp;quot;UNAUTHENTICATED&amp;quot;)&amp;nbsp;;
&lt;br&gt;   }
&lt;br&gt;   return&amp;nbsp;globals(&amp;quot;UNAUTHENTICATED&amp;quot;)&amp;nbsp;;
&lt;br&gt;  }
&lt;br&gt;  else{
&lt;br&gt;   return&amp;nbsp;$result&amp;nbsp;;
&lt;br&gt;  }
&lt;br&gt; }
&lt;/pre&gt;

&lt;h4&gt;Cookie Authentication Script&lt;/h4&gt;
&lt;p&gt;This script checks whether a user has authenticated cookie or not. If the verification succeeds then it generates a new token and returns the new token which is returned by the main authentication script and the html pages override the existing cookie with the new generated token.&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;
 function&amp;nbsp;authCookie(){
&lt;br&gt;  $conn&amp;nbsp;=&amp;nbsp;connect()&amp;nbsp;;
&lt;br&gt;  if(isset($_COOKIE[&#39;session_id&#39;])){
&lt;br&gt;   $session_id&amp;nbsp;=&amp;nbsp;$_COOKIE[&#39;session_id&#39;]&amp;nbsp;;
&lt;br&gt;   $query&amp;nbsp;=&amp;nbsp;&amp;quot;select&amp;nbsp;*&amp;nbsp;from&amp;nbsp;session&amp;nbsp;where&amp;nbsp;session_id=&#39;&amp;quot;.$session_id.&amp;quot;&#39;&amp;quot;&amp;nbsp;;
&lt;br&gt;   $result&amp;nbsp;=&amp;nbsp;mysqli_query($conn,&amp;nbsp;$query)&amp;nbsp;;
&lt;br&gt;   if(mysqli_num_rows($result)&amp;nbsp;==&amp;nbsp;1){
&lt;br&gt;    $token&amp;nbsp;=&amp;nbsp;mysqli_fetch_row($result)[1]&amp;nbsp;;
&lt;br&gt;    if(isset($_COOKIE[&#39;login&#39;])){
&lt;br&gt;     $login&amp;nbsp;=&amp;nbsp;$_COOKIE[&#39;login&#39;]&amp;nbsp;;
&lt;br&gt;     echo&amp;nbsp;&amp;quot;Session:&amp;nbsp;&amp;quot;.$session_id.&amp;quot;&amp;lt;br/&amp;gt;&amp;nbsp;Token:&amp;nbsp;&amp;quot;.$token.&amp;quot;&amp;lt;br/&amp;gt;&amp;nbsp;Cookie:&amp;nbsp;&amp;quot;.$login&amp;nbsp;;
&lt;br&gt;     if($token&amp;nbsp;==&amp;nbsp;$login){
&lt;br&gt;      $new_token&amp;nbsp;=&amp;nbsp;generateRandomString(100)&amp;nbsp;;
&lt;br&gt;      $new_time&amp;nbsp;=&amp;nbsp;date(&#39;Y-m-d&amp;nbsp;H:i:s&#39;,&amp;nbsp;strtotime(date(&#39;Y-m-d&amp;nbsp;H:i:s&#39;))+86400000)&amp;nbsp;;
&lt;br&gt;      $query&amp;nbsp;=&amp;nbsp;&amp;quot;update&amp;nbsp;session&amp;nbsp;set&amp;nbsp;token=&#39;&amp;quot;.$new_token.&amp;quot;&#39;,&amp;nbsp;expire_time=&#39;&amp;quot;.$new_time.&amp;quot;&#39;&amp;nbsp;where&amp;nbsp;session_id=&#39;&amp;quot;.$session_id.&amp;quot;&#39;&amp;quot;&amp;nbsp;;
&lt;br&gt;      $result&amp;nbsp;=&amp;nbsp;mysqli_query($conn,&amp;nbsp;$query)&amp;nbsp;;
&lt;br&gt;      return&amp;nbsp;[$session_id,&amp;nbsp;$new_token]&amp;nbsp;;
&lt;br&gt;     }
&lt;br&gt;     else{
&lt;br&gt;       return&amp;nbsp;globals(&amp;quot;THEFT&amp;quot;)&amp;nbsp;;
&lt;br&gt;     }
&lt;br&gt;    }
&lt;br&gt;    else{
&lt;br&gt;     return&amp;nbsp;globals(&amp;quot;UNAUTHENTICATED&amp;quot;)&amp;nbsp;;
&lt;br&gt;    }
&lt;br&gt;   }
&lt;br&gt;   return&amp;nbsp;globals(&amp;quot;UNAUTHENTICATED&amp;quot;)&amp;nbsp;;
&lt;br&gt;  }
&lt;br&gt;  return&amp;nbsp;globals(&amp;quot;UNAUTHENTICATED&amp;quot;)&amp;nbsp;;
&lt;br&gt; }
&lt;/pre&gt;

&lt;p&gt;A successful verification of either the cookie or the credentials lands the user to the home page which only contains a hello message in this case. For convinience, the session_id cookie and the token cookie values are displayed on each page so that you can see what is happening. Each refresh to the home page updates the token as well. There are many other scripts as well. So if you want to see the whole source code, you can visit the github &lt;a href=&quot;https://github.com/kapilgarg1996/fetch-info-auth&quot; target=&quot;_blank&quot;&gt;fetch-info-auth&lt;/a&gt; repository&lt;/p&gt;.

&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicNDRxaG8axJt7rpyE_Wc0sSNTMRoyuV3UKu6Q5LgI89bHFwsrWBT2VjDheGcOKQWnGdSwnq2H20sC7tOzQsX_9EljckjGrhpzp0O0UesFwI9BGRHi6gq2mAdweAwoXxKeO-NVpLhKNDHc/s1600/fetch-info-auth-home.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicNDRxaG8axJt7rpyE_Wc0sSNTMRoyuV3UKu6Q5LgI89bHFwsrWBT2VjDheGcOKQWnGdSwnq2H20sC7tOzQsX_9EljckjGrhpzp0O0UesFwI9BGRHi6gq2mAdweAwoXxKeO-NVpLhKNDHc/s400/fetch-info-auth-home.png&quot; width=&quot;400&quot; height=&quot;83&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;h2 id=&quot;run&quot;&gt;Running the System&lt;/h2&gt;
&lt;p&gt;Now lets see how can we quick run our system so that we can understand things more clearly. Here i am making some assumptions which are :
&lt;ol&gt;
 &lt;li&gt;You have apache installed&lt;/li&gt;
 &lt;li&gt;You have mysql installed&lt;/li&gt;
 &lt;li&gt;You have php installed&lt;/li&gt;
        &lt;li&gt;You have created the database according to the schema discussed above.&lt;/li&gt;
&lt;/ol&gt;

All things settled down, now clone the repository from github and configure the apache localhost to view pages from this cloned repository. Also changed the connection settings in &lt;code&gt;&lt;i&gt;repository&lt;/i&gt;/script/auth/connect.php&lt;/code&gt; to point to your mysql database.

If all goes accordingly then you will be able to see the &lt;code&gt;login.php&lt;/code&gt; page. To do the login, you will have to create an account i.e. to add a user in the &lt;code&gt;user&lt;/code&gt; table according to the schema we discussed above. Just create the account and do the required login and you will be redirected to the home page where you will have the option to logout.
&lt;/p&gt;

&lt;p&gt;
 Please note that this system currently does not erase the session data on detecting theft. There are also other loop holes which you can cover in the methods by changing the response based on the flags sent by the authentication functions. Also note that the code assumes that the database schema is exactly what is defined above. I will try to generalise the code in the future. 
&lt;/p&gt;
&lt;h2 id=&quot;extra-care&quot;&gt;Additional Care&lt;/h2&gt;
&lt;p&gt;We have just created a basic system which only provides the required functionalities but does not fully protect the system. There are a couple of things we can do increase the security.&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;Use encryption over your application by using SSL protocol to prevent session hijacking. &lt;/li&gt;
 &lt;li&gt;Limit the number of login attempts to prevent dictionary attacks.&lt;/li&gt;
 &lt;li&gt;In case of persistent login (&quot;remember me&quot;) option, different approach is required.&lt;/li&gt;
 &lt;li&gt;Use scripts to prevent SQL injection attacks.&lt;/li&gt;
 &lt;li&gt;Make your cookies to be read by http only to prevent cross-site scripting attacks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope you learned something new here. In case you found some incorrect information or you think it can be improved then do share your thoughts in the comments. If you have any query then you can drop it in comments and i will try to resolve it.&lt;/p&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://fetch-info.blogspot.com/2016/09/authentication-system-basic-design.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYjS59VuqIWUyG6w9h-bP2T9qSUuSFrhVAefWT_n0s6hXCIFi1i9DABuqHQlR1Jb19d3BBAyoiQJ5jCXbb_53kCVN9iKHvCKeTfatnxO21NgLBr1kpvGpvTCcK6FXAUTiMu7yPUv3Zwbt-/s72-c/auth.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-4237790416092388710</guid><pubDate>Fri, 12 Aug 2016 12:13:00 +0000</pubDate><atom:updated>2016-08-12T05:57:13.286-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">security</category><category domain="http://www.blogger.com/atom/ns#">Web</category><title>Authentication: A step to security</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhj1oORLWtDSwzwsSDZ3kAjo6v13H0hbK_1QDmhLn1W8bd7rh3KBF0cOnhhFMncmAF01ZAWrATg8zIOpy8vOw5CBuXgHQvx4DmfUQ7SSqsNmZq6x-YcNhjoGfq0QB71HMvT5J1zWwGH3WsB/s1600/security-system.jpeg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhj1oORLWtDSwzwsSDZ3kAjo6v13H0hbK_1QDmhLn1W8bd7rh3KBF0cOnhhFMncmAF01ZAWrATg8zIOpy8vOw5CBuXgHQvx4DmfUQ7SSqsNmZq6x-YcNhjoGfq0QB71HMvT5J1zWwGH3WsB/s400/security-system.jpeg&quot; width=&quot;400&quot; height=&quot;201&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Here we are, continuing our discussion on authorization and authentication. If you have red my &lt;a href=&quot;https://fetch-info.blogspot.in/2016/08/authentication-and-authorization.html&quot;&gt;previous article&lt;/a&gt; then you would have known by now how important these concepts are and why these need to be implemented efficiently. But in the previous article, we only talked about them and didn&#39;t look at how to actually implement them. Cybersecurity is so vast field that we can&#39;t understand all of its complexities in one article but what we can do is we can start with some pretty basic stuff and move along to provide our application some sort of security. So in this article we&#39;ll see how can we implement authentication checks in server-client systems and specifically in web applications. But for other type of applications like desktop apps, mobile apps you can follow the same procedure although the technology would be different. Lets have a look what we are gonna learn today:-&lt;/p&gt;
&lt;div class=&quot;index&quot;&gt;
 &lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#goal&quot;&gt;Our Goal&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#procedure&quot;&gt;General Procedure&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#answers&quot;&gt;Answers to some General Questions&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#theft&quot;&gt;Cookie Theft&lt;/a&gt;&lt;/li&gt;
 &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&quot;goal&quot;&gt;Our Goal&lt;/h2&gt;
&lt;p&gt;Just keep aside what we are going to do for a second and think about what we actually want. We want some mechanism which can ensure that the user requesting a particular web page is actually some authenticated user. And by &lt;b&gt;authenticated user&lt;/b&gt; we mean that our system recognizes the user. Currently we are not concerned about the actual user, we are only concerned whether the credentials supplied to our system by the user are authenticated or not. So, ideally when a malicious person has stolen one of our user&#39;s identity we should not let the thief into our system but as we talked earlier, start with some simple stuff and then move along. So just let that theif in because the credentials he is providing are authenticated.&lt;/p&gt;
&lt;p&gt;Also we do not want to bother the user by asking for his credentials again and again for each request so we want to ensure some way of automatic login. But http being a stateless protocol, there is only one way of automatic login and that is to store some information about the connection/session on client&#39;s machine. The data stored on client machine can be stolen easily but we are not concerned with that right now. So to summarize our goal, we want to have the following features in our mechanism.&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;Authentication check for the provided credentials.&lt;/li&gt;
 &lt;li&gt;Provide automatic login facility by using any method.&lt;/li&gt;
 &lt;li&gt;Prevent Credentials theft. (optional)&lt;/li&gt;
 &lt;li&gt;Detect credentials theft (optional)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;procedure&quot;&gt;General Procedure&lt;/h2&gt;
&lt;p&gt;Do you think the authentication systems which the big players like google, facebook, amazon etc. implement will actually be that simple which we talked about earler. Hell No. But as we are in a learning phase we can start to build something atleast. And as every thing people build at first there needs to be step by step procedure to do that. The process i am going to tell you is not a standard so it is totally upto you to follow it. Here is a basic flow diagram of the process and following it is the description of the diagram.&lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixgqolAPIIYVR9IfOpKrMGzFAdn2lcHD6HvuaVB3ENdIij9txa-KJrhIJrPmu2MiphYQaBDxy0FCL5Ubj3DZBQC7iJSEbvT_fx7ExxFFK9ibb3vrzMLxrHyqsf9wBDuvJHHjVKreExZMn9/s1600/authentication-process.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixgqolAPIIYVR9IfOpKrMGzFAdn2lcHD6HvuaVB3ENdIij9txa-KJrhIJrPmu2MiphYQaBDxy0FCL5Ubj3DZBQC7iJSEbvT_fx7ExxFFK9ibb3vrzMLxrHyqsf9wBDuvJHHjVKreExZMn9/s400/authentication-process.png&quot; width=&quot;400&quot; height=&quot;362&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;ol&gt;
 &lt;li&gt;Check whether information(could be stored in a cookie, file or anything else) exists on client&#39;s machine or not.&lt;/li&gt;
 &lt;li&gt;If not then ask for user credentials&lt;/li&gt;
 &lt;li&gt;Authenticate the credentials.&lt;/li&gt;
 &lt;li&gt;If credentials are authenticated then let the user in otherwise repeat step 2.&lt;/li&gt;
 &lt;li&gt;If information is present then validate the information on the server.&lt;/li&gt;
 &lt;li&gt;If information is not valid then delete the information and repeat step 2.&lt;/li&gt;
 &lt;li&gt;If information is valid then authenticate the user based on that information.&lt;/li&gt;
 &lt;li&gt;If authentication is done properly then let the user in.&lt;/li&gt;
 &lt;li&gt;If error occurs while authenticating then delete the information and repeat step 2.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;As you may notice, if any problem occurs we ask for user credentials no matter what. There is a lot going in the above procedure. There are some things which are not clear and you might be wondering about those things. Some of the questions coming up your mind will be :&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;What information to store ?&lt;/li&gt;
 &lt;li&gt;How to store the information ?&lt;/li&gt;
 &lt;li&gt;How to validate the information ?&lt;/li&gt;
 &lt;li&gt;How to authenticate the user based on that information ?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Before tackling these questions, lets limit ourselves to web applications only. So the platform on which our application can perform will be a browser. Now lets answer the questions one by one.&lt;/p&gt;
&lt;ins class=&quot;adsbygoogle&quot;
     style=&quot;display:block&quot;
     data-ad-client=&quot;ca-pub-6499786486961559&quot;
     data-ad-slot=&quot;8358795822&quot;
     data-ad-format=&quot;auto&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id=&quot;answers&quot;&gt;Answers to our Questions&lt;/h2&gt;
&lt;p&gt;As we already know the procedure to implement it, we first need to know the answers to our question and then we will move forward to see what are the potential vulnerabilities in the simple procedure and how to remove them to a greater extent.&lt;/p&gt;
&lt;b&gt;What information to store ?&lt;/b&gt;&lt;br/&gt;
&lt;p&gt;If we don&#39;t care about the security then we can simply store the user_id and password so that we can directly look these things up into our database and see if we found a match but the problem with this approach is that we are compromising our user&#39;s data security. If that information gets stolen then not only the malicious user will be able to see the data but will also be able to delete it because he has the password of the real user.&lt;/p&gt;
&lt;p&gt;So we need to store something else. One thing we can do is whenever a user logs into the system by providing his credentials then we generate a one time token which will be stored on the clients machine and also a table entry would be made in a seprate table which holds the token, associated user_id. We can match the token in the table and if it exists we see the user to which the token belongs to and assumes that this is the user who is making the request. Some people may already have sniffed the potential vulnerability here but there is a lot we can do about it. We&#39;ll talk about all the techniques in the next part. Lets just stick to the basics here.&lt;/p&gt;

&lt;b&gt;How to store the information ?&lt;/b&gt;&lt;br&gt;
&lt;p&gt;Speaking regarding to the web application we can store the information either in the cookie or local storage. Local storage is not cross-browser compatible and your application may work somewhere and may not somewhere. So cookies will be our primary candidate to store the information.&lt;/p&gt;

&lt;b&gt;How to validate the information ?&lt;/b&gt;&lt;br&gt;
&lt;p&gt;Considering what is the information you are storing and what technique you are using, you can validate it differently. For example, if you are using the token technique then you can add another field in the table which holds the expire time of the token and validate whether the token has been expired or not. Or if you are using signed cookies then first you can validate whether the signature matches or not and then move forward to validate the data stored in the cookie.&lt;/p&gt;

&lt;b&gt;How to authenticate the user based on that information ?&lt;/b&gt;&lt;br&gt;
&lt;p&gt;This also depends on the technique you are using for authentication. If you are storing the user_id and password then you can authenticate the user by finding an entry for those things in the table or if you are storing token then you can find the token in the table and check its associated user_id. Basically, you are authenticating user associated with the information you stored on client&#39;s machine.&lt;/p&gt;

&lt;h2 id=&quot;theft&quot;&gt;Cookie Theft&lt;/h2&gt;
&lt;p&gt;The main concern related to store information in a cookie is about the security of the cookie. Because there is currently no simple way to distinguish 2 browsers, a server will treat the same cookie present on 2 different browsers as if they are present on one machine. So if our cookie gets stolen then we can&#39;t prevent the thief to enter into our system. But there are some javascript libraries which can generate a unique string for a unique browser and the uniqueness of the string increases as we consider more parameters while generating the string. We&#39;ll see the authentication technique in the next article and how to prevent cookie theft and what to do in case our cookie data is compromised. But first see the method using which a cookie can be stolen.&lt;/p&gt;
&lt;b&gt;Cross Site Scripting&lt;/b&gt;&lt;br&gt;
&lt;p&gt;Using this method, an attacker can inject script into the page by some means or using redirection techniques by which he can capture the cookies related to that page. If it looks something unimaginable then there is an &lt;a href=&quot;http://www.go4expert.com/articles/stealing-cookie-xss-t17066/&quot; target=&quot;_blank&quot;&gt;article&lt;/a&gt; which explains how you can steal someone&#39;s cookies using cross site scripting.&lt;/p&gt;

&lt;b&gt;Packet Capture&lt;/b&gt;&lt;br&gt;
&lt;p&gt;I don&#39;t know whether you have heard of the term &quot;packet capture&quot; earlier or not so let me explain it. Packet capture means capturing the data traffic going through some network. Using a tool like wireshark, we can capture each and every byte of data which is sent over the connected network. This way you can actually see what page users are visiting and what headers, cookies etc are exchanged during the connection. Once you get the cookies, you can set them on your machine and voila, you have stolen the user&#39;s identity. There is another nice article on how to do that &lt;a href=&quot;http://www.rafayhackingarticles.net/2011/07/facebook-cookie-stealing-and-session.html&quot; target=&quot;_blank&quot;&gt;Session Hijacking&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is a lot more to what just we learnt today. Some concepts like signed cookies, encrytion, request and response headers becomes useful when we want to make our system more secure. Also there are some authentication techniques which can be used to minimize data theft but that is a whole lot to write in one article so you&#39;ll have to read the next article where we will discuss authentication techniques. You can ask anything by dropping a comment below and i&#39;ll try to solve your problem. Also, suggestions are always welcome. If you found any information misleading or wrong then please inform by commenting.&lt;/p&gt;</description><link>http://fetch-info.blogspot.com/2016/08/authentication-step-to-security.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhj1oORLWtDSwzwsSDZ3kAjo6v13H0hbK_1QDmhLn1W8bd7rh3KBF0cOnhhFMncmAF01ZAWrATg8zIOpy8vOw5CBuXgHQvx4DmfUQ7SSqsNmZq6x-YcNhjoGfq0QB71HMvT5J1zWwGH3WsB/s72-c/security-system.jpeg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-6462918148983325254</guid><pubDate>Mon, 08 Aug 2016 08:30:00 +0000</pubDate><atom:updated>2016-08-08T01:44:17.562-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">security</category><title>Authentication and Authorization</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK9XBia6qIL3nXGU-KXOxJG2kCsxBeQGNHHOAuUGvp5PV6zx0VOb7v8QhoVXAi7g29vkRtaY0uJ0EpMHVluqKayOYxFKlU_l6T5wWS3it4ZevVtZys8KlzePGelJvKuuPRK63CPXf26IsS/s1600/aaa.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK9XBia6qIL3nXGU-KXOxJG2kCsxBeQGNHHOAuUGvp5PV6zx0VOb7v8QhoVXAi7g29vkRtaY0uJ0EpMHVluqKayOYxFKlU_l6T5wWS3it4ZevVtZys8KlzePGelJvKuuPRK63CPXf26IsS/s400/aaa.png&quot; width=&quot;400&quot; height=&quot;160&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;Authentication and Authorization are 2 independent concepts both of which are constituents of security. Authentication , in a vague manner, means to recognize someone and Authorization depicts to give that person some permissions. To understand it more better, a really simple example would be to know these concepts regarding to operating systems. When we log into our computer the OS checks whether our credentials match to any user of the computer. That is what we call authentication. Once the OS finds a match, it logs us into the computer. Now if we want to open a file then the operating system checks whether we have permission to do so or not. This is the time when the OS is authorizing us. If we have permission then OS will open the file for us otherwise it won&#39;t. So in this post we&#39;ll see how can we implement these features in any system and take safety measures to prevent intrusions. This post can be a bit boring but its actually a building block of what we&#39;ll learn in the future posts and it gives an idea of how things work.&lt;/p&gt;
&lt;div class=&quot;index&quot;&gt;
 &lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#authentication&quot; title=&quot;Authentication&quot;&gt;Authentication&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#authorization&quot; title=&quot;Authorization&quot;&gt;Authorization&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#server-client&quot; title=&quot;server-client&quot;&gt;Server-Client Systems&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#best-practices&quot; title=&quot;best-practices&quot;&gt;Best Practices&lt;/a&gt;&lt;/li&gt;
 &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&quot;authentication&quot;&gt;Authentication&lt;/h2&gt;
&lt;p&gt;As we talked earlier, authentication is recognizing the user. If we think about it a bit then we&#39;ll notice that authentication should be done before authorization. Its clear that you dont want to check whether a person is allowed to do something before checking if he is even allowed to enter the premises. Authentication can be done in a pretty straight forward manner. User enters his credentials, we check whether we have some registered user with those credentials and allow him in if we found a match. All we need to implement this type of authentication is a user-credentials table where we can search for the user.&lt;/p&gt;
&lt;h2 id=&quot;authorization&quot;&gt;Authorization&lt;/h2&gt;
&lt;p&gt;Authorization deals with the user&#39;s permissions. There are some ways how it works.
 &lt;ul&gt;
  &lt;li&gt;User can only see the data what he is allowed to take action on.&lt;/li&gt;
  &lt;li&gt;User can see all the data. He tries to take action on arbitrary data and then we check whether he is authorized to do that or not.&lt;/li&gt;
  &lt;li&gt;Mixture of above methods. User only see what he is allowed and if he takes action on it then we check again whether he is allowed or not.&lt;/li&gt;
 &lt;/ul&gt;
Why do we actually need the third way ? The first two ways looks fine but why a double check. The reason for a double check is to prevent intrusions. So if someone actually seeing the allowed data but he uses some hacks to get information of the hidden data then we must ensure that he should not get it. So when a user is allowed to take actions without our provided interface then there must be a double check. In case where the system functions are totally hidden from a user where he is restricted to use only the provided interface then the first method would do fine.
&lt;/p&gt;
&lt;p&gt;But the second method is actually not supposed to be used in a system where information about the existence of the data is also sensitive. Also it might result in a lot of unauthorized actions and would confuse the user what he is actually allowed to do. So the first or third method to implement authorization would work fine. To implement authorization all we need is access-control table which stores the information about the role and its allowed permissions. The problem increases when we want fine control over a user permission for each data. We&#39;ll talk a lot about authorization in the next article and would see how to actually implement it for a server-client based system using an SQL based database and a scripting language like php or python.&lt;/p&gt;
&lt;h2 id=&quot;server-client&quot;&gt;Server-Client Systems&lt;/h2&gt;
&lt;p&gt;Server-Client systems are the systems in which one machine asks for data and other machine serves the data. There can be an API using which a client machine can ask for data and the server machine needs to implement authentication and authorization of the client requests so that it can prevent illegal access to the data. In a server-client system, if the connections are stateless i.e. the server disconnects the client after serving its request then with each request the server will have to authenticate the user first. Also there is a possiblity of exploiting the server without using the given API and thus intruding in the server system. To provide a user-friendly environment, client machines should not ask for user credentials for each and every request, instead they should do it automatically. But doing this on the client machine would require to store the credentials on the machine which ,in the future, can be stolen by any person. So never store the credentials on the client machine. There are 2 protocols which are used in a client-server system.
&lt;ul&gt;
 &lt;li&gt;&lt;b&gt;Stateless&lt;/b&gt;: In this protocol, the server is not required to retain session information or status about each client for the duration of multiple requests.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Stateful&lt;/b&gt;: A protocol which requires keeping of the internal state on the server. So if the connection is made then it is kept alive for multiple requests and the server does not need to do authentication again and again.&lt;/li&gt;
&lt;/ul&gt;
Both these protocols have their ups and downs but we are concerned with only authentication here. Most of the application use stateless protocol because it is not exhausting i.e. it does not use resources for only a limited clients. Once a request is served, it is open for other requests. But we are stuck to the problem of repeated authentication in a user-friendly manner. The solution to this problem is that we can generate an access token for each logged in user and then use that token to do the authentication on the server. Normally using only a token cannot prevent what we call &quot;session hijacking&quot; so we&#39;ll need to do a lot of more work. In the future posts, we&#39;ll see how to actually implement these features in a system. 
&lt;/p&gt;
&lt;h2 id=&quot;best-practices&quot;&gt;Best Practices&lt;/h2&gt;
&lt;p&gt;These practices are actually for developers who have aleary implemented authentication and authoriation in their system. So lets see what we can do to make our system faster and secure.
&lt;ul&gt;
 &lt;li&gt;&lt;b&gt;Indexing&lt;/b&gt;: I hope you all know what indexing means regarding to a database. So always try to use indexing on the field which will be mostly searched. Like if you have user_id, user_name and password fields in your records and you will always want to search for a user using its user_name then apply indexing on that field. It will reduce the database response time.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Never store sensitive information on client machine.&lt;/b&gt;&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Encrypted connections&lt;/b&gt;: Use encryption to secure the data you sent over a connection so that even if that data is compromised you&#39;ll be safe.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Understand wrong attempts&lt;/b&gt;: If user is providing wrong credentials over and over again then there are chances that a script might be running to tresspass the system. So lock the account in that case, inform the user whose credentials are tried to be used and then unlock the account only when the original user asks.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Use Captcha&lt;/b&gt;: By implementing captcha you are preventing the scripts from attempting false logins.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Store for lesser duration&lt;/b&gt;: If you dont have a &quot;remember me&quot; feature on your login system then store the session data for a shorter duration so that if a client is disconnected for a longer time then he&#39;ll have to provide his credentials again. A simple strategy would be to increase the session expire time by some amount, lets say 30 minutes, on every request. So if there is gap more than 30 minutes between two successive requests for the same session then decline that request and ask for the credentials again. This way even if the access token is compromised in the future, data will be safe.&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
&lt;p&gt;This much for now. In the next article we&#39;ll write some code to implement authentication in a client-server system and then we&#39;ll see how to implement authorization for different user roles in the system. If you have any suggestion then just drop a comment. You are free to ask anything if you want. Also don&#39;t forget to subscribe to get notified about new articles.&lt;/p&gt;</description><link>http://fetch-info.blogspot.com/2016/08/authentication-and-authorization.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK9XBia6qIL3nXGU-KXOxJG2kCsxBeQGNHHOAuUGvp5PV6zx0VOb7v8QhoVXAi7g29vkRtaY0uJ0EpMHVluqKayOYxFKlU_l6T5wWS3it4ZevVtZys8KlzePGelJvKuuPRK63CPXf26IsS/s72-c/aaa.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-6287280466535459740</guid><pubDate>Sat, 18 Jun 2016 11:56:00 +0000</pubDate><atom:updated>2016-06-18T06:49:42.179-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">framework</category><category domain="http://www.blogger.com/atom/ns#">Latest</category><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">mvc</category><title>MVC: At Its Core</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK9lp34gRd8wZ0cFknta6yNsi11y33De63iFxDWQaOC1q4dyl-YB6xyyB4h-pl6eKDzdaTkN5-NUYIZoGnJzop64BvQPJvAJdfrM7S2GnL05C3yWnabf_PGV0sPip13rcnhyrjDakvf6qQ/s1600/mvc.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK9lp34gRd8wZ0cFknta6yNsi11y33De63iFxDWQaOC1q4dyl-YB6xyyB4h-pl6eKDzdaTkN5-NUYIZoGnJzop64BvQPJvAJdfrM7S2GnL05C3yWnabf_PGV0sPip13rcnhyrjDakvf6qQ/s400/mvc.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;
 Designing an application requires a lot of effort And without a proper structure there are chances that your application may fail during some operations. That is the reason of choosing a framework to accomplish the task of building an application. A framework enforces you to use the defined application structure and methods so that you can easily maintain your code and refactor it if needed. MVC , short form for &lt;strong&gt;Model View Controller&lt;/strong&gt; is a designing pattern in which an application is divided into 3 components i.e. Models, Views and Controllers. (The word &lt;i&gt;Application&lt;/i&gt; infers to all type of applications like mobile apps, desktop apps, web apps etc.). A framework which follows the MVC pattern is known as a MVC framework and there are a lot of frameworks for each type of application written in most of the languages. Normally people start learning about the framework without understanding the concept behind them. Each MVC framework follows the same pattern i.e. the MVC pattern. So if we learn about MVC then we can pick any framework of our choice and it&#39;ll take lesser time to understand their API. So in this article, we&#39;ll focus on MVC pattern rather than the framework. These are things we&#39;ll be learning today.
&lt;/p&gt;
&lt;div class=&quot;index&quot;&gt;
 &lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;MVC Components&lt;/a&gt;
   &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Models&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Views&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Controllers&lt;/a&gt;&lt;/li&gt;
   &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;MVC flow&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Popular Frameworks in the market&lt;/a&gt;&lt;/li&gt;
 &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&quot;mvc-components&quot;&gt;MVC Components&lt;/h2&gt;
&lt;p&gt;
 As i mentioned earlier, using MVC pattern means that you&#39;ll have to break your application in 3 components. You&#39;ll be wondering why that is even necessary ? The reasons are many but to list a few, it helps you write maintainable code by structuring your code in different modules, easily testable, easily scalable, easy to understand by others and list is going.. Still if you don&#39;t understand the importance of MVC then lets check out the components and then you&#39;ll be able to visualise the modularity. The components of a MVC based application are modals, views and controllers.
 &lt;ul&gt;
  &lt;li&gt;
   &lt;b&gt;Modals :&lt;/b&gt;
   &lt;p&gt;This is the component which is responsible to modify the data stored by your application. The data can be stored anywhere like in a database, in a file, in dynamic arrays etc. This component provides a handy API which can be used by other parts of your application to send data to database, retrieve data from database etc. The data definition (eg. database schema) and the data store (eg. MYSQL connections) connections are also managed by the Modal component. You just tell modal what to store and it takes care of the rest of the details like where, when, how to store.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
   &lt;b&gt;Views :&lt;/b&gt;
   &lt;p&gt;This is the component which displays the data. It does nothing else but only displays the data. You must be thinking that why we need a component for displaying the data if we can simply output it ? Actually a view is a reusable fragment of code so you can use a same view to diaplay 2 different types of data. For example, you might need to show price of an item in rupees if user is indian and in dollars if he is american (Its just an example. I obviously know that rupees and dollars are currencies of many more countries.) So instead of returning the value and the sign wrapped in a label widget(GUI apps) or in HTML tags(web apps), you can simply create a view which shows a value and a sign and you can pass your data to it.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
   &lt;b&gt;Controllers :&lt;/b&gt;
   &lt;p&gt;Controllers are the machinery which actually runs your application. It forms a bridge between the views and the modals. Controllers are the ones which asks the modals for some data, performs some operations on it and then send them to views. In a non-mvc application, what most people do is either they put all of their code in controllers or use any 1 more component along with controller like MC or VC. Thats when applications start to become unstable.&lt;/p&gt;
  &lt;/li&gt;
 &lt;/ul&gt;
&lt;/p&gt;
&lt;h2 id=&quot;mvc-flow&quot;&gt;MVC Flow&lt;/h2&gt;
&lt;p&gt;
 The flow of an MVC application is really important for us to understand. We already know about the components so now lets see how they work so that we can visualise what exactly is going on. Below is a simple block diagram describing the placement of the components.
&lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2MwJxqD_qcMa4ME2ULTEciIr26suRvQcWRe5hmNl6YPNAgPIXnurd9LoLg-d3iJqLSSzkj1r0CQswaHMBjwgbcDh9EgM9IB7OKbLxAlPDMdBKSmqAxiFcu4xu65T8YaYAHl_Q2BWKoGbE/s1600/mvcflow.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2MwJxqD_qcMa4ME2ULTEciIr26suRvQcWRe5hmNl6YPNAgPIXnurd9LoLg-d3iJqLSSzkj1r0CQswaHMBjwgbcDh9EgM9IB7OKbLxAlPDMdBKSmqAxiFcu4xu65T8YaYAHl_Q2BWKoGbE/s400/mvcflow.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;
 The main thing of your application is the routes using which a user can access your application views. From the perspective of a web app, a route is the &lt;b&gt;url&lt;/b&gt; which user clicks and a controller will be called to perform an action. Consider a route to be a trigger to call a controller for handling. In web apps, triggers are urls and in mobile/desktop apps, triggers are buttons or actions (swipe). So routes are defined to trigger the controllers. Lets see the ongoing process when a user logs into an app.
 &lt;ol&gt;
  &lt;li&gt;Lets suppose user just opens your app (may be url :&#39;/home&#39; or just click app icon).&lt;/li&gt;
  &lt;li&gt;A controller is called to display the login form. That controller does nothing but returns a view containg a login form.&lt;/li&gt;
  &lt;li&gt;The returned view displays the login form to the user.&lt;/li&gt;
  &lt;li&gt;User fills out the form and clicks the signin button (trigger)&lt;/li&gt;
  &lt;li&gt;A different controller is called which recieves the data. It queries the modal to check for the existence of the user with the given credentials.&lt;/li&gt;
  &lt;li&gt;Modal checks for the user&#39;s existence and returns result to the controller.&lt;/li&gt;
  &lt;li&gt;Controller checks the result. If user exists then it return &lt;b&gt;dashboard&lt;/b&gt; view otherwise it returns the login view again&lt;/li&gt;
 &lt;/ol&gt;
&lt;/p&gt;
&lt;h2 id=&quot;mvc-frameworks&quot;&gt;Popular MVC Frameworks&lt;/h2&gt;
&lt;p&gt;The work flow of a login process which we saw earlier is a very basic one. There are a lot of things which frameworks handle for us like session management, request/responses, cache, cookies, database connections, security, form handling etc. So instead of building an application from the scratch without a framework, we should use frameworks for performance and reliability. There are hundreds of frameworks avaiable in the market for different languages and for different purposes. I&#39;ll list a few here but you are welcome to suggest any popular framework which is missing.&lt;/p&gt;

&lt;table class=&quot;table&quot;&gt;
 &lt;thead&gt;
  &lt;tr&gt;
   &lt;th&gt;Framework&lt;/th&gt;
   &lt;th&gt;Language&lt;/th&gt;
   &lt;th&gt;Type&lt;/th&gt;
  &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
  &lt;tr&gt;
   &lt;td&gt;Express&lt;/td&gt;
   &lt;td&gt;Javascript&lt;/td&gt;
   &lt;td&gt;Web&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Django&lt;/td&gt;
   &lt;td&gt;Python&lt;/td&gt;
   &lt;td&gt;Web&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Laravel&lt;/td&gt;
   &lt;td&gt;PHP&lt;/td&gt;
   &lt;td&gt;Web&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Angular&lt;/td&gt;
   &lt;td&gt;Javascript&lt;/td&gt;
   &lt;td&gt;Web&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;ASP.NET&lt;/td&gt;
   &lt;td&gt;C#/VB&lt;/td&gt;
   &lt;td&gt;Mobile/Web&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Ruby on Rails&lt;/td&gt;
   &lt;td&gt;Ruby&lt;/td&gt;
   &lt;td&gt;Web&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Spring&lt;/td&gt;
   &lt;td&gt;Java&lt;/td&gt;
   &lt;td&gt;Any&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Ionic&lt;/td&gt;
   &lt;td&gt;Web languages&lt;/td&gt;
   &lt;td&gt;Hybrid mobile&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Catalyst&lt;/td&gt;
   &lt;td&gt;Perl&lt;/td&gt;
   &lt;td&gt;Web&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Phonegap&lt;/td&gt;
   &lt;td&gt;Web languages&lt;/td&gt;
   &lt;td&gt;Hybrid mobile&lt;/td&gt;
  &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;These are a lot more frameworks other than the above listed. But these are the ones which are widely used. If you want to share some more frameworks then you can write them in comments. Also it would be nice if you take a moment in sharing the article if you learnt something. Happy coding.&lt;/p&gt;
&lt;link href=&quot;https://googledrive.com/host/0B8VOyX5ZTJ31U0o4WFVfU0xjb2s&quot; rel=&quot;stylesheet&quot;/&gt;
</description><link>http://fetch-info.blogspot.com/2016/06/mvc-at-its-core.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK9lp34gRd8wZ0cFknta6yNsi11y33De63iFxDWQaOC1q4dyl-YB6xyyB4h-pl6eKDzdaTkN5-NUYIZoGnJzop64BvQPJvAJdfrM7S2GnL05C3yWnabf_PGV0sPip13rcnhyrjDakvf6qQ/s72-c/mvc.png" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-8431141794498132645</guid><pubDate>Sat, 07 May 2016 13:30:00 +0000</pubDate><atom:updated>2016-05-09T00:28:29.257-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">debugging</category><category domain="http://www.blogger.com/atom/ns#">gdb</category><title>Debugging with gdb</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWyWIHMgFkcxxP8Ey27QsKJkPjavZ04PjRkgD7pQaesw595jBB6Ov-WkDVm7b61CHdKXZT8Rj6Od0VCtoFZskvnHDRXuTekvJehdkKRz_NbP5G5SuygjE8FKPoa4AqHyseITRzCh0hGq-v/s1600/gdb-logo.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWyWIHMgFkcxxP8Ey27QsKJkPjavZ04PjRkgD7pQaesw595jBB6Ov-WkDVm7b61CHdKXZT8Rj6Od0VCtoFZskvnHDRXuTekvJehdkKRz_NbP5G5SuygjE8FKPoa4AqHyseITRzCh0hGq-v/s400/gdb-logo.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Debugging a program is a real tedious job which can consume time more than you took to write the program. And if you are not on the right track of debugging then you are probably gonna end up writing the whole program again. If you have red my previous article on debugging (&lt;a href=&quot;http://fetch-info.blogspot.in/2016/04/the-art-of-debugging.html&quot; title=&quot;learn-debugging&quot;&gt;The Art of Debugging&lt;/a&gt;) then you already have an idea of the debugging process and what are some basic methods to do it. We also talked about how are those basic techniques not gonna help us with larger and trickier programs. To make our job easy with those programs, there is a debugging tool : GNU Debugger or simply gdb. GDB is a powerful debugging tool with all the features you require while debugging your program. There are some basic set of features which are available in every debugging tool so if you want to use some other debugger then you&#39;ll find it easy to use after you get to know about gdb. There is some great stuff we&#39;ll be studying today so sit tight. Let&#39;s have a look at the index we&#39;ll be covering.&lt;/p&gt;
&lt;div class=&quot;index&quot;&gt;
 &lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#intro-gdb&quot;&gt;Indroduction to GDB&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#debug-terms&quot;&gt;Debugging Terminology&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#gdb-commands&quot;&gt;My Favourite gdb commands&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#debug-program&quot;&gt;Debugging a program&lt;/a&gt;&lt;/li&gt;
 &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&#39;intro-gdb&#39;&gt;Introduction to GDB&lt;/h2&gt;
&lt;p&gt;We have already seen its full form so now lets see what actually gdb is. GDB is like any other debugging tool which helps you to find out what is going inside your program. It helps you to check for variable states, memory states, stack trace and much more. GDB was actually made for GNU operating system but then it was made portable and now it can run on any UNIX-based operating system(Linux, Ubuntu, Redhat, Lubuntu) and windows operating system. It supports C and C++ programs. Although it also supports D, Fortran and 1-2 more languages but the support is partial. So its best if you use it to debug C and C++ programs. GDB requires some special symbols in your program to let it know about the program but don&#39;t worry, you won&#39;t have to add those symbols yourself. The GNU GCC compiler has a flag &lt;code&gt;-g3&lt;/code&gt; or &lt;code&gt;-g&lt;/code&gt; which inserts the symbols automatically. We&#39;ll see all of this in the later section. GDB can do mainly four things to help you catch the bug.
&lt;ul&gt;
&lt;li&gt;Start your program, specifying anything that might affect its behavior.&lt;/li&gt;
&lt;li&gt;Make your program stop on specified conditions.&lt;/li&gt;
&lt;li&gt;Examine what has happened, when your program has stopped.&lt;/li&gt;
&lt;li&gt;Change things in your program, so you can experiment with correcting the
effects of one bug and go on to learn about another. &lt;/li&gt;
&lt;/ul&gt;
We&#39;ll see all these things in action while we will debug our own program. Till then lets just get ourselves acquainted with gdb.
&lt;/p&gt;
&lt;h2 id=&#39;debug-terms&#39;&gt;Debugging Terminology&lt;/h2&gt;
&lt;p&gt;It will be very helpful if we first learn about some concepts before working with a debugger. We can&#39;t get to know about all the features a debugger provide because there are too many but we can enrich ourselves with some common and basic features.
&lt;ul&gt;
 &lt;li&gt;&lt;b&gt;Breakpoints&lt;/b&gt;: A break point is the point at which the debugger will stop your program. For eg., when you run your program in gdb, you can set a breakpoint at a function so that gdb stops at that function and you can execute your program line by line from there.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Watchpoints&lt;/b&gt;: A watch point is a break point which is not specific to a line in your program, instead it stops the program when some change happens to the supplied expression or variable. For instance, you can set a watch point to stop the program execution whenever your variable crosses a specific value.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Stepping&lt;/b&gt;: Stepping means to execute your program step by step. GDB has 2 types of step. We&#39;ll see both of them in the next section&lt;/li&gt;
&lt;/ul&gt;
There are more but those are self explaining so we don&#39;t need to elaborate them.
&lt;/p&gt;
&lt;h2 id=&#39;gdb-commands&#39;&gt;My Favourite gdb commands&lt;/h2&gt;
&lt;p&gt;GDB runs on commands. You tell it what to do, when to do by commands. There are a lot of commands of gdb but we won&#39;t use much of them frequently. But there are some commands which are used again and again and those commands provide those basic features which almost every debugging tool provides. So if we get handy with these frequently used commands then we&#39;ll able to work our way with most of the other debuggers. At this point, it is not necessary that you understand all these but soon we&#39;ll be using them.
&lt;ul&gt;
 &lt;li&gt;&lt;b&gt;help&lt;/b&gt;: Get a description of gdb’s commands.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;run&lt;/b&gt;: Runs your program. You can give it arguments that get passed in to your program just as if you had typed them to the shell. Also used to restart your program from the beginning if it is already running.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;quit&lt;/b&gt;: Leave gdb, killing your program if necessary.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;break&lt;/b&gt;: Set a breakpoint. Some examples: - &lt;code&gt;break somefunction&lt;/code&gt; stops before executing the first line somefunction. - &lt;code&gt;break 117&lt;/code&gt; stops before executing line number 117.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;watch&lt;/b&gt;: Set a watchpoint. Some example: - &lt;code&gt;watch myvar&lt;/code&gt; stops when the myvar value changes. &lt;code&gt;watch myvar&gt;100&lt;/code&gt; stops when myvar value crosses 100 if it is initially less than 100.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;list&lt;/b&gt;: Show part of your source file with line numbers (handy for figuring out where to put breakpoints). Examples: - &lt;code&gt;list somefunc&lt;/code&gt; lists all lines of somefunc. - &lt;code&gt;list 117-123&lt;/code&gt; lists lines 117 through 123.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;next&lt;/b&gt;: Execute the next line of the program, including completing any procedure calls in that line.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;step&lt;/b&gt;: Execute the next step of the program, which is either the next line if it contains no procedure calls, or the entry into the called procedure.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;finish&lt;/b&gt;: Continue until you get out of the current procedure (or hit a breakpoint). Useful for getting out of something you stepped into that you didn’t want to step into.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;cont&lt;/b&gt;: (Or continue). Continue until (a) the end of the program, (b) a fatal error like a Segmentation Fault or Bus Error, or (c) a breakpoint. If you give it a numeric argument (e.g., cont 1000) it will skip over that many breakpoints before stopping.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;print&lt;/b&gt;: Print the value of some expression, e.g. &lt;code&gt;print i&lt;/code&gt;.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;display&lt;/b&gt;: Like print, but runs automatically every time the program stops. Useful for watching values that change often.&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
&lt;ins class=&quot;adsbygoogle&quot;
     style=&quot;display:block&quot;
     data-ad-client=&quot;ca-pub-6499786486961559&quot;
     data-ad-slot=&quot;8358795822&quot;
     data-ad-format=&quot;auto&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id=&#39;debug-program&#39;&gt;Debugging a program&lt;/h2&gt;
&lt;p&gt;We have already learnt a lot about gdb. Now lets see it in action. In this section we&#39;ll debug a simple program which has a function to swap 2 numbers in the array. There are some bugs in the program so lets find out those using gdb. (Note that we could use basic techniques of debugging also in this case.)
&lt;pre class=&quot;prettyprint&quot;&gt;
#include&amp;lt;stdio.h&amp;gt;
void swap(int a, int b){
    int temp = a ;
    a = b ;
    b = temp ;
}
int main(){
    int i, arr[4]={1, 2, 3, 4} ;
    printf(&quot;Before Rotating left: &quot;) ;
    for(i=0 ; i&amp;lt;4 ; i++){
        printf(&quot;%d &quot;, arr[i]) ;
    }
    printf(&quot;\n&quot;) ;
    for(i=0 ; i&amp;lt;4 ; i++){
        swap(arr[i], arr[i+1]) ;
    }
    printf(&quot;After Rotating left: &quot;) ;
    for(i=0 ; i&amp;lt;4 ; i++){
        printf(&quot;%d &quot;, arr[i]) ;
    }
    printf(&quot;\n&quot;) ;
    return 0 ;
}
&lt;/pre&gt;

Save the above program in a file gdb.c. Now lets compile the above program but this time with a special flag to insert debugging symbols for gdb. Execute this command in the directory where gdb.js is saved &lt;code&gt;gcc -g gdb.c&lt;/code&gt; It will generate an executable with name a.out in the same directory. Try to run the executable by &lt;code&gt;./a.out&lt;/code&gt;. What did just happen ? Our program is doing nothing. The list is not rotating. But why ? Lets ask gdb to help us.
&lt;/p&gt;
&lt;p&gt;Run the gdb by &lt;code&gt;gdb a.out&lt;/code&gt;. It will start the gdb prompt where we&#39;ll start debugging our program. Now type &lt;code&gt;run&lt;/code&gt; and execute. It will run the whole program without breaking at some point. It is just like normally executing our program. Lets add some breakpoints by typing &lt;code&gt;break main&lt;/code&gt; &lt;code&gt;break swap&lt;/code&gt;. Now run the program. Firstly it will stop at &lt;code&gt;main&lt;/code&gt;There is nothing much to see at main starting point. So continue executing by typing &lt;code&gt;cont&lt;/code&gt;. Now it will stop at swap function which is called at first iteration of the loop. You can see the passed in parameter values. &lt;code&gt;a=1, b=2&lt;/code&gt;. Now lets see if the numbers are being swapped or not. Execute &lt;code&gt;next&lt;/code&gt; 3 times and check the values of a and b by typing &lt;code&gt;print a&lt;/code&gt; &lt;code&gt;print b&lt;/code&gt;. Well the values have been swapped. Good. So our function is doing well. Now lets see whether the array values are swapping or not.&lt;/p&gt;
&lt;p&gt;Execute &lt;code&gt;next&lt;/code&gt; and you will be back to your loop. Lets see our array values by &lt;code&gt;print arr[0]&lt;/code&gt; &lt;code&gt;print arr[1]&lt;/code&gt;.Oops. The values haven&#39;t been changed. But whats wrong ? Our function is doing well so what&#39;s the problem. Okay i got it. And i hope you too have got the problem. We are not passing our array values by reference. We are just passing them by value. So lets change that.
&lt;pre class=&quot;prettyprint&quot;&gt;
void swap(int *a, int *b){
    int temp = *a ;
    *a = *b ;
    *b = temp ;
}
//
//previous code
//
        swap(&amp;amp;arr[i], &amp;amp;arr[i+1]) ;
//
//rest code
&lt;/pre&gt;
Again execute the code and see the results. Our expected list is &lt;code&gt;2 3 4 1&lt;/code&gt; but we are getting &lt;code&gt;2 3 4 0&lt;/code&gt;. Where that &lt;code&gt;1&lt;/code&gt; is going. How it got converted to zero. Let me not reveal the suspense. I hand it over to you guys to find out what the second bug is. Hint : Iterate the whole loop and see whats going on inside the swap loop. Just use the basic commands and find out the bug. Many of you have already gotten it by just looking but still i will recommend you to find it using gdb so that you can get familiar with it.
&lt;/p&gt;
&lt;p&gt;Did you find this article a bit too much about the gdb ? Well its just the top of it. There are so much of it which we have not even touched. So if you want to go further with gdb then you can visit the &lt;a href=&quot;https://sourceware.org/gdb/onlinedocs/gdb/index.html&quot;&gt;GDB documentaion page&lt;/a&gt;. Also if you want a simple ebook with above gdb commands and their example then you can subscribe to our newsletter so that i can mail you that ebook. One more thing, sharing is caring. So share this if you think its worth reading or share your thoughts about it by dropping a comment below.&lt;/p&gt;</description><link>http://fetch-info.blogspot.com/2016/05/debugging-with-gdb.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWyWIHMgFkcxxP8Ey27QsKJkPjavZ04PjRkgD7pQaesw595jBB6Ov-WkDVm7b61CHdKXZT8Rj6Od0VCtoFZskvnHDRXuTekvJehdkKRz_NbP5G5SuygjE8FKPoa4AqHyseITRzCh0hGq-v/s72-c/gdb-logo.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-3340696758054587050</guid><pubDate>Sat, 30 Apr 2016 16:43:00 +0000</pubDate><atom:updated>2016-05-02T02:21:53.265-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">debugging</category><category domain="http://www.blogger.com/atom/ns#">gdb</category><category domain="http://www.blogger.com/atom/ns#">programming</category><title>The Art of Debugging</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIzJvMnjKEkWUSmyjgRs47vi_W6n1cb8C_wKsjR2IcqBmzy-PlwlPc3A_WotZ0538-2OYYUTE86NUsKTbF1lXGUB7PCNjCtzUrLEg-xZhrexRAhiI869kcEmtbGeFpuL-HemKDQdqpZQDd/s1600/debug.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIzJvMnjKEkWUSmyjgRs47vi_W6n1cb8C_wKsjR2IcqBmzy-PlwlPc3A_WotZ0538-2OYYUTE86NUsKTbF1lXGUB7PCNjCtzUrLEg-xZhrexRAhiI869kcEmtbGeFpuL-HemKDQdqpZQDd/s400/debug.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Writing unorganised code for something to work is an easy task, just put anything anywhere and see if the result is coming or not. If the result comes out in first attempt then you are really lucky but normal people like me are not. Then what we should do to check where the problem is occuring, we put some print statements (or console.log) to check the result in various places and observe where the output is deviating from its path. Well, it itself is a good practise of debugging the code but it does not work always especially not with large projects. So what should we do then ? According to me, we should give debugger a chance to find the bugs in our program. Debugging is an important part of writing programs because it teaches you to write unit tests for the parts of the program where it can fail.&lt;/p&gt;
&lt;p&gt;To perform debugging, we need a debugger and thankfully we have one, gdb (GNU debugger). In this article we will start by seeing some basic debugging techniques which involves logging the output and assertions. Then in the next article, we&#39;ll use gdb which will do the heavy lifting for us and will tell us what is going in our program internally. So the topics we&#39;ll be covering in this article will be :-&lt;/p&gt;
&lt;div class=&quot;index&quot;&gt;
 &lt;ul&gt;
  &lt;li&gt;&lt;a href=&#39;#method-of-debugging&#39;&gt;Method of debugging and What to avoid&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&#39;#basic-debugging&#39;&gt;Basic debugging&lt;/a&gt;
   &lt;ul&gt;&lt;a href=&#39;#output-logging&#39;&gt;&lt;li&gt;output logging&lt;/a&gt;&lt;/li&gt;
   &lt;li&gt;&lt;a href=&#39;#assertions&#39;&gt;assertions&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;
  &lt;/li&gt;
 &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&#39;method-of-debugging&#39;&gt;Method of debugging and what to avoid&lt;/h2&gt;
&lt;p&gt;The basic approach to debugging our program follows the sequence of following steps :
&lt;ol&gt;
&lt;li&gt;Know what your program is supposed to do.&lt;/li&gt;
&lt;li&gt;Detect when it doesn’t.&lt;/li&gt;
&lt;li&gt;Fix it.&lt;/li&gt;
&lt;/ol&gt;
This seems pretty simple, right ? In reality it is not that much simple. Detection the bugging point is a tedious job itself. Also there are some tempting mistakes which we do when we see wrong output. First mistake is, we don&#39;t follow the first step and just randomly tweak the code until it works. Don&#39;t do this because it will eventually mess up your code and even if it works(which i doubt), you will end up with a hard-to-maintain code. The second mistake is to attempt to intuit where things are going wrong by staring at the code or the program’s output. Like if see a segmentation fault and we know that we have used pointers in our program then we keep staring at the pointers code and try to see where the reference is going wrong. Stop doing this and let the computer find itself the mistake using debugging tools.
&lt;/p&gt;
&lt;h2 id=&#39;basic-debugging&#39;&gt;Basic debugging&lt;/h2&gt;
&lt;p&gt;Usually there are programs which consist only 50-100 lines of code and 2-3 functions then there basic debugging techniques can be applied to find the bug easily. If basic debugging doesn&#39;t help you then move straight to use gdb instead of applying these techniques too much. The 2 basic methods of basic debugging are:
&lt;ul&gt;
 &lt;li&gt;Logging output to console&lt;/li&gt;
 &lt;li&gt;Assertions&lt;/li&gt;
&lt;/ul&gt;
Lets see them one by one
&lt;/p&gt;
&lt;h3 id=&#39;output-logging&#39;&gt;Output Logging&lt;/h3&gt;
&lt;p&gt;Output logging basically means printing the output our program generates bewteen various parts. In C, we use &lt;code&gt;printf&lt;/code&gt; function to print out the variables and we can see their state in between our program lifecycle. But priting the variables using printf has some disadvantages. Some of these disadvantages are:
&lt;ul&gt;&lt;li&gt;We can&#39;t change the output without editing the code.&lt;/li&gt;
&lt;li&gt;A lot of print statements can create mess and we&#39;ll get confused which part is generating which output.&lt;/li&gt;
&lt;li&gt;The output can be misleading: in particular, printf output is usually buffered, which means that if your program dies suddenly there may be output still in the buffer that is never flushed to stdout. This can be very confusing, and can lead you to believe that your program fails earlier than it actually does&lt;/li&gt;
&lt;/ul&gt;
There are some rules of thumb to follow when using output logging technique.
&lt;ul&gt;
 &lt;li&gt;&lt;b&gt;Use &lt;code&gt;fprintf&lt;/code&gt;&lt;/b&gt;: Instead of printf statement, use &lt;code&gt;fprintf(stderr, ...)&lt;/code&gt;. It will keep your regular output seperate from debugging output.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Flush the output&lt;/b&gt;: If you still want to use &lt;code&gt;printf&lt;/code&gt; then call &lt;code&gt;fflush(stdout)&lt;/code&gt; to prevent buffering problem.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Use &lt;code&gt;#ifdef&lt;/code&gt;&lt;/b&gt;: Wrap your code inside &lt;code&gt;#ifdef&lt;/code&gt; It will allow you to turn your debugging code on/off according to your needs.&lt;/li&gt;
&lt;/ul&gt;
Lets see an example of it in a quicksort program.
&lt;pre class=&quot;prettyprint&quot;&gt;
#include&amp;lt;stdio.h&amp;gt;
#define DEBUG
void qsort(int *arr, int l, int r)
{
 int i ;
    if(r&amp;gt;l)
    {
        int index = partition(arr, l, r ) ;
        #ifdef DEBUG
        for(i=0 ; i&amp;lt;5 ; i++){
            printf(&quot;%d &quot;, arr[i]) ;
        }
        printf(&quot;\n&quot;) ;
        fflush(stdout) ;
        #endif
        qsort(arr, l, index) ;
        qsort(arr, index, r) ;
    }
}
int partition(int *arr, int l, int r)
    {
    int i=l, j=l ;
    int pivot = arr[r-1] ;
    while(j&amp;lt;r)
        {
        if(arr[j]&amp;lt;pivot)
            {
            int temp = arr[i] ;
            arr[i++] = arr[j] ;
            arr[j] = temp ;
        }
        j++ ;
    }
    int temp = arr[r-1] ;
    arr[r-1] = arr[i] ;
    arr[i] = temp ;
    return i ;
}
int main(){
    int i, arr[5] ;
    for(i=0 ; i&amp;lt;5 ; i++){
        scanf(&quot;%d&quot;, &amp;arr[i]) ;
    }
    qsort(arr, 0, 5) ;
    for(i=0 ; i&amp;lt;5 ; i++){
        printf(&quot;%d &quot;, arr[i]) ;
    }
    return 0 ;
}
&lt;/pre&gt;
This is a simple quicksort function but there is some error in it. To debug it, i have added a loop which prints my array after each partition in the &lt;code&gt;qsort&lt;/code&gt; function. Also i have wrapped that piece of code inside a &lt;code&gt;#ifdef&lt;/code&gt; block. I have switched on the debugging by defining DEBUG using &lt;code&gt;#define DEBUG&lt;/code&gt;. You can comment that line if you want to turn off debugging(which means the code inside &lt;code&gt;#ifdef&lt;/code&gt; block won&#39;t execute). Now if you try to run it then you will infinite array prints. Also those arrays are all sorted. So it suggests that our code is working fine but it is not stopping after sorting the array. What might be the reason for endless recursion. One can think may be our recursion condition is not right. And that is the bug in our program. If you change the recursion condition &lt;code&gt;if(r&amp;gt;l&gt;&lt;/code&gt; to &lt;code&gt;if(r&amp;gt;l+1)&lt;/code&gt;, you program will start behaving correctly. Now you can turn the debugging off by commenting &lt;code&gt;#define debug&lt;/code&gt;.
&lt;/p&gt;
&lt;h3 id=&#39;assertions&#39;&gt;Assertions&lt;/h3&gt;
&lt;p&gt;We just saw the ouput logging technique and how to use it smartly and carefully. Now lets see another technique of basic debugging. In this technique we&#39;ll use C &lt;code&gt;assert&lt;/code&gt; library. Every non-trivial C program should include &lt;code&gt;&amp;lt;assert.h&amp;gt;&lt;/code&gt;, which gives you the assert macro. The assert macro tests if a condition is true and halts your program with an error message if it isn’t. A basic example could be:
&lt;pre class=&quot;prettyprint&quot;&gt;
#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;assert.h&amp;gt;
int main(){
 assert(2+2 == 5) ;
 return 0 ;
}
&lt;/pre&gt;
When you will run the above program, the assertion will fail and you will see an error message. Assertions are useful because you won&#39;t have to change your code to get more debugging output. Assertions can be used to verify assumptions made by the program and print a diagnostic message if this assumption is false.
&lt;/p&gt;
&lt;p&gt;So far we have seen how can we debug our sweet simple programs but when our programs grow in size, they become more nasty. And to handle those, we&#39;ll be needing a more powerful tool to help us. In the next article, we&#39;ll see that mighty tool (gdb) and will see how to use it, when to use it and other tools to use along with gdb. If you have any query then drop a comment below and i&#39;ll be willing to help you out.&lt;/p&gt;</description><link>http://fetch-info.blogspot.com/2016/04/the-art-of-debugging.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIzJvMnjKEkWUSmyjgRs47vi_W6n1cb8C_wKsjR2IcqBmzy-PlwlPc3A_WotZ0538-2OYYUTE86NUsKTbF1lXGUB7PCNjCtzUrLEg-xZhrexRAhiI869kcEmtbGeFpuL-HemKDQdqpZQDd/s72-c/debug.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-14711312526397510</guid><pubDate>Thu, 28 Apr 2016 17:32:00 +0000</pubDate><atom:updated>2016-04-28T21:55:14.891-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">programming</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Understanding Python Decorators</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijaeVyiIx2UnnSgX9-vg0zevx8e5zhhUJkgNLvkl9BZh2ivlCwzJIfX83gpxLIaq29bY5-FXiPMWtWmesdd8tN5A852QCe5yjRdYybcp2oWV2O3CljYOgLf0h5jDBof0eE80eSZqlfQGyY/s1600/understand-python-decorators.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijaeVyiIx2UnnSgX9-vg0zevx8e5zhhUJkgNLvkl9BZh2ivlCwzJIfX83gpxLIaq29bY5-FXiPMWtWmesdd8tN5A852QCe5yjRdYybcp2oWV2O3CljYOgLf0h5jDBof0eE80eSZqlfQGyY/s400/understand-python-decorators.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;If you have ever wondered what those &lt;code&gt;@something&lt;/code&gt;mean above a python function or method then you are going to have your answers now. This &lt;code&gt;@something&lt;/code&gt; line of code is actually called a decorator. I have red from various articles about them but some of them were not able to clarify the concept of a decorator and what we can achieve with them. So in this post we&#39;ll learn a lot about python decorators. Here is a list of topics we&#39;ll be covering.&lt;/p&gt;

&lt;div class=&#39;index&#39;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#what-is-python-decorator&quot;&gt;What is python decorator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#understand-python-decorator-concept&quot;&gt;Understanding the concept&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#multiple-python-decorators&quot;&gt;Multiple decorators on same function&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#class-method-decorators&quot;&gt;class method decorator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#how-to-use-python-decorator&quot;&gt;Where can we use decorators&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;h2 id=&quot;what-is-python-decorator&quot;&gt;What is python decorator&lt;/h2&gt;
&lt;p&gt;A python decorator is nothing but a function which accepts your given function as a parameter and returns a replacement function. So its like something this
&lt;pre class=&#39;prettyprint&#39;&gt;
def decorator(your_func):
    def replacement(your_func_args):
        #do some other work
    return replacement

@decorator
your_func(your_func_args):
    #your_func code

&lt;/pre&gt;
Now when &lt;code&gt;your_func&lt;/code&gt; gets called then the function which actually gets executed will be &lt;code&gt;replacement&lt;/code&gt;. Note that the parameter pattern of the replacement function must be same to that of your function. To understand what is happening, lets read the next section.
&lt;/p&gt;

&lt;h2 id=&quot;understand-python-decorator-concept&quot;&gt;Understanding the core concept&lt;/h2&gt;
&lt;p&gt;It is very common in python to assign function objects, to pass function objects as arguments and to call functions using function refernces. What all these mean is you can do all of the following in python.
&lt;pre class=&quot;prettyprint&quot;&gt;
def any_func():
    #do some work

your_func = any_func

your_func()
&lt;/pre&gt;
Python decorators are based on the above concept and using a python decorator is equivalent to writing the following code.
&lt;pre class=&quot;prettyprint&quot;&gt;
def decorator(func):
    def replacement(args):
        #do some other work

    return replacement

def your_func(args):
    #do some work

your_func = decorator(your_func) #assign the replacement function to your given function. So now your_func points to replacement
your_func(args) #call the replacement function
&lt;/pre&gt;
As you can see, a decorator does nothing special, it just overrides your given function definition. And whenever &lt;code&gt;your_func&lt;/code&gt; will be called then &lt;code&gt;replacement&lt;/code&gt; will be executed. Lets see a simple example now which is very helpful while understanding decorators.
&lt;pre class=&quot;prettyprint&quot;&gt;
def encoder(func):
    def decorator(html_string): #function which replace &lt; and &gt; with html codes and calls the passed in function for formatting
        html_string = html_string.replace(r&#39;&lt;&#39;, r&#39;&amp;amp;lt;&#39;)
        html_string = html_string.replace(r&#39;&gt;&#39;, r&#39;&amp;amp;gt;&#39;)
        result = func(html_string)
        return result
    return decorator

@encoder
def para_wrapper(html_string): #our function which simply wraps the html string with a paragraph tag
    return &#39;&amp;lt;p&amp;gt;{0}&amp;lt;/p&amp;gt;&#39;.format(html_string)
&lt;/pre&gt;
In this example what is happening is we use a decorator &lt;code&gt;@encoder&lt;/code&gt; which replaces the function &lt;code&gt;para_wrapper&lt;/code&gt; with &lt;code&gt;decorator&lt;/code&gt;. Just remember that before the decorator assigns the new function to your function it gets the original copy of the function via the &lt;code&gt;func&lt;/code&gt; argument. So we can call our original function using that reference. Now look what &lt;code&gt;decorator&lt;/code&gt; is doing. It firstly makes the replacements in the html_string and then call the original function for its formatted output because that is what we want to send to the caller. Then it returns the output which our function generates for the new html_string. It is one of the use of the decorators. We&#39;ll see in the later section what other purposes can a decorator be used for.
&lt;/p&gt;
&lt;script async src=&quot;//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
&lt;!-- postbody --&gt;
&lt;ins class=&quot;adsbygoogle&quot;
     style=&quot;display:block&quot;
     data-ad-client=&quot;ca-pub-6499786486961559&quot;
     data-ad-slot=&quot;8358795822&quot;
     data-ad-format=&quot;auto&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id=&quot;multiple-python-decorators&quot;&gt;Multiple decorators(Nested)&lt;/h2&gt;
&lt;p&gt;Decorators can easily be nested for multi-level uses. Nesting the decorator simply means nesting the function references.Lets see how nested decorator look
&lt;pre class=&quot;prettyprint&quot;&gt;
@decorator3
@decorator2
@decorator1
def your_func(args):
    #do some work

#This is equivalent to the following code

your_func = decorator3(decorator2(decorator1(your_func)))
&lt;/pre&gt;
Nested decorators are helpful when there is one decorator which you can&#39;t modify and you want to add some input/output formatting or something then the easy way to do this is to add one more decorator.
&lt;/p&gt;
&lt;h2 id=&#39;class-method-decorators&#39;&gt;Class method decorators&lt;/h2&gt;
&lt;p&gt;Class method decorators are used for class methods. These decorators are not any special ones. As i mentioned earlier that the parameter list of the replacement function must be same as the given function, in a class method decorator the first parameter of the replacement function is &lt;code&gt;self&lt;/code&gt; which is the reference to the current object so you can use current objects properties inside the replacement function if you want. All other things are the same. Lets see some skeleton code&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;
def greet(func):
    def replacement(self):
        return &#39;Hello&#39;+func()
    return replacement

class Person(object):
    def __init__(self, args):
        #initialisation

    @greet
    def name(self):
        return self.firstname +&#39; &#39;+ self.lastname
&lt;/pre&gt;
&lt;h2 id=&quot;how-to-use-python-decorator&quot;&gt;Where can we use decorators&lt;/h2&gt;
&lt;p&gt;Python decorators can be used for a lot of scenarios. Some of them are
&lt;ul&gt;
&lt;li&gt;Input Verification&lt;/li&gt;
&lt;li&gt;Input modification&lt;/li&gt;
&lt;li&gt;Output Modification&lt;/li&gt;
&lt;/ul&gt;
Lets see each of the use case one by one. I will show you the code skeleton only.
&lt;b&gt;Input Verification&lt;/b&gt; Suppose you want to verify the input arguments before executing the main function. Now you can write the verification code inside your main function also but suppose there are a lot of functions which require input verification then it is smart decission to write a function to do that. This is how our code will look.
&lt;pre class=&quot;prettyprint&quot;&gt;
def your_decorator(your_func):
    def replacement(your_func_args):
        #do the input verification work.
        if(verification_pass):
            return your_func(your_func_args)
        else:
            return #You can return anything you want like an error message or raise an exception

    return replacement

@your_decorator
def your_func(your_func_args):
    #do what you want with the verified input
&lt;/pre&gt;
&lt;b&gt;Input Modification&lt;/b&gt;: We have already seen an example of this in the previous section where we wrote a decorator to replace the &lt; and &gt; symbols with html codes.&lt;br/&gt;

&lt;b&gt;Output Modification&lt;/b&gt; Now suppose you want to modify the output returned by your function. For example, your function returns a data dictionary but you want to add some default keys to the data dictionary before returning the output. Something like in a client-server session where you want to add session id and expire time in the data dictionary. Now you can add those fields right in your function but as the number of function grows this becomes tedious and we won&#39;t want to repeat the code. So lets write a decorator to add the data to the output of your function.
&lt;pre class=&quot;prettyprint&quot;&gt;
def response(func):
    def decorator(func_args):
        result = func(func_args)
        result[&#39;session_id&#39;] = key
        result[&#39;expire_time&#39;] = time
        return result

    return decorator

@resposne
def your_func(your_func_args):
    data = {}
    #fill data dictionary
    return data
&lt;/pre&gt;
These were some simple cases where we can use decorators. There are some advanced uses of decorator also but those are only the modification of the above cases.
&lt;/p&gt;</description><link>http://fetch-info.blogspot.com/2016/04/understanding-python-decorators.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijaeVyiIx2UnnSgX9-vg0zevx8e5zhhUJkgNLvkl9BZh2ivlCwzJIfX83gpxLIaq29bY5-FXiPMWtWmesdd8tN5A852QCe5yjRdYybcp2oWV2O3CljYOgLf0h5jDBof0eE80eSZqlfQGyY/s72-c/understand-python-decorators.jpg" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-5782997517929316924</guid><pubDate>Sun, 27 Mar 2016 14:52:00 +0000</pubDate><atom:updated>2016-04-27T05:01:25.642-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">Information</category><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">linux</category><category domain="http://www.blogger.com/atom/ns#">programming</category><category domain="http://www.blogger.com/atom/ns#">string</category><category domain="http://www.blogger.com/atom/ns#">tricks</category><title>See More of C</title><description>&lt;style type=&quot;text/css&quot;&gt;
.chapter table, .chapter tr, .chapter thead, .chapter td, .chapter th{
 border: 1px solid;
 border-collapse: collapse;
}
.chapter .chapter-title p{
 text-align:center;
 font-size: 24px ;
color : #00796B !important ;
}
&lt;/style&gt;
&lt;div id=&quot;book&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1JVfYzS2_Nwo63KEgRb8UrrdARPHJhyREabwxNPnQMcUZsQslM10c4ANQO0Ku3C24XsAH6NbQ72Clb4B21gUbBTGHJMmAWGlK1E54ljbUvedYgy6GpFPJPz0dKlseOSqjZI7jDZfD5aRj/s1600/PicsArt_1406950660777.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1JVfYzS2_Nwo63KEgRb8UrrdARPHJhyREabwxNPnQMcUZsQslM10c4ANQO0Ku3C24XsAH6NbQ72Clb4B21gUbBTGHJMmAWGlK1E54ljbUvedYgy6GpFPJPz0dKlseOSqjZI7jDZfD5aRj/s400/PicsArt_1406950660777.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div id=&quot;contents&quot; class=&quot;contents&quot;&gt;
 &lt;ul class=&quot;contents-list&quot;&gt;
  &lt;li class=&quot;contents-item&quot;&gt;&lt;a href=&quot;#why-more-of-c&quot;&gt;Why more of c ?&lt;/a&gt;&lt;/li&gt;
  &lt;li class=&quot;contents-item&quot;&gt;&lt;a href=&quot;#function-pointer&quot;&gt;Function Pointers&lt;/a&gt;&lt;/li&gt;
  &lt;li class=&quot;contents-item&quot;&gt;&lt;a href=&quot;#search-sort&quot;&gt;Search and Sort&lt;/a&gt;&lt;/li&gt;
  &lt;li class=&quot;contents-item&quot;&gt;&lt;a href=&quot;#thread&quot;&gt;Multi-Threaded Programming&lt;/a&gt;&lt;/li&gt;
  &lt;li class=&quot;contents-item&quot;&gt;&lt;a href=&quot;#time-function&quot;&gt;Time Functions&lt;/a&gt;&lt;/li&gt;
  &lt;li class=&quot;contents-item&quot;&gt;&lt;a href=&quot;#random-number&quot;&gt;Random Numbers&lt;/a&gt;&lt;/li&gt;
  &lt;li class=&quot;contents-item&quot;&gt;&lt;a href=&quot;#string-conversion&quot;&gt;String Conversion&lt;/a&gt;&lt;/li&gt;
  &lt;li class=&quot;contents-item&quot;&gt;&lt;a href=&quot;#process-system-command&quot;&gt;Process Creation and System commands execution&lt;/a&gt;&lt;/li&gt;
  &lt;li class=&quot;contents-item&quot;&gt;&lt;a href=&quot;#database&quot;&gt;DataBase Handling&lt;/a&gt;&lt;/li&gt;
  &lt;li class=&quot;contents-item&quot;&gt;&lt;a href=&quot;#cgi-script&quot;&gt;CGI Scripting&lt;/a&gt;&lt;/li&gt;
  &lt;li class=&quot;contents-item&quot;&gt;&lt;a href=&quot;#more-links&quot;&gt;More Links&lt;/a&gt;&lt;/li&gt;
 &lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&quot;material&quot;&gt;
 &lt;div id=&quot;why-more-of-c&quot; class=&quot;chapter&quot;&gt;
  &lt;div id=&quot;chapter-title&quot; class=&quot;chapter-title&quot;&gt;
   &lt;p&gt;Why digging more of C ?&lt;/p&gt;
  &lt;/div&gt;
  &lt;div id=&quot;chapter-body&quot; class=&quot;chapter-body&quot;&gt;
   &lt;p&gt;Being a high level language and the features C support there is not a need to ask why should we learn more about C. C programs are relatively faster than most of other high level languages like python or java. That may be because C programs are compiled first or may be because C libraries and fucntions are more hardware oriented. But whatever is the reason, C programs are way better. I can show you an example of it to you. There is a program which corrects the spelling of a mis-spelled word like it changes &quot;somethinh&quot; to &quot;something&quot;. Now here is the comparison between the programs written in C and than that written in python&lt;/p&gt;
   &lt;table&gt;
    &lt;thead align=&quot;center&quot;&gt;
     &lt;tr&gt;
      &lt;th&gt;Property&lt;/th&gt;
      &lt;th&gt;Program in C&lt;/th&gt;
      &lt;th&gt;Program in Python&lt;/th&gt;
      &lt;th&gt;Result&lt;/th&gt;
     &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tr&gt;
     &lt;td&gt;
      Lines of code
     &lt;/td&gt;
     &lt;td&gt;
      184
     &lt;/td&gt;
     &lt;td&gt;
      21
     &lt;/td&gt;
     &lt;td&gt;Python is shorter&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
     &lt;td&gt;Test with file 6.9 MB&lt;/td&gt;
     &lt;td&gt;0m0.892s&lt;/td&gt;
     &lt;td&gt;0m1.911s&lt;/td&gt;
     &lt;td&gt;C is 114% faster&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
     &lt;td&gt;Test with file 50 MB&lt;/td&gt;
     &lt;td&gt;0m6.896s&lt;/td&gt;
     &lt;td&gt;0m17.892s&lt;/td&gt;
     &lt;td&gt;C is 159% faster&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
     &lt;td&gt;Test with file 100 MB&lt;/td&gt;
     &lt;td&gt;0m14.474s&lt;/td&gt;
     &lt;td&gt;1m25.579s&lt;/td&gt;
     &lt;td&gt;C is 491% faster&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
     &lt;td&gt;Test with file 168 MB&lt;/td&gt;
     &lt;td&gt;0m44.627s&lt;/td&gt;
     &lt;td&gt;killed&lt;/td&gt;
     &lt;td&gt;Uncomparable&lt;/td&gt;
    &lt;/tr&gt;
   &lt;/table&gt;
   &lt;a class=&quot;source-link&quot; alt=&quot;Spell-corrector program in C&quot; href=&quot;http://marcelotoledo.com/how-to-write-a-spelling-corrector/&quot;&gt;Spell corrector program&lt;/a&gt;
   &lt;br/&gt;
   &lt;p&gt;So its pretty obvious that C program wins. But most of us don&#39;t know what we can do with C other than writing a main() function declaring some structs etc. There is more to that. I assume that you have basic understanding of C and its concepts like functions, pointers, structs, I/O. Now lets see what other things can we do with C. I won&#39;t write in detail about these things nor i will write sample programs but i will definitely guide you to some useful sites where you can pick this stuff from.&lt;/p&gt;
  &lt;/div&gt;
 &lt;/div&gt;
 &lt;div id=&quot;function-pointer&quot; class=&quot;chapter&quot;&gt;
  &lt;div id=&quot;chapter-title&quot; class=&quot;chapter-title&quot;&gt;
   &lt;p&gt;Function Pointers&lt;/p&gt;
  &lt;/div&gt;
  &lt;div id=&quot;chapter-body&quot; class=&quot;chapter-body&quot;&gt;
   &lt;p&gt;This is a whole new topic for some of us. We have red about functions, we know about pointers but what is a function pointer. Well from the name it is inferable that a function pointer is kind of a pointer which points to a function. But still how is it useful ? Well its useful in many ways. Have you heard about callback functions (Functions which are called after execution of a function) Callback functions are passed as arguments in other languages like javascript and python but we didn&#39;t know earlier that the same thing can be happened with C also. Just like array name represents a pointer to it, function name alone represents a pointer to it. We can assign them, pass them as arguments and call them whenever we want. Lets look at a simple example.&lt;/p&gt;
   &lt;code&gt;
   &lt;pre class=&quot;prettyprint&quot;&gt;
#include&amp;lt;stdio.h&amp;gt;
typedef void (*f_pointer)(void) ;
void wrapper(cps doer, cps callback)
{
 (*doer)() ;
 (*callback)() ;
}
void func(){
 printf(&quot;I am in doing function\n&quot; ) ;
}
void tobecalled(){
 printf(&quot;I am in callback&quot;) ;
}
int main(){
 f_pointer doer, callback;
        doer = func ;
 callback = tobecalled ;
 wrapper(doer, callback) ;
 printf(&quot;\nI am in main\nExiting\n&quot;) ;
}
   &lt;/pre&gt;
   &lt;/code&gt;
  &lt;/div&gt;
 &lt;/div&gt;
&lt;!-- postbody --&gt;
&lt;ins class=&quot;adsbygoogle&quot;
     style=&quot;display:block&quot;
     data-ad-client=&quot;ca-pub-6499786486961559&quot;
     data-ad-slot=&quot;8358795822&quot;
     data-ad-format=&quot;auto&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
 &lt;div id=&quot;search-sort&quot; class=&quot;chapter&quot;&gt;
  &lt;div id=&quot;chapter-title&quot; class=&quot;chapter-title&quot;&gt;
   &lt;p&gt;Searching and Sorting&lt;/p&gt;
  &lt;/div&gt;
  &lt;div id=&quot;chapter-body&quot; class=&quot;chapter-body&quot;&gt;
   &lt;p&gt;Some of you have already heard about it but for those who haven&#39;t, there is a good news. C stdlib provides 2 methods for optimized searching and sorting and those are &lt;code&gt;qsort() and bsearch()&lt;/code&gt; which implements quicksort and binary search algorithm respectively. Here is a complete description about these methods and what parameters they take &lt;a href=&quot;https://www.cs.cf.ac.uk/Dave/C/node16.html#SECTION001640000000000000000&quot;&gt;qsort and bsearch in c&lt;/a&gt;. In qsort() method definition, you will see that there is pointer to a function being passed as an argument.&lt;/p&gt;
  &lt;/div&gt;
 &lt;/div&gt;
 &lt;div id=&quot;thread&quot; class=&quot;chapter&quot;&gt;
  &lt;div id=&quot;chapter-title&quot; class=&quot;chapter-title&quot;&gt;
   &lt;p&gt;Multi-Threaded Programming&lt;/p&gt;
  &lt;/div&gt;
  &lt;div id=&quot;chapter-body&quot; class=&quot;chapter-body&quot;&gt;
   &lt;p&gt;Yes you saw right. We are not talking about java. We are talking about C which is also capable of creating and managing threads. All this is done by pthread library which have some useful functions for thread creation and thread management. For thread management, there are semaphores, mutex variables and condition variables. Here is the webpage where you can find further details. &lt;a class=&quot;source-link&quot; alt=&quot;Pthread library&quot; href=&quot;https://computing.llnl.gov/tutorials/pthreads/&quot;&gt;Pthread Details&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
 &lt;/div&gt;
 &lt;div id=&quot;time-function&quot; class=&quot;chapter&quot;&gt;
  &lt;div id=&quot;chapter-title&quot; class=&quot;chapter-title&quot;&gt;
   &lt;p&gt;Time Functions&lt;/p&gt;
  &lt;/div&gt;
  &lt;div id=&quot;chapter-body&quot; class=&quot;chapter-body&quot;&gt;
   &lt;p&gt;Whenever we had to dealt with time it seemed so complicated because time functions return some unknown structure and data. Python has a now() function which prints current date and time. There is a similar function in C &lt;code&gt;time.h&lt;/code&gt; library which is &lt;code&gt;time()&lt;/code&gt; which returns a time data structure from which we can extract current data and time. Not only this, we can manipulate time structures, compare them and use anywhere we want (specifically in our databases). Here is the link to complete time functions &lt;a class=&quot;source-link&quot; alt=&quot;C time library&quot; href=&quot;http://www.tutorialspoint.com/c_standard_library/time_h.htm&quot;&gt;C time functions&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
 &lt;/div&gt;
 &lt;div id=&quot;random-number&quot; class=&quot;chapter&quot;&gt;
  &lt;div id=&quot;chapter-title&quot; class=&quot;chapter-title&quot;&gt;
   &lt;p&gt;Random Numbers&lt;/p&gt;
  &lt;/div&gt;
  &lt;div id=&quot;chapter-body&quot; class=&quot;chapter-body&quot;&gt;
   &lt;p&gt;
    Random numbers are much useful when creating test files for our programs and generating random numbers looks like a magical task which only more advanced languages can do like java, python, perl etc. But you would like to hear that C also has the functions which can generate random numbers (pseudo-random actually). The main functions are &lt;code&gt;srand() and rand()&lt;/code&gt;. First of these is used to plant a seed to set range of random numbers. rand() is used to generate those random numbers. There are more functions to get random numbers. Here is the link for further reading about random numbers &lt;a class=&quot;source-link&quot; href=&quot;https://www.cs.cf.ac.uk/Dave/C/node16.html&quot; alt=&quot;Random Numbers using C&quot;&gt;Random Numbers using C&lt;/a&gt;.
   &lt;/p&gt;
  &lt;/div&gt;
 &lt;/div&gt;
 &lt;div id=&quot;string-conversion&quot; class=&quot;chapter&quot;&gt;
  &lt;div id=&quot;chapter-title&quot; class=&quot;chapter-title&quot;&gt;
   &lt;p&gt;String conversion&lt;/p&gt;
  &lt;/div&gt;
  &lt;div id=&quot;chapter-body&quot; class=&quot;chapter-body&quot;&gt;
   &lt;p&gt;We see in java how we can convert string to integer using new Integer(string). This can also be done using C &lt;code&gt;string.h&lt;/code&gt; library. There are many functions but lets see some of them. &lt;code&gt;atoi()&lt;/code&gt; converts string to integer. &lt;code&gt;atol()&lt;/code&gt; converts string to long int. Similarly we can convert character from upparcase to lowercase using &lt;code&gt;tolower()&lt;/code&gt; and from lowercase to upparcase using &lt;code&gt;touppar()&lt;/code&gt;. For details of string conversion and character testing i would prefer the same source i posted for random numbers. The next section to random numbers is string conversion.&lt;/p&gt;
  &lt;/div&gt;
 &lt;/div&gt;
 &lt;div id=&quot;process-system-command&quot; class=&quot;chapter&quot;&gt;
  &lt;div id=&quot;chapter-title&quot; class=&quot;chapter-title&quot;&gt;
   &lt;p&gt;Process Creation and System commands execution&lt;/p&gt;
  &lt;/div&gt;
  &lt;div id=&quot;chapter-body&quot; class=&quot;chapter-body&quot;&gt;
   &lt;p&gt;This is specific to Unix-like operating systems. In C you can create another process from within your C program and can hand over some task to it just like threads. But the created process will be totally independent of other processes. You can then apply Interprocess Communication techniques to get the result produced by the forked process. C is very unix friendly because Unix is totally built using C. Unix like operating systems like linux also supports this functionality and you can create another processes there too. The main functions related to process creation are &lt;code&gt;fork() wait() kill()&lt;/code&gt;.&lt;/p&gt;
   &lt;p&gt;Just like creating another process for random task, you can execute another file from within your program using &lt;code&gt;execv()&lt;/code&gt; and its variants. If you are using linux, ubuntu etc then just do &lt;code&gt;man exec&lt;/code&gt; and you will see other variants of execv(). For detailed description of process creation, i would suggest this documentation &lt;a alt=&quot;Process management in C&quot; href=&quot;http://www.gnu.org/software/libc/manual/html_node/Processes.html&quot;&gt;Process Management using C&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
 &lt;/div&gt;
 &lt;div id=&quot;database&quot; class=&quot;chapter&quot;&gt;
  &lt;div id=&quot;chapter-title&quot; class=&quot;chapter-title&quot;&gt;
   &lt;p&gt;DataBase Handling&lt;/p&gt;
  &lt;/div&gt;
  &lt;div id=&quot;chapter-body&quot; class=&quot;chapter-body&quot;&gt;
   &lt;p&gt;Okay so we have pulled ourselves into some serious stuff. Now the things i will be talking will be dependent upon some libraries, APIs etc. I am writing about all these because these things are fascinating and there is no harm in knowing about them. We can write programs in C which can connect to database and handle the data. As C is fast relative to other languages, this seems really helpful but there is one thing C lacks and that is faster string parsing. That is the reason we feel need for python, perl etc. because these languages provide faster and efficient string parsing functions. Still mysql API for C exist and can be used for database handling. Those who are interested can visit &lt;a href=&quot;https://dev.mysql.com/doc/refman/5.7/en/c-api.html&quot;&gt;MySQL website&lt;/a&gt; which contains the API documentation.&lt;/p&gt;
  &lt;/div&gt;
 &lt;/div&gt;
 &lt;div id=&quot;cgi-script&quot; class=&quot;chapter&quot;&gt;
  &lt;div id=&quot;chapter-title&quot; class=&quot;chapter-title&quot;&gt;&lt;p&gt;
   CGI Scripting
  &lt;/p&gt;&lt;/div&gt;
  &lt;div id=&quot;chapter-body&quot; class=&quot;chapter-body&quot;&gt;
   &lt;p&gt;CGI is the interface which makes it possible for any language to serve as a scripting language. ANy language that can handle strings can be used as a scripting language. Writing CGI scripts using C may seem to be really fun task but i have red somewhere that it is not secure so i don&#39;t think any website uses C to write CGI programs in C. Still we can do it and if you are interested to know then there is a library with which you can write CGI scripts in C. Here is the link to that library API &lt;a href=&quot;https://boutell.com/cgic/&quot; alt=&quot;CGI library for C&quot;&gt;CGI library for C&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
 &lt;/div&gt;
 &lt;div id=&quot;more-links&quot; class=&quot;chapter&quot;&gt;
  &lt;div id=&quot;chapter-title&quot; class=&quot;chapter-title&quot;&gt;
   &lt;p&gt;Some other Interesting Sources&lt;/p&gt;
  &lt;/div&gt;
  &lt;div id=&quot;chapter-body&quot; class=&quot;chapter-body&quot;&gt;
   &lt;p&gt;We saw so many things which we can accompalish using C but still there is more. I am posting some links to interesting C stuff&lt;/p&gt;
   &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;https://www.cs.cf.ac.uk/Dave/C/node1.html&quot;&gt;Everything about C and Unix specific things.&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;http://www.tutorialspoint.com/c_standard_library/&quot;&gt;All about C libraries&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;http://www.cprogramming.com/tutorial/rpc/remote_procedure_call_start.html&quot;&gt;Remote Procedure Calls (client-server)&lt;/a&gt;&lt;/li&gt;
   &lt;/ul&gt;
  &lt;/div&gt;
 &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description><link>http://fetch-info.blogspot.com/2016/03/see-more-of-c-programming-language.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1JVfYzS2_Nwo63KEgRb8UrrdARPHJhyREabwxNPnQMcUZsQslM10c4ANQO0Ku3C24XsAH6NbQ72Clb4B21gUbBTGHJMmAWGlK1E54ljbUvedYgy6GpFPJPz0dKlseOSqjZI7jDZfD5aRj/s72-c/PicsArt_1406950660777.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-8350095584415521887</guid><pubDate>Sun, 03 Jan 2016 13:53:00 +0000</pubDate><atom:updated>2016-08-12T05:54:34.616-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">commits</category><category domain="http://www.blogger.com/atom/ns#">git</category><category domain="http://www.blogger.com/atom/ns#">github</category><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">tricks</category><category domain="http://www.blogger.com/atom/ns#">ubuntu</category><title>Learn Git &amp; Github - 4</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot; id=&quot;lesson-4&quot;&gt;
&lt;style type=&quot;text/css&quot;&gt;
 .topics, #lesson-4 form{
  color: black;
  text-decoration: none;
  padding: 5%;
  background-color: #F5F5F5 ;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
 }
 .topics a{
  text-decoration: none;
  color: #F44336 ;
 }
&lt;/style&gt;
&lt;div id=&quot;lessons&quot;&gt;
&lt;select onChange=&quot;window.open(this.value, &#39;_blank&#39;)&quot;&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2015/12/learn-git-github-lesson-1.html&quot;&gt;Lesson 1&lt;/option&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2016/01/learn-git-github-lesson-2.html&quot;&gt;Lesson 2&lt;/option&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2016/01/learn-git-github-lesson-3.html&quot;&gt;Lesson 3&lt;/option&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2016/01/learn-git-github-lesson-4.html&quot;&gt;Lesson 4&lt;/option&gt;
&lt;/select&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
     var lesson = document.getElementById(&quot;lessons&quot;) ;
    var sel = lesson.getElementsByTagName(&quot;SELECT&quot;)[0] ;
        sel.value = window.location.href.split(&quot;?&quot;)[0] ;
&lt;/script&gt;
&lt;/div&gt;
&lt;h1&gt;Git Tree Navigation And Management: Lesson-4&lt;/h1&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrdKamWxJw6TRhO8E_ILlN9UcH_mWiPna_Ykdptkeq9wgxYjilHoVZDvzRUwq_D-n9o6tyjWQO-Cj-8Bb8Mvxdp9b0Zx4oO6n9BIwEIZCMFOtmi4RxcctGdiC8-EhnPGh2pOyvH99ESD2M/s1600/Git-Logo.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrdKamWxJw6TRhO8E_ILlN9UcH_mWiPna_Ykdptkeq9wgxYjilHoVZDvzRUwq_D-n9o6tyjWQO-Cj-8Bb8Mvxdp9b0Zx4oO6n9BIwEIZCMFOtmi4RxcctGdiC8-EhnPGh2pOyvH99ESD2M/s400/Git-Logo.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;topics&quot;&gt;&lt;b&gt;Topics&lt;/b&gt;
 &lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;#change-repo&quot;&gt;How to change remote repository&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#commit-navigation&quot;&gt;Commit navigation in git&lt;/a&gt;
   &lt;ol&gt;
    &lt;li&gt;&lt;a href=&quot;#jump-to-specific-commit&quot;&gt;How to jump to a specific commit&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#create-branch-from-commit&quot;&gt;How to create a branch from a speicific commit&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#edit-commit&quot;&gt;How to edit a specific commit&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#merge-commit&quot;&gt;How to merge commits&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#remove-commit&quot;&gt;How to delete commits&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#pick-move-commit&quot;&gt;How to pick/move commits from one branch to other&lt;/a&gt;&lt;/li&gt;
   &lt;/ol&gt;
  &lt;/li&gt;
 &lt;/ol&gt;
&lt;/div&gt;
&lt;h2 id=&quot;change-repo&quot;&gt;Changing Remote Repository&lt;/h2&gt;
&lt;p&gt;In the &lt;a href=&quot;http://fetch-info.blogspot.in/2016/01/learn-git-github-lesson-3.html&quot; target=&quot;_blank&quot;&gt;previous lesson&lt;/a&gt;, we learned how to make commits. If totally followed the lesson then i am sure you world have made some commits also. Now change your working directory to your project directory. Check you are on master and Use &lt;code&gt;git pull origin master&lt;/code&gt; to update your master. Since Now, we were syncing our local repository with our forked repository. But what we need is to keep updated with the original project repository. So we need to add the url of original project as our main remote repository. We can do so by typing &lt;code&gt;git remote add main https://github.com/kapilgarg1996/fetch-info-github.git&lt;/code&gt;. If you have forked another project then just add its clone url. Now type &lt;code&gt;git remote -v&lt;/code&gt; and you will notice that there are 2 remote repositories. One is origin which is your forked repo and another is main which is original project repo. You have control only over forked repo so you can &quot;push&quot; your changes to &quot;origin&quot; only. To update your forked with original project , you need to perform these 2 steps. Perform these steps when you are on your master branch&lt;/p&gt;
&lt;ol&gt;
 &lt;li&gt;Pull from main repo by &lt;code&gt;git pull main master&lt;/code&gt;&lt;/li&gt;
 &lt;li&gt;Push to your origin repo by &lt;code&gt;git push origin master&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This way, your local project and forked project will be in sync with the original project.&lt;/p&gt;
&lt;div id=&quot;mypostads&quot;&gt;&lt;/div&gt;
&lt;h2 id=&quot;commit-navigation&quot;&gt;Commits Navigation&lt;/h2&gt;
&lt;p&gt;There are times when you make many commits and then realise that somewhere back you made some wrong changes. Then you can go to that commit and edit that commit. Or If you want to start a branch from a specific previous commit, you can go to that commit and start a branch from there.I told you in previous lesson, how to find a commit id. But if you dont recall then go to previous lesson and read about commit.&lt;/p&gt;
&lt;h3 id=&quot;jump-to-specific-commit&quot;&gt;Jump to a specific commit&lt;/h3&gt;
&lt;p&gt;Before doing anything, create a test branch so that you can harmlessly run these commands. So i am assumming now that you have the id of the commit to which you want to go to. Now just type &lt;code&gt;git rebase -i &lt;i&gt;commit-id&lt;/i&gt;&lt;/code&gt;. This will bring you to that commit. It will open up an editor showing you the commits made after your given commit.Now lets what can we do in that editor.&lt;/p&gt;
&lt;h3 id=&quot;create-branch-from-commit&quot;&gt;Creating a branch from that commit&lt;/h3&gt;
&lt;p&gt;To create a branch from that commit, replace the word &quot;pick&quot; before that commit with &quot;edit&quot;. Not press &lt;code&gt;Ctrl + X&lt;/code&gt; and it will prompt to exit. Type &quot;y&quot; and press &quot;Enter&quot;. Now the HEAD is at the commit id you supplied. Type &lt;code&gt;git branch &lt;i&gt;branch-name&lt;/i&gt;&lt;/code&gt; to create branch. Now to bring HEAD back to latest commit or to continue the work flow, type &lt;code&gt;git rebase --continue&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;edit-commit&quot;&gt;Editing a Commit&lt;/h3&gt;
&lt;p&gt;Now lets see how to edit a specific commit.Editing a commit means editing the changes that commit introduced. To do so, just rebase to that commit id and replace &quot;pick&quot; with &quot;edit&quot; in the editor. Exit the editor and Now change whatever you want to change. Now you can either make multiples commits there or you can ammend that commit only by typing &lt;code&gt;git commit --amend&lt;/code&gt;. Ammending a commit will keep the same commit but new changes you put.Now again continue your work flow by &lt;code&gt;git rebase --continue&lt;/code&gt;&lt;/p&gt;
&lt;h3 id=&quot;merge-commit&quot;&gt;Merging 2 commits on 1&lt;/h3&gt;
&lt;p&gt;This is sometimes the case when you have created so many commits and want to merge some because some commits reflect only a change of a line or two. Lets see how to merge commits. Go to the specific commit and then the editor will open. The first commit in the editor will be your given commit. To merge the commits, replace &quot;pick&quot; with &quot;sqaush&quot; of the commits which you want to merge. Those commits will be merged with the previous commit whose command is &quot;pick&quot;. Then exit the editor but this time it will ask you to edit commit message if you want. Then continue your workflow and check your git log. You will see that the commits have been merged.&lt;/p&gt;
&lt;h3 id=&quot;remove-commit&quot;&gt;Removing/Deleting Commits&lt;/h3&gt;
&lt;p&gt;This feature reflects the real power of git. Git is made so that you can control what changes you need and what you don&#39;t. Also, reverting the changes should be simpler than a text editor. In git we can remove commits and can undo the changes that commit made so far. Let me tell you how to delete specific commit or whole commit range in git.Suppose you have commit history like:&lt;/p&gt;
&lt;ul style=&quot;list-style-type:none&quot;&gt;
 &lt;li&gt;commit id34&lt;/li&gt;
 &lt;li&gt;commit id33&lt;/li&gt;
 &lt;li&gt;commit id32&lt;/li&gt;
 &lt;li&gt;commit id31&lt;/li&gt;
 &lt;li&gt;commit id30&lt;/li&gt;
 &lt;li&gt;commit id29&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And you want to delete commits id31, id32. Lets suppose your branch is &quot;branch1&quot;. Now to delete those commits type &lt;code&gt;git rebase --onto id30 id32 branch1&lt;/code&gt;. Understand this code carefully because putting wrong commit ids can delete some important commits.&lt;/p&gt;
&lt;h3 id=&quot;pick-move-commit&quot;&gt;Pick/Move specific commit&lt;/h3&gt;
&lt;p&gt;Lets suppose that you have a fully working branch. Now you create a test branch to add some features. You made some important commits and then some not-so-important commits. Now if you want to add 1 or 2 specific commits of test branch on your master branch then there are two options. One is moving the commits to the master branch and second option is picking the commits from test and placing them on master. Lets have a look on both of them&lt;/p&gt;
&lt;ol&gt;
 &lt;li&gt;&lt;b&gt;Pick commits :&lt;/b&gt;You can use cherry-pick command. Just type &lt;code&gt;git cherry-pick &lt;i&gt;commit-id&lt;/i&gt;&lt;/code&gt;. It will place the commit specified on the top of your current working branch.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Moving Commits :&lt;/b&gt;You can move commits but before moving you must make sure that in the range of the commits, the last commit must be on top. For example, for this commit history :
 &lt;ul style=&quot;list-style-type:none&quot;&gt;
 &lt;li&gt;commit id34&lt;/li&gt;
 &lt;li&gt;commit id33&lt;/li&gt;
 &lt;li&gt;commit id32&lt;/li&gt;
 &lt;li&gt;commit id31&lt;/li&gt;
 &lt;li&gt;commit id30&lt;/li&gt;
 &lt;li&gt;commit id29&lt;/li&gt;
&lt;/ul&gt;
 If you want to move commits id31 and id32 then go to commit id32 and create a branch from there. Lets say the branch name is &quot;temp&quot;. Now temp will look like:
 &lt;ul style=&quot;list-style-type:none&quot;&gt;
 &lt;li&gt;commit id32&lt;/li&gt;
 &lt;li&gt;commit id31&lt;/li&gt;
 &lt;li&gt;commit id30&lt;/li&gt;
 &lt;li&gt;commit id29&lt;/li&gt;
&lt;/ul&gt;
 Now you can move id31 and id32 to master by typing &lt;code&gt;git rebase --onto master id31 temp&lt;/code&gt;
 &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This is all for this lesson and for this guide. Whatever you have learned in these four lessons is sufficient to handle git and github.If you come across any problem then you can dig into stackoverflow or drop a comment below. If you have come across a problem called &quot;merge conflict&quot; then i would recommend you to visit &lt;a href=&quot;https://help.github.com/articles/resolving-a-merge-conflict-from-the-command-line/&quot;&gt;This&lt;/a&gt; to see how to resolve merge conflicts. Also, i remind you to subscribe you to our newsletter which will update you about future posts directly by e-mail. So signup now..&lt;/p&gt;
&lt;br /&gt;
&lt;form style=&quot;&quot; action=&quot;http://feedburner.google.com/fb/a/mailverify&quot; method=&quot;post&quot; target=&quot;popupwindow&quot; onsubmit=&quot;window.open(&#39;http://feedburner.google.com/fb/a/mailverify?uri=blogspot/infogate&#39;, &#39;popupwindow&#39;, &#39;scrollbars=yes,width=550,height=520&#39;);return true&quot;&gt;&lt;p&gt;Enter your email address:&lt;/p&gt;&lt;p&gt;&lt;input placeholder=&quot;example@domain.com&quot; type=&quot;text&quot; name=&quot;email&quot;&gt;&lt;/p&gt;&lt;input type=&quot;hidden&quot; value=&quot;blogspot/infogate&quot; name=&quot;uri&quot;&gt;&lt;input type=&quot;hidden&quot; name=&quot;loc&quot; value=&quot;en_US&quot;&gt;&lt;input type=&quot;submit&quot; value=&quot;Subscribe&quot;&gt;&lt;/form&gt;&lt;br /&gt;
&lt;p&gt;If you face any problem then feel free to drop a comment below and i&#39;ll tell you a loophole.&lt;/p&gt;
&lt;br /&gt;&lt;/div&gt;</description><link>http://fetch-info.blogspot.com/2016/01/learn-git-github-lesson-4.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrdKamWxJw6TRhO8E_ILlN9UcH_mWiPna_Ykdptkeq9wgxYjilHoVZDvzRUwq_D-n9o6tyjWQO-Cj-8Bb8Mvxdp9b0Zx4oO6n9BIwEIZCMFOtmi4RxcctGdiC8-EhnPGh2pOyvH99ESD2M/s72-c/Git-Logo.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-8196413953381124719</guid><pubDate>Sat, 02 Jan 2016 14:28:00 +0000</pubDate><atom:updated>2016-08-12T05:54:20.261-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">git</category><category domain="http://www.blogger.com/atom/ns#">github</category><category domain="http://www.blogger.com/atom/ns#">Information</category><category domain="http://www.blogger.com/atom/ns#">Latest</category><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">tricks</category><category domain="http://www.blogger.com/atom/ns#">ubuntu</category><title>Learn Git &amp; Github - 3</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot; id=&quot;lesson-3&quot;&gt;
&lt;style type=&quot;text/css&quot;&gt;
 .topics, #lesson-3 form{
  color: black;
  text-decoration: none;
  padding: 5%;
  background-color: #f69a9a ;
 }
 .topics a{
  text-decoration: none;
  color: red ;
 }
        #lesson-3 #lesson-3-command li b{
                font-size: 20px ;
         }
        #lesson-3 #lesson-3-command li{
                padding: 5px ;
                margin : 5px ;
                border : 1px solid black ;
         }
&lt;/style&gt;
&lt;div id=&quot;lessons&quot;&gt;
&lt;select onChange=&quot;window.open(this.value, &#39;_blank&#39;)&quot;&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2015/12/learn-git-github-lesson-1.html&quot;&gt;Lesson 1&lt;/option&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2016/01/learn-git-github-lesson-2.html&quot;&gt;Lesson 2&lt;/option&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2016/01/learn-git-github-lesson-3.html&quot;&gt;Lesson 3&lt;/option&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2016/01/learn-git-github-lesson-4.html&quot;&gt;Lesson 4&lt;/option&gt;
&lt;/select&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
     var lesson = document.getElementById(&quot;lessons&quot;) ;
    var sel = lesson.getElementsByTagName(&quot;SELECT&quot;)[0] ;
        sel.value = window.location.href.split(&quot;?&quot;)[0] ;
&lt;/script&gt;
&lt;/div&gt;
&lt;h1&gt;Git Commands: Lesson-3&lt;/h1&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrdKamWxJw6TRhO8E_ILlN9UcH_mWiPna_Ykdptkeq9wgxYjilHoVZDvzRUwq_D-n9o6tyjWQO-Cj-8Bb8Mvxdp9b0Zx4oO6n9BIwEIZCMFOtmi4RxcctGdiC8-EhnPGh2pOyvH99ESD2M/s1600/Git-Logo.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrdKamWxJw6TRhO8E_ILlN9UcH_mWiPna_Ykdptkeq9wgxYjilHoVZDvzRUwq_D-n9o6tyjWQO-Cj-8Bb8Mvxdp9b0Zx4oO6n9BIwEIZCMFOtmi4RxcctGdiC8-EhnPGh2pOyvH99ESD2M/s400/Git-Logo.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;topics&quot;&gt;&lt;b&gt;Topics&lt;/b&gt;
 &lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;#what-we-learned&quot;&gt;What we learned in previous lesson&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#git-terms&quot;&gt;Git Terminology&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#git-commands&quot;&gt;Git Commands for project development&lt;/a&gt;&lt;/li&gt;
 &lt;/ol&gt;
&lt;/div&gt;
&lt;h2 id=&quot;what-we-learned&quot;&gt;What we learned in Previous Lesson&lt;/h2&gt;
&lt;p&gt;In the &lt;a href=&quot;http://fetch-info.blogspot.in/2016/01/learn-git-github-lesson-2.html&quot; target=&quot;_blank&quot;&gt;previous lesson&lt;/a&gt;, we learned about github and how can we use it along with git to create an open source project(A project which is free to use and distribute). We set up our project on github and cloned it in our machine to modify it locally(On our machine only). Now either you have cloned fork of an existing project or your own project, the development process is nearly the same. I&#39;ll point out the difference wherever applicable.&lt;/p&gt;
&lt;h2 id=&quot;git-terms&quot;&gt;Git Terminology&lt;/h2&gt;
&lt;p&gt;In first lesson, we talked about 3 most occuring terms of git which are branch, commit and head. Now Lets talk about some more terms which are associated with git&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;&lt;b&gt;Index :&lt;/b&gt;Index is the current state of the project for git. If you have changed some files then you will have to add them to git index so that git can track those files for future changes. If you don&#39;t add some file to index then it won&#39;t be tracked. Also it is necessary to add files to index to make commits to those files.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Push :&lt;/b&gt;Pushing means sending the changes we made on our machine in our project to the hosting service or remote repository which is our github repository. So what does it actually mean that if we changed some files of our project and we want to upload the changes to github then it is called pushing to the remote.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Pull :&lt;/b&gt;Pulling means getting the current state of the remote branch. There can be lots of branches in the github project. If a team is working on a project and some developers have brought changes to some files of a branch then you can update your local project(Project on your machine) by pulling those changes. So if you have forked an existing project then what you do to keep your branches updated is you pull the changes from the original project branch to your local project branch then you push those changes to your github forked project.&lt;br/&gt;&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Rebase :&lt;/b&gt; Rebasing means putting your commits over the main project commits. For example, if you are editing some files and make some commits but before you push them to your github project some team member has updated original github project then what you would want will be to put your changes on the top to prevent conflicts between your changes and other changes which that team member brought. So in this case we pull that developers&#39;s changes with a rebase which will update our local project with the main project and then put our local commits on top of commit history. Now when you will push your changes then your changes will be the most recent ones.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There can be some other terms also which you may notice while searching for a answer but those terms are actually the command names which we&#39;ll learn in the next section.&lt;/p&gt;
&lt;div id=&quot;mypostads&quot;&gt;&lt;/div&gt;
&lt;h2 id=&quot;git-commands&quot;&gt;Git Commands&lt;/h2&gt;
&lt;p&gt;Git is actually a command line software which means you will type commands in terminal or git-bash to tell what to do to git. So to use git, you must be familiar with many of its commands. In this section, i&#39;ll tell you some git commands with which we&#39;ll continue our project development. If you have set up your project on your machine then you are ready to go but if you have not then i&#39;ll advise you to go to previous lesson and set it up first.Also make sure that your present working directory is your project directory otherwise running the following commands will throw error. Also i recommend you to fork and clone &lt;a href=&quot;https://github.com/kapilgarg1996/fetch-info-github&quot; target=&quot;_blank&quot;&gt;fetch-info-github&lt;/a&gt; repository so that you can make sure that nothing gets messed up and i can visualise what you are seeing right now.&lt;/p&gt;
&lt;p&gt;Before we start, lets set our usename and email on git by typing &lt;code&gt;git config --global user.name &lt;i&gt;your-name&lt;/i&gt;&lt;/code&gt; and &lt;code&gt;git config --global user.email &lt;i&gt;your-email&lt;/i&gt;&lt;/code&gt;&lt;/p&gt;
&lt;ul id=&quot;lesson-3-command&quot;&gt;
 &lt;li&gt;
  &lt;b&gt;branch :&lt;/b&gt;We have cloned the project. So first lets see what are branches do we have initially. The command &lt;code&gt;git branch&lt;/code&gt; will list the branches which are present in out working tree. Currently there should be only master branch. So lets create one for development purposes. You should not change the master branch directly because you can have it as reference point from where to continue the work. Always create a new branch for some further development. Make commits to that branch. If everything works fine then merge that branch with master and push it or push it to some other branch on github and merge it there with master and pull the remote master to your local master. But for now, lets just create a branch by typing &lt;code&gt;git branch -l &lt;i&gt;any-branch-name&lt;/i&gt;&lt;/code&gt;. Now run &lt;code&gt;git branch&lt;/code&gt; again and you will see that there is your new branch now along with your master. You can delete a branch by typing &lt;code&gt;git branch -d &lt;i&gt;branch-name&lt;/i&gt;&lt;/code&gt;
 &lt;/li&gt;
 &lt;li&gt;
  &lt;b&gt;checkout :&lt;/b&gt;Checkout is used to navigate your git tree. It can help you switch your working branch, create a new branch and move between commits. Here we&#39;ll limit ourself to only switching and creating branch. Moving between commits and doing stuff there will be covered in next lesson. First check your working branch by &lt;code&gt;git branch&lt;/code&gt;. Working branch will be colored. Now switch to some other branch by typing &lt;code&gt;git checkout &lt;i&gt;branch-name&lt;/i&gt;&lt;/code&gt;. Again check your working branch. You can also create branches with checkout command by typing &lt;code&gt;git checkout -b &lt;i&gt;new-branch-name&lt;/i&gt;&lt;/code&gt;. So far so good.
 &lt;/li&gt;
 &lt;li&gt;
  &lt;b&gt;log and reflog :&lt;/b&gt;Log and Reflog commands are used to see the git history. Type &lt;code&gt;git log&lt;/code&gt; and it will show the commits made so far along with their authors and time stamp. If you want to see associated file names then type &lt;code&gt;git log --name-status&lt;/code&gt;. To see the movement of HEAD pointer you can view reference log by typing &lt;code&gt;git reflog&lt;/code&gt;
 &lt;/li&gt;
 &lt;li&gt;
  &lt;b&gt;status :&lt;/b&gt;Status command tells you the current status whether there are some untracked files(those files which haven&#39;t been added to git index) or any merge conflicts. To check status type &lt;code&gt;git status&lt;/code&gt;
 &lt;/li&gt;
 &lt;li&gt;
  &lt;b&gt;add :&lt;/b&gt;Now lets start some real stuff. Create a branch named &lt;i&gt;&quot;test&quot;&lt;/i&gt; by checkout command. Check your working branch and it must be &lt;i&gt;&quot;test&quot;&lt;/i&gt;. Now create any file you want or open any file you want to modify. Change some code or write some code in new file and save the file. Now check status of your project and this time you will see some red lines telling you that your modified file or created file hasn&#39;t been added to tracked list. To add that file type &lt;code&gt;git add &quot;file name&quot;&lt;/code&gt;. You can also add the whole directory to index by typing &lt;code&gt;git add .&lt;/code&gt;. Now again check the status. It will show that files have been added to index but changes haven&#39;t been commited yet.
 &lt;/li&gt;
 &lt;li&gt;
  &lt;b&gt;commit :&lt;/b&gt;Now lets commit our changes. Type &lt;code&gt;git commit -m &lt;i&gt;any-message-or-change-description&lt;/i&gt;&lt;/code&gt;. NOw check status again and it will show that everything is fine. Check your log by log command and you&#39;ll see that your commit is on the top of history. The first 7 characters of colored line in git log is the commit id. For example, if you see something like &lt;i&gt;commit 18b67246fa50778bd64fe0462bce45357fc5abd5&lt;/i&gt; then your commit id is &lt;i&gt;18b6724&lt;/i&gt;.
 &lt;/li&gt;
 &lt;li&gt;
  &lt;b&gt;pull :&lt;/b&gt;Okay so we have made our first commit. But we are not sure whether we have the updated version of the branch or not. To update the project, we will pull the remote project changes by typing &lt;code&gt;git pull --rebase origin master&lt;/code&gt; This way our project will be synced with the remote project and our commits will be on top if there are no merge conflicts. Merge conflicts resolution will be covered in later lesson.
 &lt;/li&gt;
 &lt;li&gt;
  &lt;b&gt;push :&lt;/b&gt; Now lets push our commits to the remote project. In your case, your forked project. We do so by typing &lt;code&gt;git push origin &lt;i&gt;branch-name&lt;/i&gt;&lt;/code&gt;. Then you&#39;ll be prompted with github username and password then it will upload your changes to desired branch.
 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Finally we have made some commits and have pushed them to remote project. Now if you have created a new project then your changes will be in your branch and you can merge them with master directly on github. But if have a forked project then the changes will be uploaded to the branch of your forked project. To let the developers of original project know about your changes and to request them to add those changes on their original project, you&#39;ll have to create a pull request directly on github. All these minute details will be covered in next lesson and we&#39;ll also learn to navigate git tree so that we can precisely control our project versions. So stay with us. You can subscribe to our newsletter just by entering your e-mail address and you&#39;ll get notified about the next lesson by your e-mail directly. So what are you waiting for..Sign up Now...&lt;/p&gt;
&lt;br /&gt;
&lt;form style=&quot;&quot; action=&quot;http://feedburner.google.com/fb/a/mailverify&quot; method=&quot;post&quot; target=&quot;popupwindow&quot; onsubmit=&quot;window.open(&#39;http://feedburner.google.com/fb/a/mailverify?uri=blogspot/infogate&#39;, &#39;popupwindow&#39;, &#39;scrollbars=yes,width=550,height=520&#39;);return true&quot;&gt;&lt;p&gt;Enter your email address:&lt;/p&gt;&lt;p&gt;&lt;input placeholder=&quot;example@domain.com&quot; type=&quot;text&quot; name=&quot;email&quot;&gt;&lt;/p&gt;&lt;input type=&quot;hidden&quot; value=&quot;blogspot/infogate&quot; name=&quot;uri&quot;&gt;&lt;input type=&quot;hidden&quot; name=&quot;loc&quot; value=&quot;en_US&quot;&gt;&lt;input type=&quot;submit&quot; value=&quot;Subscribe&quot;&gt;&lt;/form&gt;&lt;br /&gt;
&lt;b&gt;Please don&#39;t forget to respond to confirmation mail sent to you for this subscription. If you don&#39;t click the link sent to you, you won&#39;t be subscribed.&lt;/b&gt;
&lt;p&gt;If you have any doubt or need any help then just a comment below.&lt;/p&gt;
&lt;br /&gt;&lt;/div&gt;</description><link>http://fetch-info.blogspot.com/2016/01/learn-git-github-lesson-3.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrdKamWxJw6TRhO8E_ILlN9UcH_mWiPna_Ykdptkeq9wgxYjilHoVZDvzRUwq_D-n9o6tyjWQO-Cj-8Bb8Mvxdp9b0Zx4oO6n9BIwEIZCMFOtmi4RxcctGdiC8-EhnPGh2pOyvH99ESD2M/s72-c/Git-Logo.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-8461578077798260316</guid><pubDate>Fri, 01 Jan 2016 15:31:00 +0000</pubDate><atom:updated>2016-08-12T05:54:01.445-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">git</category><category domain="http://www.blogger.com/atom/ns#">github</category><category domain="http://www.blogger.com/atom/ns#">Information</category><category domain="http://www.blogger.com/atom/ns#">Latest</category><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">linux</category><category domain="http://www.blogger.com/atom/ns#">tricks</category><category domain="http://www.blogger.com/atom/ns#">ubuntu</category><title>Learn Git &amp; Github - 2</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;style type=&quot;text/css&quot;&gt;
 .topics{
  color: black;
  padding: 5%;
  background-color: #f69a9a ;
 }
        .topics a{
                text-decoration:none ;
                color : red ;
}
&lt;/style&gt;
&lt;div id=&quot;lessons&quot;&gt;
&lt;select onChange=&quot;window.open(this.value, &#39;_blank&#39;)&quot;&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2015/12/learn-git-github-lesson-1.html&quot;&gt;Lesson 1&lt;/option&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2016/01/learn-git-github-lesson-2.html&quot;&gt;Lesson 2&lt;/option&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2016/01/learn-git-github-lesson-3.html&quot;&gt;Lesson 3&lt;/option&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2016/01/learn-git-github-lesson-4.html&quot;&gt;Lesson 4&lt;/option&gt;
&lt;/select&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
    var lesson = document.getElementById(&quot;lessons&quot;) ;
    var sel = lesson.getElementsByTagName(&quot;SELECT&quot;)[0] ;
        sel.value = window.location.href.split(&quot;?&quot;)[0] ;
&lt;/script&gt;
&lt;/div&gt;
&lt;h1&gt;Learn Github: Lesson-2&lt;/h1&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN5wKTaa1rRb1Qxz_V7CJnG1Kd0UHSAwR-ucYcPus80jZr9_xg8FiCZAqmW3n46NWmEc83XQh1Fo7GPCGzxFnFnx6x5nW-4t6VOnaR0x1hYtvyM5fEOsUNt0RQ2ra0PRVrwC3VyZ6eo62C/s1600/github.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN5wKTaa1rRb1Qxz_V7CJnG1Kd0UHSAwR-ucYcPus80jZr9_xg8FiCZAqmW3n46NWmEc83XQh1Fo7GPCGzxFnFnx6x5nW-4t6VOnaR0x1hYtvyM5fEOsUNt0RQ2ra0PRVrwC3VyZ6eo62C/s320/github.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;topics&quot;&gt;&lt;b&gt;Topics&lt;/b&gt;
 &lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;#difference-git-github&quot;&gt;Difference between Git and Github&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#start-with-github&quot;&gt;Geting started with github&lt;/a&gt;
   &lt;ol&gt;
    &lt;li&gt;&lt;a href=&quot;#work-github-project&quot;&gt;Work on existing github project&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#new-github-project&quot;&gt;Creating a new project on github&lt;/a&gt;&lt;/li&gt;
   &lt;/ol&gt;
  &lt;/li&gt;
 &lt;/ol&gt;
&lt;/div&gt;
&lt;h2 id=&quot;difference-git-github&quot;&gt;Difference between git and github&lt;/h2&gt;
&lt;p&gt;In the &lt;a href=&quot;http://fetch-info.blogspot.in/2015/12/learn-git-github-lesson-1.html&quot; target=&quot;_blank&quot;&gt;first lesson&lt;/a&gt; we learned how git works, Now we will see what is github. People often get confused between git and github. These two are entirely different things providing the same functionality. The major difference between git and github is that git works on your machine and github is a cloud service. You can see github as a cloud version of github where an entire team can work on a project. So there is not a point of choosing either one of them. You use git only if you have a project on which you are working alone and then you use github along with git if you are handling a team project. Its not mandatory to have a team to use github. If you want to share your code to people then you can push/upload it to github also. Don&#39;t worry about getting it changed by developers because the project will be in your hands and you will be the person to choose which changes should your project accept.&lt;/p&gt;
&lt;h2 id=&quot;start-with-github&quot;&gt;Getting started with Github&lt;/h2&gt;
&lt;p&gt;Lets see how can we use github. There are 2 reasons to get to github.&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;Either you want to work on an existing github project&lt;/li&gt;
 &lt;li&gt;You want to start your own project and want to share it with the world.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;work-github-project&quot;&gt;Work on existing github project&lt;/h3&gt;
&lt;p&gt;So we&#39;ll consider the first reason first. Suppose you want to work on an existing project. Then you&#39;ll definitely have your desired project url but in case you haven&#39;t decided yet then you can go to this &lt;a href=&quot;https://github.com/kapilgarg1996/fetch-info-github&quot; title=&quot;github repo&quot; target=&quot;_blank&quot;&gt;Fetch-info Github Tutorial&lt;/a&gt; repository (Open it in a new tab so you can go along). Every project uploaded on github is called a repository. Now when you have opened the wep page. The first thing to do is &quot;Fork the project&quot; by clicking a button &quot;Fork&quot; on right side in the top &lt;a href=&quot;#buttons&quot;&gt;See Image&lt;/a&gt;. When you fork a project, github creates a copy of that project under your account which you can modify anytime but those changes won&#39;t reflect in the original project.&lt;/p&gt;
&lt;p&gt;Lets assume that you have forked the project and now the code url will be something like &lt;i&gt;https://github.com/your-username/fetch-info-github&lt;/i&gt;.Now check a text field containing a link aside a button &quot;Download Zip&quot; &lt;a href=&quot;#buttons&quot;&gt;See Image&lt;/a&gt;. Copy the link from the field. Its the link from which you can download that project on your machine. Downloading a project is called cloning in github terminology.&lt;/p&gt;
&lt;p&gt;Once you have copied the link, open your terminal/git-bash on your machine and go the directory where you want to download your project. Now type &lt;code&gt;git clone &lt;i&gt;your-link&lt;/i&gt;&lt;/code&gt;. Now git will download the project along with the edit history of that project which means all of its commits history. Go to your forked project webpage and see a drop-down menu in left with label &quot;branch&quot; &lt;a href=&quot;#buttons&quot;&gt;See Image&lt;/a&gt;. If you click it then you will see that there are more than one branch. One branch being the master and other being alpha if you have forked my dummy repository. Now go back to terminal and change your directory to your project name which git downloaded.&lt;/p&gt;
&lt;p&gt;Once inside the directory, type &lt;code&gt;git branch&lt;/code&gt;. You will notice that there is only one branch &quot;master&quot;. Its advantageous becuase all other branches are normally either for testing or development. The branch which matters is &quot;master&quot;. So you have your project set up on your machine. You can type &lt;code&gt;git log&lt;/code&gt; to see what commits have been happened. This is just set up. Main development on that project will continue later when we&#39;ll be discussing about git commands so sit tight. Also these steps are for people who want to contribute to existing projects. Those who want to create a new project read the next section.&lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCLQ6shqsSRbGXiHXJLgKC4Q3pPVsan9cPflZ4Cz9bnssEm-h4OQyTm3t6dt7Vf0eydmek3M8aeOLqXZ0Gr9iw_20lxqf0UHI3suHL3fzzskwYXrFZRQhoq4R3CH0DJXEX-40lAuCKX-Fd/s1600/buttons.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img id=&quot;buttons&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCLQ6shqsSRbGXiHXJLgKC4Q3pPVsan9cPflZ4Cz9bnssEm-h4OQyTm3t6dt7Vf0eydmek3M8aeOLqXZ0Gr9iw_20lxqf0UHI3suHL3fzzskwYXrFZRQhoq4R3CH0DJXEX-40lAuCKX-Fd/s400/buttons.png&quot; alt=&quot;github-buttons&quot;/&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h3 id=&quot;new-github-project&quot;&gt;Creating a new project on github&lt;/h3&gt;
&lt;p&gt;Creating a new project on github is not a difficult task to do. Just click the &quot;+&quot; button on the top right corner of the webpage aside your &quot;profile pic&quot; &lt;a href=&quot;#buttons&quot;&gt;See Image&lt;/a&gt;. There you will be presented with many options but we&#39;ll click &quot;New Repository&quot; because we want to create a project. When you will click the &quot;New Repository&quot; button, a page will open which will ask for project name. Enter the name which doesn&#39;t exist earlier. Now add description of your project. Public repositories is our option because we haven&#39;t paid for the private service. Now if you want a Readme file in your project which will contain the description then click &quot;Initialise with Readme&quot;. Choosing a licence is very important if you are doing some series development. Here is something which can help you &lt;a href=&quot;http://choosealicense.com/licenses/&quot;&gt;Choose License&lt;/a&gt; But if you are doing some basic development then you can choose MIT license which distribute your project for research purposes. &lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjs5bFXFSCGs-MWXg5b62c7AkljQtDTqDeNPaWuKnHORBMxmMV_hWI29WUQm8DZumbCa0jc7GK6FqRAonInYi0qYk7psryHYGF6VItwn2r4-ydcWbBcPhPrYR0RZnD5POYXvZI2CUax-6v9/s1600/create.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjs5bFXFSCGs-MWXg5b62c7AkljQtDTqDeNPaWuKnHORBMxmMV_hWI29WUQm8DZumbCa0jc7GK6FqRAonInYi0qYk7psryHYGF6VItwn2r4-ydcWbBcPhPrYR0RZnD5POYXvZI2CUax-6v9/s400/create.png&quot; alt=&quot;github-create-repo-screen&quot;/&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;When you have done all this then click &quot;Create Repository&quot;. Then you will have your project. Now check a text field containing a link aside a button &quot;Download Zip&quot;. Copy the link from the field. Its the link from which you can download that project on your machine. Downloading a project is called cloning in github terminology.&lt;/p&gt;
&lt;p&gt;Once you have copied the link, open your terminal/git-bash on your machine and go the directory where you want to download your project. Now type &lt;code&gt;git clone &lt;i&gt;your-link&lt;/i&gt;&lt;/code&gt;. Now git will download the project along with the edit history of that project which means all of its commits history. This is just set up. Main development on that project will continue later when we&#39;ll be discussing about git commands so keep following this guide.&lt;/p&gt;
&lt;br/&gt;
&lt;p&gt;Okay folks. Now you have set up your project. In the next lesson, we will study some basic git commands and will use them to develop our project further.&lt;/p&gt;
&lt;br /&gt;&lt;/div&gt;</description><link>http://fetch-info.blogspot.com/2016/01/learn-git-github-lesson-2.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN5wKTaa1rRb1Qxz_V7CJnG1Kd0UHSAwR-ucYcPus80jZr9_xg8FiCZAqmW3n46NWmEc83XQh1Fo7GPCGzxFnFnx6x5nW-4t6VOnaR0x1hYtvyM5fEOsUNt0RQ2ra0PRVrwC3VyZ6eo62C/s72-c/github.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-7562127405762814030</guid><pubDate>Thu, 31 Dec 2015 09:01:00 +0000</pubDate><atom:updated>2016-01-04T08:23:07.524-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Computer</category><category domain="http://www.blogger.com/atom/ns#">git</category><category domain="http://www.blogger.com/atom/ns#">github</category><category domain="http://www.blogger.com/atom/ns#">Latest</category><category domain="http://www.blogger.com/atom/ns#">learn</category><category domain="http://www.blogger.com/atom/ns#">linux</category><category domain="http://www.blogger.com/atom/ns#">tricks</category><category domain="http://www.blogger.com/atom/ns#">Web</category><title>Learn Git &amp; Github</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;style type=&quot;text/css&quot;&gt;
 .topics{
  color: black;
  text-decoration: none;
  padding: 5%;
  background-color: #f69a9a ;
                border: 1px solid black ;
                margin : 5%;
 }
&lt;/style&gt;
&lt;div id=&quot;lessons&quot;&gt;
&lt;select onChange=&quot;window.open(this.value, &#39;_blank&#39;)&quot;&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2015/12/learn-git-github-lesson-1.html&quot;&gt;Lesson 1&lt;/option&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2016/01/learn-git-github-lesson-2.html&quot;&gt;Lesson 2&lt;/option&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2016/01/learn-git-github-lesson-3.html&quot;&gt;Lesson 3&lt;/option&gt;
&lt;option value=&quot;http://fetch-info.blogspot.in/2016/01/learn-git-github-lesson-4.html&quot;&gt;Lesson 4&lt;/option&gt;
&lt;/select&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
     var lesson = document.getElementById(&quot;lessons&quot;) ;
    var sel = lesson.getElementsByTagName(&quot;SELECT&quot;)[0] ;
        sel.value = window.location.href.split(&quot;?&quot;)[0] ;
&lt;/script&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h1&gt;Learn Git : Lesson 1&lt;/h1&gt;
&lt;h2&gt;Why Git or any VCS&lt;/h2&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;figure&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7CqEjH99BvKJi1mgdwrkAG_HhlGsLPPvSwH0fyJmSur2hwbCX7rO0DEC0ZRLinSCC2myHXc2YMLDW6Rm1KF8_Nowzd6UHeiXJ7CeIiU9g6HXltsgikNZg1tN_acaCIgzTN2RP-fpE5wgs/s320/Git-Logo.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;git logo&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7CqEjH99BvKJi1mgdwrkAG_HhlGsLPPvSwH0fyJmSur2hwbCX7rO0DEC0ZRLinSCC2myHXc2YMLDW6Rm1KF8_Nowzd6UHeiXJ7CeIiU9g6HXltsgikNZg1tN_acaCIgzTN2RP-fpE5wgs/s320/Git-Logo.png&quot; /&gt;&lt;/a&gt;&lt;figcaption&gt;Image from &lt;a href=&quot;https://git-scm.com/images/logos/downloads/Git-Logo-1788C.png&quot;&gt;git-scm&lt;/a&gt;&lt;/figcaption&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;Version control system is a software which maintains the records of changes done to a project and also the states of the project which you can switch anytime. As a programmer, it sometimes happens to me when i write a piece of code and it works but when i try to optimize it then it gets broken. Also i could not be able to traceback the changes and get my working code back. If this happens to you also then you might undo tens of times to get back the previous code. &lt;/p&gt;
&lt;p&gt;But what will you do if your project contains multiple files with hundred lines of code in each file. If you now brong some changes to more than one file then its not easily possible to get back the working code. Here comes the importance of a version control system. In a VCS, you can save the current state and continue to work on a different state but it doesn&#39;t mean that vcs creates a copy of the current state. No. It maintains the record of the changes bring to a project. It maintains a history which you can navigate to get what state you want. &lt;/p&gt;
&lt;h2&gt;About this guide&lt;/h2&gt;
&lt;p&gt;Git has becoming very popular nowadays as the open source softwares are emerging in the market so a lot of developers want to know what exactly git is. Not only knowing about the git, learning and understanding it is also needed to develop large softwares/products with a team. There are many tutorials, guides, video lectures are available to learn git then why this one more guide ? The reason i am writing this guide is that after learning git from various guides, we&#39;ll still be wandering stackoverflow to get help for our unique scenarios. So what&#39;s wrong with other guides ?
&lt;br/&gt;All other guides lack one common thing, User perspective. Many of the guides are written to provide the theoretical knowledge of git commands but don&#39;t tell the other uses of the same command. In this guide, i&#39;ll not only cover the git basic commands, but will also tell you various tips and tricks to get your task done in git. You won&#39;t just get to know about git commands but will understand what these commands do and can do. Not only the commands, how git works is also important to understand.&lt;br/&gt;
So this guide will make you fall in love with git. Its not that other VCS are not good but i know about only git and other VCS also work in the same way but the syntax gets different. Below is the list of topics, i&#39;ll be covering.&lt;/p&gt;
&lt;br/&gt;
&lt;div class=&quot;topics&quot;&gt;&lt;b&gt;Topics&lt;/b&gt;
 &lt;ol&gt;
  &lt;li&gt;How Git works&lt;/li&gt;
  &lt;li&gt;Get yourself on Github&lt;/li&gt;
  &lt;li&gt;Using commands the smart way&lt;/li&gt;
 &lt;/ol&gt;
&lt;/div&gt;
&lt;h2&gt;How git works&lt;/h2&gt;
&lt;p&gt;
 Git and many vcs have such terms in their terminology like branches, commits, head, master etc. We&#39;ll use these terms and will see how actually git works. 
&lt;/p&gt;
&lt;ol&gt;
 &lt;li&gt;&lt;b&gt;Branch :&lt;/b&gt;A branch is a state of the project on which you are working. For example, suppose you create a project and name it P1.0. Now you want to modify some code without breaking the working code then what you do is you create a branch and name is what you want. Now whatever changes you bring to that branch does not effect the branch P1.0. In this way you can safely work on your differnt branches without breaking anything. &lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Commit :&lt;/b&gt;Saving a change is called commit. If you change something in your branch then you should commit/save it so that it gets recorded it git history. An important advice:- Commits should be small changes. If you have changed a lot in your project and you commit it. Then you find out that it has broken the code then you&#39;ll have to write the whole changes again to fix it. Instead of doing large commits, you should change small, commit it, then change small, commit it and so on. This way you can navigate git history to get to the commit where the code seems to broken. 10 smaller commits are much much better than 1 large commit.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Head :&lt;/b&gt;Git maintains a pointer called HEAD to the current working branch and commit. When you navigate the git history then what you do is you actually change this HEAD pointer to that branch or that commit. So HEAD is just a pointer.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Git Tree&lt;/h2&gt;
&lt;p&gt;Now lets have a look at the git tree.&lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBg1eFfivAciJszqrSi3YDxyFXanhLin1ddcpHvaj5IO7MCRUEj3lJ0B3VSjjZolY7OQ_Rx0bxVtEqB0aEGauWvqvHrsm5MmEfUQ3TQJ6bfyshXBfiuPjQZeIn_IYlc1GSSRScA0NC3SAI/s1600/git-tree.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; alt=&quot;Git Tree&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBg1eFfivAciJszqrSi3YDxyFXanhLin1ddcpHvaj5IO7MCRUEj3lJ0B3VSjjZolY7OQ_Rx0bxVtEqB0aEGauWvqvHrsm5MmEfUQ3TQJ6bfyshXBfiuPjQZeIn_IYlc1GSSRScA0NC3SAI/s400/git-tree.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;In this tree, you can see that the nodes of this tree are commits which you do in a branch. There you can see 3 branches. In this tree i have a growing branch which has stable versions of my project which are 1.0, 1.2 and 1.6. Then there are 2 branches for testing phase of my versions which are 1.0-test and 1.2-test. The main thing about git is that you can jump between branches and commits whenever you want. In the 3rd lesson, i will tell you how to navigate git history. So stay tuned.&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;</description><link>http://fetch-info.blogspot.com/2015/12/learn-git-github-lesson-1.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7CqEjH99BvKJi1mgdwrkAG_HhlGsLPPvSwH0fyJmSur2hwbCX7rO0DEC0ZRLinSCC2myHXc2YMLDW6Rm1KF8_Nowzd6UHeiXJ7CeIiU9g6HXltsgikNZg1tN_acaCIgzTN2RP-fpE5wgs/s72-c/Git-Logo.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-4768735495541155578</guid><pubDate>Sun, 20 Dec 2015 15:07:00 +0000</pubDate><atom:updated>2015-12-20T07:10:49.595-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Information</category><category domain="http://www.blogger.com/atom/ns#">Latest</category><category domain="http://www.blogger.com/atom/ns#">programming</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Python : Learn it my way</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHvOtXGlXyacZ9jtPBa4NBO1a2nJ7xmgABYz1_6W74JlNGTfFy2-aTAVa8eInIORKG1IN6RFZAd727S2Xdfmo87MtoY92ujdDXIDGYOKgbfRHevvyL0mpwagjEg7xugCOXQ8Gx-6hWub-g/s1600/python-logo.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHvOtXGlXyacZ9jtPBa4NBO1a2nJ7xmgABYz1_6W74JlNGTfFy2-aTAVa8eInIORKG1IN6RFZAd727S2Xdfmo87MtoY92ujdDXIDGYOKgbfRHevvyL0mpwagjEg7xugCOXQ8Gx-6hWub-g/s320/python-logo.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;h2&gt;Why Python&lt;/h2&gt;&lt;p&gt;The first question we should ask ourself before learning something is &lt;i&gt;&quot;Why should we learn it&quot;&lt;/i&gt;.I don&#39;t know about other stuff but i can answer why should you learn python. Python is a scripting as well as programming language. What does that mean is we can use it for web servers, softwares etc. Also it has a vast open source community behind it which provides thousands of libraries, frameworks and applications based on python.Today, development is moving forward to use languages like python, perl , ruby etc. So if we want to keep ourself updated with latest technologies then we&#39;ll have to learn them.Python also becomes handy for GUI development.Learning python is considered to be easier than any other object oriented language.&lt;/p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;Is this a python tutorial ?&lt;/h2&gt;&lt;p&gt;Obviously not. This is not a python tutorial. I am just gonna show you the right path to get yourself into python. When i first faced python(during sublime plugin development), it seems so scary that i skipped the plugin development and moved to something else. But recently i found out some useful resources to learn python from and it took me a week to learn python sufficient for software development and obviously coding.So if you want to find out the right tutorials and stuff to learn python then keep reading otherwise you can skip all of this post.&lt;/p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;Particular Resources&lt;/h2&gt;&lt;p&gt;There are thousands of tutorials on the internet about the language but some of them don&#39;t start from the right point. Also finding right books is a hard task to do. So the first step you are gonna do which i did, just visit google python class web page and there you will learn the basics of pyhton in just one day.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;https://developers.google.com/edu/python/?hl=en&quot;&gt;Google Python Class&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Then the next step is optional, you can attend codecademy course to practise what you learned at google python class. In that course you will come to learn about functions and classes as well.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;https://www.codecademy.com/learn/python&quot;&gt;Codecademy Course&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Then the main part comes in. Getting a really good book or e-book. There are many books out there but many of them are just not so good. The books which i studied are:-&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Learn Python the Hard Way&lt;/li&gt;
&lt;li&gt;Dive into Python&lt;/li&gt;
&lt;li&gt;Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;br /&gt;
All these books are good to read but the 3rd one is concise about python programing and the &lt;i&gt;Dive into Python&lt;/i&gt; is a very elaborated material. The first one is actually teach you python in a simple way but its not much of use.&lt;/p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;Want these e-books ?&lt;/h2&gt;&lt;p&gt;If you want these e-books right in your e-mail inbox then just fill up the subscription form and i&#39;ll send you the books within 24 hours. Subscription to this blog is totally free and i don&#39;t spam my subscribers mail box so feel free to take advantage of this blog&#39;s free subscription.&lt;br /&gt;
&lt;br /&gt;
&lt;form style=&quot;&quot; action=&quot;http://feedburner.google.com/fb/a/mailverify&quot; method=&quot;post&quot; target=&quot;popupwindow&quot; onsubmit=&quot;window.open(&#39;http://feedburner.google.com/fb/a/mailverify?uri=blogspot/infogate&#39;, &#39;popupwindow&#39;, &#39;scrollbars=yes,width=550,height=520&#39;);return true&quot;&gt;&lt;p&gt;Enter your email address:&lt;/p&gt;&lt;p&gt;&lt;input placeholder=&quot;example@domain.com&quot; type=&quot;text&quot; name=&quot;email&quot;&gt;&lt;/p&gt;&lt;input type=&quot;hidden&quot; value=&quot;blogspot/infogate&quot; name=&quot;uri&quot;&gt;&lt;input type=&quot;hidden&quot; name=&quot;loc&quot; value=&quot;en_US&quot;&gt;&lt;input type=&quot;submit&quot; value=&quot;Subscribe&quot;&gt;&lt;/form&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;Get your machine ready&lt;/h2&gt;&lt;p&gt;Python is a interpreted language so you will be needing a python interpreter which you can easily download from their website &lt;a href=&quot;https://www.python.org/downloads/&quot;&gt;Python Download&lt;/a&gt;. Ubuntu users don&#39;t need to install it at all becuase it comes pre-installed. How to run your programs is well explained in google class. If you want to know anything else then just drop a comment below and i&#39;ll help you asap. &lt;/p&gt;&lt;/div&gt;&lt;/div&gt;</description><link>http://fetch-info.blogspot.com/2015/12/python-learn-it-my-way.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHvOtXGlXyacZ9jtPBa4NBO1a2nJ7xmgABYz1_6W74JlNGTfFy2-aTAVa8eInIORKG1IN6RFZAd727S2Xdfmo87MtoY92ujdDXIDGYOKgbfRHevvyL0mpwagjEg7xugCOXQ8Gx-6hWub-g/s72-c/python-logo.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-5905103347480870407</guid><pubDate>Fri, 04 Sep 2015 16:34:00 +0000</pubDate><atom:updated>2016-01-03T01:27:21.936-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">Latest</category><category domain="http://www.blogger.com/atom/ns#">linux</category><category domain="http://www.blogger.com/atom/ns#">ubuntu</category><title>C-Mate : Your companion for C in Sublime Text</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h2&gt;What is C-Mate ?&lt;/h2&gt;&lt;p&gt;When it comes to writing code then either we choose an IDE like codeblocks or ecllipse, OR we use some text editor like notepad++ or sublime text. Sublime text is a very powerful, flexible and free text editor. You can install any package you want in sublime which help you writing codes easily. C-Mate is also such a sublime package which will help you write C code efficiently on ubuntu. It consists of many useful snippets, pre-coded functions and a powerful build system.&lt;/p&gt;&lt;br/&gt;&lt;br /&gt;
&lt;h2&gt;Why C-Mate&lt;/h2&gt;&lt;p&gt;Although there is a pre-installed c++ sublime package which helps you writing c as well as c++ codes but the feature it lags is the code running in the terminal. People like writing code in sublime but when it comes to build and run the code then they need to go the directory containing the code file and then build and run it from there. It gets so annoying when we debug the code. No sublime package allows to run the code within the sublime. Here C-Mate makes the exception. Its build system is capable of running the code in terminal from within the sublime.&lt;br/&gt;&lt;br /&gt;
Also it comes with some pre-coded functions which noone wants to write again and again but need to write them unwillingly. These functions are min(int *), swap(int, int) and much more. You all just need to start typing the name of the function and it will write the function declaration and function body of that function.&lt;br/&gt;&lt;br /&gt;
C-Mate also consists many useful shortcuts/snippets which speed up you writing. Like include, for loop, while loop, main function, structure, typedef and many more.&lt;/p&gt;&lt;br/&gt;&lt;br /&gt;
&lt;h2&gt;How to get it ?&lt;/h2&gt;&lt;p&gt;It has not come to the sublime package install yet but it will be there soon. Till that time, you&#39;ll need to install it manually. Don&#39;t worry, you just need to follow 2 steps.&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Download the C-Mate from here. &lt;a href=&quot;https://github.com/kapilgarg1996/C-Mate/releases/download/v1.0-beta/C-Mate.sublime-package&quot;&gt;Download C-Mate&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Copy the downloaded file to &lt;code&gt;/home/.config/sublime-text-3/Installed Packages/&lt;/code&gt;. Thats it&lt;/li&gt;
&lt;li&gt;Now just type your c code and press &lt;code&gt;ctrl+b&lt;/code&gt; and select C-Mate.&lt;/li&gt;
&lt;/ol&gt;&lt;/p&gt;&lt;br/&gt;&lt;br /&gt;
&lt;h2&gt;How to use it ?&lt;/h2&gt;&lt;p&gt;After the installation, just start typing your code and C-Mate will start helping you. When you are finished your code, just press &lt;code&gt;ctrl+b&lt;/code&gt; and select C-Mate. You wont have to select C-Mate every time. Its just for the first time.&lt;br /&gt;
&lt;h3&gt;Associated keys&lt;/h3&gt;&lt;ol&gt;&lt;li&gt;&lt;code&gt;ctrl+b&lt;/code&gt; : Build the code&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ctrl+shift+b&lt;/code&gt; : Run the code in the terminal.&lt;/li&gt;
&lt;/ol&gt;&lt;h3&gt;Some snippets keywords&lt;/h3&gt;&lt;ol&gt; &lt;li&gt;&lt;code&gt;main&lt;/code&gt; : for main() function&lt;/li&gt;
 &lt;li&gt;&lt;code&gt;inc&lt;/code&gt; : include statement&lt;/li&gt;
 &lt;li&gt;&lt;code&gt;for&lt;/code&gt; : for loop&lt;/li&gt;
 &lt;li&gt;&lt;code&gt;while&lt;/code&gt; : while loop&lt;/li&gt;
 &lt;li&gt;&lt;code&gt;type&lt;/code&gt; : typedef&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;.......and many more&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;div id=&quot;mypostads&quot;&gt;&lt;/div&gt;
&lt;b&gt;Note :&lt;/b&gt; &lt;i&gt;It is to note that the application file of your code will be create in the same directory in which the code file reside. And its name will be the same as of the code file but without the .c extension. This means that you can create multiple application files in the same directory without the fear of losing the previous one.&lt;/i&gt; &lt;br /&gt;
&lt;/p&gt;&lt;p&gt;In the near future, i&#39;ll try to incorporate auto function declaration feature in C-Mate. If you want to contribute to C-Mate then you can visit its &lt;a href=&quot;https://github.com/kapilgarg1996/C-Mate&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtxh7B4SYzpfDb_R2OBILwkYr-eC_9g_s_hORoH9vXmwvD_HgWkZNNlprj9ZLd1o_EMWtUQZ4hXD8A7EA-Cqzduq1HwjiYPF9_0I6vKxcrvMi0r_VefukuWt3_2d1sU6jLfSwsmzVHA1TW/s200/github.jpg&quot; alt=&quot;github repo&quot; width=&quot;30&quot; height=&quot;30&quot;/&gt;&lt;/a&gt;. If you have any doubt regarding its functionality then just comment it out below and i&#39;ll be happy to solve your query.&lt;/p&gt;&lt;/div&gt;</description><link>http://fetch-info.blogspot.com/2015/09/c-mate-your-companion-for-c-in-sublime.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtxh7B4SYzpfDb_R2OBILwkYr-eC_9g_s_hORoH9vXmwvD_HgWkZNNlprj9ZLd1o_EMWtUQZ4hXD8A7EA-Cqzduq1HwjiYPF9_0I6vKxcrvMi0r_VefukuWt3_2d1sU6jLfSwsmzVHA1TW/s72-c/github.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2321045628204084378.post-7011835473632710572</guid><pubDate>Mon, 20 Jul 2015 08:16:00 +0000</pubDate><atom:updated>2015-07-20T02:21:04.041-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Computer</category><category domain="http://www.blogger.com/atom/ns#">Information</category><category domain="http://www.blogger.com/atom/ns#">Latest</category><category domain="http://www.blogger.com/atom/ns#">linux</category><category domain="http://www.blogger.com/atom/ns#">ubuntu</category><title>Why to Use Ubuntu</title><description>&lt;!DOCTYPE html&gt;&lt;br /&gt;
&lt;html&gt;&lt;br /&gt;
&lt;head&gt;&lt;br /&gt;
&lt;meta name=&quot;keywords&quot; content=&quot;ubuntu, linux, use ubuntu, ubuntu benefits, learn ubuntu, ubuntu vs windows&quot;&gt;&lt;br /&gt;
&lt;meta name=&quot;description&quot; content=&quot;This article is about the benefits of ubuntu and other linux based operating systems. You can learn how to use ubuntu and linux and why ubuntu is prefferable over windows&quot;&gt;&lt;br /&gt;
&lt;title&gt;Why Ubuntu&lt;/title&gt;&lt;br /&gt;
&lt;style&gt;
.post-header{
margin-bottom:-10em !important;
}
&lt;/style&gt;&lt;br /&gt;
&lt;/head&gt;&lt;br /&gt;
&lt;body&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnrWeFOlZB71NYJDInGYIkRKkPQkM9pImQ9XAQUB4tbw2OPltkci2x8aLnvSAzNwNtbHycx5Rk-3rAWwTulRGApATtsTtO9C2BjBCpJup4ckeWOlw7JF-D1evnno5U8vsJa6SQ8OnKh4gZ/s1600/ubuntu.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnrWeFOlZB71NYJDInGYIkRKkPQkM9pImQ9XAQUB4tbw2OPltkci2x8aLnvSAzNwNtbHycx5Rk-3rAWwTulRGApATtsTtO9C2BjBCpJup4ckeWOlw7JF-D1evnno5U8vsJa6SQ8OnKh4gZ/s320/ubuntu.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;h2&gt;Brief Intro to Ubuntu&lt;/h2&gt;&lt;p&gt;This section follows the legacy of article writting in which an intro to the subject is necessary. Ubuntu, as you all know, is an operating system under linux distribution. Ubuntu started as a command like operating system but now it offers the gui also to accompalish all the tasks. Ubuntu is open source software so it comes free. Using it is easy and it suits best for development process. The next section of this article describes the benefits come along with ubuntu.&lt;/p&gt;&lt;h2&gt;What comes in the box ?&lt;/h2&gt;&lt;p&gt;There are so much benefits which ubuntu brings. But listing all of them is not necessary. The following few things about ubuntu will be enough to persuade you to use it or atleast give it a try.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;b&gt;Open Source&lt;/b&gt;: What it means is that it is totally free of cost. It also specifies that there are thousands of developers sitting who are willing to help you anytime you are stuck with your machine. New updates come regularly and bugs are tackled as soon as they are confirmed. Not only this, thousands of applications built for ubuntu are also open source. So you can think, there is a family of open source softwares which come along with ubuntu for free.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Fully Customizable&lt;/b&gt;: This is also one of the best features which only an open source software can provide. You can customize the code as you like(If you want to but why would you). If you have ever rooted your phone then you know whats it like to have a fully customizable operating system.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Powerful file handling&lt;/b&gt;: It is incredible the way we can handle our files in ubuntu. Just one command and we can make our file restricted. Just one command and we can delete the whole operating system files. Password protected files, hidden files, directories and links are easy to create and delete.File compression and decompression can easily be done either via gui or cli.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Incredible Package Management&lt;/b&gt;: The reason why developers prefer linux based operating system is its awesome package management capabilities. Ubuntu has got a &lt;blockquote&gt;Advanced Packaging Too(apt)&lt;/blockquote&gt;which can install almost any package in your system. &lt;code&gt;apt-get install&lt;/code&gt; is the most basic command we use to install/update the applications.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Superb Network Management&lt;/b&gt;: When we think about networking or network sharing then our thoughts are very limited to routers, adapters and ip addresses. But what is behind all these is the management of these. In ubuntu, network management is so handy that you can learn it easily. Changing IP addresses is just a child&#39;s play. You can learn more about networking through various sources which can be found at the end of this article which is about to come.&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;Getting Ubuntu on your machine&lt;/h2&gt;&lt;p&gt;After reading all of the above points, you are definitely thinking of trying ubuntu atleast once. In this section i&#39;ll tell you how to get it ready on your machine. There are basically 3 methods to use ubuntu.A detailed description of each is not necessary in this article because there are already the official documentation of each method on the internet. But still to reduce your google work, i have posted the links to those documentations at the end of the article.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;b&gt;Using Virtual machine&lt;/b&gt;: Virtual machine is a software which can run system images (iso files) without affecting the other operating system in which it is running. Its the safest and easiest method to run ubuntu. Just install the virtual machine and run the iso file using it. The detailed documentaion is available on howtogeek website.(link is at the bottom). Its good to use virtual machine to get started with ubuntu but virtual machine degrades the performance of the OS, so use it temporarily.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Using Live CD/pendrive&lt;/b&gt;: Live CD is another safer option to try ubuntu but its just for the trial basis. You can look and try the ubuntu features and work for as long as the cd/pendrive is inserted in your machine.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Dual boot&lt;/b&gt;: Once you have tried ubuntu then if you want it permanently then dual booting is the only option. It will install the grub(a software to manage os booting) and you can switch between windows and ubuntu by just restarting. However, the process of dual booting is longer than the rest two.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;So thats all. I love ubuntu and thought everyone should give it a try, so wrote this article. If you are also an ubuntu lover then encourage your friends by sharing this article to them or if you are new to ubuntu and have some questions then comment it below and i&#39;ll defintely try to help you out. In the next article, i&#39;ll highlight some wonderful ubuntu commands. Till then, Hakuna Matata.&lt;/p&gt;&lt;caption&gt;&lt;b&gt;Helpful Links&lt;/b&gt;&lt;/caption&gt;&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://www.ubuntu.com/download&quot;&gt;&lt;/a&gt;Download Ubuntu&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.howtogeek.com/howto/11287/how-to-run-ubuntu-in-windows-7-with-vmware-player/&quot;&gt;Virtual Machine Installation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://help.ubuntu.com/community/LiveCD&quot;&gt;&lt;/a&gt;Live CD method&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://help.ubuntu.com/community/WindowsDualBoot?action=show&amp;redirect=DualBoot%2FWindows&quot;&gt;&lt;/a&gt;Dual Booting&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Ubuntu_%28operating_system%29&quot;&gt;&lt;/a&gt;Ubuntu wiki&lt;/li&gt;
&lt;/ul&gt;&lt;/body&gt;&lt;br /&gt;
&lt;/html&gt;</description><link>http://fetch-info.blogspot.com/2015/07/why-to-use-ubuntu.html</link><author>noreply@blogger.com (Kapil Garg)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnrWeFOlZB71NYJDInGYIkRKkPQkM9pImQ9XAQUB4tbw2OPltkci2x8aLnvSAzNwNtbHycx5Rk-3rAWwTulRGApATtsTtO9C2BjBCpJup4ckeWOlw7JF-D1evnno5U8vsJa6SQ8OnKh4gZ/s72-c/ubuntu.jpg" height="72" width="72"/><thr:total>1</thr:total></item></channel></rss>