λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
운영체제

ν”„λ‘œμ„ΈμŠ€ 계측 ꡬ쑰

by μ„œμ•„λž‘πŸ˜ 2023. 10. 31.

 

 

ν”„λ‘œμ„ΈμŠ€ 계측 ꡬ쑰

ν”„λ‘œμ„ΈμŠ€ κ³„μΈ΅ κ΅¬μ‘°λŠ” μš΄μ˜μ²΄μ œμ—μ„œ ν”„λ‘œμ„ΈμŠ€λ“€ κ°„μ˜ κ΄€κ³„λ₯Ό λ‚˜νƒ€λ‚΄λŠ” λ°©λ²•μ„ λ§ν•©λ‹ˆλ‹€. μš΄μ˜μ²΄μ œλŠ” μ—¬λŸ¬ ν”„λ‘œμ„ΈμŠ€λ“€μ„ κ΄€λ¦¬ν•˜κ³  μ΄λ“€ κ°„μ˜ μƒν˜Έμž‘μš©μ„ μ‘°μœ¨ν•˜λŠ”데, μ΄λŸ¬ν•œ ν”„λ‘œμ„ΈμŠ€λ“€μ€ μ’…μ’… κ³„측적인 κ΅¬μ‘°λ₯Ό ν˜•μ„±ν•©λ‹ˆλ‹€. μ΄ κ΅¬μ‘°λŠ” μΌλ°˜μ μœΌλ‘œ λΆ€λͺ¨-μžμ‹ κ΄€κ³„λ‘œ λ‚˜νƒ€λ‚Ό μˆ˜ μžˆμŠ΅λ‹ˆλ‹€.

1. λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€(Parent Process): μš΄μ˜μ²΄μ œμ—μ„œ μƒˆλ‘œμš΄ ν”„λ‘œμ„ΈμŠ€λ₯Ό 생성할 λ•Œ, λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€λŠ” μƒˆλ‘œμš΄ μžμ‹ ν”„λ‘œμ„ΈμŠ€λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€. λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€λŠ” μžμ‹ ν”„λ‘œμ„ΈμŠ€λ₯Ό μƒμ„±ν•˜κ³ , μžμ›μ„ ν• λ‹Ήν•˜λ©°, μžμ‹ ν”„λ‘œμ„ΈμŠ€μ˜ λ™μž‘μ„ κ°μ‹œν•  μ±…μž„μ΄ μžˆμŠ΅λ‹ˆλ‹€.  

2. μžμ‹ ν”„λ‘œμ„ΈμŠ€(Child Process): λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€μ— μ˜ν•΄ μƒμ„±λœ ν”„λ‘œμ„ΈμŠ€λ₯Ό μžμ‹ ν”„λ‘œμ„ΈμŠ€λΌκ³  ν•©λ‹ˆλ‹€. μžμ‹ ν”„λ‘œμ„ΈμŠ€λŠ” λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€λ‘œλΆ€ν„° μžμ›κ³Ό κΆŒν•œμ„ μƒμ†λ°›μŠ΅λ‹ˆλ‹€. λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€μ™€ μžμ‹ ν”„λ‘œμ„ΈμŠ€ κ°„μ—λŠ” νŠΉλ³„ν•œ 톡신 λ©”μ»€λ‹ˆμ¦˜μ΄λ‚˜ 데이터 곡유 방법을 μ‚¬μš©ν•˜μ—¬ μƒν˜Έμž‘μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.  

 

λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€μ™€ μžμ‹ ν”„λ‘œμ„ΈμŠ€λŠ” λ‹€λ₯Έ ν”„λ‘œμ„ΈμŠ€μ΄κΈ° λ•Œλ¬Έμ— 각기 λ‹€λ₯Έ PIDλ₯Ό κ°€μ§‘λ‹ˆλ‹€. 일뢀 μš΄μ˜μ²΄μ œμ—μ„œλŠ” μžμ‹ ν”„λ‘œμ„ΈμŠ€μ˜ PCB에 λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€μ˜ PID인 PPID(Parent Process ID)κ°€ κΈ°λ‘λ©λ‹ˆλ‹€. 그리고 λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€λ‘œλΆ€ν„° μƒμ„±λœ μžμ‹ ν”„λ‘œμ„ΈμŠ€λŠ” μ‹€ν–‰ κ³Όμ •μ—μ„œ 또 λ‹€λ₯Έ μžμ‹ ν”„λ‘œμ„ΈμŠ€λ₯Ό 생성할 수 μžˆμŠ΅λ‹ˆλ‹€. λ§Žμ€ μš΄μ˜μ²΄μ œμ—μ„œλŠ” 이처럼 ν”„λ‘œμ„ΈμŠ€κ°€ ν”„λ‘œμ„ΈμŠ€λ₯Ό λ‚³λŠ” 계측적인 ꡬ쑰둜써 ν”„λ‘œμ„ΈμŠ€λ“€μ„ κ΄€λ¦¬ν•©λ‹ˆλ‹€.

 

 

좜처: https://velog.io/@hongxeob/%ED%94%84%EB%A1%9C%EC%84%B8%EC%8A%A4-%EC%83%81%ED%83%9C%EC%99%80-%EA%B3%84%EC%B8%B5-%EA%B5%AC%EC%A1%B0

 


μ΄λŸ¬ν•œ ν”„λ‘œμ„ΈμŠ€λ“€μ˜ 계측 ꡬ쑰λ₯Ό ν”„λ‘œμ„ΈμŠ€ 계측 ꡬ쑰라고 ν•©λ‹ˆλ‹€. μœ λ‹‰μŠ€ μš΄μ˜μ²΄μ œμ—μ„œλŠ” κ°€μž₯ 졜초의 ν”„λ‘œμ„ΈμŠ€μΈ init ν”„λ‘œμ„ΈμŠ€λΆ€ν„° μ‹œμž‘ν•©λ‹ˆλ‹€. λ¦¬λˆ…μŠ€λŠ” systemd, macOSμ—μ„œλŠ” launchdμž…λ‹ˆλ‹€.

λ¦¬λˆ…μŠ€ μƒμ—μ„œ pstree둜 계측 ꡬ쑰λ₯Ό 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

 

 

 

ν”„λ‘œμ„ΈμŠ€ 생성 기법

λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€λ₯Ό 톡해 μƒμ„±λœ μžμ‹ ν”„λ‘œμ„ΈμŠ€λ“€μ€ λ³΅μ œμ™€ 옷 κ°ˆμ•„μž…κΈ°λ₯Ό 톡해 μ‹€ν–‰λ©λ‹ˆλ‹€. λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€λŠ” fork()λ₯Ό ν†΅ν•΄μ„œ μžμ‹ μ˜ 볡사본을 μžμ‹ ν”„λ‘œμ„ΈμŠ€λ‘œ 생성해내고, λ§Œλ“€μ–΄μ§„ μžμ‹ ν”„λ‘œμ„ΈμŠ€λŠ” exec()λ₯Ό 톡해 μžμ‹ μ˜ λ©”λͺ¨λ¦¬ 곡간을 λ‹€λ₯Έ ν”„λ‘œκ·Έλž¨μœΌλ‘œ κ΅μ²΄ν•©λ‹ˆλ‹€.

 

좜처 : https://velog.io/@hongxeob/%ED%94%84%EB%A1%9C%EC%84%B8%EC%8A%A4-%EC%83%81%ED%83%9C%EC%99%80-%EA%B3%84%EC%B8%B5-%EA%B5%AC%EC%A1%B0

 

