๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ–ฅ๏ธ Linux

ํ”„๋กœ์„ธ์Šค/ps ๋ช…๋ น์–ด/pstack ๋ช…๋ น์–ด/kill ๋ช…๋ น์–ด

์˜ค๋Š˜์€... ์ด๋Ÿฐ ๋ถ„๋“ค๊ป˜ ๋„์›€์ด ๋˜๋ฉด ์ข‹๊ฒ ๋‹ค

 

ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ ์‹œ์ผฐ๋Š”๋ฐ ์ด๊ฒŒ ์ง„์งœ ์‹คํ–‰๋œ๊ฑด์ง€ ํ™•์ธํ•ด๋ณด๊ณ  ์‹ถ๋‹ค ๐Ÿ–

์ž์‹ ์ด ์‹คํ–‰์‹œํ‚จ ํ”„๋กœ๊ทธ๋žจ์ด ์•ˆ์ฃฝ์–ด๋ณธ ์ ์ด ์žˆ๋‹ค ๐Ÿ–

์™œ ์•ˆ์ฃฝ๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค ๐Ÿ–

์ข€๋น„ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ๋‹นํ™ฉํ•œ์  ์žˆ๋‹ค ๐Ÿ–๐Ÿ–๐Ÿ–

 

๋ฐ”๋กœ ๋‚˜์•ผ๋‚˜

 

๋ฆฌ๋ˆ…์Šค ํ™˜๊ฒฝ์—์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์งœ๋ดค๋‹คํ•˜๋ฉด ํ•œ๋ฒˆ์ฏค์€ ๋“ค์–ด๋ดค์„

process(ํ”„๋กœ์„ธ์Šค)์™€ PID์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ณ ์ž ํ•œ๋‹ค

๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์–ด๋„ ์ •๋ฆฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค

 


1. Process(ํ”„๋กœ์„ธ์Šค)๋ž€?

 

ํ”„๋กœ์„ธ์Šค๋ž€ ๋‹จ์–ด๋Š” ๋งŽ์ด ์ต์ˆ™ํ• ํ…๋ฐ, ๋”ฑ ๋ญ๋ผ๊ณ  ์ •์˜ํ•˜๊ธฐ๋Š” ์–ด๋ ค์šด ๋‹จ์–ด๋‹ค

๋ณดํ†ต ํ”„๋กœ๊ทธ๋žจ๊ณผ ๋งŽ์ด ๋น„๊ต๊ฐ€ ๋œ๋‹ค

 

ํ”„๋กœ๊ทธ๋žจ์€?

ํ•˜๋“œ ๋””์Šคํฌ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์‹คํ–‰์ฝ”๋“œ

 

ํ”„๋กœ์„ธ์Šค๋Š”?

ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌ๋™ํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ ์ž์ฒด์™€

ํ”„๋กœ๊ทธ๋žจ์˜ ์ƒํƒœ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์ƒ์—์„œ ์‹คํ–‰๋˜๋Š” ์ž‘์—… ๋‹จ์œ„

์ฆ‰, ์‹คํ–‰์‹œํ‚จ ํ”„๋กœ๊ทธ๋žจ์„ ๋œปํ•œ๋‹ค

 

(์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ”„๋กœ๊ทธ๋žจ์„ ๋„์›Œ์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ€ํ‹ฐํ”„๋กœ์„ธ์‹ฑ -_-a)

 

 

(1) ํ”„๋กœ์„ธ์Šค ์ƒํƒœ

์šด์˜์ฒด์ œ๋ฅผ ๊ณต๋ถ€ํ•˜๋‹ค๋ณด๋ฉด ํ•ญ์ƒ ์ ‘ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค ์ƒํƒœ

 

์ง„์งœ ์žฌ๋ฏธ์—†๊ฒŒ ์ƒ๊น€

์ข€ ์ด๋ก ์ ์ธ ์–˜๊ธฐ๋ฅผ ์ •๋ฆฌํ•ด๋ณด์ž๋ฉด...

์œ„์—์„œ ์„ค๋ช…ํ–ˆ๋“ฏ์ด ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•œ๊ฒŒ ํ”„๋กœ์„ธ์Šค์ธ๋ฐ,

