#include #include #include #include #include #include int main (int argc, char** argv) { int status; pid_t child_pid; child_pid = fork(); switch (child_pid) { // fork error case -1: perror("fork"); return EXIT_FAILURE; // child code case 0: { printf("Child here. My pid is %d. Parent's pid is %d\n", getpid(), getppid()); const char *args[] = {"/bin/echo", "Changed the process with a new one by using execv", NULL}; execv("/bin/echo", (char *const *)args); //execlp("echo", "echo", "Changed the process with a new one by using execlp", NULL); printf("If this code is reached, then exec has caused an error (for example the file to be executed was not found)\n"); break; } // parent code default: { printf("Parent here. My pid is %d. Child's pid is %d\n", getpid(), child_pid); // wait for child to end execution pid_t waited_pid = waitpid(child_pid, &status, 0); if (waited_pid < 0) { perror("waitpid"); return EXIT_FAILURE; } printf("Parent here. Succesfully waited for child %d\n", waited_pid); } } return 0; }