fork()λŠ” 자기 μžμ‹  ν”„λ‘œμ„ΈμŠ€μ˜ 볡사본을 λ§Œλ“œλŠ” μ‹œμŠ€ν…œ ν˜ΈμΆœμž…λ‹ˆλ‹€. μžμ‹ ν”„λ‘œμ„ΈμŠ€λŠ” λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€μ˜ 볡사본이기 λ•Œλ¬Έμ— λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€μ˜ μžμ›λ“€μ΄ μžμ‹ ν”„λ‘œμ„ΈμŠ€μ— μƒμ†λ©λ‹ˆλ‹€. 그리고 λ§Œλ“€μ–΄μ§„ 볡사본은 exec() ν˜ΈμΆœμ„ 톡해 μƒˆλ‘œμš΄ ν”„λ‘œκ·Έλž¨μœΌλ‘œ μ „ν™˜λ©λ‹ˆλ‹€. exec()λŠ” μžμ‹ μ˜ λ©”λͺ¨λ¦¬ 곡간을 μƒˆλ‘œμš΄ ν”„λ‘œκ·Έλž¨ λ‚΄μš©μœΌλ‘œ μ „ν™˜ν•˜μ—¬ λ…λ¦½μ μœΌλ‘œ μ‹€ν–‰ν•˜λŠ” μ‹œμŠ€ν…œ ν˜ΈμΆœμž…λ‹ˆλ‹€.

 

 

좜처 : https://velog.io/@hongxeob/%ED%94%84%EB%A1%9C%EC%84%B8%EC%8A%A4-%EC%83%81%ED%83%9C%EC%99%80-%EA%B3%84%EC%B8%B5-%EA%B5%AC%EC%A1%B0

 

exec()λ₯Ό ν˜ΈμΆœν•˜λ©΄ μ½”λ“œ μ˜μ—­κ³Ό 데이터 μ˜μ—­μ΄ μ‹€ν–‰ν•  ν”„λ‘œκ·Έλž¨μ˜ λ‚΄μš©μœΌλ‘œ λ°”λ€Œκ³  λ‚˜λ¨Έμ§€ μŠ€νƒκ³Ό νž™ μ˜μ—­μ€ μ΄ˆκΈ°ν™”λ©λ‹ˆλ‹€. 

 

#!/bin/bash

echo "Enter a command:"
read command

# λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€μ—μ„œ μžμ‹ ν”„λ‘œμ„ΈμŠ€ 생성
if fork() == 0; then
    # μžμ‹ ν”„λ‘œμ„ΈμŠ€μ—μ„œ μž…λ ₯된 λͺ…λ Ήμ–΄ μ‹€ν–‰
    exec $command
else
    # λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€λŠ” μžμ‹ ν”„λ‘œμ„ΈμŠ€κ°€ λλ‚˜κΈ°λ₯Ό κΈ°λ‹€λ¦Ό
    wait
    echo "Command execution completed."
fi

 

μœ„ μŠ€ν¬λ¦½νŠΈμ—μ„œ, μ‚¬μš©μžκ°€ μž…λ ₯ν•œ λͺ…λ Ήμ–΄λŠ” fork()λ₯Ό 톡해 μžμ‹ ν”„λ‘œμ„ΈμŠ€λ‘œ μ „λ‹¬λ˜κ³ , μžμ‹ ν”„λ‘œμ„ΈμŠ€λŠ” exec()λ₯Ό μ‚¬μš©ν•˜μ—¬ ν•΄λ‹Ή λͺ…λ Ήμ–΄λ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€. λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€λŠ” μžμ‹ ν”„λ‘œμ„ΈμŠ€κ°€ 싀행을 마칠 λ•ŒκΉŒμ§€ κΈ°λ‹€λ¦° ν›„ κ²°κ³Όλ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€.

μ΄λŸ¬ν•œ λ°©μ‹μœΌλ‘œ fork()와 exec()λŠ” ν”„λ‘œμ„ΈμŠ€ κ°„μ˜ κ³„μΈ΅ κ΅¬μ‘°λ₯Ό ν˜•μ„±ν•˜κ³  μƒˆλ‘œμš΄ ν”„λ‘œκ·Έλž¨μ„ μ‹€ν–‰ν•˜λŠ” λ° μ‚¬μš©λ  μˆ˜ μžˆμŠ΅λ‹ˆλ‹€.

 

 

좜처: 혼자 κ³΅λΆ€ν•˜λŠ” 컴퓨터 ꡬ쑰와 운영체제

 

λŒ“κΈ€