์š”๊ฑธ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ์šด์˜์ฒด์ œ์ผ๊ฑฐ๋‹ค

 

์šด์˜์ฒด์ œ์—์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์–ด๋–ป๊ฒŒ ์‹คํ–‰์‹œํ‚ค๊ณ  ์–ธ์ œ ์‹คํ–‰์‹œํ‚ค๊ณ  ์ข…๋ฃŒํ•˜๋Š”์ง€๊ฐ€

ํ”„๋กœ์„ธ์Šค ์ƒํƒœ๋ผ๊ณ  ํ•œ๋‹ค

๊ทธ๋ฆผ์—์„œ ๋‚˜์™€์žˆ๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ 5๊ฐ€์ง€์˜ ์ƒํƒœ๊ฐ€ ์žˆ๋‹ค

 

์‹คํ–‰(running) : ํ”„๋กœ์„ธ์Šค๊ฐ€ CPU๋ฅผ ์ฐจ์ง€ํ•˜์—ฌ ๋ช…๋ น์–ด๋“ค์ด ์‹คํ–‰๋˜๋Š” ๋‹จ๊ณ„
์ƒ์„ฑ(create) : ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ์ค‘
์ค€๋น„(ready) : ํ”„๋กœ์„ธ์Šค๊ฐ€ CPU๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€๋Š” ์•Š์ง€๋งŒ ์–ธ์ œ๋“ ์ง€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๋กœ, CPU๊ฐ€ ํ• ๋‹น๋˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ค€๋น„ ์ƒํƒœ์˜ ํ”„๋กœ์„ธ์Šค ์ค‘ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ํ”„๋กœ์„ธ์Šค๊ฐ€ CPU๋ฅผ ํ• ๋‹น๋ฐ›๋Š”๋‹ค.
๋Œ€๊ธฐ(waiting) : ๋ณด๋ฅ˜(block)๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค. ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ž…์ถœ๋ ฅ ์™„๋ฃŒ, ์‹œ๊ทธ๋„ ์ˆ˜์‹  ๋“ฑ ์–ด๋–ค ์‚ฌ๊ฑด์„ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ๋Š” ์ƒํƒœ๋ฅผ ๋งํ•œ๋‹ค.
์ข…๋ฃŒ(terminated) : ํ”„๋กœ์„ธ์Šค์˜ ์‹คํ–‰์ด ์ข…๋ฃŒ๋œ ๋‹จ๊ณ„

 

(↑ ์œ„ํ‚ค๋ฐฑ๊ณผ์—์„œ ๊ฐ€์ ธ์˜ด)

์•”ํŠผ ์ด๋Ÿฐ ๋‹จ๊ณ„๋“ค์ด ์žˆ๋Š”๋ฐ, 

์šฐ๋ฆฌํ•œํ…Œ ํ•„์š”ํ•œ๊ฑฐ๋Š” ์‹คํ–‰/์ข…๋ฃŒ ์ƒํƒœ์ด๋‹ค

 

๊ทธ๋ฆฌ๊ณ  ๋ฆฌ๋ˆ…์Šค์—์„œ๋Š” ์ด ํ”„๋กœ์„ธ์Šค ์ƒํƒœ๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋Š”

๋ช…๋ น์–ด๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค → ps

์š”๊ฑด ์•„๋ž˜์—์„œ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค

 

(2) PID (Process ID)

๋ง ๊ทธ๋Œ€๋กœ ํ”„๋กœ์„ธ์Šค ID์ด๋‹ค

์šด์˜์ฒด์ œ๊ฐ€ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋ถ™์—ฌ๋†“์€ ์‹๋ณ„ ๋ฒˆํ˜ธ์ด๋‹ค

๋ง์€ ๊ฑฐ์ฐฝํ•˜์ง€๋งŒ ์‚ฌ๋žŒํ•œํ…Œ ์ฃผ๋ฏผ๋ฒˆํ˜ธ ํ• ๋‹นํ•œ ๊ฒƒ ์ฒ˜๋Ÿผ

ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ID์ด๋‹ค

 

 

2. PS (Process Status)

