You need to have an account on the CSE/ECE Suns (this is not the same as your university LTS account). If you do not have an account, or do not remember your password, send e-mail to help@cse.lehigh.edu and share with a friend for today.
When you are not in lab, you can still work on these machines. To do so from a PC on campus, use ssh and connect to a machine such as: antares, ariel, europa, ganymede, mars, mercury, metis, phobos, pluto, puck, sirius, tethys, titania, triton, proteus, oberon, ceres, chiron, nix, xena, hydra, and pan. When not coming from a CSE machine, you'll need to use the full machine name, which is: NAME.cse.lehigh.edu.
Please read all of this lab text! There is lots of useful information here and if you ask a question that shows you haven't read it, we're likely to point you back here anyway.
On the Suns you are presented with a login window which includes the ability to choose a desktop environment (under one of the options). Make a selection -- I usually use the Java Desktop System (even though a little slow because it is similar to what I use under Linux), but other people like CDE (although it is a little old). XFce and fvwm are lightweight environments (read old) that some people like because they are fast. AfterStep is modeled after the NextStep environment, and is another reasonable possibility. Choose any for now, and you can choose a different one later. In fact, I encourage you to try additional environments (after completing the lab exercises).
Window Managers. Once you have logged in, explore the interface. Notice that these window managers treat application windows differently. Buttons to minimize, close, resize, etc. are all functionst that can be mapped by the window manager. If your window manager didn't automatically open a shell console or terminal window, look for the tool that will do so and open it. You'll need it for the rest of this lab. Once you have one open, you can open additional ones the same way, or start one or more xterms from the one shell that is running.
Copying and Pasting. X-Windows provides a simple and intuitive way to copy and paste using a mouse. Whenever you highlight text (anywhere) using the left mouse button, that text is available for pasting. Pasting is performed by clicking on the middle mouse button (or both buttons together on a two-button mouse).
Web Browsers. While some window managers have icons and links to web browsers (e.g., Konquerer), most likely they are for old browsers. However, from your shell you can always type firefox, which is modern. You need to start a web browser and find today's lab to see the rest. (Hint: start at the course home page.)
Editors. Similarly, some window managers may have a link to editors, but probably not for vi or emacs. You can type their names at the shell. If you run emacs, you'll get the X-windows interface automatically whenever it can run (unless you specify the -nw option to prevent it).
Office Applications. Some windowing environments will include menu items for office applications such as word processors and spreadsheets. An additional one that might be of interest is OpenOffice and Sun's version, called StarOffice. StarOffice is free for educational use, is available for many platforms, including Solaris, Linux, and Windows, and can read and write Microsoft Office compatible files.
Remote Execution. One thing that makes X-Windows great is the ability to log into a machine remotely, and then have a program use your local display. You can do this too---just ssh from your machine to another (that is, type something similar to ssh -X <newhost>), and then run a program (that uses windows) on the other machine and see it show up on your screen. (Note that ssh typically requires the use of an option like -X or -Y to set up the X-Windows forwarding.) This separation of display and execution makes it easy to use high-powered, centralized servers, even though you might not be able to sit in front of them. Lehigh provides a number of such computational servers (such as vega.cc.lehigh.edu which has 32 processors and 128GB of RAM and runs Linux).
New to UNIX? If you've never used UNIX before, please tell me and consider working through a tutorial such as this one. Lehigh's LTS often offers UNIX seminars that might be of use as well.
Thanks to Prof. Ralph Droms (now at Cisco) for this lab exercise.
.c, and object modules are identified by the suffix
.o.
gcc supports the construct #include
"filename" which copies the contents of
filename from the current working directory into the
module being compiled. The construct #include
<filename> copies the contents of filename
from a standard library of header files into the compiled module.
Typically, all the external definitions are collected into a single
header file with suffix .h, which is then included in all
the C modules. Copy the files from directory
~brian/cse342/lab01
into a directory of your own. The three files: decls.h,
main.c, foo.c demonstrate the use of
separate compilation and a .h file.
To compile the example programs, we can use:
gcc -c main.c gcc -c foo.c gcc main.o foo.oalthough later we will see a better mechanism to compile your code. The first two commands generated object files (e.g., main.o and foo.o), and the last command generated the actual program that can be run, called
a.out.
Emacs automatically processes C language files under a special ``C mode''. There are lots of useful key bindings in C mode; the most useful are:
^j: (control-j) go to next line and indent.
M-;: (meta-;) add a comment to the end of the current
line.
M-^j (meta-control-j when already in a comment):
continue the current comment on the next line.
make automates the procedures for
compiling files into programs. To rebuild a program, make
reads a database of commands from a file called Makefile.
The commands are of the form:
target: dependency1 dependency2 dependency3 ...
action1
action2
.
.
.
where target is the name of a file to be recompiled,
dependency1 ... are the files on which
target depends and action1 ... are the
actions to construct target. Note that make
is picky, and the whitespace before each action must be
a tab, not spaces.
Without any additional parameters, make will attempt only the
first target: line it finds, unless that line contains
additional dependencies that are addressed later in the Makefile.
The file
Makefile in ~brian/cse342/lab01 is an example of
a make database to compile the C modules in
~brian/cse342/lab01.
To experiment with make:
make to construct the program you
copied from ~brian/cse342/lab01.
.c files and use make
again. Notice that only the file you modified is recompiled and the
executable module is relinked.
.h file and use make
again. Because both .c files depend on the
.h file, both .c files are recompiled before
the executable module is relinked.
All modified files are saved before the compilation is run. The output
of the compilation is displayed in a separate buffer. If there are
syntax errors, ^X-` moves the cursor to the line containing the
next error, reading in the appropriate source code file if necessary.
int main(int argc, char *argv[])
argc gives the number of command line
parameters to this program, and argv is an array of character
strings that contain the parameters themselves. For example, the
command line input:
% ./foo bar baz
(where % is the prompt) would result in:
argc = 3 argv[0] = "foo" argv[1] = "bar" argv[2] = "baz"
The program:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
for (i = 0; i < argc; i++)
printf("argument %2d = %s\n", i, argv[i])
return 0;
}
will simply copy its command line arguments to standard output.
Every progam has access to standard input (stdin),
standard output (stdout) and error output
(stderr) through file descriptors 0,
1 and 2, respectively. A program can
perform file I/O through system calls on these file descriptors
without taking any other action.
read and write accept
input from and send output to files identifed by file descriptors.
read and write take three parameters:
the file descriptor,
the address of a buffer for the data and
the number of bytes to read or write. read
and write return the number
of bytes actually read or written.
int n_read, fd, n; char *buf; n_read = read(fd, buf, n); n_written = write(fd, buf, n);The following program copies its standard input to standard output:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
char buf[BUFSIZ];
int n;
while ((n = read(0, buf, BUFSIZ)) > 0)
write(1, buf, n);
return 0;
}
Try compiling and running this program. Without any I/O redirection,
the program will copy from the keyboard to the screen (you will
get two copies of what you type as the keyboard input echoes each
line you type and then the program copies them). You can also redirect
input and output from/to files to copy from the keyboard to a file
or display a file on the screen.
open system call allows a program to attach an
existing file to a file descriptor for reading or writing:
#include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int fd, flags, perms; char *name; ... fd = open(name, flags, perms);
| Parameter | Type | Use | ||||||
name | Character string | Name of file to open | ||||||
flags | Integer | How to open file | ||||||
| ||||||||
perms | N/A | Always 0 |
fd = open("foo", O_RDONLY, 0);
The close system call closes an open file gracefully:
close(fd);
int main(int argc, char *argv[])
{
int fd_in, fd_out, nbytes;
char buf[512];
fd_in = open(argv[1], 0);
fd_out = creat(argv[2], 0644);
nbytes = read(fd_in, buf, 512);
while (nbytes > 0) {
write(fd_out, buf, nbytes);
nbytes = read(fd_in, buf, 512);
}
return 0;
}
copies the contents of the file identified by the first command line argument to a new file identified by the second command line argument.
'\n'. Use the wc command to verify
the counts that your program generates on a non-trivial file.
if ((a = b + c) == d) { ...}
is valid C code. Use this construct to eliminate a call to read() in the copy program.
Be sure to properly label your program listing; e.g., something like:
/**********************/
/* Joe Smith */
/* CSE342 */
/* Lab 1 */
/* 28 August 2007 */
/**********************/
as you will lose points for not doing so in assignments that are handed
in (as well as for lack of comments, white space, modularity, etc.).