Multithread and Multiprocess

Multithreading

Multithreading์˜ ์žฅ์ 

  • Process๋ฅผ ์ด์šฉํ•˜์—ฌ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜๋˜ ์ผ์„ thread๋กœ ๊ตฌํ˜„ํ•  ๊ฒฝ์šฐ, memory ๊ณต๊ฐ„๊ณผ system ์ž์› ์†Œ๋ชจ๊ฐ€ ์ค„์–ด๋“ค๊ฒŒ ๋œ๋‹ค

  • Thread ๊ฐ„์˜ ํ†ต์‹ ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋„ ๋ณ„๋„์˜ ์ž์›์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ „์—ญ ๋ณ€์ˆ˜์˜ ๊ณต๊ฐ„ ๋˜๋Š” ๋™์ ์œผ๋กœ ํ• ๋‹น๋œ ๊ณต๊ฐ„์ธ Heap ์˜์—ญ์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค

    • ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— process ๊ฐ„ ํ†ต์‹  ๋ฐฉ๋ฒ•์— ๋น„ํ•ด thread๊ฐ„ ํ†ต์‹  ๋ฐฉ๋ฒ•์ด ํ›จ์”ฌ ๊ฐ„๋‹จํ•˜๋‹ค

  • Thread์˜ context switch ๋Š” process์˜ context switch์™€๋Š” ๋‹ฌ๋ฆฌ ์บ์‹œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋น„์šธ ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋” ๋น ๋ฅด๋‹ค

    • ๋”ฐ๋ผ์„œ ์‹œ์Šคํ…œ์˜ throughput (์ฒ˜๋ฆฌ๋Ÿ‰)์ด ํ–ฅ์ƒ๋˜๊ณ ,

    • ์ž์› ์†Œ๋ชจ๊ฐ€ ์ค„์–ด๋“ค๋ฉฐ,

    • ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ”„๋กœ๊ทธ๋žจ์˜ ์‘๋‹ต์‹œ๊ฐ„์ด ๋‹จ์ถ•๋œ๋‹ค

  • ์ด๋Ÿฌํ•œ ์žฅ์  ๋•Œ๋ฌธ์— ์ž‘์—…๋“ค์„ ํ•˜๋‚˜์˜ process์—์„œ thread๋กœ ๋‚˜๋ˆ  ์ˆ˜ํ–‰ํ•œ๋‹ค

Multithreading์˜ ๋ฌธ์ œ์ 

  • Multi-process๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•  ๋•Œ๋Š” process ๊ฐ„์— ๊ณต์œ ํ•˜๋Š” ์ž์›์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋™์ผํ•œ ์ž์›์— ๋™์‹œ์— ์ ‘๊ทผํ•  ์ผ์ด ์—†์ง€๋งŒ, multi-threading ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•  ๋•Œ๋Š” ์ด ๋ถ€๋ถ„์„ ์‹ ๊ฒฝ์จ์ค˜์•ผ ํ•œ๋‹ค

    • ์„œ๋กœ ๋‹ค๋ฅธ thread๊ฐ€ data์™€ heap ์˜์—ญ์„ ๊ณต์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค thread๊ฐ€ ๋‹ค๋ฅธ thread์—์„œ ์‚ฌ์šฉ์ค‘์ธ ๋ณ€์ˆ˜๋‚˜ ์ž๋ฃŒ๊ตฌ์กฐ์— ์ ‘๊ทผํ•˜์—ฌ ์—‰๋šฑํ•œ ๊ฐ’์„ ์ฝ์–ด์˜ค๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค

  • ๊ทธ๋ž˜์„œ multi-threading ํ™˜๊ฒฝ์—์„œ๋Š” ๋™๊ธฐํ™” ์ž‘์—… ์ด ํ•„์š”ํ•˜๋‹ค

    • ๋™๊ธฐํ™”๋ฅผ ํ†ตํ•ด ์ž‘์—… ์ฒ˜๋ฆฌ ์ˆœ์„œ๋ฅผ ์ปจํŠธ๋กคํ•˜๊ณ , ๊ณต์œ  ์ž์›์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ์ปจํŠธ๋กคํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

      • but, ์ด๋กœ ์ธํ•ด ๋ณ‘๋ชฉ ํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜์—ฌ ์„ฑ๋Šฅ์ด ์ €ํ•˜๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค

        • ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๊ณผ๋„ํ•œ ๋ฝ ์œผ๋กœ ์ธํ•œ ๋ณ‘๋ชฉ ํ˜„์ƒ์„ ์ค„์—ฌ์•ผ ํ•œ๋‹ค

  • multi-thread ํ™˜๊ฒฝ์—์„œ ์—ฌ๋Ÿฌ thread๊ฐ€ ํ•˜๋‚˜์˜ ์ž์›์„ ๊ณต์œ ํ•˜๋ ค๊ณ  ํ•  ๋•Œ Deadlock ๊ณผ ๊ฐ™์€ ๋ฌธ์ œ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค

Multithread vs Multiprocess

  • Multi-thread

    • ์žฅ์ 

      • ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ๋Š” ๋ฉ€ํ‹ฐ ํ”„๋กœ์„ธ์Šค๋ณด๋‹ค ์ ์€ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜๊ณ  context switching์ด ๋น ๋ฅด๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์ง€๋งŒ,

    • ๋‹จ์ 

      • ์˜ค๋ฅ˜๋กœ ์ธํ•ด ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ์ „์ฒด ์Šค๋ ˆ๋“œ๊ฐ€ ์ข…๋ฃŒ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ๊ณผ

      • ๋™๊ธฐํ™” ๋ฌธ์ œ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค

  • Multi-process

    • ์žฅ์ 

      • ๋ฉ€ํ‹ฐ ํ”„๋กœ์„ธ์Šค ๋ฐฉ์‹์€ ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ฃฝ๋”๋ผ๋„ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์—๋Š” ์˜ํ–ฅ์„๋ผ์น˜์ง€ ์•Š๊ณ  ์ •์ƒ์ ์œผ๋กœ ์ˆ˜ํ–‰๋œ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์ง€๋งŒ,

    • ๋‹จ์ 

      • ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ๋ณด๋‹ค ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„๊ณผ CPU ์‹œ๊ฐ„์„ ์ฐจ์ง€ํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์กด์žฌํ•œ๋‹ค

  • ์ •๋ฆฌ

    • Multi-thread์™€ multi-process๋Š” ๋™์‹œ์— ์—ฌ๋Ÿฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๋Š” ์ ์—์„œ ๊ฐ™์ง€๋งŒ,

      • ์ ์šฉํ•ด์•ผํ•˜๋Š” ์‹œ์Šคํ…œ์— ๋”ฐ๋ผ ์ ํ•ฉ/๋ถ€์ ํ•ฉ์ด ๊ตฌ๋ถ„๋œ๋‹ค

    • ๋”ฐ๋ผ์„œ ๋Œ€์ƒ ์‹œ์Šคํ…œ์˜ ํŠน์ง•์— ๋”ฐ๋ผ ์ ํ•ฉํ•œ ๋™์ž‘ ๋ฐฉ์‹์„ ์„ ํƒํ•˜๊ณ  ์ ์šฉํ•ด์•ผํ•œ๋‹ค

Last updated