other versions
- jessie 3.23.01-1
- stretch 4.04.00-1
- testing 4.08.06-1
- unstable 4.08.06-1
- experimental 4.09.00-1
other sections
FBB::Fork(3bobcat) | Template Design Pattern around fork(2) | FBB::Fork(3bobcat) |
NAME¶
FBB::Fork - Implements fork(2) using the Template Design PatternSYNOPSIS¶
#include <bobcat/fork>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:- o
- int WIFEXITED(int status):
- o
- int WEXITSTATUS(int status):
- o
- int WIFSIGNALED(int status):
- o
- int WTERMSIG(int status):
- o
- int WCOREDUMP(int status):
- o
- int WIFSTOPPED(int status):
- o
- int WSTOPSIG(int status):
NAMESPACE¶
FBBINHERITS FROM¶
-CONSTRUCTORS¶
- o
- Fork():
DESTRUCTOR¶
- o
- virtual ~Fork():
MEMBER FUNCTIONS¶
- o
- void fork():
PROTECTED MEMBER FUNCTIONS¶
- o
- virtual void childProcess() = 0:
- o
- virtual void childRedirections():
- o
- virtual void parentProcess() = 0:
- o
- virtual void parentRedirections():
- o
- pid_t pid() const:
- o
- void prepareDaemon() const:
- o
- void prepareDaemon(std::string const &out, std::string const
&err, mode_t mode = 0600) const:
- o
- int waitForChild():
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 interfaceSEE ALSO¶
bobcat(7), ipipe(3bobcat), fork(2), opipe(3bobcat), pipe(3bobcat), redirector(3bobcat), wait(2), waitpid(2)BUGS¶
None Reported.DISTRIBUTION FILES¶
- o
- bobcat_3.23.01-x.dsc: detached signature;
- o
- bobcat_3.23.01-x.tar.gz: source archive;
- o
- bobcat_3.23.01-x_i386.changes: change log;
- o
- libbobcat1_3.23.01-x_*.deb: debian package holding the libraries;
- o
- libbobcat1-dev_3.23.01-x_*.deb: debian package holding the libraries, headers and manual pages;
- o
- http://sourceforge.net/projects/bobcat: public archive location;
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).2005-2014 | libbobcat-dev_3.23.01-x.tar.gz |