|
Computer Science 322 Operating Systems Mount Holyoke College Spring 2010
|
|
Lecture 04: Unix Systems Programming
Date: Monday, February 8, 2010
Quote: UNIX system calls, reading about those can be about as
interesting as reading the phone book... - George Williams, 3/12/91
Agenda
- Announcements
- Lab 1 now due.
- Be sure to use the Spring 2010 site, not Spring 2008!
Search engines will send you to the old one.
- Lecture assignment recap
- Lab 2 Out
- you can get started right away on the basic framework
- we'll discuss all the needed system calls, hopefully by
the end of today, definitely by Wednesday
- Unix Systems Programming
- Error checking and reporting
- always check return values of system calls!
- errno variable
- perror(3), strerror(3)
- Process management
- PID queries: getpid(2), getppid(2)
- Process creation: fork(2), vfork(2)
- Process termination: exit(3), _exit(2)
- Parent notification: wait(2), waitpid(2)
- Starting new programs - exec system calls
- To fork(2) anything but a copy of yourself...
- execl(3), execv(3), execlp(3), execvp(3)
- varags vs. arrays, search path or not?
- Signals
- Interprocess communication mechanism
- kill -l to list signal types
- kill -SIGNAL pid to send a signal to a process
- Every process has signal handlers - react to signals
sent to the process.
- A default signal handler is attached when a process is
created
- signal(3) replaces signal handler
- custom function
- SIG_IGN, SIG_DFL
- setitimer(2) to set alarm to deliver SIGALRM
- kill(2) sends signals from a program
- Low-level file operations
- C stdio routines (printf and friends) are fairly high
level
- low-level operations: open(2), close(2),
read(2), write(2)
- work with file descriptors
- Pipes
- Another interprocess communication mechanism
- Powerful command-line tool:
|
- Unnamed pipe created with pipe(2)
- fd[0] is the "read end", fd[1] is the
"write end"
- these are file descriptors
- Named pipe created with mkfifo(1) or mkfifo(2)
- Duplicating file descriptors
- dup2(2): reroute things intended for one file
descriptor to another
- redirect existing file descriptors (0=stdin, 1=stdout,
2=stderr)
- hook the output of one pipe to the input of another
- Other useful C tips and tricks
- gcc -Wall
- debuggers: gdb, ddd
- initialization of memory (or not)
- string manipulation: see string(3)
- memory management: malloc(3), free(3) - no
garbage collector here!
- "object oriented" C programming
No New Lecture Assignment
Examples