/* File: ~liux/public_html/courses/cop4610/examples/simple-cmd-background.cc Purpose: Demonstrate how to use fork and exec system calls COP4610, FSU Author: Xiuwen Liu On the web: http://www.cs.fsu.edu/~liux/courses/cop4610/examples/simple-cmd-background.cc Compile: g++ -o simple-cmd-background simple-cmd-background.cc */ #include #include #include #include #include #include #include #include #define MAX_ARG 50 int main(int argc, char *argv[]) { char fname[256]; pid_t pid, child_pid; char *my_argv[MAX_ARG]; int status; if (argc < 2) { cerr << "Usage: " << argv[0] << " command-to-run arg1 arg2 .... " << endl; exit(-1); } /* The following is a standard pattern to use fork and exec to run new programs */ pid = fork(); if (pid == ((pid_t)-1)) { // Something must be wrong with the system if the program gets here perror("Fork failed: "); exit(-1); } else { /* Prepare command-line arguments */ if (pid == 0) { // This is the child process cout << "Child-> This is the child process " << getpid() << " whose parent is " << getppid() << endl; if (argc <= MAX_ARG) { int i; cout << "Child-> I will start the following command: "< Could not execute \"" << my_argv[0] << "\".\n"; perror("execvp"); exit(-1); } } else { cerr << "Child-> Too many command-line arguments\n"; exit(-1); } } else { char str[256]; // This is the parent process cout << "This is the parent. I want my child " << pid << " to run in the background " << "because I am running." << endl; do { cout << "Please exit to quit: "; cin >> str; if (strcasecmp(str,"exit")==0) break; } while(1); exit(0); } } return 0; }