pipes - cs.usfca.edu

26
Pipes CS 326: Operating Systems Lecture 9

Upload: others

Post on 26-Dec-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pipes - cs.usfca.edu

PipesCS 326: Operating Systems

Lecture 9

Page 2: Pipes - cs.usfca.edu

â–Ș 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

Page 3: Pipes - cs.usfca.edu

â–Ș Pipes: The Basics

â–Ș I/O Streamsâ–Ș Piping Data

Today’s Agenda

CS 326: Operating Systems 3

Page 4: Pipes - cs.usfca.edu

â–Ș Pipes: The Basics

â–Ș I/O Streamsâ–Ș Piping Data

Today’s Agenda

CS 326: Operating Systems 4

Page 5: Pipes - cs.usfca.edu

â–Ș 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

Page 6: Pipes - cs.usfca.edu

â–Ș 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

Page 7: Pipes - cs.usfca.edu

â–Ș 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

Page 8: Pipes - cs.usfca.edu

â–Ș 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

Page 9: Pipes - cs.usfca.edu

â–Ș 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

Page 10: Pipes - cs.usfca.edu

â–Ș 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

Page 11: Pipes - cs.usfca.edu

â–Ș 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

Page 12: Pipes - cs.usfca.edu

Another Take (Julia Evans)

CS 326: Operating Systems 12

Page 13: Pipes - cs.usfca.edu

â–Ș Pipes: The Basics

â–Ș I/O Streamsâ–Ș Piping Data

Today’s Agenda

CS 326: Operating Systems 13

Page 14: Pipes - cs.usfca.edu

â–Ș 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

Page 15: Pipes - cs.usfca.edu

â–Ș 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

Page 16: Pipes - cs.usfca.edu

â–Ș 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

Page 17: Pipes - cs.usfca.edu

â–Ș 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

Page 18: Pipes - cs.usfca.edu

â–Ș 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

Page 19: Pipes - cs.usfca.edu

â–Ș Pipes: The Basics

â–Ș I/O Streamsâ–Ș Piping Data

Today’s Agenda

CS 326: Operating Systems 19

Page 20: Pipes - cs.usfca.edu

â–Ș 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

Page 21: Pipes - cs.usfca.edu

â–Ș 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

Page 22: Pipes - cs.usfca.edu

â–Ș 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

Page 23: Pipes - cs.usfca.edu

â–Ș 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

Page 24: Pipes - cs.usfca.edu

â–Ș 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

Page 25: Pipes - cs.usfca.edu

â–Ș 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

Page 26: Pipes - cs.usfca.edu

â–Ș pipe.c

â–Ș io-redir.câ–Ș Shell commands

Demos

CS 326: Operating Systems 26