![]() | College of Engineering and Computer Science | School of Computer Science |
| [ANU][CECS][CS] | ||
CS Student Information Computer Networks Home page Computer Networks Overview Some useful links Some acronyms Computer Networks Lectures (2012) (requires authentication) Computer Networks Labs (2012) | COMP3310 - Computer Networks (2012)C Programming ExerciseThis lab exercise is designed to allow students to brush up on their "C" coding abilities or to introduce students to the "C" language. We will be making extensive use of "C" for many of the labs and for the second assignment. Most modern networking code is written in "C", the same language used to write most modern operating systems. For this reason, having a reasonable degree of "C" programming ability is essential to performing most network-oriented exercises. The following programming exercise should be attempted on the Linux machines in the Lab. You will need to have a "command-line" interface to the system. In the CSIT computer labs, the default desktop environment is "KDE", which may not provide a "Terminal" icon in the panel. If this is the case for your login, you can add one by clicking the right mouse button whilst the mouse pointer is over the panel at the bottom of the screen, then selecting "Add Applet..." and select "Terminal". Also, it is prudent to organise your files in a sensible way. This is a good time to create a directory for "Computer Networks"-related work, such as "Networks": mkdir Networks cd Networks mkdir Lab1 cd Lab1 Hello WorldThe traditional first "C" program is the "Hello World" code:
#include <stdio.h>
int main () {
printf ("Hello World\n");
}
Enter the above code using your favorite text editor (emacs, vi, kedit etc.) and save it as, say, hello.c. Next, you need to "compile" the program into an executable format. The make command is "clever" enough to know how to do this: make hello Should create an executable file "hello" in the same directory. Execute this program file using: ./hello If you don't understand everything that just happened, ask your tutor. Using a variableRewrite the same program to use a variable as the string to print. Use the man page to learn about using printf to print a string: man 3 printf Here, the 3 tells man to use section 3 of the manual, otherwise you will see the entry for a different printf in the first section of the manual. Call your new source file something else (eg. hello2.c) and use the above procedure to compile and execute the code. Using a functionWrite a function that takes an array of pointers to strings and prints each one until it reaches a NULL pointer (needs a loop). You can test the function by passing the "argv" array passed to the main function of your program. Modifying an existing programDownload the Modula-2 file Sort.mod to your own directory area. Examine it carefully, and then consider how to re-write it in C. You should also practice writing a Makefile for this exercise - it will come in useful for the second (programming) assignment. Also, compile with the -Wall (all warnings) compile option to make sure that you catch all discrepancies that the C compiler may pick up. Additional exerciseIf you are bored/keen, you can modify the sort program to sort lines of records, each record consisting of a sort key (a decimal number) followed by a string of text. Testing and sanity checkYes, this same sorting function is easily done by the Unix shell command to use sort
% sort -n [input file]
You are advised to use this to check your results - and compare the speed, using the Unix shell time command, as in:
% time sort -n [input file]
Your solution should allow for very very long - or short - lines of text; you should use a dynamic storage allocation technique (such as malloc() | |
| top of page | ||
| Author: R. Edwards: Phone: +61 2 612 54090; Fax +61 2 612 50010 Feedback: Please direct comments about this page to: Bob.Edwards@cs.anu.edu.au | Last Modified: |