15-123 Systems Skills in C and Unix
Recitation 1

Navigating your account in Linux
Editing your program
Compiling your program
Running your program
Accessing your Andrew files using ssh
Exercise how to hand in your program

Navigating your account in Linux

Open up a Terminal window to access the Linux command prompt. (Unlike typical graphical interfaces for operating systems, here you are entering commands directly to the OS and can change hundreds of options to give finer control of what you're doing.) On the Linux machines in the computer labs in Gates (rooms 5205, 5201, and 3000), you can access a Terminal window using the Applications --> Accessories menus.

In the Gates computer labs, your terminal window will give you a prompt and you will be in your home directory in your Andrew account. Type ls (to list files) to see what files and directories are there. Back in the day, we used to call folders "directories". To move to another directory, use the cd command (for "change directory"):

cd private

ACADEMIC INTEGRITY NOTE: You should store your program files inside the private directory (or a subdirectory inside this directory) since this directory is automatically set to prevent viewing by other users. Remember that you should protect your code from being viewed by other students as part of the academic integrity policy for this course.

Since you will write a number of programs for this course, it pays to make a subdirectory inside the private directory. Once you cd into the private directory, make a new directory named 15123:

mkdir 15123

Now go into this directory using cd again:

cd 15123

To go back up one directory if you need to, use this command:

cd ..

You can go up two directories and down another directory (e.g. public) like this:

cd ../../public

If you ever get lost while you change from one directory to another, you can use pwd to find your present working directory. Or if you want to go back to your home directory, simple type:

cd

You can also access any directory from your home directory using the shell variable $HOME or the "tilde" character ~. For example, you can get to your 15123 directory from any directory on Andrew by typing this:

cd $HOME/private/15123

or this:

cd ~/private/15123

LOGGING OUT: Remember that you must log out from the computer in the Gates lab. Closing the terminal window is not enough! To log out, go to the System menu and choose Log Out. You don't want others accessing your account if you leave yourself logged in.

COURTESY NOTE: In the Gates labs, if you don't use your lab computer for a set period of time, the screen saver will launch and you can't access the terminal unless you type your password in. Some students have left the computer this way so they can "reserve" the machine for themselves later. If we find a machine in screen saver mode and no user at that terminal, we will automatically log you out so you may lose work if you're not careful. Please be considerate of others and don't lock the terminal.

FOR MORE INFORMATION:

Computing Services - Unix Help Page

Editing your program

You can use any editor you wish to write and edit your programs but we highly recommend you try out emacs since this editor can do much more than just help you edit your code (as you will see).

To set up emacs for your Andrew account, you will need to create a file named .emacs in your home directory. You can do this with emacs:

cd
emacs test.c

type some thing


Save the file and exit emacs (if desired).

When you want to edit a program now, you can enter emacs specifying the file name you want to edit. You should be in the directory where the file is or should be (to keep things simple for now). For example, to open the file test.c in emacs, you'd go to the directory where this file should be stored and enter:

emacs test.c

If the file exists, it will be loaded into the emacs editor. If the file does not exist, you will get a blank editor to start typing in the code.

FOR MORE INFORMATION:

LinuxHelp.next Emacs Help

Compiling your program

Now to compile your code, go to the directory that contains your C code and type:

gcc filename.c

If your code compiles without syntax errors, then you will just get the Linux prompt back. If there are syntax errors, you will get one or more (ugly) messages. The key here is to look at the first message for the line number where things started to go wrong.

If your program has more than one file, simply list the files one at a time on the command line when you compile (with dynamic checking if desired):

gcc filename1.c filename2.c filename3.c

Running Your Program

Once your program compiles without syntax errors, it will generate an executable file in the same directory named a.out. To run this program, type:

./a.out

This command says to look in the current directory (.) and run a.out. Normally the system doesn't look in the current directory for executable programs unless you have "." in your PATH.

If you compiled your program with the -d option, then your program will run and all contracts/annotations will be checked during runtime. Examine your output for runtime and logical errors and for contract violations. If you have any, go back to edit your code again with emacs, save the file, recompile and run again.

In some cases, when you run your program, you will need to specify additional command line options (similar to when you compiled your program). Look for further instructions in these cases.

Accessing your Andrew files using ssh

You can always access your Andrew files on the Andrew Linux server in the Gates labs (rooms 5205, 5201, and 3000) when the labs have no classes scheduled. In addition, you can access your files via ssh in several ways. ssh (secure shell) a protocol for securely logging into remote servers like the Andrew system

Windows machines: Download a free ssh client like PuTTY. When you log in, specify the server linux.andrew.cmu.edu.

Mac and Linux machines: Open up a terminal window and run this command:

ssh -l yourusername linux.andrew.cmu.edu

and enter your password when prompted to log in. (Your machine might ask if you want to add the server to a list of known hosts; if so, answer yes.)

Exercise

Type in the csort code give to you on the course website, compile it and run it to make sure you can follow this process for a real C program. As you do this, read through the code to see how it works. Examine the test cases. Feel free to modify the code to generate errors so you feel more familiar with the process described above.

Generating history of commands

for each lab assignment you are required to submit history of commands file. For example if you are working on lab1 for the first time, type the following *before* you start the session > script session1.txt This will write the commands and output history of the session to session1 file. Second time, you run lab1, write it to session2.txt file. You can clean up (delete) any commands that are not relevant to your working on program. At the end of your program development, you must have few session files saved. These needs to be saved in a s subfolder named sessionfiles and handin to your lab folder. You are also encouraged to report your times manually. see selflog.txt for a sample. You will get 3 points for these log activities.

You should do this exercise as soon as possible since you will need to edit, compile, and run several programs for your first programming assignment shortly. If you have any problems or questions, feel free to see any teaching assistant during office hours or a course assistant in the Gates lab during evening help hours. See the course website home page for locations and times.


Handin

Once you are successful in compiling and runnning your csort.c program learn how to hand it in to /afs/andrew/course/15/123/handin/SL/SL0/yourID If you do not have a folder, please let a TA know about this asap. If you added the class after sunday please send email to guna@andrew.cmu.edu

written by Ananda Gunawardena, 01/10/11