next up previous
Next: Copyright notice Up: Computer Networks/Computer Networks and Previous: Textbook

Subsections


Programming

This course is primarily based on Linux, one of the many flavours of Unix. Programming in this course happens in principle in C++, but I am also accepting (and supporting) C.

This section sketches the programming process under Linux. Read it carefully, you will need all of this for your programming assignments.


Reference Material

Unix and network programming

C++

There is a huge amount on information on the Web about C++ programming (and programming in general). Here are some of these documents:

G++, the GNU C++ compiler

Makefiles

Note that the makefiles presented in class are good for GCC. In Windows, the name of the compiler should be changed, as well as the compiler options (in particular, options are introduced in Windows by / instead of -). In addition, the make command supplied with Microsoft Visual C++ appears to be actually called nmake. Consult the documentation that comes with your system for details.

C++ Compilers

Editors

Programs may be composed and edited using any text editor, such as notepad on Windows. In any case, make sure that you have a good text editor. A programmer's editor is a very different thing from a word processor. Most importantly, it saves your work in plain text files and it doesn't insert extra carriage returns beyond the ones you actually type (so, Microsoft Word does not qualify). A good programmer's text editor will do a lot more than this, including:

There are many text editors that have some or all of these features. The most widely used editors on UNIX systems are vi and Emacs (or a variant called XEmacs). They are also subjects of a long standing holy war. Various ``modes,'' supporting various programming languages, exist for both of them.

You are of course free to program in any editor you like (including ed). For this course though, I recommend either XEmacs or Kate (they are both installed in J118).

My personal preference is XEmacs, but in honesty it does not have such a good C/C++ support as it does for Java, Prolog, or Lisp. It does though offer auto indentation, syntax highlighting and so on.

The disadvantage of using XEmacs is that XEmacs interface is quite different from anything you have probably seen. While most commands you are likely to use are accessible from menus or the toolbar, keyboard shortcuts do come handy, and XEmacs' are quite different from a usual Windows environment. Here is a list of the most used commands and their shortcuts (in Emacs-speak, C stands for Control, and M stands for Meta; the Meta key is usually bound to Alt, but Esc can be used as well: M-x stands thus for pressing (and holding) Meta/Alt and then pressing x, or pressing (and releasing) Esc and then pressing x):

Command XEmacs-speak Shortcut
Open find-file C-x C-f
Save save-buffer C-x C-s
Save as write-file C-x C-w
Copy kill-ring-save M-w
Cut kill-region C-w
Paste yank C-y
  XEmacs function call M-x function-name

Unix Documentation

During the labs, you will work under Linux. The major difference between Linux and Windows is that one way of interaction with the system happens through a command line interface (CLI), much like the DOS prompt but much more powerful. You have however the usual window system (a bunch of them actually) at your service.

Bringing Your Program to Life

The following is a quick guide to what you have to do to write, compile, and run a program. The guide assumes that you write C++ code; if your code is actually C, the guide still applies with the following changes: (a) source files have extension .c instead of .cc, and (b) you should use gcc instead of g++.

  1. Obtain a terminal.

    Once you log into a machine in J118 you end up with a desktop in front of you similar to the desktop of a Windows workstation. There exists on the desktop an icon called ``Terminal.'' Double click on it, and you will obtain a window featuring a prompt looking like this: [bruda@j118-18 ~]%. Unless otherwise noted, the remainder of these steps will be accomplished by typing commands in this window which is henceforth called ``the terminal window.''

  2. Create a place for your program.

    It is easier to manage things if you create a separate directory for each of your programs. Let's say that you want to put your program in a directory called foo. First, you create foo:

        mkdir foo
    

    and then you change directory to foo:

        cd foo
    

    Also see the note on directories.

  3. Create your program.

    First, you open an editor (say, XEmacs) to create your program. Let's say that your decide to call your program foo.cc. Type then:

        xemacs foo.cc &
    

    Another window will open. This is an editor, so go away and write your program then click ``Save.''

    If you prefer Kate to XEmacs, substitute kate to xemacs in the above command.

    Copy and paste
    Under Unix GUIs, selecting some piece of text also places the selected text on the clipboard, so there is no need to press any key to copy it. To paste the clipboard, place the mouse cursor in the desired position and press the middle button.

  4. Compile your program.

    Get back to the terminal window, and type

        g++ -g -Wall -o foo foo.cc
    

    If no errors appear, you will end up with an executable called foo. If the name foo is not to your liking, change the above command. For example, if you want to call your executable big_hairy_program, then issue the following command instead:

        g++ -g -Wall -o big_hairy_program foo.cc
    

    If you wonder about the meaning of -g and -Wall, see the section on manual pages.

  5. See what you have.

    To list a directory, type

        ls
    

    This will display the name of the files that reside in the current directory. If you want a more detailed listing, type instead

        ls -l
    

    On most Linux systems, you can also type

        ll
    

    If you want more information about ls, see the section on manual pages.

  6. Run your program.

    Assuming that the name of the executable is foo, type:

        ./foo
    

    Your program will do its stuff (why ``./''? see the note on directories).

  7. Stop your program.

    So you wrote this infinite loop and your program just stays there and runs happily no matter what you do. Most programs can be actually stopped by going to the terminal they run into and typing control-c (i.e., press and hold control and type c).

  8. Change your program.

    You also need to change your program from time to time (let's say that it won't compile). To do this, go to the editor window, do whatever changes you wish, and click again ``Save.'' Repeat then from Step 4.

  9. Move information around.

    Let's say that you want to create a new program based on an existing one (which you do not want to destroy). In this case, the first thing to do is to copy someplace else the original program. You may also want to move files around.

    To move the file or directory source to the directory or file dest do:

    mv source dest

    Both source and dest can be either relative or absolute (see the note on directories for the meaning of absolute and relative). For instance,

        mv foo.cc foo.cpp
    

    will move (rename) the file foo.cc in the current working directory to foo.cpp, and

        mv foo.cc ~/courses/csc414
    

    will move foo.cc to the directory ~/courses/csc414.

    To copy a file file to the directory or file dest do:

    cp file dest

    Again, both file and dest can be either relative or absolute.

    To delete a file file (given with relative or absolute path) do:

    rm file

    See the manual pages of cp and mv for more options.

    Caution. If you move or copy a file over another file with the same name, then the latter will be lost. There is no way in Unix to recover overwritten or deleted files.


