David Chiu

Professor of Computer Science
University of Puget Sound
dchiu@pugetsound.edu


home | teaching | research | people | service | faq for students

CS 475 - Operating Systems

Hwk: Remote C Development

In this tutorial, we will set you up to develop on a remote server. Here’s why we have to do this, instead of developing on your own machines. C is a very finicky language, and is highly dependent on the environment on which it compiles and executes. This is everybody’s worst nightmare: turning in an assignment you’ve spent hours on, only to have it not compile or execute on your professor’s machine. Indeed, having a common compiling and runtime environment was what made Java (and the Java Virtual Machine JVM) so successful when it was introduced in the mid-90s. Today, most languages have a common runtime environment, and C has certainly made significant efforts to being more portable across systems, but alas I find that it’s still very system dependent.

It’s therefore important that we all code in a common environment, so I’ve prepared a remote server for everyone to log into, including myself. This means we’re all coding and running on the same machine, which leads to more predictable and repeatable results!

Student Outcomes

Installing C Development Tools

Connecting to the Remote Server

Using the Terminal (Shell) and VS Code Editor

Setting up Git and Github

To download and submit your homework assignments for this class, you’ll need to get connected to github and configure it all to work with this server. Follow these steps.

  1. Open your browser, and go to github.com. If you don’t already have an account, create one now.

  2. Navigate to https://github.com/settings/tokens to create a new “personal access token.” You can also get there via Settings > Developer Settings.

  3. Click to generate new a (classic) token. On the next screen, give your new token a good note, like “For OS assignments” or “CS 475” so you know what it is later. Assign it an expiration period that will take you to the end of the course, and it may be easier to just give it no expiration.

  4. Below that, select the boxes: repo, admin:org, and admin:public_key.

  5. Click the Generate token button on the bottom of the page, and take note (save) the token in a secure place. Don’t share it. Treat this like your password.

  6. Go back into your VS Code’s terminal window. Type:
    1
    
     $ gh auth login
    
  7. Follow the prompts in this program. Use your up/down keys to choose the following selections to their questions:

    1
    2
    3
    4
    5
    6
    7
    8
    
     ? What account do you want to log into? 
     > GitHub.com
    
     ? What is your preferred protocol for Git operations?
     > HTTPS
    
     ? How would you like to authenticate GitHub CLI?
     > Paste an authentication token
    

    Finally, get your github access token ready for pasting:

    1
    
     ? Paste your authentication token: ****************************************
    
  8. Now run this to complete the setup.
    1
    
     $ gh auth setup-git
    
  9. That should set you up for authentication to github. We can start our homework.

Our First C Program

  1. From your browser, find my code repository here: https://github.com/davidtchiu/os-first.

    • Click on the green Use this template button and select the Create new repository option. In the next page, give your repository a good name (the “suggestion” they give is fine). My only request is that you don’t name it to be the same as mine. This is hide your homework solution from Google searches.

    • Leave your repository with public visibility.

    • Copy the URL of your new from the browser window.

  2. Now from VS Code, open a terminal, and *clone* your new Github repository down to your local working directory using:

    1
    
     $ git clone <your-github-repo-url>
    
  3. You will be prompted in the terminal for your github username and password. Enter those now – however, you will use the “access token” we previously generated as your password. Do not attempt to use your regular github login password here. If successful, it will download a directory called os-first.

  4. Navigate into the new directory you just cloned. Inside, create a new directory create a new file called hwk1.c:

    1
    2
    3
    
     $ cd ~
     $ cd os-first/
     $ code hwk1.c
    
    • The first command cd ~ changes your current-working-directory to your home directory. You should remember that ~ is a shortcut to your home directory, and now you won’t ever get “lost!”

    • The second command cd os-first changes your current-working-directory to the os-first directory that git should have cloned in the previous steps.

    • Finally, code hwk1.c opens a new file called hwk1.c.

    • Of course, you could’ve done all that using VS Code’s file explorer on the left-hand panel, but it’s important to make sure that your terminal is in that new project directory when you’re ready to compile.

  5. With hwk1.c open in your editor, type in the following “hello world” code and save it.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
     #include <stdio.h>
    
     int main(int argc, char *argv[]) {
       const int n = 10;   // n is declared to be constant (read only)
       for (int x = 0; x < n; x++) {
         printf("Hello world %d of %d!\n", x, n);
       }
       return 0;
     }
    
  6. Go back down to the terminal window to compile and run it:

    1
    
     $ gcc -Wall -g hwk1.c
    

    Here, gcc invokes the gnu c compiler (gcc). The -Wall flag instructs the compiler to display any warnings (even if the code compiles.) The -g flag generates debugging information for debuggers that we’ll use later on.

    The executable file that is produced is called a.out. Type ls on the terminal to make sure it’s there. If you don’t see a.out, that means the compilation failed and there was a syntax error that need to fix. To run it, use the command ./<executable-file>

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
     $ ./a.out
     Hello world 0 of 10!
     Hello world 1 of 10!
     Hello world 2 of 10!
     Hello world 3 of 10!
     Hello world 4 of 10!
     Hello world 5 of 10!
     Hello world 6 of 10!
     Hello world 7 of 10!
     Hello world 8 of 10!
     Hello world 9 of 10!
    
  7. It seems a bit odd that your executable file would be named a.out by default. To instruct the compiler to output the executable under a different name, you can use the -o <executable-name> flag:

    1
    
     $ gcc -Wall -g -o helloworld hwk1.c
    

    This would output the binary as helloworld, and you can run it using ./helloworld on the terminal.

  8. After you’re done, commit and push the hwk1.c file to github for grading. From the terminal,

    1
    2
    3
    
     $ git add hwk1.c
     $ git commit -m "<write-a-commit-msg>"
     $ git push origin main
    
  9. It may ask you for your github credentials again. Enter it now – remember once again to use your “access token” for the password. (You should only have to do this once for the initial push).

  10. Navigate to your github repository from your browser to make sure that hwk1.c exists. If so, you have successfully committed your code and pushed to github! This is how you will submit all assignments in this course.

Submission

To prove that you have logged in successfully:

  1. Change your password on the server using passwd. Yep, I will login using the old password just to check.

  2. Commited and pushed your git repository. Make sure your repo is public.

  3. On canvas, simply submit the URL to your Github repo. No other form of submission is accepted.

Credits

Written by David Chiu. 2022. Updated 2024.