Welcome to CS 3410! In this course we will learn about computer systems organization and programming. Most systems programming is done in the C programming language, or in a similar language. Today you will compile a simple C program. Also, you will install a virtual machine for the course that can run on your local personal computer. If you do not have a personal computer, then you can use the departments' CS undergrad lab (CSUG) computers. Simply follow the instructions below.
IMPORTANT NOTE: If you have issues with logging on to CSUG, creating the VM, or setting up Sublime Text SFTP, see the Troubleshooting section at the bottom of this document. If your problem is not listed there, ask a TA (in section, in office hours, or on Piazza) for assistance.When writing code, it is important to be aware of the programming environment you are working in - this term covers the text editors, programming languages, compilers, and other useful software that is installed on your computer. For this class, we use a standardized programming environment to ensure you have access to all the programs and utilities you will need to complete your assignments. Programming assignments in CS 3410 should be completed either by working remotely on the CSUG computers or by setting up a course virtual machine on your personal computer. We will introduce both these methods in this lab.
For Linux and Mac users, simply type the command:
ssh [netid]@csugXX.csuglab.cornell.edu
(Replace XX with a number between 01-14 and [netid] with your NetID.)
For Windows users, download an SSH client. The recommended SSH client is PuTTY. To connect to CSUG machines using PuTTY:
1.) Open PuTTY
2.) Under Host Name enter: [netid]@csugXX.csuglab.cornell.edu (Replace XX with a number between 01-14 and [netid] with your NetID.)
3.) Press Open
4.) A window labeled 'PuTTY Security Alert' should pop-up which provides a warning about the host key. Click Yes.
5.) You are connected.
If you are working on your own computer, you will need to download and set up the CSUG lab virtual machine in order to have access to a copy of the lab's Linux environment. First you have to obtain the disk image as follows:
\\en-csuglab.files.cornell.edu\en-csuglab\courses\vm and copy the file en-cs-uglab-vm-x11-20140206.7z onto your local computer. csug01.csuglab.cornell.edu or csug02.csuglab.cornell.edu or ... or csug03.csuglab.cornell.edu. Log in with your NetID and password. Then go to the directory /courses/vm and copy en-cs-uglab-vm-x11-20140206.7z over to your computer.smb://en-csuglab.files.cornell.edu/en-csuglab/courses/vm and click Connect. Then copy en-cs-uglab-vm-x11-20140206.7z over to your computer.scp [netid]@csug14.csuglab.cornell.edu:/courses/vm/en-cs-uglab-vm-x11-20140206.7z ~/Desktop" where you replace [netid] with your NetID. If asked a yes/no question, type in yes. You will be asked for your NetID password. After that, scp will copy the virtual machine disk image to your Desktop.
scp [netid]@csug14.csuglab.cornell.edu:/courses/vm/en-cs-uglab-vm-x11-20140206.7z ~/Desktop" where you replace [netid] with your NetID. If asked a yes/no question, type in yes. You will be asked for your NetID password. After that, scp will copy the virtual machine disk image to your Desktop.
Login to the VM as root, with the password csuglab. We recommend that you setup a new user (follow the instructions here to create a new user) as soon as possible. Running commands as root can be dangerous, especially if you are new to Linux. Once you have created a new user and can log in, close the VM. Remember the username and password for your new VM user account, as you will need these later.
Now that you are logged in to your VM or a CSUG computer, it's time to familiarize yourself with some basic UNIX commands.
1.) The ls command shows you all the files in your current working directory. Try it out.
2.) Type the command mkdir hello_world to create a new directory called "hello_world". Afterwards, use the ls command again - you should see your new "hello_world" directory among the other files.
3.) Use cd hello_world to change your working directory to "hello_world". The cd command is used to change the current working directory.
4.) cd .. moves you up from your current working directory to its parent. Use this command now to return to your original working directory.
cd and ls let you navigate around directories and view files. You can also use the rmdir and rm commands to remove directories and files, respectively. Be careful when using these. Finally, man lets you get help on how different commands work. Call it using man command_name.
Working on another computer remotely is useful for this course and in many other cases, but you'll also need a way to get your work from CSUG or the VM on to your personal computer. This is necessary to submit files to CMS, for example. We'll install a text editing program called Sublime Text and use its FTP (File Transfer Protocol) capabilities to work with files on your CSUG account or VM.
Follow these steps to get Sublime Text and SFTP working:
1.) Download Sublime Text 3 here.
2.) Open Sublime Text and install the SFTP package by following the installation instructions here.
3.) Check that SFTP installed successfully. If it worked, you should see an "SFTP/FTP" entry under the "File" menu in the Sublime Text menu bar.
4.) Click on File -> SFTP/FTP -> Setup Server. A text document with server settings should appear.
5.) Edit the settings as described below. The settings you enter will be different depending on whether you want to connect to a CSUG machine or to your VM.
To connect to CSUG:
a.) On the "host" line, replace "example.com" with "csugXX.csuglab.cornell.edu", where XX is any number between 01-14.
b.) On the "user" line, replace "username" with your netID in quotation marks.
c.) Uncomment the "password" line (remove the //) and replace the second "password" with your Cornell password (i.e. the password you use with your netID to sign into Cornell web pages) in quotation marks.
d.) On the "remote_path" line, replace "/example/path" with "/home/netid". Substitute in your netID.
To connect to your VM:
a.) On the "host" line, replace "example.com" with "localhost".
b.) On the "user" line, replace "username" with the username of the user account you created for your VM, in quotation marks.
c.) Uncomment the "password" line (remove the //) and replace the second "password" with the password of your VM user account, in quotation marks.
d.) On the "remote_path" line, replace "/example/path" with "/home".
e.) Uncomment the "port" line and replace the default port "22" with "3410".
f.) In VirtualBox, select your VM and click Settings. Click Network, then Advanced, then Port Forwarding. Click the green button to add a new row, and change the Host Port to 3410, and Guest Port to 22
6.) Hit Save and enter a filename for your server.
7.) Click File -> SFTP/FTP -> Browse Server. Your server should now appear. Click on it to connect. If you're having issues connecting, check over your settings (File -> SFTP/FTP -> Edit Server) and, if you're still stuck, ask a TA for help.
8.) You are now connected to the CSUG machine/your VM. You can view files, open a file to edit, make changes, and then save, and the changes will be saved to the CSUG machine/VM automatically.
Remember, even though Sublime Text is running on your personal computer, when you are using SFTP/FTP, you are saving changes to a file that exists on a different computer - that is, the CSUG machine or your virtual machine.
Now that you can use Sublime Text to edit files on the virtual machine or on the CSUG computer, you should be able to write, compile, and run simple C programs.
Click File -> SFTP/FTP -> Browser Server again in Sublime, and connect to the CSUG or VM server you created in the previous step. Once you're connected, look for the hello_world directory you created in Step 1. Click on it to enter the hello_world directory, then click Folder Actions -> New file. Sublime will ask you for a name for the new file, call it "hello.c" . Type in the following C program:
#include <stdio.h>
int main() {
printf("Hello world! I am [netid].\n");
return 0;
}
But replace [netid] with your NetID. When you are done typing, press Ctrl+s to save. SFTP will save your changes to the VM/CSUG. Now you are ready to compile and run the program you just created! Log on to your VM, or ssh on to the CSUG machine. Navigate to the hello_world directory if it's not your current working directory, and run the command:
gcc -o sayhello hello.c
If this gives you any errors, go back to Sublime and fix the program. Otherwise, you have just compiled a C program. You can run your program by running the command:
./sayhello
And your program should run! It should print "Hello world! I am [netid]." and do nothing else.
What does ./sayhello mean? The C compiler (GCC) has compiled your source code hello.c into a program sayhello. The command ./sayhello means, "Run the program sayhello in the current directory."
hello.c and sayhello files to CMSNow you must upload the hello.c and sayhello files to CMS. To do this, you must first get those files out of the virtual machine.
On Mac or Linux, you can transfer files by using the scp command from the command line. The scp command is called with scp host:source_filename destination_directory. To get hello.c from CSUG or your VM on to your personal computer, open a new terminal window, then type in one of the following commands.
If using CSUG: scp netID@csugXX.csuglab.cornell.edu:~/hello_world/hello.c ~/Desktop
Substitute in for your netid and csugXX.
If using the VM: scp -P 3410 VMusername@localhost:~/hello_world/hello.c ~/Desktop
Substitute in for your VM username. Note that we have to specify the port to use with the -P 3410 option. You may be prompted with a confirmation message, type "yes" and hit Enter if this happens. You will also need to enter the password for your VM user and hit Enter again.
The scp command you entered copies hello.c from the remote host to your Desktop. Feel free to replace ~/Desktop with any directory on your personal computer that is convenient for you. Note that ~ is shorthand in UNIX for the home directory. Next, use the same command with sayhello instead of hello.c to transfer the sayhello file.
On Windows, the most efficient way to transfer files is to use an SCP/SFTP client, such as FileZilla.
First, you will need to set up port forwarding on your VM (if you are using CSUG, skip this step). In VirtualBox, select your VM and click Settings. Click Network, then Advanced, then Port Forwarding. Click the green button to add a new row, and change the Host Port to 3410, and Guest Port to 22. You can leave the others as their default values.
Now, open FileZilla and type sftp://localhost into the host box, root (or whatever user you created, if you did) into the username box, csuglab (or the password for your newly created user) into the password box, and then 3410 into the port box. Now, you can hit Quickconnect to connect to the VM.
You should see the filesystem of your local machine on the left, and the VM on the right. You should then be able to find the hello.c and sayhello files, and you can drag them over to your local filesystem.
Once you've transfered your files using scp or FileZilla, upload both files to CMS, and pat yourself on the back. You've officially completed your first C program!
This occurs if your netID has not been given CSUG access, which is often the case if you are not an affiliated CS major (but can happen sometimes even if you are a CS major). If you are in lab, let one of the TAs know and he/she will be able to provide you a flash drive with the VM files so that you can complete the lab.
On a Windows computer, the Red Hat 64-bit option does not show up in VirtualBox.To fix this, you must turn on Hardware Virtualization in your computer's BIOS. If you aren't sure how to do this, ask a TA for help.
I've created my VM in VirtualBox, but it freezes when I try to turn it on.Force quit your VM (Make sure you quit the VM and not VirtualBox. Use command-option-esc on Mac or ctrl-alt-delete on Windows). In VirtualBox, right-click on your VM and click "Settings". Click on "Storage", then "Controller: SATA" and then check "Use Host I/O Cache". Your VM should now work.
I can't connect to my VM in SFTP.SFTP only works if your VM is currently powered on. If your VM is powered on and SFTP still does not work, we'll have to change some settings in VirtualBox. Exit your VM, and click "Power off the machine" if prompted. In VirtualBox, right-click your VM and click "Settings". Go to "Network" and open the "Advanced" settings. Click on "Port Forwarding". Double check that the Host Port matches the port in the SFTP server settings - both should be 3410. If the port is correct and your VM still does not work, exit the Port Forwarding settings. Change the "Attached to" setting from NAT to Bridged Adapter. Power on your VM and try connecting again.
Section 1.3 of the assigned book "C: A Reference Manual" is a good overview of C programming, and it includes a "Hello World" example like this lab.
Part 1 of the alternate assigned book "All of Programming" is another good introduction to C programming. It also has an appendix that teaches the basics of UNIX, emacs, debugging, and other useful tools. You can download a book excerpt from the above link, the last 20 pages of which is all about UNIX.
For more information on Sublime Text: Sublime Text and its SFTP plugin