COP 4600 (Fall 2007) Tutorial Session
“Introduction to C and UNIX based systems”
A.L.
Purpose of this tutorial
Basic navigation within Unix environment
Creating files and directories
Using an editor in Linux to write C code
Using a compiler to create a binary from source code
IMPORTANT: You need a UNIX account to proceed with this tutorial. If you don't have one, you will need to see Engineering Computing to have them create a UNIX account for you!
Tutorial 1.1: “Basic Navigation within Unix environment”
For the purpose of this tutorial you must be sitting behind a lab machine or download a ssh client. For Windows XP/Vista, you should use the free ssh client named Putty which you can download by clicking this link: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html). You should download the file called: 'putty.exe'.
If you're sitting behind a lab machine (otherwise go to step 3):
Press CTRL-ALT-F2 – This will switch you to terminal '2'. In theory every version of Linux has 8 different terminal you can access by pressing CTRL-ALT-F1...8. Although you could now login, I want to show you how to also open a terminal window in a graphical environment.
Please return to your graphical environment by pressing CTRL-ALT-F7.
Enter your UNIX user name and password
After login, select 'Terminal' from the Applications => Accessories menu on the top of your screen. This should open a new terminal window (= shell).
Type:
ssh
c4labpc11.csee.usf.edu
This
will establish a connection with PC 11 of the Linux lab.
Type:
w
This
will show you all the other local and remote users of this machine
currently using this machine.
Type:
exit
This
will return you (in this particular case) to your local machine.
Basically, ssh is for Linux what putty is for Windows,
namely a program that makes use of the ssh protocol to login into a
machine remotely. It is similar to a outdated protocol named telnet
that no longer is used due to security concerns (no encryption).
If you're following this tutorial from a Windows based PC connected to the Internet (otherwise skip this step):
Double
click putty,
and enter the following parameters:
Hostname:
c4labpcXX.csee.usf.edu
where
XX
is
a number between 11 and 29. For example: c4labpc11.csee.usf.edu
Then click open, it should open a windows asking you for your username.
After typing your password, you should see a prompt (or command line) which is your shell.
Tutorial 1.2: “Creating and removing directories and files”
It is important to figure out where you are. If you
type:
$ pwd (ignore
the $ sign, it is traditionally used as a placeholder for the prompt
character, you most likely will see something like
[myusername@c4labpc15 ~]$ ).
It
will print the path to the current directory, which in our case
should be your home directory.
Next you may want to know what is in your directory by
typing:
$ ls
Now, to create a directory for this course, you should do:
$mkdir -p
~/operatingsystems/tutorials/tutorial1/
This will create
the directory “operatingsystems”, which contains a directory
called “tutorials” which again contains a directory called
“tutorial1” in your home directory (symbolized by the tilde
symbol ~).
Now we will change to this directory. In UNIX we use the
shell in-built command called “cd”, which is short for change
directory. The following 4 ways of changing directories are the most
common:
$ cd
- = which changes to the last directory you visited
$
cd or $ cd ~ = which changes to your home directory or the
directory shown in $ echo
$HOME
$ cd
/specific/path/ = which changes to a specific path
$
cd .. = go to the directory in the level above (i.e. from
/foo/somedir/ to /foo/)
Please change to the directory we
created in step 2 by issuing:
$
cd ~/operatingsystems/tutorials/tutorial1/
Next we'll create a dummy
file (mind you, you don't need to do this if using an editor):
$
touch file1
If you run
ls, you should see it:
$
ls
file1
To remove our dummy file,
we issue the command:
$ rm
file1 (note: to remove directories you should use rm
-r, to remove all files
in a directory use $ rm
*)
Tutorial 1.3: “Using the editor and writing a simple C program”
There
are many editors to use for. Classically, the most common editor in
a Unix based system (and that used by countless professionals) is
called “vi”. The alternative developed by the GNU foundation
“emacs” is an alternative to “vi”.
However, we
believe that neither “vi” or “emacs” are suitable for
beginning students. We recommend that you therefore use either
“nano” or “pico” if you aren't familiar with vi or emacs at
this point.
To start editing a simple Hello World C source
file, we type:
$
nano helloworld.c (where
nano is the editor's name, and helloworld.c is the file to be
created).
Once
you are in nano, enter the following information:
#include
<stdio.h>
int main(int argc, char** argv)
{
printf("Hello World!\n");
return 0;
}
Now press CTRL-O to save the file, and CTRL-X to exit nano.
Next
we need to compile our source code, we use the gcc compiler to do
so:
$
gcc helloworld.c -o helloworld (please
use the -o option to define the name of the executable).
Tutorial 1.4: “Executing programs”
To execute the program from the previous exercise, we
simply issue:
$./helloworld
(the './' tells the
loader to look for the binary in the current directory).
You
can also execute your Hello World program by typing the full path
name to it:
This will print “Hello World” and should be
proof that the file was executed correctly. However, be aware that
only programs that are listed in your current directory can be
executed with the ' ./ ' in front it.
First type:
$ echo
$PATH
In a Unix based OS, the Kernel will look at a
users environment path to see what directories it should look
through to find a particular program to execute.
Linux executables, unlike Windows programs do not end in
.exe. Although you could in theory name executables to end in .exe,
it is not common to do so.
We will now try to execute a
system program called 'date' that lists the current date and time.
Please type:
$ date
The
Kernel will look into all directories listed on $PATH until it finds
the program 'date'. To find out where date is located, you need to
type:
$ which date
(which shows /bin/date ).
This concludes this first tutorial!