execvp effectively reboots a process to run a different program from scratch.
pathis relative or absolute pathname of the executable to be invoked.
argvis the argument vector that should be funneled through to the new executable's
argvgenerally end up being the same exact string.
execvpfails to cannibalize the process and install a new executable image within it, it returns -1 to express failure.
execvpsucceeds, it 😱 never returns 😱.
execvphas many variants (
execlp, and so forth. Type
execvpto see all of them). We typically rely on
execvpin this course.
mysystemfunction is just the first example where
waitpidall work together to do something genuinely useful.
mysystemis operationally a miniature shell.
execvpwork in practice.
stsh, for Stanford shell—to imitate the functionality of the shell (c-shell aka
csh, or bash-shell aka
bash, or z-shell aka
zsh, or tc-shell aka
tcsh, etc. are all different shell implementations) you've been using since you started using Unix.
execvpthat I can think of: a miniature shell not unlike those you've been using since the first time you logged into a
simpleshoperates as a read-eval-print loop—often called a repl—which itself responds to the many things we type in, typically by forking off child processes.
cp, find, make, or even
&—is an instruction to execute the new process in the background without forcing the shell to wait for it to finish. That means we can launch other programs from the foreground before that background process finishes.
simpleshis presented on the next slide. Where helper functions don't rely on CS110 concepts, I omit their implementations (but describe them in adequate detail in lecture).
simplesh(full implementation is right here):
xargsfor the full read) is useful when one program is needed to programmatically generate the argument vector for a second.
xargsreads tokens from standard input (delimited by spaces and newlines).
xargsthen appends those tokens to the end of its original argument list and executes the full list of arguments—original plus those read from standard input—as if we typed them all in by hand.
factorprogram, which prints out the prime factorizations of all of its numeric arguments, as with:
printf—is a brute force representative of an executable capable of supplying or extending the argument vector of a second executable—in this case,
factor; they can be anything that works.
assign2solution , I might use
xargsto do this:
pipesystem call takes an uninitialized array of two integers—we'll call it
fds—and populates it with two file descriptors such that everything written to
fdscan be read from
pipeis particularly useful for allowing parent processes to communicate with spawned child processes.
pipeworks and how messages can be passed from one process to a second, let's consider the following program (available for play right here):
forkwork together in this example?
fdsis shared with the call to
pipeallocates two descriptors, setting the first to read from a resource and the second to write to that same resource. Think of this resource as an unnamed file that only the OS and its support for pipe know about.
pipethen plants copies of those two descriptors into indices 0 and 1 of the supplied array before it returns.
forkcall creates a child process, which itself inherits a shallow copy of the parent's
forkcall, anything printed to
fdsis readable from the parent's
fdsand the child's
forkwork together in this example?
fdsbefore it writes to anything to
fdsto emphasize the fact that the parent has no need to read anything from the pipe.
fdsbefore it reads from
fdsto emphasize the fact that it has zero interest in publishing anything to the pipe. It's imperative all write endpoints of the pipe be closed if not being used, else the read end will never know if more text is to come or not.
writein the parent presses all six bytes of
'\0'included) in a single call. Similarly, I assume the one call to
readpulls in those same six bytes into its local
bufferwith just the one call.
closecalls as I do in both the child and the parent before allowing them to exit.