Linux

Linux Process ํ”„๋กœ์„ธ์Šค ์ƒํƒœ ํ™•์ธ - ps, top

๐Ÿง Yon 2023. 3. 6. 23:54
๋ฐ˜์‘ํ˜•

Definition: Process

ํ”„๋กœ์„ธ์Šค๋Š” ์‹คํ–‰์ค‘์ธ ๋ช…๋ น์˜ ๋ณต์‚ฌ๋ณธ(์ธ์Šคํ„ด์Šค)๋กœ์จ PID๋กœ ์‹๋ณ„๋œ๋‹ค.

์‹คํ–‰์ค‘์ธ ํ”„๋กœ์„ธ์Šค์— ๊ด€ํ•œ ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ช…๋ น(ps, top ๋“ฑ)์€ ๋Œ€๋ถ€๋ถ„ /proc ํŒŒ์ผ์‹œ์Šคํ…œ์— ์ €์žฅ๋œ ์›์‹œ ๋ฐ์ดํ„ฐ์—์„œ ํ•ด๋‹น ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค. ํ”„๋กœ์„ธ์Šค๋“ค์€ ์ด ์ •๋ณด๋ฅผ /proc์— ์„œ๋ธŒ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด ์ €์žฅํ•˜๋Š”๋ฐ, ์ด ๋•Œ ์„œ๋ธŒ ๋””๋ ‰ํ† ๋ฆฌ์˜ ์ด๋ฆ„์ด ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค ID๋กœ ์ €์žฅ๋œ๋‹ค. ์ €์žฅ๋œ ์›์‹œ ๋ฐ์ดํ„ฐ๋Š” ์ด ๋””๋ ‰ํ† ๋ฆฌ์— ๋‹ด๊ธด ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๋ฉด ์ผ๋ถ€์ด๊ธด ํ•˜์ง€๋งŒ cat, less ๋“ฑ์˜ ์ปค๋งจ๋“œ๋ฅผ ํ†ตํ•ด ์ง์ ‘ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


Command: ps

$> ps #ํ”„๋กœ์„ธ์Šค ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ
$> ps aux #ํ”„๋กœ์„ธ์Šค ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ + bg ํ”„๋กœ์„ธ์Šค ํฌํ•จ

ps ๋ช…๋ น์„ ์‚ฌ์šฉ์ž๋ณ„๋กœ ํ‘œ์‹œํ•˜๋„๋ก ์‹คํ–‰ํ•œ ์˜ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

$> ps u

USER     PID  %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
yonlog 91854   0.1  0.0 408693440   6288 s001  S    11:07PM   0:00.12 -zsh
yonlog 63578   0.0  0.0 408955584    368 s000  S+    3:11PM   0:00.14 -zsh

๋‚ด์šฉ์„ ํ•ด์„ํ•˜๋ฉด, yonlog ์œ ์ €๊ฐ€ -zsh ์…ธ์„ ์—ด๊ณ  ๋กœ๊ทธ์ธ ํ–ˆ์œผ๋ฉฐ STAT ์—ด์˜ S ๋ฅผ ํ†ตํ•ด ์ž ์ž๊ธฐ ์ƒํƒœ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
S ์˜†์˜ + ๊ธฐํ˜ธ๋Š” ํ”„๋กœ์„ธ์Šค๊ฐ€ foreground๋กœ ๋™์ž‘ํ•˜๊ณ  ์žˆ์Œ์„ ์˜๋ฏธํ•œ๋‹ค. ํ”„๋กœ์„ธ์Šค State ๋ณ„ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

PROCESS STATE CODES
   R  running or runnable (on run queue)
   D  uninterruptible sleep (usually IO)
   S  interruptible sleep (waiting for an event to complete)
   Z  defunct/zombie, terminated but not reaped by its parent
   T  stopped, either by a job control signal or because
      it is being traced

%CPU, %MEM ์—ด์€ ๊ฐ๊ฐ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์†Œ๋น„ํ•˜๋Š” ํ”„๋กœ์„ธ์„œ์™€ ๋ฉ”๋ชจ๋ฆฌ(RAM) ์‚ฌ์šฉ๋ฅ ์„ ํ‘œ์‹œํ•œ๋‹ค.
VSZ ์—ด์€ ๊ฐ€์ƒ ์„ธํŠธ ํฌ๊ธฐ(Virtual Set Size)๋กœ ํ”„๋กœ์„ธ์Šค์˜ ํฌ๊ธฐ๋ฅผ ํ‚ฌ๋กœ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋ณด์—ฌ์ฃผ๋ฉฐ, ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
RSS ์—ด์€ ์ƒ์ฃผ ์„ธํŠธ ํฌ๊ธฐ(Resident Set Size)๋กœ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์‹คํ–‰์ค‘์ธ ํ”„๋กœ๊ทธ๋žจ์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์‹ค์ œ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

