FBB::Fork(3bobcat)

Template Design Pattern around fork(2)
(libbobcat-dev_4.08.03-x.tar.gz)

2005-2018

NAME

FBB::Fork - Implements fork(2) using the Template Design Pattern

SYNOPSIS

#include <bobcat/fork>
Linking option: -lbobcat

DESCRIPTION

FBB::Fork objects may be used to implement the fork(2) call as part of the Template Algorithm Design Pattern. The class was designed as a virtual base class for classes implementing the essential parts of the forking process. The class is a virtual base class. Derived classes must implement the members childProcess and parentProcess as part of the `Template Method Design Pattern' (see Gamma et al., 1995).

Terminating child processes send SIGCHLD signals to their parents. The C library offers the following macros to analyze the status values received by the parent process using a wait(2) or waitpid(2) system call:

NAMESPACE

FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.

INHERITS FROM

-

CONSTRUCTORS

Note that there is no copy constructor.

DESTRUCTOR

MEMBER FUNCTIONS

PROTECTED MEMBER FUNCTIONS

EXAMPLES

#include <iostream>
#include <unistd.h>

#include <bobcat/fork>

using namespace std;
using namespace FBB;

class Background: public Fork
{
    public:
        void childProcess()     override;
        void parentProcess()    override;
};

void Background::childProcess()
{
    for (int idx = 0; idx < 3; ++idx)
    {
        cout << "Hello world # " << idx << endl;
        sleep(1);
    }
    throw 0;    // caught in main()
}    

void Background::parentProcess()
{
    cout << "Waiting for the child process to end...\n";

    cout << "The child returns value " << waitForChild() << endl;
}    

int main()
try
{
    Background bg;

    bg.fork();
    cout << "This is from the parent\n";

    return 0;
}
catch(int x)
{
    cout << "The child terminates with: " << x << endl;
    return x;
}


Here's a more extensive example:

#include <iostream>
#include <string>

#include <signal.h>
#include <unistd.h>

#include <sys/types.h>

#include <bobcat/pipe>
#include <bobcat/ofdstream>
#include <bobcat/ifdstream>
#include <bobcat/process>
#include <bobcat/fork>

class ChildIO: public FBB::Fork
{
    FBB::Pipe childInput;   // child reads this
    FBB::Pipe childOutput;   // child writes this

    public:
        void childRedirections()    override;
        void childProcess()         override;
        void parentProcess()        override;
};

using namespace std;
using namespace FBB;


void ChildIO::childRedirections()
{
    childInput.readFrom(Redirector::STDIN);
    childOutput.writtenBy(Redirector::STDOUT);
}

void ChildIO::childProcess()
{
        // The /bin/cat program replaces the
        // child process started by Fork::fork()
    Process process(Process::DIRECT, "/bin/cat");
    process.start();
   
    // this point is never reached
}

void ChildIO::parentProcess()
{
        // Set up the parent's sides of the pipes
    IFdStream fromChild(childOutput.readOnly());
    OFdStream toChild(childInput.writeOnly());

        // write lines to the child, read its output
    string line;
    while (true)
    {
        cout << "? ";
        line.clear();
        getline(cin, line);

        if (line.empty())
        {
            kill(pid(), SIGTERM);
            break;
        }

        toChild << line << endl;

        getline(fromChild, line);
        cout << "Got: " << line << endl;
    }
    cout << "The child returns value " << waitForChild() << endl;
}

int main()
try
{
    ChildIO io;

    io.fork();

    return 0;
}
catch(exception const &exc)
{
    cerr << "Exception: " << exc.what() << endl;
}
catch(int x)
{
    cout << "The child terminates with: " << x << endl;
    return x;
}

FILES

bobcat/fork - defines the class interface

SEE ALSO

bobcat(7), cerrextractor(3bobcat), cininserter(3bobcat), coutextractor(3bobcat), exec(3bobcat), fork(2), pipe(3bobcat), redirector(3bobcat), stdextractor(3bobcat), wait(2), waitpid(2).

BUGS

None Reported.

DISTRIBUTION FILES

BOBCAT

Bobcat is an acronym of `Brokken's Own Base Classes And Templates'.

COPYRIGHT

This is free software, distributed under the terms of the GNU General Public License (GPL).

AUTHOR

Frank B. Brokken (f.b.brokken@rug.nl).