ํ”„๋กœ์„ธ์Šค ์ƒํƒœ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์–ด๋Š” ps๋ผ๋Š” ๊ฒƒ์ด ์žˆ๋‹ค

์ปค๋งจ๋“œ๋ผ์ธ์— help๋ฅผ ํ•ด๋ณด๋ฉด

 

CLI> ps --help

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).

 

์ด๋ ‡๊ฒŒ ์„ธ์ƒ ๊ฐ„๋‹จํ•œ ์„ค๋ช…์ด ์žˆ๋‹ค

์ „์ฒด ์„ค๋ช…์„ ๋ณด๊ณ  ์‹ถ์œผ๋ฉด ์˜ต์…˜ a๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค

 

CLI> ps --help a

Usage:
 ps [options]

Basic options:
 -A, -e               all processes
 -a                   all with tty, except session leaders
  a                   all with tty, including other users
 -d                   all except session leaders
 -N, --deselect       negate selection
  r                   only running processes
  T                   all processes on this terminal
  x                   processes without controlling ttys

Selection by list:
 -C <command>         command name
 -G, --Group <GID>    real group id or name
 -g, --group <group>  session or effective group name
 -p, p, --pid <PID>   process id
        --ppid <PID>  parent process id
 -q, q, --quick-pid <PID>
                      process id (quick mode)
 -s, --sid <session>  session id
 -t, t, --tty <tty>   terminal
 -u, U, --user <UID>  effective user id or name
 -U, --User <UID>     real user id or name

  The selection options take as their argument either:
    a comma-separated list e.g. '-u root,nobody' or
    a blank-separated list e.g. '-p 123 4567'

Output formats:
 -F                   extra full
 -f                   full-format, including command lines
  f, --forest         ascii art process tree
 -H                   show process hierarchy
 -j                   jobs format
  j                   BSD job control format
 -l                   long format
  l                   BSD long format
 -M, Z                add security data (for SELinux)
 -O <format>          preloaded with default columns
  O <format>          as -O, with BSD personality
 -o, o, --format <format>
                      user-defined format
  s                   signal format
  u                   user-oriented format
  v                   virtual memory format
  X                   register format
 -y                   do not show flags, show rss vs. addr (used with -l)
     --context        display security context (for SELinux)
     --headers        repeat header lines, one per page
     --no-headers     do not print header at all
     --cols, --columns, --width <num>
                      set screen width
     --rows, --lines <num>
                      set screen height

Show threads:
  H                   as if they were processes
 -L                   possibly with LWP and NLWP columns
 -m, m                after processes
 -T                   possibly with SPID column

Miscellaneous options:
 -c                   show scheduling class with -l option
  c                   show true command name
  e                   show the environment after command
  k,    --sort        specify sort order as: [+|-]key[,[+|-]key[,...]]
  L                   show format specifiers
  n                   display numeric uid and wchan
  S,    --cumulative  include some dead child process data
 -y                   do not show flags, show rss (only with -l)
 -V, V, --version     display version information and exit
 -w, w                unlimited output width

        --help <simple|list|output|threads|misc|all>
                      display help and exit

For more details see ps(1).

์ •๋ง ๋งŽ์€ ์˜ต์…˜์ด ์žˆ๊ตฐ!

์ด ์ค‘ ๋‚ด๊ฐ€ ์ž์ฃผ ์“ฐ๋Š” ์˜ต์…˜์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค

 

์ฐธ๊ณ ) -๊ฐ€ ๋ถ™์€ ์˜ต์…˜๊ณผ ์•ˆ ๋ถ™์€ ์˜ต์…˜์€ ๋‹ค๋ฅด๊ฒŒ ์“ฐ์ž„

 

์˜ต์…˜๋ช… ์„ค๋ช…
-e all processes
ํ˜„์žฌ ์ปค๋„์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค๋“ค ๋ณด๊ณ ์‹ถ์„ ๋•Œ
-L possibly with LWP and NLWP columns
LWP๋Š” ๊ฒฝ๋Ÿ‰ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋œปํ•จ
ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋Œ๊ณ  ์žˆ๋Š” ์Šค๋ ˆ๋“œ ๋‹ค ๋ณด๊ณ  ์‹ถ์„ ๋•Œ ์”€
-f full-format, including command lines
์‹œ๊ฐ„ ์ •๋ณด ๊ฐ™์€๊ฑฐ๋„ ๋‹ค ๋ณผ ์ˆ˜ ์žˆ์Œ

 

