fork download
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/wait.h>
  4. #include <sys/types.h>
  5. #include <stdlib.h>
  6.  
  7. int main() {
  8. int pid;
  9. pid = fork();
  10.  
  11. if (pid < 0) {
  12. printf("\nFORK FAILED\n");
  13. exit(-1);
  14. }
  15. else if (pid == 0) {
  16. // Use execvp to run the "ls" command
  17. execlp("ls", "ls", "-l", NULL); // Fixed the function and arguments
  18. }
  19. else {
  20. wait(NULL); // Wait for child to finish
  21. }
  22.  
  23. printf("\nCHILD COMPLETE\n");
  24. exit(0);
  25. }
  26.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
total 16
-rwxr-xr-x 1 root root 14312 Feb 12 08:32 prog

CHILD COMPLETE