SYSTEM CALL: pipe();
PROTOTYPE: int pipe( int fd[2] );
RETURNS: 0 on success
-1 on error: errno = EMFILE (no free descriptors)
EMFILE (system file table is full)
EFAULT (fd array is not valid)
NOTES: fd[0] is set up for reading, fd[1] is set up for writing
#include <iostream> #include <string> // Required by for routine #include <sys/types.h> #include <unistd.h> #include <cstdlib> // Declaration for exit() int globalVariable = 2; int main() { std::string identifier; int stackVariable = 20; pid_t pID = fork(); if (pID == 0) // child { // Code only executed by child process identifier = "Child Process: "; globalVariable++; stackVariable++; } else if (pID < 0) // failed to fork { std::cerr << "Failed to fork" << std::endl; exit(1); // Throw exception } else // parent { // Code only executed by parent process identifier = "Parent Process:"; } // Code executed by both parent and child. std::cout << identifier << " Global variable: " << globalVariable << " Stack variable: " << stackVariable << std::endl; return 0; }