fork(1) download
  1. #include <unistd.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6.  
  7. struct MyClass {
  8. static void foo();
  9. };
  10.  
  11. void MyClass::foo () {
  12. static std::string uuid;
  13. std::cout << "Foo: b4: " << uuid << "\n";
  14. uuid = "A new value";
  15. std::cout << "Foo: after: " << uuid << "\n";
  16. }
  17.  
  18. int main () {
  19. int rc = fork();
  20. if(rc < 0) {
  21. perror("fork");
  22. return 1;
  23. }
  24. if(rc) {
  25. std::cout << "Parent, waiting for child pid == " << rc << "\n";
  26. wait(0);
  27. std::cout << "Parent, done waiting\n";
  28. } else {
  29. std::cout << "Child\n";
  30. }
  31. MyClass::foo();
  32. }
  33.  
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
Child
Foo: b4: 
Foo: after: A new value
Parent, waiting for child pid == 16847
Parent, done waiting
Foo: b4: 
Foo: after: A new value