All pastes #28772 Raw Edit

simple fork() test

public cpp v1 · immutable
#28772 ·published 2005-11-15 10:42 UTC
rendered paste body
#include <iostream>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>using std::cout;int main(){        int child;        int pidchild;        cout << "1. parent prozess\n";        child = fork();        if(child == 0)        {          cout << " 2. child gestartet\n";          pidchild=getpid();          cout << " childpid= " << pidchild <<"\n";          sleep(10);          cout << " 4. child nach 10sec\n";          return 0;        }        else if(child == -1)        {          cout <<"Fehler: " <<errno<<"\n";        }        else // child > 0        {          sleep(5);          cout<<"3. parent nach 5sec\n";          waitpid(pidchild, 0, 0);        }}