csc 552.201 - advanced unix programming, fall, 2008 welcome to unix system programming! tuesday,...

20
CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

Upload: willis-wilkinson

Post on 04-Jan-2016

217 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

CSC 552.201 - Advanced Unix Programming, Fall, 2008

Welcome to UNIX System Programming!

Tuesday, September 2 uses Monday’s schedule!

Page 2: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

Who am I?

• Dr. Dale E. Parson (a.k.a. Professor Parson), http://faculty.kutztown.edu/parson/

• In a previous life I was an AT&T technician and then a Bell Labs software engineer for over 25 years. Also, I consult as Amplified Computing.

• I have taught at Albright College, Millersville University and Lehigh University.

Page 3: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

Who are you?

• Excited Kutztown University computer science grad students who want to become or may already be Unix System software engineers!

• You have already taken CSC 352, UNIX: Systems Programming and Administration, or you have permission of the instructor.

• This course deals with the study of the UNIX operating system, particularly systems programming and administration. Under the former, such topics as UNIX commands, filters, shell scripts, system security, user accounts, system backup and rebooting, and associated utilities are studied. In addition, software procurement, using the WWW, and installation will be illustrated. Under the latter, such topics as file primitives and directory access, system utilities, processes, signals and signal handling, inter-process communication, semaphores, file and record-locking, sockets, and terminal manipulation are studied. Meaningful applications which illustrate the topics will be examined. (Ah, this is the fine print for CSC 352!)

Page 4: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

What are we planning to do?

• We will build multi-process, multi-threaded and distributed software systems in Unix.

• We will use C++ interface and module construction techniques.

• The underlying building blocks are mostly C language system calls and library functions.

• man –s2 write (or read, fork, exec, dup, open, close, pipe)• man –s3c fprintf (or fgets, fopen, fclose, popen)• We will not use C++ iostream in this course.

Page 5: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

References

• We start with the First Day Handout (syllabus).• gcc and g++ - http://gcc.gnu.org/onlinedocs/

http://sunsite.ualberta.ca/Documentation/Gnu/gcc-2.8.1/man/gcc.1.html http://sunsite.ualberta.ca/Documentation/Gnu/gcc-2.8.1/man/g++.1.html

• GNU make http://www.gnu.org/software/make/manual/ • Invoke gmake on SunOS systems.

• gdb - http://www.gnu.org/software/gdb/documentation/ • Solaris - http://docs.sun.com/app/docs/coll/40.10 • Use man command for section 1 utilities such as ls and ps.• /export/home/faculty/parson/UnixSysProg on

bill.kutztown.edu

Page 6: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

An simple example makefile• makefile in UnixSysProg/lecture1

all: buildTARGET = printdemoDEBUG = 1include $(HOME)/makelibbuild: $(TARGET)

$(TARGET): $(OBJFILES) $(CPPCC) $(OBJFILES) -o $@

clean: subclean /bin/rm -f $(TARGET) $(TARGET).out $(TARGET).dif

test: $(TARGET) ./$(TARGET) a b c d e > $(TARGET).out diff $(TARGET).out $(TARGET).ref > $(TARGET).dif

Page 7: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

makelib in my $HOME/• makelibCPPCC= g++CXXFILES := $(wildcard *.cxx)OBJFILES := $(subst .cxx,.o,$(CXXFILES))DEFFLAGS =ifeq ($(DEBUG),1)DEBUGFLAG = -gelseDEBUGFLAG =endifINCFLAGS = -I.CPPFLAGS= $(DEFFLAGS) $(INCFLAGS) $(DEBUGFLAG)

%.o : %.cxx $(CPPCC) -c $(CPPFLAGS) $<

subclean: /bin/rm -f *.o

Page 8: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

Some other files in my $HOME/• .bash_profile-bash-3.00$ cat .bash_profileexport EDITOR=/usr/local/bin/vimexport VISUAL=/usr/local/bin/vimalias vi=vimalias make=gmake• .vimrc-bash-3.00$ cat .vimrcset aiset ts=4set sw=4set expandtabset sta

Page 9: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

Interface definitions split source along module / driver boundaries.

• printargs.h defines the interface to its module#ifndef PRINTARGS_H#define PRINTARGS_H#include <stdio.h>/* Function: printargs Prints an array of strings to an output file. Parameters: outfile: output file for writing. stringv: array of \0-terminated strings to be printed. stringc: numbers of strings in stringv Return value: none Side Effects: none Preconditions: The outfile must be a non-NULL, open FILE. stringc must equal the number of strings in stringv. Each string in stringv must be terminated with a \0 character. Postconditions: none (no return values from this function). Invariants: none*/voidprintargs(FILE *outfile, const char **stringv, const int stringc);#endif

Page 10: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

