Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <unistd.h>
  4.  
  5. using namespace std;
  6.  
  7. void spawn(const int& number, pid_t * processTable) {
  8. pid_t forkedProcess;
  9. if (number > 0) {
  10. forkedProcess = fork();
  11. if (forkedProcess == -1) {
  12. exit(0);
  13. }
  14. else if (forkedProcess == 0) {
  15. // 부모프로세스일 경우 아무것도 안함
  16. }
  17. else {
  18. processTable[number] = forkedProcess;
  19. spawn(number - 1, processTable);
  20. }
  21. }
  22. }
  23.  
  24. int main() {
  25. int number = 5;
  26. int mainProcess = getpid();
  27. pid_t processTable[number] = {-1};
  28.  
  29. // 프로세스 생성
  30. // 여기서 부터 로직이 분기된다.
  31. spawn(number, processTable);
  32.  
  33. // 작업 수행 및 로직
  34. // doSomething1();
  35. // ...
  36. // doSomething5();
  37.  
  38. return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement