COMP 530: Lab 0: C Warm-Up

Due 11:59 PM, Monday, August 29, 2022

Introduction

This lab will be a very gentle introduction to C programming in a Linux (or Linux-like) environment.

Getting Started

Set up Docker

Setup the your Docker environent by following the steps outlined in the Linux getting started guide (Docker Environment) document. Using the same Docker environment ensures consistency during both the coding and grading process.

Mac/Linux: When you run ./learncli.sh and start the Docker image, Docker will create the workdir directory that is bridged between the image and your native operating system, where you can copy and move files between the the two.

Windows: When you run ./learncli.ps1 and start the Docker image, Docker will create the workdir directory that is bridged between the image and your native operating system, where you can copy and move files between the the two.

Learn the Command Line

Read Chapters 2 through 6 in the Linux Command Line textbook. This will teach you everything you need to know about running programs and navigating directories in your LINUX environment. Make sure you practice running the commands, the shell will be your playground for the semester, so gain familiarity with it.

Learn vim or emacs

We recommend using a terminal-based editor, such as vim or emacs. They are designed to make editing code very fast, although it may not seem like it at first.

At this point, we recommend you pick one of these two editors and complete the tutorial as explained below. You can also try both and see which one you prefer. You may also open your preferred graphical text editor and edit files from learncli211/workdir.

It will take a while before you remember everything from the tutorial. We recommend you just learn enough to be comfortable enough to complete Part 1 of the assignment in vim or emacs, then you can go back to the tutor or look at other online guides for the next two weeks to learn more as you go.

Note that the vim and emacs in your Docker image are configured with features for the C programming language and may look different than on your home computer.

Vim Tutorial

Start your Ubuntu environment that you set up earlier and type vimtutor. When you press enter, a tutorial document will be opened with vim explaining how to use it.

Emacs Tutorial

Start your Ubuntu environment that you set up earlier and type emacs. When you press enter, an emacs session will start. You can begin the tutorial by holding down the Control key and typing h, then release the control key and type t. This can be abbreviated C-h t. To exit emacs, hold down Control while typing x, then release control and type q, or C-x q.

Git Environment Variables

Git has certain config values it relies on, specifically your name and email which are transcribed in git commit logs. If you see the following output from a git command:

Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@07a5f55598d3.(none)')

Then those values have not been set. You can run the commands git recommends here, but they will be cleared once you end your Docker session. Instead of doing that, you can edit the environment variables created once the container starts as follows:

  1. Start your container (./learncli.sh or ./learncli.ps1).
  2. Open ../.bash_profile in your favorite text editor (vim ../.bash_profile).
  3. Change lines 8-11 from...
        #export GIT_AUTHOR_NAME=""
        #export GIT_COMMITTER_NAME=""
        #export GIT_AUTHOR_EMAIL=""
        #export GIT_COMMITTER_EMAIL=""
    
    to...
        export GIT_AUTHOR_NAME="Ram Zees"
        export GIT_COMMITTER_NAME="Ram Zees"
        export GIT_AUTHOR_EMAIL="ramzs@live.unc.edu"
        export GIT_COMMITTER_EMAIL="ramzs@live.unc.edu"
    
    but with, of course, your own name/email instead of our mascot's fake email. Save the file.
  4. Restart the container.

Again if you don't do this, you will have to enter git config --global ... after each restart if you want to commit, which is extra unnecessary work.

Github classroom

The files you will need for this and subsequent lab assignments in this course are distributed using the Git version control system.

This semester, we will use github classroom to hand-out and submit assignment. This means you will need to create an account at github.com (if you don't have one already). When you click the link below to accept the started code, it will ask you to link your github account to the class roster. If you have issues, send your github user ID to the instructor. If you create a new account you will need to verify your email address. Once you are invited to join the class, you will need to authorize github classroom to access your account.

The next step will be to create either an SSH key or a Personal Access Token.

Setting up SSH authentication

In order to interact with GitHub from the command line, SSH authentication must be set up. This is a standard procedure and should only need to be done once.

On Your Local Computer

  1. Navigate to the learncli211 directory that contains the learncli.sh file, but do not execute it! This must be done outside of the container. If you are not sure whether or not your are in a container, then restart your console, and cd into learncli211.
  2. Type ssh-keygen, type .ssh/id_rsa as the location to save the key, and press enter for no passphrase at the password prompt.
  3. $ ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/root/.ssh/id_rsa): .ssh/id_rsa
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in .ssh/id_rsa.
    Your public key has been saved in .ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:4yheFb7P2rzq/fJ71+AKPssyEQq7+bI0YcnK8+rzfr8 ceclia@topkek
    The key's randomart image is:
    +---[RSA 2048]----+
    |                 |
    |                 |
    |        .        |
    |   ... ...       |
    |    =o .S.       |
    | . o...+.o    .  |
    |  + +oo o..  . ..|
    |  .=++. oOo.  o o|
    | .o=*=.oE*@B=+ . |
    +----[SHA256]-----+
    
  4. Inside of your learncli211 folder, a folder called .ssh should now exist. Running cat .ssh/id_rsa.pub should output the needed key as text to the console, which you should copy for the next part. For the macOS Terminal, highlighting the key and then pressing CTRL + C should copy it; on Windows Terminal, you must highlight it and then right-click the highlighted text to copy it. If nothing is highlighted in Windows Terminal, a right-click will paste.

On GitHub

  1. Click your profile in the top right corner.
  2. Click Settings -> SSH and GPG keys -> New SSH Key.
  3. Paste the contents of .ssh/id_rsa.pub into the "Key" section.
  4. Give it a Title and click "Add SSH Key".

