// syscall5.cpp
// Glenn G. Chappell
// 25 Jan 2012
//
// For CS 321 Spring 2012
// System calls demo #5
// Fork-exec-wait

#include <unistd.h>
// Used: fork, pid_t, execl, _exit
#include <sys/wait.h>
// Used: waitpid
#include <iostream>
using std::cout;
using std::endl;


int main()
{
    cout << "Parent - attempting to fork" << endl;
    pid_t p = fork();
    if (p != 0) // Parent
    {
        if (p < 0)
        {
            cout << "Parent - fork failed; exiting" << endl;
            _exit(1);
        }

        cout << "Parent - fork succeeded; waiting for child" << endl;
        int dummyStatus;
        waitpid(p, &dummyStatus, 0);
    }
    else // p == 0; child
    {
        cout << "Child - attempting to exec" << endl;
        execl("/bin/ls", "ls", "-l", (char *)(0));
        cout << "Child - exec failed (are you on NetRun?); exiting"
             << endl;
        _exit(1);
    }

    cout << "Parent - all done" << endl;
    _exit(0);

    return 0;
}

