CSC195, Class 54: Even More Fun with Processes Overview: * Questions from previous class? * Changing stdin and stdout * Detour: Command line parameters * Executing other programs * Exercise: cat grep sort cat Notes: * I imagine many of you are having a hard time right now, whether or not you knew Mr. Stefanov. I know I am. Is there anything I can do to help? * Email me extra credit summaries * Questions on the exam? Extended until Friday. * Tomorrow: Evaluate the class. * Friday: Meet at Dari Barn. Questions on the exam? Q: Do I care what version of binary search you do? A: No. Q: How much to cite? A: Don't cite wp. Cite code. Q: How to cite web pages? Smith, John (2003). "How to detect memory leaks in C programs." The University of Toronto. Online document available at http://www.utoronto.edu.ca/smithj/memory-leaks.html (last modified 23 May 2003; last visited 25 May 2003). Questions on yesterday's stuff? What does wait do if you have no children? It returns -1. So, to wait for all of your children (when you've forgotten how many you have), repeatedly call wait until it returns -1. One of the thigns we learned on Friday was how to create a Unix-style pipe. * What is a pipe? A communication mechanism. You shove stuff in one end of the pipe and it comes out the other one. * How do we create one? + Create an array of two integers int fd[2]; + Call pipe with that as a parameter pipe(fd); * Why don't we need to pass the address of fd? + We (almost) never pass the address of an array. + The array name is an address, so you're passing the address of the first element * How do we use one? + Use read and write. + write(fd[1], buffer, amount-of-buffer-to-write); + read(fd[0], buffer, amount-of-buffer-to-read); Interesting mismatch. Sometimes we use integers and write and read. Sometimes we use FILE *s with printf and scanf/getc. Can we unify the two? There is a way to convert file decriptors to FILE *s. I'll admit that I just forgot it. More importantly, there's a way to make standard output and standard input use different file descriptors. We do so with dup2 dup2(fd[0], STDIN_FILENO); dup2(fd[1], STDOUT_FILENO); To save first dup2(STDIN_FILENO, SAVESTDIN); dup2(fd[0], STDIN_FILENO); ... dup2(SAVESTDIN, STDIN_FILENO); Exercise: Parent: + Creates a pipe, forks a child. Child: + Reidrects standard output to the pipe. + Prints to the pipe with printf Parent: + Redicrect standard input from the pipe. + Reads with getc. + Prints to standard output When that works, restore the parent's standard input