Setting up a Personal Access Token

Alternatively to an SSH key, you may authenticate to github using a Personal Access Token by following these steps:

  1. Create your Github Personal Access Token based on the following tutorial. You may find the way to create it in **Settings --> Developer settings (end of the left column) --> Personal access tokens**
  2. Find your repository HTTPS web URL on Github.
  3. Clone your repository by interpolating your personal access token before the github.com, following this pattern: git clone https://[personal-access-token]@[repository-address]. For example, if repository is lab0-team-max.git and your token is ghp_hTndummytoken2N9msm, you would type the following to clone your repository:
# Example
git clone https://ghp_hTndummytoken2N9msm@github.com/comp530-f22/lab0-team-max.git

Getting familiar with git

Git is a powerful, but tricky, version control system. We highly recommend taking time to understand git so that you will be comfortable using it during the labs. We recommend the following resources to learn more about git, if you are not already a proficient user:

  1. Understanding git conceptually This is a MUST READ if you want to work on git smoothly. (You may skip the last part: Rebasing, for now)
  2. Quick 15-20 mins online exercise to get to know git.
  3. Git user's manual
  4. If you are already familiar with other version control systems, you may find this CS-oriented overview of Git useful.

Working in a group

You may complete this assignment alone or with as many teammates as you like, per the course collaboration policy. However, you are expected to understand all components of the work that is handed in, and you may find it useful to (re-)work any portions independently that a teammate completes on your behalf. Moreover, this assignment will also help you warm up on C programming.

Once you join the group, you will be asked to create a team, or join a team in github. Once a team is created, you can add members to your team.

Getting the starter code

You will need to click on this link to create a private repository for your code.

Once you have a repository, you will need to clone your private repository (see the URL under the green "Clone or Download" button, after selecting "Use SSH". You will want to issue this command from your Docker environment. For instance, if your private repo is called lab0-team-don:

git clone git@github.com:comp530-f22/lab0-team-don.git

Editing the source

Next, you’ll want to edit a new file named hello.c. To begin editing this file in vim, simply run the command:

vim hello.c

Authorship

Each source file in COMP 530 will begin with a standard header comment below. Note this header is checked by the autograder for an exact match. Please be sure to format your PID as a single 9-digit number with no spaces nor dashes. Since we do grade manually for style we do not include names on code listings to avoid biasing the grading.

// PID: 123456789

If you have multiple members in your team, include one comment line per member:

// PID: 123456789
// PID: 912345678
// PID: 891234567

Compiling and running the code

We provided a Makefile which effectively scripts compilation. To build the code simply type make. You may wish to read the Makefile to understand the individual commands to compile the code.

To run the code, simply type ./hello. Your initial output will be empty.

Lab Requirements

Exercise 0. (2 points.) Add code to print the following message, followed by a newline:

hello, welcome to comp 530!
Also modify the code to return the value EXIT_SUCCESS.

Note that EXIT_SUCCESS is defined in a header file, stdlib.h. Your starter code includes a line:

#include <stdlib.h>

This include line brings all of the declarations from this file into the program that is being compiled. System-level headers are typically under /usr/include.

Exercise 1. (4 points.) Add code to print a second line, with the process ID of the current process. For instance, if 2834 is the current process ID, the output should be:

hello, welcome to comp 530!
2834

Each running process in a system has a unique integer to identify it. You can see the current set of processes on a Linux system using the ps -eax command. Hint: Check out the manual for getpid() using man getpid in your provided Docker environment. Use the q key to exit.

Note that your pid should be different each time hello runs.

Exercise 2. (4 points.) Add code to print a third line, which lists the current working directory of the running process. For instance, if you are running hello from /root/workdir/lab0 and the process ID is 2834, the output should be:

hello, welcome to comp 530!
2834
/root/workdir/lab0

Note that the current directory is not always where the hello binary is. For instance, if I cd .. out of my lab0 directory, I can type ./lab0/hello and the binary should run, but the output would then be:

hello, welcome to comp 530!
2834
/root/workdir

Hint: Check out man getcwd for a helper function to get the current working directory. Do NOT hard-code a path in your code.

Hand-In Procedure

You will be handing in the code via gradescope. You should have been added to the class; if not, please contact the instructors as soon as possible. If you work in a team, you should only submit one copy of your code in gradescope; you can add your teammates to the handin. We recommend handing in directly from your github repository to the assignment. Gradescope will run the autograding program, giving you immediate feedback on the assignment. You may hand in more than once and we will take the most recent, applying lateness penalties as appropriate (out-of-band).

The autograder should be running the exact same container environment as your workspace, so we do not expect discrepancies. However, should you notice a difference in behavior in the autograder that you cannot reproduce, let course staff know.

Generally, unless the homework assignment specifies otherwise, you should compile your program using the provided Makefile (e.g., by just typing make on the console). Do not add any special command line arguments ("flags") or compiler options to the Makefile.

Note: We do not have an automated way to calculate late penalties. These will be applied manually at the end of the semester.

The program should be neatly formatted (i.e., easy to read) and well-documented. The Style Guide gives additional guidance on lab code style.

Make sure you put your pid(s) in a header comment in every source file you modify. For provided files that you do not modify, you should leave those files as-is.

If you complete any challenge problems, please describe the solution and how to demonstrate it in challenge.txt. Note that we have a separate submission option in gradescope for submitting challenge problems, in order to accommodate later submissions without charging late hours. Even if you are submitting on time, please submit your challenge problems a second time through the appropriate challenge assignment --- we will only manually grade assignments handed in through the challenge option.

This completes the lab.


Last updated: 2022-11-03 10:50:48 -0400 [validate xhtml]