| 1 |
#include <iostream> |
| 2 |
#include <unistd.h> |
| 3 |
#include "fork.h" |
| 4 |
|
| 5 |
class Daemon: public Fork |
| 6 |
{ |
| 7 |
void parentProcess() override // the parent does nothing. |
| 8 |
{} |
| 9 |
void childProcess() override // actions by the child |
| 10 |
{ |
| 11 |
sleep(3); |
| 12 |
// just a message... |
| 13 |
std::cout << "Hello from the child process\n"; |
| 14 |
throw 0; // The child process ends |
| 15 |
} |
| 16 |
}; |
| 17 |
|
| 18 |
int main() |
| 19 |
try |
| 20 |
{ |
| 21 |
Daemon{}.fork(); |
| 22 |
} |
| 23 |
catch (...) |
| 24 |
{} |
| 25 |
|
| 26 |
/* |
| 27 |
Generated output: |
| 28 |
The next command prompt, then after 3 seconds: |
| 29 |
Hello from the child process |
| 30 |
*/ |