#include #include #include #include #include #include "err.h" using namespace std; #define NR_PROC 5 int main () { pid_t pid; int i; cout << "My process id = " << getpid() << endl; for (i = 1; i <= NR_PROC; i++) switch ( pid = fork() ) { case -1: syserr("Error in fork"); case 0: cout << "Child process: My process id = " << getpid() << endl; cout << "Child process: Value returned by fork() = " << pid << endl; return 0; default: cout << "Parent process. My process id = " << getpid() << endl; cout << "Parent process. Value returned by fork() = " << pid << endl; } /* switch */ for (i = 1; i <= NR_PROC; i++) if (wait(NULL) == -1) syserr("Error in wait"); return 0; }