COMP 3430 Operating systems – APUE Chapter 15 and man pages reading notes
Winter 2022
About these reading notes
About this week’s content
Copyright By PowCoder代写 加微信 powcoder
Chapter 15: Interprocess Communication
15.1Introduction ………………………………….. 2
15.2Pipes………………………….. Example: creating a pipe between a parent and its child . . . . Example:pager…………………….. Implementation of the TELL and WAIT functions using pipes
15.3popenandpcloseFunctions……………… Example:pagerusingpopen……………… Example:popenandpcloseFunctions . . . . . . . . . . . . Example: Transforming input using popen . . . . . . . . . . .
15.5FIFOs………………………….. UsingFIFOstoDuplicateOutputStreams . . . . . . . . . . . . Client-Server Communication Using a FIFO . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
. 3 . 4 . 5 . 5 . 5 . 6 . 6 . 7 . 7 . 8 . 8
Signaldispositions ………………………
Sendingasignal ………………………………….. 9 Waitingforasignaltobecaught…………………………… 9 Standardsignals ………………………………….. 9 Queueinganddeliverysemanticsforstandardsignals . . . . . . . . . . . . . . . . . . . . . 10 Signalnumberingforstandardsignals ……………………….. 10 Real-timesignals ………………………………….. 10 Interruption of system calls and library functions by signal handlers . . . . . . . . . . . . . 10 Interruptionofsystemcallsandlibraryfunctionsbystopsignals . . . . . . . . . . . . . . . 10
signal(3) 11 kill(3) 11 raise(3) 11
COMP 3430 Operating systems – APUE Chapter 15 and man pages reading notes Winter 2022
About these reading notes
These are my own personal reading notes that I took (me, Franklin) as I read the textbook. I’m pro- viding these to you as an additional resource for you to use while you’re reading chapters from the textbook. These notes do not stand alone on their own — you might be able to get the idea of a chap- ter while reading these, but you’re definitely not going to get the chapter by reading these alone.
These notes are inconsistently all of the following:
• Mesummarizingpartsofthetext.
• Mecommentingonpartsofthetext.
• Measkingquestionstomyselfaboutthetext.
– …andsometimesansweringthosequestions.
The way that I would expect you to read or use these notes is to effectively permit me to be your in- ner monologue while you’re reading the textbook. As you’re reading chapters and sections within chapters, you can take a look at what I’ve written here to get an idea of how I’m thinking about this content.
About this week’s content
Our textbook surprisingly does not discuss interprocess communication (IPC), so we need to use some external sources.
An excellent source is Advanced Programming in the UNIX Environment (APUE) by W Richard Stevens and Rago. I’ve requested that this chapter be scanned by the library, and it’s available for you to view on the course web page through the course schedule.
You’re also going to want to take a look at some manual pages for the system calls that you’re going to be using for IPC (e.g., signal, kill, and pipe).
Chapter 15: Interprocess Communication
15.1 Introduction
• Threadscantalktoeachotherbyreadingandwritingmemory,processesneedtobeabletotalk to each other, too!
COMP 3430 Operating systems – APUE Chapter 15 and man pages reading notes Winter 2022
But the only way for these processes to exchange information is by passing open files across a fork or an exec or through the file system.
• Why is that? Given what we know so far about processes, how else could you communicate between them without using any extra infrastructure?
• Why is “through the file system” a reasonable way to communicate between processes? Think about the abstraction of memory vs the abstraction of files for processes. Can processes see each other’s memory? Can you open the same file in two different processes?
• Figure15.1showsmanydifferentkindsofmechanismsforIPC,andOSsupportforthesediffer- ent mechanisms. The ones we’re mainly interested in are pipes, FIFOs (first-in-first-out), and signals.
– You can generally safely ignore the acronyms UDS, SUS, XSI, etc. We care almost exclu- sively about Linux in this course.
– Semaphoresandsharedmemoryarealsogoodwaysforprocessestocommunicatewith one another; semaphores are more of a signalling/locking mechanism. Shared memory is something you might see in class, but it’s a way for processes to share memory similar to threads, but it’s not anywhere near as easy to use.
– If you’re deeply interested in the idea of semaphores, you should check out “The Little Book of Semaphores” by Downey. It’s free! And it’s ~300 pages about semaphores! Fun!
– If you want to spend more time with sockets and streams, take COMP 3010 Distributed Computing or COMP 4300 Computer Networks.
15.2 Pipes
• Pipesareold.
• ApipeisanIPCmechanismforcommunicationbetweenprocessesthatshareacommonances-
tor (i.e., they have the same parent/grandparent/great grant parent/… process).
Every time you type a sequence of commands in a pipeline for the shell to execute…
• Thisistalkingaboutthepipesymbolonthecommandline(i.e.,|).Youcanusethiscommand to connect the standard output of the command on the left side of the pipe to the standard input of the command on the right side of the pipe. For example, try running this:
yes | head -10
COMP 3430 Operating systems – APUE Chapter 15 and man pages reading notes Winter 2022
This command says “run the yes command (which prints out y repeatedly to standard output) and connect this standard output to the standard input for the command head -10 (which prints out the first 10 lines of input on standard input)”.
• Usesfiledescriptorstofacilitatecommunication(intfd[2]).
– Why do you think that we’re using file descriptors? This is what we’re using with system calls like open, read, write, and lseek, why are we using this for communicating be- tween processes?
• Figure 15.2 shows using a pipe in “half-duplex” mode. For the purposes of this course (and as stated above: for maximum compatibility), we’re limiting ourselves to “half-duplex pipes”.
“Half-duplex pipes” is a really fancy way of saying “one way pipes”.
A pipe in a single process is next to useless.
• Why would that be? Why would a pipe in one process be useless? Think about it: Who would you even be communicating with if you created a pipe in one process?
• Figure15.3nowshowsusamorerealisticuseofpipewithfork.
– Notethatthepipesystemcallseemstobeusedinconjunctionwithfork!Youmustcall
pipe before fork, then close different ends of the pipe in the child and parent processes.
* If the pipe system call creates file descriptors, and the file descriptors are valid for both the child and parent process after a fork, what does this tell you about file de- scriptors in terms of ownership? Where does a file descriptor live? (e.g., the TCB? PCB?)
• Whydothetwoparent/childdiagramsshowthepipeexistinginthekernel?
• Doyouthinkthishasanyissuesthatmightberelatedtocriticalsectionsandthreads?
• Theauthorsarewritinghereabouthowsignalsaresenttoaprocesswhenawritesystemcall is made, but the write-end of the pipe is closed. This pretty closely resembles a pattern you’ve seen before in programming! What is it?
Example: creating a pipe between a parent and its child
• Thisshowsustheorderofoperationsintermsofcallingpipe,thendoingfork,andhoweach of the parent and child have to close different ends of the pipe.
It’s possible to duplicate a pipe descriptor so that multiple processes have the pipe open for writ- ing.
COMP 3430 Operating systems – APUE Chapter 15 and man pages reading notes Winter 2022 – Thelinewrite(STDOUT_FILENO,line,n);iseffectivelywhatprintforputs
are ultimately calling. STDOUT_FILENO is the file handle number for … standard output. Example: pager
• Thisisaslightlymorecomplicatedexample.
• A“pager”isaprogramonthecommandlinethat’sjobistoshowyouone“page”ofoutputata time. Two programs that are used to do this are more and less. less is a terrible pun (less is more).
– Whenyou’reusingman(themanualpages),you’reusingapager!
• Thisprogramopensthefilenamepassedonthecommandline,thenreadsitscontents,writing
them to the write end of the pipe, and the child reads from the read end of the pipe.
– The child doesn’t explicitly read from the pipe, but instead, we’re using this new system call dup2 to overwrite the child’s standard input with the read end of the pipe that was created. You should refer to man dup2 for more information about this system call.
• Thisprogrambasicallydoesthefollowingpipelinethatmightberunonacommandline:
cat file.txt | /bin/more
This is an example of a useless use of cat, but is an alternative view of what’s happening in this
Implementation of the TELL and WAIT functions using pipes
You can safely ignore this section, it’s referring to a different chapter in APUE.
15.3 popen and pclose Functions
• OK,sowe’refeelingcomfortablewithforkandexec(maybe).We’realsosortoffeelingcom-
fortable with pipe, and maybe dup2 (dup2 is in Figure 15.6 and is used to replace file handles).
– Thepopenandpclosepairoffunctionskindofmakeiteasytorunaprocessandtreat its standard output like a file that you can read or its standard input as a file you can write. It’s a fairly common pattern to launch a process from within your own C program and want to read from it or write to its standard input.
COMP 3430 Operating systems – APUE Chapter 15 and man pages reading notes Winter 2022 – You can learn more about popen and pclose by inspecting the manual pages (man
popen and man pclose).
• Notethatthesefunctionsarepartofthestandardlibrary,theyareexplicitlynotsystemcalls.
– Without any other information (i.e., pretend that we can’t see #include
• IfwegetaFILE*backfrompopen,whydoyouthinkwecan’tjustusefclose?
• Noticethattheargumentyou’repassingtopopendescribeshowyou’regoingtointeractwith the process that’s forked (e.g., you’re reading its stdout or writing to its stdin). Figures 15.9
and 15.10 show us the difference between passing “r” and “w”.
– Canyoupassboth“r”and“w”?Checkthemanpage!Trydoingit!
– Does it even make sense to pass both? What would that mean? The physical thing that
you’re reading from the FILE * would be different from the physical thing that you’re writing to the FILE *…
• The authors refer to the system function. system is sort of like fork and exec together, except instead of launching the program you specify directly, the entire string is passed to a shell to interpret and execute. That can be pretty convenient compared to doing fork and exec yourself, especially if you want to use special shell features like file globbing or redirection.
Example: pager using popen
• Thissimplifiesthepagerprogramsignificantly.Thefunctionalityisthesame,butwedon’thave
to dance around fork, exec, pipe, and dup2, it’s all just handled for us.
– Plus,wegettousethoseniceFILE*functionsinsteadofdealingwithfilehandles.
• Check out how they define PAGER. Does that expression work in the shell on your local ma- chine? Does it work for you aviary’s default shell? (hint: it doesn’t) What shell does it work on? (it’s bash).
Example: popen and pclose Functions
• Remember:thesefunctionsarenotsystemcalls.Thatmeansthatyoucanimplementthiskind
of a function in your own code using the basic fork, exec, and dup2 system calls.
– This snippet of code is actually doing some cool stuff around file descriptors and using
them as array indices. This is effectively building an open file table! This is something that
COMP 3430 Operating systems – APUE Chapter 15 and man pages reading notes Winter 2022 the OS needs to track for both a PCB (e.g., Linux’s struct task_struct) or at the
OS-scope level (all open files for all processes).
• Forreference,youcanalsolookatthesourcecodeforglibc’spopen.
• ReadingthroughthesourceforAPUE’spopengivesusananswertotheabovequestionabout using “r” and “w” for popen. Or, at least it tells us that we can’t do it, even if it doesn’t explain why.
• ReadingthroughthesourceforAPUE’spclosemightgiveyousomehintsaboutwhywecan’t just use fclose on the FILE * returned by popen.
• There are some security concerns around popen! Yeah, it’s convenient, but it’s really easy to get it wrong.
– There is a lot going on under this statement, but basically: it’s straightforward for a mali- cious user to get your program to run any other program as your user. This is really bad if your code is running as root.
Example: Transforming input using popen
• Thisisthelastexampleforpopen,showingasmallprogramcalledmyuclc.
• Figure15.3isshowingthevisualrelationshipbetweentherunningprogramsinFigures15.14and
15.15 and the “terminal” where you (a user) is typing stuff in.
• This is another nice example, showing how different processes can interact with one another,
but this time with popen. 15.5 FIFOs
• Allthepipingwesawupuntilthispointareanonymouspipes.
– They have no names, and the only way to even know about the existence of these pipes is to have had the pipe be created by your parent process (i.e., you were fork-ed off of a process that called pipe before fork).
• Sometimesyouwantdifferentprocessestotalktoeachotherthatarenotrelatedtoeachother this way!
We saw in Chapter 4 that a FIFO is a type of file.
• OK, so you didn’t read chapter 4; that’s OK. A FIFO is a type of file. That’s a really short way of saying that it’s a thing that’s provided by the OS that you can interact with via system calls like open, read and write.
COMP 3430 Operating systems – APUE Chapter 15 and man pages reading notes Winter 2022
• Wegettwonewsystemcallstodothis,butmkfifoisreallytheonewecareaboutrightnow.
– mkfifo takes a path to a file because it makes a file.
– This means that working with the FIFO is effectively the same as working with a file (as
• Again (and again and again and again): Once you’ve created the FIFO, you’re literally working with a file. OK, not literally. Figuratively? Abstractly? Yeah, abstractly. A FIFO is the same ab- straction as a file, so you can use the same file interface of open, read, and write.
– Asfarasyoucantell,though,aFIFOlooksandbehaveslikeafile,evenifyoudolsinthe directory where the FIFO lives.
Using FIFOs to Duplicate Output Streams
• Now we’re starting to build up fairly complex pipelines of information. In the first figure, the output from prog1 is being fed into inputs for both prog3 and prog2.
– Canyoudothiswithanonymouspipes?
• pee is a command from the moreutils package that effectively accomplishes what this dia- gram does. Its name is inspired from the tee command, which reads from standard input and writes to both standard output and a file (see man tee).
Client-Server Communication Using a FIFO
• Ohheylook,it’stee!
• Notethisspecificideaofa“well-known”pathname.There’snomagichere(nothere,anyway);
your distributed client-server architecture program needs to have some known thing between clients and servers so that they can communicate with each other. That known thing is a path to a FIFO that clients and servers can read/write.
• This“client-server”architectureisafairlycommonuseofFIFOs.
– It’snicebecauseitmakesiteasytobuildaclient-serverarchitectureonasinglemachine,
then spread it across multiple machines fairly easily later on if you need to scale up.
• Theproblemdescribedherewithmultiplereadersisthatreaderscan’tknowwhensomething was written for them. The solution is to add even more FIFOs, basically one for each “client”.
• Ultimately,figures15.22and15.23arewaysthatwecangetaroundsomeoftheissuesofFIFOs,
like them being “half-duplex”.
COMP 3430 Operating systems – APUE Chapter 15 and man pages reading notes Winter 2022
– One of the issues figure 15.23 is trying to address is that (as above), clients can’t know when a message is for them. Could you handle this situation without actually adding more FIFOs? What would you have to change about how you (the server) communicate with the clients in terms of the messages you’re sending?
• Thisisanoverviewofsignals!
• Youonlyneedtoreallycareaboutthesectionsthatarestatedhere.
Signal dispositions
• Allsignalshaveadefaultaction,including,uh,stoprunningtheprocesswhenaspecificsignal is received.
Sending a signal
• Thereare6differentfunctions(they’relistedas“systemcallsandlibraryfunctions”)forsending signals to processes.
– Whyaretheresomany???
– Which ones do you think are system calls and which are library calls? Why are they differ-
ent? What is the difference between a system call and a library call? Waiting for a signal to be caught
• Therearealsotwosystemcallsforwaitingaroundforasignaltobesent.
– In terms of what you saw with threads and synchronization primitives, does this match any of those concepts?
Standard signals
• There’salsoalonglistofstandardsignals.Definitelydon’tmemorizethis!
– Beawareofthislist,andbeawareofsignalsthat“cannotbecaught,blocked,orignored”.
* Why wouldn’t you want to, for example, catch or ignore SIGKILL/SIGSTOP??? 9
COMP 3430 Operating systems – APUE Chapter 15 and man pages reading notes Winter 2022 Queueing and delivery semantics for standard signals
• Thisisinteresting:aprocesscanreceivemultiplesignalsatthesametime,but“Standardsignals do not queue”.
– Whatdotheydo,then?
Signal numbering for standard signals
• Again,definitelydon’tmemorizethis!
• Howisthis(numbering)relatedtosystemcalls?
• Whydoyouthinkdifferentarchitectureshavedifferentsignalnumbers?
Real-time signals
• Thisisoutsidethescopeofourcourse.
Interruption of system calls and library functions by signal handlers
• OK, interesting. A signal can be received by a process (or, at least, sent to a process) while the OS is servicing a system call on its behalf.
– That means that a signal can be received by a process while in the middle of something like a read or a write.
• LookingatthedescriptionforA1andthehard_workfunction,doesthisexplainwhywehave to keep sleeping?
Interruption of system calls and library functions by stop signals
• Thisistellingusthatwecanpauseaprocess,thenrestartitagainusingsignals.Neat!
– Some system calls have the behaviour of indicating a failure when this happens. Why do you think they would do this? Look at the list of system calls that exhibit this behaviour, what do they have in common?
COMP 3430 Operating systems – APUE Chapter 15 and man pages reading notes Winter 2022 signal(3)
• This manual page almost immediately tells you to avoid using this system call and to instead use sigaction. In terms of our course, we care about the signal system call because it’s simple. Look at the manual page for sigaction, how is it different from the signal system call?
• kill is a terrible name for what this does.
• ThemanualpageheretellsusthataprocesscanhaveagroupID(inthesectionstarting“Ifpidis
0…”). This extends our previous perception of what a process might have in its PCB. Why might
a process have a group at all?
• WhymightthekillsystemcallfailwithEPERM?Canyousendsignalstoaprocessthat’snot
owned by you? Why not?
• Whilethisisasystemcall,honestly,it’saconveniencesystemcall(doinggetpidonyourbe- half).
– Also,neat:anewsystemcall!getpid
• Whyonearthwouldyouwanttosendasignaltoyourself?
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com