Note on Directories

In Unix, all the files accessible on a given machine reside in a directory tree whose root is called / (``the root directory''). This is similar to Windows but not the same--indeed, on a Windows machine you have a directory tree for each disk rather than for the entire machine.

At any time, one directory is the working directory. You can find the current directory by typing in a terminal:

    pwd

All of you files reside in a directory called ``home directory,'' which is of course somewhere inside the / directory. This is your working directory when you log into a Unix machine.

You can change to any directory by using the cd command. You can give an absolute or relative path to the new directory. The following is an example of an absolute path:

    cd /usr/local/bin

will change the working directory to /usr/local/bin (meaning ``the subdirectory bin of the subdirectory local of the subdirectory usr of the root directory''). You could also specify the new working directory relative to the current working directory by omitting the first /. For example, the command:

    cd usr/local/bin

changes the current directory to the subdirectory bin of the subdirectory local of the subdirectory usr of the current working directory.

To make your life easier, there are some special directory names:

Name Meaning
~ your home directory
. the current directory
.. the parent of the current directory


The Manual Pages

Most Unix commands and C functions are documented in manual pages. To obtain the documentation for a Unix command (say, ls) go to a terminal and type:

    man ls

Similarly, the documentation for g++, the GNU C++ compiler is obtained by typing in a terminal

    man g++

The manual pages are organized in sections. You can find manual pages for publicly available commands (Section 1), system services (Section 2), library functions (Section 3), and so on. When you type

    man time

the manual pages will be searched for an entry called time starting with Section 1, and the first occurrence will be displayed. In this particular case, there exists an entry for time in Section 1, so this is what you will end up with. However, there also exists a manual page for time in Section 2. How do you reach it? By passing a section number to the command man using the -S switch. For example,

    man -S2 time

will display the documentation for time found in Section 2 (-S2).

Manual pages are searchable. To find pages containing the string time do:

    apropos time

If you want to look for complete words only, do:

    whatis time

For more information about what I just wrote, use the...(drum roll) manual pages, i.e., issue the following commands:

    man man
    man apropos
    man whatis

To Dig Further

Here are some tutorials that offer a quick introduction to Unix.


Working Remotely on the J118 System

So it's past 11 pm, the lab is closed, but you have to submit this assignment for CSC 414. Well, you actually can.``Remote login'' (i.e., having access to a machine from a remote location) is a built-in facility in Unix.

Remote login:
In Unix, obtain a terminal and type

ssh username@linux.ubishops.ca

where username is your J118 user name. Once presented with a prompt on the remote machine you can launch any application you like (such as submit), including graphical applications (whose windows will show up on your screen--the Unix SSH client does display forwarding for you automatically).

Under Windows you need an SSH client such as putty. Launch then the client, enter linux.ubishops.ca in the address/host field (don't remember the exact name of the field, sorry), and make sure you check the ``SSH'' radio button. Log then in using the same user name and password you use in J118, and you will end up with a terminal similar to the one you have used already. No graphical interface is available in this setting, but you can open as many terminals as you wish by repeating this connection procedure. This should at least allow you to use the submit program.

File transfer:
To copy files to and from the J118 system you need a SCP client. Under Unix such a client is called scp and works almost like cp except that either the source or the destination may be on a remote machine and specified under the following form:

username@machine-name:remote-directory

with username the name of your account on the respective machine, machine-name the name of the machine you want to copy from (or to), e.g., j118-n.ubishops.ca, and remote-directory the thing you want to copy (or into which you want to copy, as the case may be). Use the -r switch to recursively copy entire directories.

A graphical SCP client for Windows is WinSCP.


next up previous
Next: Copyright notice Up: Computer Networks/Computer Networks and Previous: Textbook
Stefan Bruda 2012-04-28