Forks current process exiting child at end of process A fork duplicates a parent process execution state, os handles and private memory. More...
#include <Process.h>
Public Types | |
enum | Side { ForkParent , ForkChild } |
enum | State { Suspended , Immediate } |
Public Member Functions | |
ProcessFork (const ProcessFork &)=delete | |
ProcessFork * | operator= (const ProcessFork &)=delete |
Side | getSide () const |
Obtain process parent / fork side. More... | |
Result | fork (State state) |
Forks current process (use ForkProcess::getType to know the side) More... | |
Result | resumeChildFork () |
Sends 1 byte on parentToFork to resume State::Paused child fork. More... | |
Result | waitForChild () |
Waits for child fork to finish execution. More... | |
int32_t | getExitStatus () const |
Gets the return code from the exited child fork. More... | |
FileDescriptor & | getWritePipe () |
Gets the descriptor to "write" something to the other side. More... | |
FileDescriptor & | getReadPipe () |
Gets the descriptor to "read" something from the other side. More... | |
Forks current process exiting child at end of process A fork duplicates a parent process execution state, os handles and private memory.
Its semantics are quite different from platform to platform but on its most common denominator it can be used to carry on "background" operations on snapshots of current program memory. One relevant use case is serializing to disk or network a live, complex and large data structure. Without the fork the program should either:
Fork avoids memory duplication because it will be shared through Copy On Write (COW) mechanisms. COW ensures that un-modified duplicated memory pages will not occupy additional Physical RAM.
A pair of pipes makes it easy to do some coordination between parent and forked process.
Example: Fork current process modifying memory in forked process leaving parent's one unmodified.
Enumerator | |
---|---|
Suspended | Start the forked process suspended (resume it with ProcessFork::resumeChildFork) |
Immediate | Start the forked process immediately. |
Forks current process (use ForkProcess::getType to know the side)
|
inline |
Gets the return code from the exited child fork.
FileDescriptor & SC::ProcessFork::getReadPipe | ( | ) |
Gets the descriptor to "read" something from the other side.
|
inline |
Obtain process parent / fork side.
FileDescriptor & SC::ProcessFork::getWritePipe | ( | ) |
Gets the descriptor to "write" something to the other side.
Result SC::ProcessFork::resumeChildFork | ( | ) |
Sends 1 byte on parentToFork to resume State::Paused child fork.
Result SC::ProcessFork::waitForChild | ( | ) |
Waits for child fork to finish execution.