START๋Š” ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹œ์ž‘๋œ ์‹œ๊ฐ„, TIME์€ ์ด ์‚ฌ์šฉ ์‹œ๊ฐ„์ด๋‹ค. ๋งŽ์€ ์ปค๋งจ๋“œ๊ฐ€ CPU ์‹œ๊ฐ„์„ ๊ฑฐ์˜ ๋นผ์•—์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— 0:00.12์™€ ๊ฐ™์ด CPU ์‹œ๊ฐ„์„ 1์ดˆ๋„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€ ์•Š์€ ์ƒํƒœ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

ํ˜„์žฌ ์‚ฌ์šฉ์ž์šฉ์œผ๋กœ ๋ฆฌ๋ˆ…์Šค ์‹œ์Šคํ…œ์—์„œ ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค๋ฅผ ํŽ˜์ด์ง€๋ณ„๋กœ ์ถœ๋ ฅํ•˜๋ ค๋ฉด ps ux์— less๋ฅผ ํŒŒ์ดํ”„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

$> ps ux | less

์ถœ๋ ฅ๋  ๋‚ด์šฉ์„ ํ‘œ์‹œํ•  ์—ด์˜ ํ‚ค์›Œ๋“œ๋กœ ์ง€์ •ํ•˜์—ฌ ์ •๋ ฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์‹คํ–‰ ์ค‘์ธ ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค๋ฅผ ์ถœ๋ ฅํ•˜๋˜(-e) ํŠน์ • ์—ด๋งŒ ์ง€์ •ํ•˜์—ฌ(-o) ํ˜„์žฌ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ๊ธฐ์ค€์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ๊ฐ€์žฅ ๋งŽ์€ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋งจ ์œ„ ํ–‰์œผ๋กœ ์ •๋ ฌ๋˜๋„๋ก(-m) ์ถœ๋ ฅํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

$> ps -eo pid,user,group,gid,vsz,rss,comm -m | less
# ๋˜๋Š”
$> ps -evm | less

Sort Order๋ฅผ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•์€ ps manํŽ˜์ด์ง€์— ์ƒ์„ธํ•˜๊ฒŒ ์•ˆ๋‚ด๋˜์–ด์žˆ๋‹ค.

The -m, -r, and -v options will change the sort order.  If more than one sorting option was given, then the selected processes will be sorted by the last sorting
option which was specified.

-r      Sort by current CPU usage, instead of the combination of controlling terminal and process ID.
-m      Sort by memory usage, instead of the combination of controlling terminal and process ID.
-v      Display information associated with the following keywords: pid, state, time, sl, re, pagein, vsz, rss, lim, tsiz, %cpu, %mem, and command.  The -v option implies the -m option.

 


Command: top

top ๋ช…๋ น์€ ps๋ณด๋‹ค ํ™”๋ฉด์— ๋ณด๋‹ค ์ตœ์ ํ™”๋œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์‹œ์Šคํ…œ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ 1,5,15๋ถ„ ๋‹จ์œ„ Load Average๋ฅผ ํ†ตํ•ด CPU ๋‹น ํ‰๊ท  ๋กœ๋“œ๋ฅผ ํ™•์ธํ•˜๊ฑฐ๋‚˜ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์ด๊ฑฐ๋‚˜ ์šฐ์„  ์ˆœ์œ„๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ํ”„๋กœ์„ธ์Šค ๋ชจ๋‹ˆํ„ฐ๋ง ๋ฐ ๊ด€๋ฆฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

top์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ๋˜๋Š” CPU๋ฅผ ํ•„์š” ์ด์ƒ์œผ๋กœ ์ ์œ ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฐ€๋ ค๋‚ด ์–ด๋– ํ•œ ์กฐ์น˜๋ฅผ ์ทจํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. CPU ์ ์œ ์œจ์ด ์ง€๋‚˜์น˜๊ฒŒ ๋†’์€ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ทธ ์šฐ์„ ์ˆœ์œ„(nice number)๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ , ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋„ˆ๋ฌด ๋งŽ์ด ์†Œ๋น„ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์ผ ์ˆ˜๋„ ์žˆ๋‹ค.

ํ”„๋กœ์„ธ์Šค ์šฐ์„ ์ˆœ์œ„ ๋ณ€๊ฒฝํ•˜๊ธฐ: top ํ™”๋ฉด์—์„œ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ข…๋ฃŒํ•  ํ”„๋กœ์„ธ์Šค์˜ PID๋ฅผ ์‹๋ณ„ํ•˜์—ฌ $> r ์ปค๋งจ๋“œ(renice)๋ฅผ ํ†ตํ•ด ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
ํ”„๋กœ์„ธ์Šค ์ฃฝ์ด๊ธฐ: kill -15 ๋˜๋Š” -9 + PID๋ฅผ ํ†ตํ•ด ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. 15๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์ข…๋ฃŒํ•  ๋•Œ ์‚ฌ์šฉํ•˜๊ณ , 9๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฆ‰์‚ฌ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ์ด ์‹ ํ˜ธ๋ฅผ ๋ฐ›์€ ํ”„๋กœ์„ธ์Šค๋Š” ํ•˜๋˜ ์ผ์ด ์–ด๋–ค ์ƒํƒœ์˜€๋Š”์ง€ ์ƒ๊ด€ ์—†์ด ๊ณง๋ฐ”๋กœ ์ข…๋ฃŒ๋œ๋‹ค.