Implement the interface in a .cxx file.• printargs.cxx implements the module of printargs.h/* printargs.cxx -- Function implementations for printargs.h.

CSC552, Fall, 2008, Dr. Dale Parson, Class 1 example.

This is a simple demo library module that prints an array of strings to an output file.*/

#include "printargs.h"

voidprintargs(FILE *outfile, const char **stringv, const int stringc) { int i ; for (i = 0 ; i < stringc ; i++) { fputs(stringv[i], outfile); fputs("\n", outfile); }}

Page 11: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

Invoke the interface from a client module or test driver.

• printmain.cxx is the test driver/* printmain.cxx -- main function that invokes printargs.

CSC552, Fall, 2008, Dr. Dale Parson, Class 1 example.

This is a simple driver for a demo library module that prints an array of strings to an output file.*/

#include "printargs.h"

intmain(int argc, const char *argv[]) { printargs(stdout, argv, argc); return 0 ;}

Page 12: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

Makefile-driven testing• makefile is structured for testing-bash-3.00$ make clean test/bin/rm -f *.o/bin/rm -f printdemo printdemo.out printdemo.difg++ -c -I. -g printargs.cxxg++ -c -I. -g printmain.cxxg++ printargs.o printmain.o -o printdemo./printdemo a b c d e > printdemo.outdiff printdemo.out printdemo.ref > printdemo.dif

Page 13: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

GDB for debugging-bash-3.00$ gdb --args printdemo a b c(gdb) break mainBreakpoint 1 at 0x107f4: file printmain.cxx, line 13.(gdb) break printargsBreakpoint 2 at 0x10780: file printargs.cxx, line 14.(gdb) runStarting program:

/export/home/faculty/parson/UnixSysProg/lecture1/printdemo a b cBreakpoint 1, main (argc=4, argv=0xffbffc54) at printmain.cxx:1313 printargs(stdout, argv, argc);14 (gdb) p argc15 $1 = 416 (gdb) p &argc17 $2 = (int *) 0xffbffc34

Page 14: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

GDB continued(gdb) contContinuing.Breakpoint 2, printargs (outfile=0x20b20, stringv=0xffbffc54, stringc=4) at printargs.cxx:1414 for (i = 0 ; i < stringc ; i++) {(gdb) step15 fputs(stringv[i], outfile);(gdb) next/export/home/faculty/parson/UnixSysProg/lecture1/printdemo16 fputs("\n", outfile);(gdb) next14 for (i = 0 ; i < stringc ; i++) {(gdb) p i$3 = 0(gdb) p &i$4 = (int *) 0xffbffb6c

Page 15: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

Program Image, Sample Layout

• Figure 2.1. How can we verify? gdb and /proc!high address argc/argv, env

stack activation recs.return addr.,params, savedregisters, autos

heap malloc or newuninitialized static datainitialized static data

low address program text

Page 16: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

Looking inside a process

• Use nm to get static layout information.• Supply C address-of operator to gdb.• /proc utilities examine process layout

• pstack [ -F ] PID examines stack• pfiles [ -F ] PID examines open files• pldd [ -F ] PID examines dynamic libraries• pmap [ –xlF ] PID examines memory layout

Page 17: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

Process status

• gdb is debugging printdemo-bash-3.00$ ps -al S UID PID PPID TIME CMD O 11236 720 580 0:00 ps S 11236 569 29452 0:01 gdb T 11236 570 569 0:00 printdemo

• nm -psC printdemo | sort | more• This shows symbol layout in executable printdemo.

Page 18: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

C++ interfaces and dynamic loading

• ~parson/UnixSysProg/lecture1/oo_variant• We will walk through code and test execution in class.

• Important points• C++ abstract classes are used as interfaces.• Derived C++ concrete classes provide implementation.• An abstract factory method uses dynamic loading and

run-time type checking (RTTI) to load a concrete derived class and its concrete factory method.• An factory method runs a constructor.• See man pages for dlopen(), dlsym() and dynamic_cast.

Page 19: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

fork / exec / wait example code

• ~parson/UnixSysProg/lecture1/forkexec• We will walk through code and test execution in class.

• Important points• argc / argv command parameter usage• Use C string library to parse a command parameter.• fork()’s return value indicates parent or child PID.• getpid() returns child PID to child; getppid() gets

parent.• exec() returns only on failure.• Use pmap to examine both processes.

Page 20: CSC 552.201 - Advanced Unix Programming, Fall, 2008 Welcome to UNIX System Programming! Tuesday, September 2 uses Monday’s schedule!

Programming practices

• Always check and handle library return codes!• malloc() and new are exceptions that we will discuss.• Error logs, exit codes and C++ exceptions are useful.

• Buffer overruns are annoying and dangerous• See man page for gets(), for example.

• Make malloc/free or new/delete symmetrical.• Always use { curly braces } for control blocks.• Use both healthy and degenerate tests.

» Ignoring these rules will cost you points.