GithubHelp home page GithubHelp logo

recitation02-solutions's Introduction

Computer Systems Organization : Recitation 02

Some of these exercises have multiple ways to solve them.

Basic Unix usage

    mkdir unix/backup
    cp unix/foo.txt unix/backup/
    mv unix/foo.txt unix/answers.txt
    mkdir -p multiple/directories
    touch multiple/directories/test.txt
    echo "Hello!" > multiple/directories/test.txt
    cat multiple/directories/test.txt
    rm -r multiple/
    wget http://nginx.org/download/nginx-1.9.2.tar.gz
    tar -xvf nginx-1.9.2.tar.gz
    grep -r "main(" nginx-1.9.2 (or just grep -r "main(" assuming you are in nginx-1.9.2)

    OR

    find . -name '*.c' | xargs grep "main("

And by inspection, the file is nginx-1.9.2/src/core/nginx.c

    wc -l nginx-1.9.2/src/core/nginx.c 

And there are 1362 lines.


C Programming

Part 1

    int findMax(int a, int b)
    {
        if (a > b)
            return a;
        else
            return b;    
    } 

Part 2

    int fib(int n) 
    {
        if (n <= 1)
            return n;
        else {
            return fib(n - 1) + fib(n - 2);
        }
    }

Part 3

    int sum = 0;
    for (i = 0 ; i < size ; i++) {
        sum += a[i];
    }

Debugging

  • After the foo binary is compiled with debugging enabled, run gdb with gdb foo

  • To start running the foo binary, type run.

  • To set a breakpoint, type b file.c:5 where file.c is a source code file, and 5 is the line to break at.

  • To view a snippet of the source code, type l.

  • To continue execution until the next breakpoint, type c.

  • To continue execution until the next line of code, type n.

  • With this information, we see that the program continuously executes lines 6 and 7, which are the lines for the condition of a while loop, and its body. We can example the contents of sum and i per iteration by doing print i or print sum. We notice that every iteration, sum is incremented by 1, and i stays constant. With this knowledge, we see that the variable i is never incremented, so our loop executes forever.

  • To fix this bug, we simply add the line i++;.

  • gcc -g hello_world.c -o hello_world

  • For graders: Students will have made the fix to foo. If they have, it is correct.

recitation02-solutions's People

Contributors

huppy309 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.