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. abort(); // Abnormal termination
  20. }
  21. else {
  22. // This is the parent process
  23. cout << "Parent Process: PID = " << getpid() << ", Child PID = " << pid << endl;
  24. }
  25.  
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Parent Process: PID = 1988305, Child PID = 1988308