Command: nice, renice

nice ์ปค๋งจ๋“œ๋Š” ๋‹จ์ผ ํ”„๋กœ์„ธ์Šค์— CPU ์‹œ๊ฐ„์„ ๋” ํ• ๋‹นํ•˜๊ฑฐ๋‚˜ ์ค„์ผ ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ nice ๊ฐ’์ด ๋‚ฎ์„ ์ˆ˜๋ก CPU์— ๋” ๋งŽ์ด ์ ‘๊ทผํ•œ๋‹ค.
ํ”„๋กœ์„ธ์Šค๋Š” ์ฒ˜์Œ๋ถ€ํ„ฐ nice ๊ฐ’์„ ์ง€์ •ํ•˜์–ด ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์‹คํ–‰ ์ค‘์ธ ํ”„๋กœ์„ธ์Šค์˜ nice ๊ฐ’์„ renice ์ปค๋งจ๋“œ๋ฅผ ํ†ตํ•ด ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
์•„๋ž˜ ์˜ˆ์‹œ๋Š” ํŒŒ์ผ ์‹œ์Šคํ…œ์— ์กด์žฌํ•˜๋Š” ํŒŒ์ผ๋“ค์„ ์ผ์ผ์ด ํ›‘์–ด ๊ทธ ์ด๋ฆ„์„ locate ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ๋งŒ๋“œ๋Š” updatedb ์ปค๋งจ๋“œ๋ฅผ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์— ๋ฐฉํ•ด ๋ฐ›์ง€ ์•Š๊ณ  ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์‹คํ–‰(&) ํ•˜์˜€์œผ๋ฉฐ, ๊ธฐ๋ณธ ์šฐ์„ ์ˆœ์œ„๋Š” 5๊ฐ€ ๋˜๋„๋ก ์„ค์ •ํ•œ ๋ถ€๋ถ„์ด๋‹ค.

$> nice +5 updatedb &

์ด๋ ‡๊ฒŒ ์‹คํ–‰๋œ ํ”„๋กœ์„ธ์Šค์˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋†’์ด๋Š” ์ž‘์—…์€ (์ฆ‰, nice ๊ฐ’์„ ๋‚ฎ์ถ”๋Š” ์ž‘์—…์€) ๋ฃจํŠธ ์‚ฌ์šฉ์ž๋งŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ผ๋ฐ˜ ์‚ฌ์šฉ์ž๋Š” nice ๊ฐ’์„ ๋‚ฎ์ถ”๊ฑฐ๋‚˜ ์Œ์ˆ˜ ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ์ผ๋ฐ˜ ์‚ฌ์šฉ์ž๋Š” ํ”„๋กœ์„ธ์Šค๋“ค์ด ๊ธฐ๋ณธ์œผ๋กœ ๋ฐ›๊ณ  ์žˆ๋Š” ๊ด€์‹ฌ ๊ฐ’ ์ด์ƒ์„ ์š”๊ตฌํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ๋˜ํ•œ  ์ž์‹ ์˜ ํ”„๋กœ์„ธ์Šค์—๋งŒ nice ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.


Basic Activities

  • ์‹œ์Šคํ…œ์—์„œ ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค๋ฅผ ์ถœ๋ ฅํ•œ ๋’ค ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์„ ํƒ์ ์œผ๋กœ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค
    (i.e. PID, ์‚ฌ์šฉ์ž๋ช…, ๊ทธ๋ฃน๋ช…, ๊ฐ€์ƒ ๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ, ์ƒ์ฃผ ๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ, ์ปค๋งจ๋“œ)
  • top ์ปค๋งจ๋“œ๋ฅผ ํ†ตํ•ด ์‹œ์Šคํ…œ์—์„œ ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ํ™•์ธํ•œ ๋’ค, CPU ์‚ฌ์šฉ๋Ÿ‰๊ณผ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

Advanced Activities

  • ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ cgroup์„ ์ƒ์„ฑํ•˜์—ฌ ํŠน์ • CPU ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ์ ์œ ์œจ ๊ฐ’์„ ๊ณ ์ •ํ•ด๋ณด์ž

 

References:
[+] ๋ฆฌ๋ˆ…์Šค ๋ฐ”์ด๋ธ” ์ œ 9ํŒ
[+] ์ƒํ™œ์ฝ”๋”ฉ - Linux Process 1 - https://www.youtube.com/watch?v=mrOxyFzD8SY
[+] ์ƒํ™œ์ฝ”๋”ฉ - Linux Process 2 - https://www.youtube.com/watch?v=6anZhqAAaQk
[+] https://idea.popcount.org/2012-12-11-linux-process-states/

๋ฐ˜์‘ํ˜•