File: parentslurp.yo

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (50 lines) | stat: -rw-r--r-- 3,012 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    The class ti(ParentSlurp), derived from tt(Fork), starts a child process
executing a stand-alone program (like tt(/bin/ls)). The (standard) output of
the executed program is not shown on the screen but is read by the parent
process.

    For demonstration purposes the parent process writes the lines it
receives to its standard output stream, prepending linenumbers to the
lines. It is attractive to redirect the parent's standard em(input) stream to
allow the parent to read the em(output) from the child process using its
tt(std::cin) em(input) stream. Therefore, the only pipe in the program is used
as an em(input) pipe for the parent, and an em(output) pipe for the child.

    The class tt(ParentSlurp) has the following characteristics:
    itemization(
    it() It is derived from tt(Fork). Before starting tt(ParentSlurp)'s class
interface, the compiler must have read tt(fork.h) and tt(pipe.h). The class
only uses one data member, a tt(Pipe) object tt(d_pipe).
    it() As tt(Pipe)'s constructor already defines a pipe, and as tt(d_pipe)
is automatically initialized by tt(ParentSlurp)'s default constructor, which
is implicitly provided, all additional members only exist for
tt(ParentSlurp)'s own benefit so they can be defined in the class's (implicit)
tt(private) section. Here is the class's interface:
        verbinclude(//CLASS examples/parentslurp.h)
    it() The tt(childRedirections) member configures the writing end of the
pipe. So, all information written to the child's standard output stream ends
up in the pipe. The big advantage of this is that no additional streams are
needed to write to a file descriptor:
        verbinclude(//CHILDREDIR examples/parentslurp.h)
    it() The tt(parentRedirections) member, configures the reading end of
the pipe. It does so by connecting the reading end of the pipe to the parent's
standard input file descriptor (tt(STDIN_FILENO)). This allows the parent to
perform extractions from tt(cin), not requiring any additional streams for
reading.
        verbinclude(//PARENTREDIR examples/parentslurp.h)
    it() The tt(childProcess) member only needs to concentrate on its own
actions. As it only needs to execute a program (writing information to its
standard output), the member can consist of  one single statement:
        verbinclude(//CHILDPROC examples/parentslurp.h)
    it() The tt(parentProcess) member simply `slurps' the information
appearing at its standard input. Doing so, it actually reads the child's
output. It copies the received lines to its standard output stream prefixing
line numbers to them:
        verbinclude(//PARENT examples/parentslurp.cc)
    )
    The following program simply constructs a tt(ParentSlurp) object, and
calls its tt(fork()) member. Its output consists of a numbered list of files
in the directory where the program is started.  Note that the program also
needs the tt(fork.o, pipe.o) and tt(waitforchild.o) object files (see
earlier sources):
        verbinclude(//MAIN examples/parentslurp.cc)