fork download
  1. #include <iostream>
  2. #include <unistd.h> // For fork()
  3. #include <stdlib.h> // For abort()
  4. #include <sys/types.h>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9. pid_t pid = fork(); // Create a new process
  10.  
  11. if (pid < 0) {
  12. cout << "Fork failed!" << endl;
  13. return 1;
  14. }
  15. else if (pid == 0) {
  16. // This is the child process
  17. cout << "Child Process: PID = " << getpid() << endl;
  18. cout << "Something went wrong in child process! Aborting..." << endl;
  19. cout.flush(); // Ensure output is flushed
  20. abort(); // Abnormal termination
  21. }
  22. else {
  23. // This is the parent process
  24. cout << "Parent Process: PID = " << getpid() << ", Child PID = " << pid << endl;
  25. }
  26.  
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Parent Process: PID = 3949679, Child PID = 3949682
Child Process: PID = 3949682
Something went wrong in child process! Aborting...