fork download
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <sys/wait.h>
  5.  
  6. int main()
  7. {
  8. int pid, choice;
  9. char cmd[20];
  10.  
  11. pid = fork(); // Create child process
  12.  
  13. if (pid < 0)
  14. {
  15. printf("Process creation unsuccessful\n");
  16. exit(1);
  17. }
  18.  
  19. if (pid == 0) // Child process
  20. {
  21. printf("\nChild process\n");
  22.  
  23. do
  24. {
  25. printf("Enter the command: ");
  26. scanf("%s", cmd);
  27.  
  28. system(cmd); // Execute command
  29.  
  30. printf("Enter 1 to continue or 0 to exit: ");
  31. scanf("%d", &choice);
  32.  
  33. } while (choice == 1);
  34.  
  35. exit(0);
  36. }
  37. else // Parent process
  38. {
  39. wait(NULL); // Parent waits for child
  40. printf("\nChild process completed.\n");
  41. }
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Child process
Enter the command: Enter 1 to continue or 0 to exit: 
Child process completed.