Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
PipesCS 326: Operating Systems
Lecture 9
▪ Code reviews and grading will start Friday in lab▪ Don’t worry, you won’t be bored during lab time! (I’ll
post new lab assignments that will help with P2)
Code Review
CS 326: Operating Systems 2
▪ Pipes: The Basics
▪ I/O Streams▪ Piping Data
Today’s Agenda
CS 326: Operating Systems 3
▪ Pipes: The Basics
▪ I/O Streams▪ Piping Data
Today’s Agenda
CS 326: Operating Systems 4
▪ Pipes are a common way for programs tocommunicate on Unix systems
▪ Most useful for sharing unstructured data (such a text)between processes
▪ They work like how they sound: if you want to senddata to another process, send it through the pipe
Pipes [1/2]
CS 326: Operating Systems 5
▪ Pipes are one of the fundamental forms of Unix IPC
▪ With pipes, we can “glue” several utilities together:▪ grep neato file.txt | sort
▪ This will search for “neato” in �le.txt and print eachmatch
▪ Next, these matches get sent over to the ‘sort’utility
▪ This is achieved via I/O redirection▪ We can redirect the standard output stream away
from our terminal and into another program instead!
Pipes [2/2]
CS 326: Operating Systems 6
▪ As we’ve seen, pipes are used frequently in the shell
▪ We can mix and match di�erent utilities, and they allwork well together
▪ Awesome!
▪ Some genius must have designed all these programsto work this way, right?
▪ Well, no. They all just read from stdin and then writeto stdout (and stderr )
▪ No coordination required between developers
In the Shell
CS 326: Operating Systems 7
▪ When you enter ‘ls’ in your shell, you’re running aprogram
▪ This functionality is NOT built into your shell. Bashsimply �nds and runs the ‘ls’ program. That’s it!
▪ So the most basic shell is:▪ fork()▪ exec()
▪ Wait for the child process to �nish
To be clear…
CS 326: Operating Systems 8
▪ There are some shell “commands” that actually aren’tprograms, called built-ins
▪ For example, history : printing previous commands▪ The shell knows what commands you’ve entered, so
having an external program do this would be…somewhat questionable
▪ And, obviously, exit▪ If you create a new process, it’s not like it can quit for
the parent process, right?▪ Well, I guess there IS a way to do that…
Built-Ins
CS 326: Operating Systems 9
▪ Can you think of why cd needs to be a built-incommand?
▪ It’s similar to the problem with exit
▪ We can change the current directory of ourselves, butnot our parent
▪ This is true for general Unix-based systems, but if wethink back to our discussion on clone() …
▪ …Linux allows us to actually share the CWD with ourparent (man pages of clone talk about this cd case)
Another Built-In: cd
CS 326: Operating Systems 10
▪ I have posted a video from Bell Labs on the schedulethat discusses several design aspects of Unix
▪ It’s actually pretty interesting… well, at least as far asOS content goes!
▪ Discussion on pipes starts right around the 5 minutemark
Going to the Source
CS 326: Operating Systems 11
Another Take (Julia Evans)
CS 326: Operating Systems 12
▪ Pipes: The Basics
▪ I/O Streams▪ Piping Data
Today’s Agenda
CS 326: Operating Systems 13
▪ Each program gets allocated three streams by default:▪ stdout (standard output)▪ stderr (standard error)
▪ stdin (standard input)
▪ As with most things in Unix, these are represented as�les (via �le descriptors)
▪ These streams have di�erent functions…
Input/Output Streams
CS 326: Operating Systems 14
▪ When you call printf , you are writing to stdout
▪ This stream is designed for general program output;for example, if you type ls then the list of �les shoulddisplay on stdout
▪ You can pipe stdout to other programs:▪ ls -l / | grep 'bin'
▪ …or redirect to a �le:▪ ls -l / > list_of_files.txt
stdout
CS 326: Operating Systems 15
▪ The standard error stream is used for diagnosticinformation
▪ This way, program output can still be passed to otherprograms/�les but we’ll still see diagnostics printed tothe terminal
▪ Helps us know when something went wrong
▪ Unlike stdout , stderr is not bu�ered
stderr
CS 326: Operating Systems 16
▪ The �nal stream, stdin, one way to provide programinput (via scanf , for example)
▪ This can be entered by the user, or we can pipe inputdirectly into a program:
▪ ls -l / | grep 'bin'
▪ ls -l / – writes �le list to stdout▪ grep 'bin' – reads �le list from stdin
stdin
CS 326: Operating Systems 17
▪ We can control where printed output goes with fprintf
▪ File printf
▪ stderr is actually a �le – in fact, on Unix systems mostdevices are represented as �les
▪ Try ls /dev to view the devices on your machine(includes macOS)
▪ So if we want to write data to a �le, just pass it in:▪ fprintf(file, "My name is: %s", "Bob");
fprintf
CS 326: Operating Systems 18
▪ Pipes: The Basics
▪ I/O Streams▪ Piping Data
Today’s Agenda
CS 326: Operating Systems 19
▪ Now back to pipes: we can create them with the pipe()function
▪ Returns a set of �le descriptors: the input andoutput sides of the pipe
▪ Pipes aren’t very useful if they aren’t connected toanything, though
▪ We can do this by fork() ing another process
The pipe function
CS 326: Operating Systems 20
▪ After calling fork() , both processes have a copy ofthe pipe �le descriptors
▪ Pipes only operate in one direction, though, so weneed to close the appropriate ends of the pipe
▪ You can think of a forked() pipe as one with fourends: two input and output ends each
▪ We eliminate the ends we don’t need to control thedirection of data �ow
▪ Amazing ASCII art drawing: >---<
Piping to Another Process
CS 326: Operating Systems 21
▪ To control data �ow through the pipe, we close theends we won’t use
▪ For example:▪ Child process closes FD 0 and reads from FD 1
▪ Parent process closes FD 1 and writes to FD 0
Controlling Flow
CS 326: Operating Systems 22
▪ You may be wondering: what good are pipes when wehave to start all the cooperating processes?
▪ There’s actually another option: FIFOs, aka namedpipes
▪ Create with the mkfifo command, then open as youwould a regular �le descriptor
Async Process Creation
CS 326: Operating Systems 23
▪ dup2 allows us to redirect streams▪ int dup2(int fildes, int fildes2);
▪ Let’s say we want to make our standard output streamgo through the pipe we just created
▪ We’ll do:▪ dup2(fd[0], STDOUT_FILENO);
▪ This also deallocates (closes) the second fd (�ldes2)▪ You won’t see text printing directly to your terminal
Redirecting Streams: dup2
CS 326: Operating Systems 24
▪ We can also redirect to a �le instead:▪ int output = open("output.txt",
O_CREAT | O_WRONLY | O_TRUNC, 0666);
▪ dup2(output, STDOUT_FILENO);
▪ Cool, right? Ok, let’s try this out…
Redirecting to a File
CS 326: Operating Systems 25
▪ pipe.c
▪ io-redir.c▪ Shell commands
Demos
CS 326: Operating Systems 26