/* * fork/wait example. * Godmar Back * CS 3204 Fall 2006 */ #include #include #include #include #include int main(int ac, char *av[]) { pid_t child = fork(); if (child < 0) perror ("fork"), exit(-1); if (child != 0) { // parent takes 'then' branch wait(NULL); // wait for our only child printf ("I'm the parent %d, my child is %d\n", getpid(), child); } else { // child takes 'else' branch printf ("I'm the child %d, my parent is %d\n", getpid(), getppid()); // now changing program execl("/bin/echo", "echo", "Hello, World", NULL); // should only get here if "execl" failed for some reason perror ("execl"); exit (-1); } return 0; }