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
- Setting up VS code for remote development on a Linux server
- Setting up git and github remote connection
- Compiling and running our first C program
Installing C Development Tools
-
Download and install VS Code if you don’t already have it installed on your computers. This is what we’ll be using for writing all our assignments for this class.
- Open VS Code and click on the “Extensions” tab on the left hand side. You’ll want to search for and install the following packages:
- C/C++ (required) by Microsoft
- Remote Development (required) by Microsoft
- C/C++ Themes (optional) by Microsoft
- You may have to restart VS Code after installing these extensions.
Connecting to the Remote Server
- I’ve set up a Linux server for you to connect to and develop on. Make a note of the following information:
1 2 3
Server IP Address: 149.165.174.87 Your Username: <your-pugetsound-username> Your Password: <given-to-you>
- Open up VS Code, then click on the
View
menu and selectCommand Palette
. Then type:Remote-SSH: Add New SSH Host...
. It should auto-complete after typing the first few letters. Choose to run this command. In the box that pops up, enter:1
ssh <your-pugetsound-username>@IPAddress
For example, mine would look like:
1
ssh dchiu@149.165.174.87
-
Next, VS Code may ask you to choose an SSH configuration file. If it asks, just select the one that’s already selected.
-
Now click on the “Remote Explorer” icon the left-hand panel, shown below. Near the top left, select the
Remote
(orSSH Target
on older versions) option from the pull-down menu that may have “Containers” selected currently. Now you should see the149.165.174.87
and your username below it. Click on the arrow next to your username to connect. -
You’ll be prompted for your password in a box at the top (it’s easy to miss). Enter the one that I assigned.
-
VS Code may ask you what platform the remote server is using. Select
Linux
from the option menu if asked. It’s going to take a couple minutes for it to install the necessary packages to get you set up. Once everything is installed, you should see a front page with some information and options on it. Go ahead and exit out of this page to show an empty VS Code editor.- From the left-hand panel, click on the “Explorer” icon that looks like like two sheets of paper. Then click on the button called
Open Folder
. Choose/home/<your-pugetsound-username>
(which should be the default option). Important: This path is your home directory on the server. Only you (and I) have access to it. This is where all your code will go for this class.
- From the left-hand panel, click on the “Explorer” icon that looks like like two sheets of paper. Then click on the button called
-
You may be prompted for your password again up on the top of the screen, followed by a prompt to trust and accept the secure certificate. Click the check box then select
Yes
on this dialog box. -
If you did all the steps correctly, you should see the contents of your home directory on the left-hand panel. Here’s what mine looks like:
- Now, change your password to something more secure. From the terminal (command-line) window on the bottom of your screen, enter
passwd
, and follow the instructions. When you type of your password, the screen will remain blank, but your input is registered. That is normal.
Using the Terminal (Shell) and VS Code Editor
-
You only have to do all those steps listed in the previous section that one time. Let’s get a feel for how to start up the coding environment from this point forward, so go ahead and quit VS Code.
-
Before getting started again, keep in mind that, because we’re coding on a remote server, a stable internet connection is essential. This means it will not be possible to develop while you’re offline or only have an intermittent wifi connection (e.g., on a plane), so plan wisely throughout the semester.
-
Pro Tip: Save Often Anytime you lose wifi connection or close down your computer, you will sever your connection to the remote server and lose any unsaved work! As you’re developing, save often! (It’s just
ctrl+s
on Windows andcommand+s
on a mac.)
-
-
Open VS Code back up. If you don’t see a new, blank window, then choose the
File
menu and selectNew Window
. Click on Remote Explorer icon, then choose the profile you want to connect. It should simply be called/home/<your-pugetsound-username>
. -
Enter your new password, and you should see the contents of your home directory once again. If you do, then you’re connected back to the server! Happy programming!
-
Accessing the Terminal The command-line terminal (also called a shell) should show up on the bottom of your VS Code editor. If it’s not already there, go to the
Terminal
menu on the top of your screen and selectNew Terminal
. Here’s what mine looks like: -
I assume you have a working knowledge of the command-line shell, but here are some time-saving tips.
Tip [tab]
Type the first few letters of a file or directory, and hit [tab]
to auto-complete. When the there are multiple matches, hitting[tab]
will do nothing. However, hit[tab]
twice in succession will reveal all the matches.up
anddown
keysHitting the up
key will display the most recently issued command. You can hitup
continuously to scroll through your command history.ctrl + c
Will interrupt the currently-running command, and return the shell prompt. I suspect you’ll be using this one a lot! You use it to terminate a process that you ran. ctrl + u
Will delete from your cursor to the beginning of the line. ctrl + k
Will delete from your cursor to the end of the line. ctrl + l
Will clear out the terminal window. (Same as the clear
command). -
If you need a refresher on how to work on the shell, here are some resources (Strongly recommended)
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.
-
Open your browser, and go to github.com. If you don’t already have an account, create one now.
-
Navigate to https://github.com/settings/tokens to create a new “personal access token.” You can also get there via
Settings > Developer Settings
. -
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.
-
Below that, select the boxes:
repo
,admin:org
, andadmin:public_key
. -
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. - Go back into your VS Code’s terminal window. Type:
1
$ gh auth login
-
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: ****************************************
- Now run this to complete the setup.
1
$ gh auth setup-git
- That should set you up for authentication to github. We can start our homework.
Our First C Program
-
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.
-
-
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>
-
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
. -
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 theos-first
directory that git should have cloned in the previous steps. -
Finally,
code hwk1.c
opens a new file calledhwk1.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.
-
-
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; }
-
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
. Typels
on the terminal to make sure it’s there. If you don’t seea.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!
-
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. -
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
-
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).
-
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:
-
Change your password on the server using
passwd
. Yep, I will login using the old password just to check. -
Commited and pushed your git repository. Make sure your repo is public.
-
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.