January 9, 2006
Coverage: [USP] Chapter 1
Introduction to CPS 445: Systems Programming II
- course objectives, outline, and policies
Why study this stuff anyways?
- communication and concurrency are everything in today's software
- ability to write reliable and secure code is indispensable
(counter-terrorism)
- gateway to studies in networking and system administration
Major Sources of Bugs
- poor indentation
- failure to check return values, esp. NULL
- failure to check error codes
Good Practices in C
when allocating memory, always verify that the memory was allocated
successfully
example:
if ((node_ptr = malloc (sizeof (Node))) == NULL) {
fprintf (stderr, "out of memory!");
exit (1);
} else {
...
exit (0);
}
once finished, always free memory that you explicitly allocated
example:
if ((node_ptr = malloc (sizeof (Node))) == NULL) {
fprintf (stderr, "out of memory!");
exit (1);
} else {
...
free (node_ptr);
exit (0);
}
when opening a file, always verify that the file was opened successfully
example:
if ((fp = fopen (filename, "r")) == NULL) {
fprintf (stderr, "cannot open %s\n", filename);
exit (1);
} else {
...
exit (0);
}
always close files that you explicitly opened
example:
if ((fp = fopen (filename, "r")) == NULL) {
fprintf (stderr, "cannot open %s\n", filename);
exit (1);
} else {
...
fclose (fp);
exit (0);
}
always print error and debugging
messages to stderr (output written to stdout is buffered)
example:
if ((fp = fopen (filename, "r")) == NULL) {
fprintf (stderr, "cannot open %s\n", filename);
exit (1);
} else {
...
}
Quick Review of Operating System Nomenclature
- program vs. process
- process vs. thread
- multiprogramming (vs. batch processing)
- timesharing
- context switch
- context switch time
- quantum (time slice)
- system call
- process control block (or PCB)
- execution stack
- activation record
- multiprocessor system
- asynchronous operation
- concurrency
- communication
- (asynchronous or synchronous) interrupt (hardware)
- (asynchronous or synchronous) signal (software)
- device driver
- job scheduling
- process scheduling
- paging
- UNIX is a multiprogramming and timeshared OS
Concurrency
- achieved in UNIX with functions fork, wait, and exec
- processes with a common ancestor can communicate through pipes
- processes without common ancestor can communicate through signals,
queues, semaphores, monitors, shared address space, messages
Distributed Computation
- client-server model, e.g., ftp and http
- object-based model
- theme: objects passing messages to each other
- encourages code reuse
- application (software) layers, e.g.,
- MPI (Message Passing Interface)
- CORBA (Common Object Request Broker Architecture)
- RMI (Remote Method Invocation)
- and other P2P (Peer to Peer) approaches
Buffer Overflows
- programs and data represented uniformly in main memory
- layout of the stack in main memory (stack grows from high to low memory)
- how a root shell might be attained through telnet
- 1988, Robert Morris' worm exploited a buffer overflow in
the finger daemon, nearly brought down the Internet
- CERT (Computer Emergency Response Team) formed
- some recent articles on buffer overflows
UNIX Standards
- POSIX (Portable Operating System Interface)
- IEEE standard for UNIX libraries to promote
the development of reliable software
- Solaris, Mac OS X, and many other flavors of UNIX are moving toward
POSIX standards