CSC195, Class 52: Unix Processes (1) Overview: * About processes * Creating new processes in Unix * Lab Notes: * Email me your extra credit. * I stole Mr. Walker's lab for today. * Read Mr. Walker's "An Introduction to Concurrence in Unix-based [GNU] C Through Annotated Examples" * Picnic at 5! * Questions on the exam? ---------------------------------------- In the Unix system, each copy of a program runs as its own "process". A process is "a program while it's running" * More likely to currently be consuming processing time. * More likely to be in active memory. * Computer knows it's running, so it has an entry in the "these are the running programs" table. On unix systems, the index of this entry is the "process id" (pid). * A process has a "current state of execution", a non-run program does not. + Where you are in the thread of execution. + What the state of all the variables is. There are problems more naturally solved or more easily solved by multiple processes * You could migrate a process to another computer to save time. * Servers often use multiple processes or threads so that they can more easily serve more than one client at the same time * It's how modern operating systems work. * When you're listening to multiple inputs, it may be useful to have more than one thread of computation, each ready to respond. A thread is a lot like a process How do processes and threads differ? * Thread is usually just the control. * Processes also include the state of the program. Threads usually share variables. Processes usually do not. Multiprocessing in Unix/C fork() * No parameters * Creates a nearly-exact copy of the current process + The new copy is at the same point in the program (immediately after the fork) and all variables are set to the same values. + I don't know what's happened with malloc'd stuff, but I expect it's duplicated too. * fork() returns 0 in the copy (child) and the id of the new processs in the original (parent) Traditionally, the parent calls wait(childid) as its last step. How do processes communicate with each other? * sockets: General structures for communication between programs TCP/IP Unix sockets + Follow a client/server model. + Once created, permit standard Unix write(...) and read(...), * files * pipe : you put data in at one end of a pipe and read data from the other end. It is a pair of file descriptors. Each file descriptor is just an integer. int fd[2]; pipe(fd); write(fd[1], "a string", length); read(fd[0], buf, length);