CLI> ps -eLF | grep {Process ์ด๋ฆ„}

 

์š”๋ ‡๊ฒŒ ๋งŽ์ด ์“ด๋‹ค

์˜ต์…˜์ด ๋งŽ์œผ๋‹ˆ๊นŒ ํ•„์š”ํ•œ๊ฑฐ ์ฐพ์•„์„œ ์“ฐ๋ฉด ๋ ๋“ฏ.

 

์ฐธ๊ณ ) grep ๋ช…๋ น์€ ๋งŽ์€ ์ •๋ณด์—์„œ ํ•„์š”ํ•œ๊ฑฐ๋งŒ ์ฐพ๊ณ  ์‹ถ์„ ๋•Œ ์”€

 

๊ทธ๋Ÿฌ๋ฉด ps ๋ช…๋ น์–ด๋กœ ๋ญ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š๋ƒ....๋ฅผ ๋ณด๊ธฐ์œ„ํ•ด

์•„๋ž˜์™€ ๊ฐ™์ด ์ปค๋งจ๋“œ๋ฅผ ์ž…๋ ฅํ•ด๋ณด์ž

 

CLI> ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
{USER}     764 47766  0 18:02 pts/20   00:00:00 ps -f
{USER}   47766 47765  0 10:00 pts/20   00:00:00 -csh

 

UID..? PID๋Š” ๋ญ”๋ฐ..?

๊ฐ ์—ด์ด ๋ญ˜ ๋œปํ•˜๋Š”์ง€ ๊ฐ„๋‹จ์„ค๋ช… ํ•˜์ž๋ฉด

 

๋จธ์„  ์—ด ์„ค๋ช…
USER ํ”„๋กœ์„ธ์Šค ์†Œ์œ ์ž ์ด๋ฆ„
(๊ณ„์ • ์ด๋ฆ„์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.)
UID USER ID
๋ง ๊ทธ๋Œ€๋กœ ์†Œ์œ ์ž ID ๋˜์‹œ๊ฒ ๋‹ค
PID Process ID
์š”๊ฒƒ์ด ๋ฐ”๋กœ PID!! 
PPID Parent Process ID
์š”๊ฒƒ์ด  ๋ถ€๋ชจ ํ”„๋กœ์„ธ์Šค ID ใ…‹
์ข€๋น„ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ƒ๊ฒผ์„ ๋•Œ ๋ถ€๋ชจ ํ”„๋กœ์„ธ์Šค ID๋ฅผ ์ฃฝ์ด๋ผ๋Š” ๋ง์„ ๋“ค์–ด๋ดค์„ ํ…๋ฐ ๊ทธ๋•Œ PPID๋ฅผ ์กฐํšŒํ•˜๋ฉด ๋จ
C CPU ์‚ฌ์šฉ๋Ÿ‰์ธ๋ฐ, ์ง€๊ธˆ์€ ์•ˆ์“ฐ๋Š” ํ•„๋“œ๋ผ๊ณ  ํ•จ
STIME Start Time
ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹œ์ž‘๋œ ์‹œ๊ฐ„
TTY Tele Typewriter? Teletype? 
ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰๋œ ํ„ฐ๋ฏธ๋„ "์ข…๋ฅ˜/๋ฒˆํ˜ธ"
TIME CPU๋ฅผ ์‚ฌ์šฉํ•œ ์ด ์‹œ๊ฐ„
(ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๊ธฐ ์ „์—๋Š” 0์ด๋ผ๊ณ  ํ•จ)
CMD ํ”„๋กœ์„ธ์Šค ์ด๋ฆ„ ๋˜๋Š” ๋ช…๋ น์–ด ์‚ฌ์šฉ
ex) ./test ํ•˜๋ฉด test๋กœ ๋‚˜์˜ค๊ฒ ์ฃต?
์œ„์˜ ์˜ˆ์ œ์—์„œ "ps -f" ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด์„œ CMD์— ํ‘œ์‹œ๋œ๊ฑฐ๋„ ๋ณด์ž„
LWP Light weight process
์œ„์—์„œ ์ž ๊น ์–˜๊ธฐํ–ˆ๋˜ ๊ฒฝ๋Ÿ‰ ํ”„๋กœ์„ธ์Šค์ธ๋ฐ, ์ง€๊ธˆ ๋– ์žˆ๋Š” ์Šค๋ ˆ๋“œ๋ฅผ ๋œปํ•˜๋Š”๊ฑฐ์ž„

 

