// modified p3.c to use execlp() instead of execvp() #include #include #include #include #include int main(int argc, char *argv[]) { printf("hello world (pid:%d)\n", (int) getpid()); int rc = fork(); if (rc < 0) { // fork failed; exit fprintf(stderr, "fork failed\n"); exit(1); } else if (rc == 0) { // child (new process) printf("hello, I am child (pid:%d)\n", (int) getpid()); /* char *myargs[3]; myargs[0] = strdup("wc"); // program: "wc" (word count) myargs[1] = strdup("p3.c"); // argument: file to count myargs[2] = NULL; // marks end of array execvp(myargs[0], myargs); // runs word count */ execlp( "wc", "wc", "-l", "p3.c", NULL); printf("this shouldn't print out"); } else { // parent goes down this path (original process) int wc = wait(NULL); printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", rc, wc, (int) getpid()); } return 0; } /* sample run: $ ./p3 hello world (pid:1707) hello, I am child (pid:1708) 43 p3.c hello, I am parent of 1708 (wc:1708) (pid:1707) $ wc -l p3.c 43 p3.c $ */