fork download
  1. #include <iostream>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4.  
  5. using std::cout;
  6. using std::endl;
  7.  
  8. void function()
  9. {
  10. pid_t pid1, pid2, pid3, pid4;
  11. pid1 = fork();
  12. if (pid1 == 0)
  13. {
  14. cout << "hi " << getpid() << " " << getppid()<< endl; /*first child process should print "hi"*/
  15. }
  16. pid2 = fork();
  17. cout << "hell " << getpid() <<" " << getppid() << endl;
  18. pid3 = fork();
  19. cout << "how " << getpid() <<" "<<getppid() << endl;
  20. pid4 = fork();
  21.  
  22. if (pid1 == 0 && pid2 == 0 && pid3 == 0 && pid4 == 0){
  23. return;/* final child process should exit from the system with out doing anything*/
  24. } else if (pid1 > 0 && pid2 > 0 && pid3 > 0 && pid4 > 0){
  25. cout << "areyou "<< getpid() << " "<< getppid() << endl;
  26. }
  27. }
  28.  
  29. int main() {
  30. /* and the root process should print "are you"*/
  31. function();
  32. sleep(100);
  33. }
Time limit exceeded #stdin #stdout 5s 15240KB
stdin
Standard input is empty
stdout
hell 32032 32030
how 32032 32030
areyou 32032  32030
how 32110 32032
hell 32109 32032
how 32109 32032
hi 32108 32032
hell 32108 32032
how 32108 32032
how 32113 32109
how 32116 32108
hell 32115 32108
how 32115 32108
how 32120 32115