Lecture 13 ----------------- Announce ----------------- This Week ----------------- Today ----------------- - Finish up syscalls - UNIX C/C++ Devel InterProcess Communication (IPC) --------------------------------------- - processes often times need to communicate with eachother - use IPC methods - network connections - through files - through shared memory - through pipes - simplest are pipes - pipe() syscall creates a communication channel in OS - takes a 2 element array, fills with two new file descriptors - first is for reading - second is for writing - by itself is useless in a single process - parent creates pipe, then forks - pipe FDs are copied to new process - parent closes end of pipe it's not using - child closes end of pipe it's not using - now the two processes can communicate with read/write - | character in shells uses dup2 and pipe to perform | task - named pipes for non parent/child processes (mkfifo()) Signals ----------------------------------------------- - UNIX defines a set of software signals that can be sent and recieved to and from processes - for of very simple IPC - by default, most signals are ignored by the process - we can change this default behaviour through the signal() syscall - void *signal(int signum, void *handler); - signum is the signal number we're setting a handler for - signal.h sets up #defines for the signals, gives them names SIGINT (ctrl-c) SIGTSTP SIGKILL SIGSTOP etc - man 7 signal - we write a function that is to be called when the process recieves a signal. - OS remembers where this function is and if process recvs a sig, stops current execution of process, runs handler, returns to normal execution - 'handler' is a function pointer - one more use of pointers...function pointers!! - functions can be defined to accept a pointer to a function as an argument - this way, the program can switch which functions are used in a generic way void sigfunc(int in) { printf("I got a signal!\n"); signal(SIGTSTP, sigfunc); } main() { signal(SIGTSTP, sigfunc); while(1) { } } - currently, under linux, must reset the sighandler C IO -------------------- - we've talked about UNIX system calls for handling IO - C defines it's own way to do IO which is very similar - FILE * instead of file descriptor - fopen, fclose, fread, fwrite, fprintf, fscanf, fseek