๊ทธ๋Ÿผ ์ด์ฏค์—์„œ ์˜ˆ์‹œํ•˜๋‚˜๋ฅผ ์‹คํ–‰ํ•ด๋ณด๊ฒ ๋‹ค

์•„์ฃผ ๊ฐ„๋‹จํ•œ 10์ดˆ๋งˆ๋‹ค ์‹œ๊ฐ„์ฐ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค

 

#include <ctime>
#include <csignal>
#include <thread>
#include <chrono>

using namespace std;

bool signal_flag = true;
string getNowTime()
{
    time_t now = time(0);
    return ctime(&now);
}

void signalHdlr(int signum)
{
    cout << "\nSignal: " << signum << endl;
    signal_flag = false;
}

int main ( )
{
    signal(SIGINT, signalHdlr);
    signal(SIGTERM, signalHdlr);
    signal(SIGQUIT, signalHdlr);
    signal(SIGKILL, signalHdlr);

    int active_cnt = 0;

    while( signal_flag )
    {
        if( active_cnt >= 10 )
        {
            cout << getNowTime() << endl;
            active_cnt = 0;
        }
        std::this_thread::sleep_for(std::chrono::seconds(1));
        active_cnt++;
    }
    return 0;
}

 

sinal ์ฒ˜๋ฆฌ๋งŒ ์ถ”๊ฐ€ํ•ด์ค€ ๊ฐ„๋‹จํ•œ ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค

์š”๊ฑธ makeํ•˜๊ณ  ์‹คํ–‰์‹œํ‚ค๋ฉด

 

CLI> ./test_r
Tue Jun  8 11:11:20 2021

Tue Jun  8 11:11:30 2021

Tue Jun  8 11:11:40 2021
.....

 

์š”๋ ‡๊ฒŒ ์‹œ๊ฐ„์„ ์ฐ๋Š”๋ฐ, ํ”„๋กœ์„ธ์Šค๊ฐ€ ์–ด๋–ค ์ƒํƒœ์ธ์ง€ ๋ด๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด?

ps ๋ช…๋ น์–ด๋กœ ๋ณด๋ฉด ๋œ๋‹ค

 

CLI> ps -eLF | grep test_r
{UID}   11421 10718 11421  0    1  4186   988  22 11:22 pts/19   00:00:00 ./test_r
{UID}   11433   738 11433  0    1 29204   984   5 11:22 pts/9    00:00:00 grep -n --exclude=tags test_r

์ด๋ ‡๊ฒŒ ๋‚˜์˜จ๋‹ค. 

์ปค๋งจ๋“œ๋กœ ์น˜๋‹ˆ๊นŒ ์œ„์— ํ•„๋“œ๊ฐ€ ์•ˆ๋‚˜์™€์„œ ใ… ใ…  ์™œ ์•ˆ๋‚˜์˜ค์ง€...

UID PID PPID LWP STIME TTY TIME CMD
{USER ID} 11421 10718 11421 11:22 pts/19 00:00:00 ./test_r

๋งค์นญ์‹œ์ผœ์„œ ์„ค๋ช…ํ•˜๋ฉด ์š”๋Ÿฌํ•˜๋‹ค

๊ทผ๋ฐ ์ƒํƒœ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋Š”๊ฑด ์•Œ๊ฒ ๋Š”๋ฐ ์Šค๋ ˆ๋“œ๋ณ„๋กœ ๋ณด๊ณ  ์‹ถ์€๋ฐ

