Henu
개발냥발
Henu
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • DevOps (52)
      • Kubernetes (19)
      • Docker (14)
      • AWS (3)
      • Nginx (4)
      • Linux (4)
      • ArgoCD (1)
      • CN (2)
      • NATS (0)
      • Git (5)
    • Back-End (30)
      • Django (18)
      • Spring (5)
      • JPA (1)
      • MSA (5)
    • CS (87)
      • SystemSoftware (20)
      • OS (25)
      • Computer Architecture (16)
      • Network (23)
      • Database (2)
    • Lang (21)
      • Java (9)
      • Python (4)
      • C# (8)
    • Life (12)
    • 블록체인 (2)
    • Algorithm (204)
      • BOJ (160)
      • 프로그래머스 (19)
      • LeetCode (4)
      • SWEA (1)
      • 알고리즘 문제 해결 전략 (8)
      • DS, algorithms (7)
      • Checkio (5)
    • IT (2)

블로그 메뉴

  • GitHub
  • 글쓰기
  • 관리자

공지사항

  • Free!

인기 글

태그

  • BFS
  • 백트래킹
  • docker
  • django
  • DFS
  • Kubernetes
  • 다이나믹 프로그래밍
  • 프로그래머스
  • boj
  • Network

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

SystemSoftware - Creating Processes #2
CS/SystemSoftware

SystemSoftware - Creating Processes #2

2021. 6. 6. 02:06
 

SystemSoftware - Creating Processes #1

OS에서 process를 만드는 방식에 대해서 알아보자 process는 fork()라는 syscall을 통해서 child process를 생성한다. fork를 호출한 process가 parent process가 되고, 새로 생긴 process가 child가 process이다...

hyeo-noo.tistory.com

 

 

int wait(int *child_status)

child를 reaping하기 위해서 명시적으로 child가 종료할 때까지 기다리는 syscall

 

return : child의 pid 값

 

만약 int *child_status 가 null이 아니라면 문제가 발생한 것이다.

이때 여러개의 매크로를 통해서 어떤 문제가 발생했는지 알 수 있다. 


위 코드에서 parent가 fork() 된 경우를 보자(else 부분)

wait(&child_status)를 통해서 child 가 종료될 때까지 기다리는 것을 볼 수 있다.

 

이 과정을 그래프로 나타내 보자

 

 

wait()를 통해서 parent는 child가 종료할 때까지 기다리고 CT 와 Bye를 차례로 출력하며 parent도 종료된다.

 

HC -> HP -> CT -> Bye       (O)

HP -> CT -> Bye -> HC       (X)


 

코드가 조금 복잡해 졌다.

N 만큼 for loop을 돌면서 fork()를 수행하고 있다. (N개의 child 생성)

child인 경우에는 바로 종료되는 코드이다.

 

parent인 경우에는 다시 N 만큼 for loop을 돌면서 N번의 wait()를 호출한다.

 

WIFEXITED(child_status) : 정상적인 종료이면 true를 return

WEXITSTATUS(child_status) : child가 exit할 때 가지고 있는 값(exit(100+i))

 

만약 child가 정상적으로 종료된다면 종료할 때 가지고 있는 exitstatus값을 출력하게 되므로 몇 번째 child가 종료되었는지를 출력으로 알 수 있다.

종료되는 child의 순서는 OS의 scheduling 알고리즘에 의해 결정되므로 예측하기 힘들다.


 

이번 코드는 wait() 대신 waitpid()라는 syscall을 통해서 child를 기다린다.

 

-> 특정 child의 종료를 대기할 수 있다.

-> 생성된 순서대로 reaping을 할 수 있다.


execve : child가 parent와 다른 프로그램을 수행하기 위해서 실행하는 syscall

child 의 PID는 유지한 채로 나머지 stack, data들이 모두 바뀐다.

 

호출은 되지만 return을 하지는 않는다. (호출된 함수로 다시 돌아감)

'CS > SystemSoftware' 카테고리의 다른 글

SystemSoftware - Cache Memory #2  (0) 2021.06.06
SystemSoftware - Cache Memory #1  (1) 2021.06.06
SystemSoftware - Creating Processes #1  (0) 2021.06.06
SystemSoftware - Exceptional Control Flow #2  (0) 2021.06.05
SystemSoftware - Exceptional Control Flow #1  (0) 2021.06.05
    'CS/SystemSoftware' 카테고리의 다른 글
    • SystemSoftware - Cache Memory #2
    • SystemSoftware - Cache Memory #1
    • SystemSoftware - Creating Processes #1
    • SystemSoftware - Exceptional Control Flow #2

    티스토리툴바