CS60 Lecture 12 ----------------------------- Announce ----------------------------- -hw8 due -project 3 posted Today ----------------------------- - Project 3 - Review of syscalls, file IO, process syscalls - Signals Project 3 --------------------------- - write your own shell! - just like shells you've been using to enter commands - functions of a shell - read a string inputted by the user - parse (tokenize) the input into pieces that make sense - the command itself - arguments to the command - special characters like > - options to special character > file - fork a new process - new process sets up any special redirections - new process execs new program - parent goes back to wait for more commands - keep in mind during lecture Review of Syscalls ---------------------- - OS notion of process has many components - PID - file descriptors - state of process - Current Registers and Program counter **picture - OS has q of processes - type 'ps', walks through q - OS goes through the q one process at a time and executes the processes - saving state of old - putting on new - called context switching - OS Also runs on the CPU! - Apps Do Normal Things - no OS intervention - Apps Also Need OS from time to time - Use System Calls - System Calls - process is running on the CPU - reaches a syscall, uses hardware instruction to gen soft trap - OS takes over, knows what process called syscall - performs operation - modifies PCB contents - puts process back on Q - UNIX provides a C interface to system calls which can be called expilicitly ------------------------------------------------------------------ IO System Calls ------------------ - open, read, write, seek, close - dup() and dup2(); - int dup(int a); - makes a direct copy of fd 'a' and makes a new fd 'b' - int dup2(int oldfd, int newfd); - closes newfd, copies oldfd to newfd - used for IO redirection Process System Calls ----------------------- getpid(), fork(), execvp(), getppid(), wait() - fork() way to make new processes in UNIX - PCB is directly copied, including all file descriptors - only difference is return value from fork() (child gets 0, parent gets pid) - when child exits, process is still hanging around - parent must gather up remains of child when child exits - wait() syscall performs this task - wait() blocks parent until child has exited - can use waitpid() with W_NOHANG to not block Ways Fork is Used -------------------------- - network servers - server listening on a port (80 for http) - new client makes a connection to port 80 - communication channel is created using fds - server forks a child to handle connection - server goes back to listening for more conn while child comm with client - multi processor super computers - 256 processors, parent can fork 256 children to perform parallel codes - new process execution - shells use to start new processes - execvp syscall() - replaces current process contents, memory, etc with new program 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 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