My professor shows the following example in C:
#include <sys/types.h> #include <stdio.h> #include <unistd.h> int main() { pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); return 1; } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child */ wait (NULL); printf("Child Completed its execution\n"); } return 0; }
I compiled it and ran it. I saw weird behavior in this code:
The result of the ls' command / command is printed, which is in the else if condition, but also the printed line is "Child Completed its execution \ n", which is in another place.
Isn't that weird behavior?
source share