๊ทธ๊ฑด ์–ด๋–ป๊ฒŒ ๋ณด์ง€???? ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ๊ทธ๋Ÿด ๋•Œ ์“ฐ๋Š” ๋ช…๋ น์–ด๊ฐ€ ๋˜ ์žˆ๋‹ค

 

3. PSTACK(Process Stack)

๋ฐ”๋กœ๋ฐ”๋กœ pstack์ด๋ผ๋Š” ๋ช…๋ น์–ด์ด๋‹ค

์Šค๋ ˆ๋“œ๊ฐ€ ์–ด๋””์„œ ๋ธ”๋ก๋˜์–ด์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•˜๊ฑฐ๋‚˜,

ํ”„๋กœ์„ธ์Šค๋ฅผ ๋ถ„๋ช…ํžˆ ์ข…๋ฃŒ์‹œ์ผฐ๋Š”๋ฐ, ๋‚จ์€ ์Šค๋ ˆ๋“œ๊ฐ€ ์žˆ๋‹ค๊ฑฐ๋‚˜!!

ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ง€๊ธˆ ๋ฌด์Šจ ์ง“์„ ํ•˜๊ณ  ์žˆ๋Š”์ง€  ๋ณผ ์ˆ˜ ์žˆ๋‹ค

 

์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ์•„์ฃผ ๊ฐ„๋‹จํ•˜๋‹ค

 

CLI> pstack {PID}
CLI> pstack {LWP}

 

ps ๋ช…๋ น์–ด๋กœ ํ™•์ธํ•œ PID๋ฅผ ์ž…๋ ฅ๋งŒ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค

 

CLI> pstack 12759
#0  0x00002b4bbee2abc0 in __nanosleep_nocancel () at ../sysdeps/unix/syscall-template.S:81
#1  0x000000000040125c in std::this_thread::sleep_for<long, std::ratio<1l, 1l> > (__rtime=...) at /usr/local/gcc-4.9.4/include/c++/4.9.4/thread:281
#2  0x0000000000400f09 in main () at main.cpp:38

 

์ด๋ ‡๊ฒŒ ์Šค๋ ˆ๋“œ์— ๋Œ€ํ•œ ์ถ”์ (?)ํ•œ ๋ถ€๋ถ„์„ ๋ณด์—ฌ์ค€๋‹ค

์ฐธ๊ณ ) ํ”„๋กœ์„ธ์Šค ์†Œ์œ ์ž๊ฑฐ๋‚˜ root ๊ณ„์ •์—์„œ๋งŒ ์ˆ˜ํ–‰ ๊ฐ€๋Šฅ

 

#0์€ ์œ„ ์˜ˆ์‹œ์—์„œ sleep ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๊ฑฐ๊ณ 

#1์€ std::this_thread ์‚ฌ์šฉํ•œ ๋ถ€๋ถ„์ด๋‹ค

#2๋Š” main ์Šค๋ ˆ๋“œ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค

 

๋‘ ๋ฒˆ์งธ ํ•„๋“œ๋Š” ์Šค๋ ˆ๋“œ ID๋ฅผ ๋œปํ•œ๋‹ค (TID)

 

์ด๊ฑด ๊ฐ„๋‹จํ•œ ํ”„๋กœ๊ทธ๋žจ์ด๋ผ 3๊ฐœ์˜ ์Šคํƒ๋งŒ ๋ณด์ด๋Š”๋ฐ,

ํฐ ํ”„๋กœ๊ทธ๋žจ์„ ๋‹ค๋ฃจ๋‹ค๋ณด๋ฉด ์Œ์ฒญ ๋งŽ์€ ์Šค๋ ˆ๋“œ๊ฐ€ ๋œฐํ…๋ฐ,

๊ทธ ๋•Œ ps๋‚˜ pstack ๋ช…๋ น์–ด ๋“ฑ์„ ์ด์šฉํ•ด์„œ ์ถ”์ ํ•ด๋ณด์ž

 

4. KILL (์ฃฝ์—ฌ)

์˜ค๋Š˜ ๋งˆ์ง€๋ง‰์œผ๋กœ ์„ค๋ช…ํ•  ๋ช…๋ น์–ด๋Š” kill ์ด๋‹ค

๋ง๊ทธ๋Œ€๋กœ ์ฃฝ์ด๋Š”๊ฑฐ๋‹ค ใ…Žใ„ทใ„ท

 

ํ”„๋กœ์„ธ์Šค์— ์‹œ๊ทธ๋„ ์ฒ˜๋ฆฌ๋ฅผ ์ œ๋Œ€๋กœ ์•ˆํ•˜๊ฑฐ๋‚˜

์ž˜๋ชป๋œ ์ฝ”๋“œ๋ฅผ ์ง  ๊ฒฝ์šฐ์—๋Š” ์ œ๋Œ€๋กœ ์ข…๋ฃŒ๊ฐ€ ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ์žˆ๋‹ค

๊ทธ๋ƒฅ ๋ฌด์‹œํ•˜๊ฒŒ ๋˜๋ฉด, CPU๊ฐ€ ๋ชป๋ฒ„ํ‹ธ ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ๊นŒ

์ œ๋Œ€๋กœ๋œ ์ข…๋ฃŒ๋ฅผ ํ•ด์ฃผ๋Š”๊ฒŒ ์ข‹๋‹ค!

์ข€๋น„ ํ”„๋กœ์„ธ์Šค๋„ ์ด๋Ÿฐ ๊ฒฝ์šฐ์—์„œ ์ƒ๊ธฐ๊ธฐ ๋งˆ๋ จ์ด๋‹ค

 

CLI> kill -{SIGNAL ๋ฒˆํ˜ธ} {PID}

 

์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ์š”๋ ‡๊ฒŒ PID๋ฅผ ์ฃฝ์—ฌ์ค˜! ๋กœ ์“ฐ์ธ๋‹ค

SIGNAL ๋ฒˆํ˜ธ๋Š” ์–˜๋ฅผ ์–ด๋–ป๊ฒŒ ์ฃฝ์—ฌ์ค„๊นŒ?๋ผ๋Š” ๋ฒˆํ˜ธ์ด๊ณ ,

1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋‹ค

kill ๋ช…๋ น์–ด๋กœ ์š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค

 

CLI> kill -l
HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT
STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS RTMIN RTMIN+1 RTMIN+2
RTMIN+3 RTMAX-3 RTMAX-2 RTMAX-1 RTMAX

 

์ด๋ ‡๊ฒŒ -l ์˜ต์…˜์„ ์ค˜์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” signal ๋ชฉ๋ก์„ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ,

๋ณดํ†ต ํ”„๋กœ๊ทธ๋žจ์„ ๋งŽ์ด ์ข…๋ฃŒํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š”๊ฑด KILL(9๋ฒˆ์งธ๊บผ)์ด๋‹ค

์ฐธ๊ณ ) 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง„๋‹ค

 

CLI> kill -9 11421

 

์‹ค์ œ ์‚ฌ์šฉํ•˜๋ฉด ์š”๋ ‡๊ฒŒ๋ž„๊นŒ!?

์ด๋Ÿฌ๋ฉด ๋œ๋‹ค.. ์ž˜ ์ข…๋ฃŒ๊ฐ€ ๋˜์—ˆ๋Š”์ง€๋Š” ps ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค

 

(1) SIGNAL 

kill ๋ช…๋ น์–ด๋ฅผ ๊ทธ๋ž˜๋„ ํ•œ๋ฒˆ์ฏค ์“ด๋‹ค๋ฉด signal์ด ๋ญ๋ญ ์žˆ๋Š”์ง€๋Š” ๋ด์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค

ํ•œ๋ฒˆ ์ฝ์–ด๋ณด๋Š”๊ฑฐ์— ์˜์˜๋ฅผ ๋‘๊ธฐ ์œ„ํ•ด, man ํŽ˜์ด์ง€๋งŒ ์บก์ณํ•ด์„œ ์ฒจ๋ถ€ํ•˜๊ฒ ๋‹ค

๊ท€์ฐฎ์€๊ฑฐ ์•„๋‹˜

 

CLI> man 7 signal

 

 

 

 

๊ทธ๋Ÿผ ์ด๋งŒ