Post

[OS] Operating System(3-2): Process

[OS] Operating System(3-2): Process

๐Ÿ€ ์šด์˜์ฒด์ œ ์ „๊ณต ์ˆ˜์—… ์ •๋ฆฌ

IPC - Message Passing


๐Ÿ“šMessage Passing์€ ๋‘ ๊ฐ€์ง€ ์ฃผ์š” ์šฉ๋„๋ฅผ ๊ฐ€์ง„๋‹ค

  1. communicate
  2. synchronize
  • ํ”„๋กœ์„ธ์Šค๊ฐ€ ์„œ๋กœ ์ •๋ณด๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ๋•Œ, ์ง์ ‘ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์“ฐ์ง€ ์•Š๊ณ (๊ณต์œ  ๋ณ€์ˆ˜ ์—†์ด)
    send(message)์™€ receive(message) ๊ธฐ๋Šฅ์„ ํ†ตํ•ด ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • message size is fixed or variable

  • ๋‘ ํ”„๋กœ์„ธ์Šค P์™€ Q๊ฐ€ ํ†ต์‹ ํ•˜๋ ค๋ฉด:
    • communication link๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•˜๋ฉฐ, ๊ทธ ํ›„ send/receive๋ฅผ ํ†ตํ•ด ๋ฉ”์‹œ์ง€๋ฅผ ์ฃผ๊ณ ๋ฐ›์Œ

โœ…Implementation issues:

  • ๋งํฌ ์„ค์ • ๋ฐฉ๋ฒ•?
  • ๋งํฌ๋‹น ํ—ˆ์šฉ๋˜๋Š” ํ”„๋กœ์„ธ์Šค์˜ ์ˆ˜?
  • ํ†ต์‹ ์— ํ•„์š”ํ•œ ๋งํฌ์˜ ์ˆ˜?
  • ๋งํฌ์˜ ์šฉ๋Ÿ‰?
  • ๋ฉ”์„ธ์ง€์˜ ํฌ๊ธฐ๋Š” ๊ณ ์ •/๊ฐ€๋ณ€ ?
  • ๋งํฌ๋Š” ๋‹จ๋ฐฉํ–ฅ์ผ๊นŒ ์–‘๋ฐฉํ–ฅ์ผ๊นŒ?

๐Ÿ“**link์˜ ๊ตฌํ˜„ ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹**:

  1. ๐Ÿ“ฆ Physical
    • Shared Memory
    • Hardware Bus
    • Network
  2. ๐Ÿง  Logical
    • Direct vs Indirect (์ง์ ‘ ํ†ต์‹  vs ๊ฐ„์ ‘ ํ†ต์‹ )
    • Synchronous vs Asynchronous (๋™๊ธฐ vs ๋น„๋™๊ธฐ)
    • Automatic vs Explicit Buffering (์ž๋™ vs ๋ช…์‹œ์  ๋ฒ„ํผ๋ง)

Direct Communication


Processes must name each other explicitly
โ†’ ๊ฐ ํ”„๋กœ์„ธ์Šค๋Š” ์ƒ๋Œ€๋ฐฉ์˜ ์ด๋ฆ„์„ ๋ช…์‹œ์ ์œผ๋กœ ์•Œ์•„์•ผ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Œ

  • send(P, message) โ†’ P๋ผ๋Š” ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ƒ„
  • receive(Q, message) โ†’ Q๋ผ๋Š” ํ”„๋กœ์„ธ์Šค๋กœ๋ถ€ํ„ฐ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์Œ

โœ…Properties of communication link

  1. Links are established automatically
  2. A link is associated with exactly one pair of communicating processes
  3. Between each pair, there exists exactly one link
  4. Link may be unidirectional, but is usually bi-directional

๋‹จ์ˆœํ•˜์ง€๋งŒ, ์œ ์—ฐ์„ฑ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์Œ (์ƒ๋Œ€๋ฐฉ ์ด๋ฆ„์„ ๋ฏธ๋ฆฌ ์•Œ์•„์•ผ ํ•จ)

Indirect Communication


๐Ÿ“šIndirect Communication: Mailbox๋ฅผ ์ด์šฉํ•œ ๋ฉ”์‹œ์ง€ ์ „๋‹ฌ

  • ๋ฉ”์‹œ์ง€๋Š” `mailbox`๋ฅผ ํ†ตํ•ด ์†ก์ˆ˜์‹ ๋œ๋‹ค (Mach์—์„œ๋Š” mailbox๋ฅผ โ€œportโ€๋ผ๊ณ  ์นญํ•จ)
  • ๊ฐ mailbox๋Š” ๊ณ ์œ ํ•œ ID๋ฅผ ๊ฐ€์ง€๋ฉฐ, ์ด๋ฅผ ๊ณต์œ ํ•ด์•ผ๋งŒ ํ”„๋กœ์„ธ์Šค ๊ฐ„ ํ†ต์‹ ์ด ๊ฐ€๋Šฅ

โœ…**Properties of communication link**:

  1. Link establish only if processes share a common mailbox
  2. ํ•˜๋‚˜์˜ mailbox๋Š” ์—ฌ๋Ÿฌ ํ”„๋กœ์„ธ์Šค์™€ ์—ฐ๊ฒฐ ๊ฐ€๋Šฅ
  3. ํ”„๋กœ์„ธ์Šค ์Œ๋งˆ๋‹ค ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ†ต์‹  ๋งํฌ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ
  4. Link may be unidirectional or bi-directional
  • Mailboxes๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ operations์„ ์ง€์›
    • ์ƒˆ๋กœ์šด mailbox ์ƒ์„ฑ
    • ๋ฉ”์‹œ์ง€ ์†ก์ˆ˜์‹ 
    • mailbox ์ œ๊ฑฐ
  • ์›์‹œ ํ•จ์ˆ˜(primitive)๊ฐ€ ์กด์žฌ:
    • send(A, message) โ†’ mailbox A์— ๋ฉ”์‹œ์ง€ ๋ณด๋ƒ„
    • receive(A, message) โ†’ mailbox A๋กœ๋ถ€ํ„ฐ ๋ฉ”์‹œ์ง€ ๋ฐ›์Œ

โœ… Mailbox ๊ณต์œ  ๋ฌธ์ œ & ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

P1, P2, P3๊ฐ€ ๊ฐ™์€ mailbox A๋ฅผ ๊ณต์œ 
P1์ด ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ƒ„
๊ทธ๋Ÿฐ๋ฐ P2์™€ P3๊ฐ€ ๋™์‹œ์— ๋ฐ›์œผ๋ ค๊ณ  ํ•˜๋ฉดโ€ฆ?

โ“ ๋ˆ„๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›๊ฒŒ ๋ ๊นŒ?

โœ… Solutions:

  • mailbox๋Š” ์ตœ๋Œ€ 2๊ฐœ์˜ ํ”„๋กœ์„ธ์Šค๋งŒ ์—ฐ๊ฒฐ ๊ฐ€๋Šฅํ•˜๋„๋ก ์ œํ•œ
  • ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค๋งŒ receive ๊ฐ€๋Šฅ
  • ์‹œ์Šคํ…œ์ด receiver ์„ ํƒ, Sender์—๊ฒŒ ๋ˆ„๊ฐ€ ๋ฐ›์•˜๋Š”์ง€ ์•Œ๋ ค์คŒ

๐Ÿง  Synchronization

  • Message passing ๋ฐฉ์‹์€ blocking๊ณผ non-blocking์ด ์žˆ๋‹ค.
  • Blocking
    • Blocking = synchronous
    • Blocking send: ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ธ ํ”„๋กœ์„ธ์Šค๋Š” ์ƒ๋Œ€๊ฐ€ ๋ฐ›์„ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆผ โ†’ Queue ์‚ฌ์šฉ ์‹œ non-blocking ํšจ๊ณผ ๊ฐ€๋Šฅ
    • Blocking receive: ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์„ ํ”„๋กœ์„ธ์Šค๋Š” ๋ฉ”์‹œ์ง€๊ฐ€ ๋„์ฐฉํ•  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆผ
    • ๋‘˜ ๋‹ค ๊ธฐ๋‹ค๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ๋™๊ธฐํ™”๊ฐ€ ์ž˜ ๋งž์Œ โ†’ ์•ˆ์ •์ ์ด์ง€๋งŒ ๋А๋ฆด ์ˆ˜ ์žˆ์Œ
  • Non-blocking
    • Non-blocking = Asynchronous
    • Non-blocking send: ๋ฉ”์‹œ์ง€ ๋ณด๋‚ธ ํ›„ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ๋‹ค์Œ ์ผ ์ˆ˜ํ–‰ โ†’ ๋ฉ”์‹œ์ง€ ์†์‹ค ๋ฐฉ์ง€๋ฅผ ์œ„ํ•ด Queue ํ•„์š”
    • Non-blocking receive: ์ˆ˜์‹ ์ž๋Š” ๋‹ค์Œ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ฆ‰์‹œ ๋ฐ›์Œ
      • ์œ ํšจํ•œ ๋ฉ”์‹œ์ง€
      • ๋นˆ ๋ฉ”์‹œ์ง€ (null)
  • ๋‹ค์–‘ํ•œ ์กฐํ•ฉ ๊ฐ€๋Šฅ
    • Blocking + Blocking: rendezvous(๋Ÿฐ๋ฐ๋ทฐ)(:์„œ๋กœ ๋”ฑ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€๋งŒ ๋Œ€๊ธฐ)
    • Non-blocking + Non-blocking: ์„œ๋กœ ์‹ ๊ฒฝ ์•ˆ ์”€ โ†’ ๋ฉ”์‹œ์ง€ ์†์‹ค ๊ฐ€๋Šฅ์„ฑ ํผ

๐Ÿ“Blocking send/receive๋ฅผ ์‚ฌ์šฉํ•œ producer/consumer ๋ฌธ์ œ โ†’ rendezvousํ˜•์‹

Producer-message Passing:

1
2
3
4
5
6
message next_produced;

while (true){
    /* produce an item in next_produced */
    send(next_produced); // blocking send
}

Consumer-message Passing:

1
2
3
4
5
6
message next_consumed;

while (true){
    receive(next_consumed); // blocking receive
    /* consume the item in next_consumed */
}
  • ์ „์ฒด ํ๋ฆ„ ๊ทธ๋ฆผ
    1
    2
    3
    4
    
    ๐ŸŸข Producer                      ๐ŸŸข Consumer
    produce item                    wait for item
      โ†“                                โ†‘
    send(item)  โ”€โ”€โ”€โ”€โ”€[rendezvous]โ”€โ”€โ”€ receive(item)
    

    ๋‘˜ ๋‹ค blocking ์ƒํƒœ์ด๋ฏ€๋กœ, ๋”ฑ ๋งŒ๋‚˜์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตํ™˜ํ•จ
    ์ผ์ข…์˜ โ€œ์•ฝ์†๋œ ๋งŒ๋‚จโ€์ด๋ฏ€๋กœ ๋ฐ์ดํ„ฐ ์†์‹ค ์—†์Œ

Buffering


๐Ÿ“šBuffering: Blocking send/receive์˜ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ด

โœ…Implemetns in one of three ways:

  1. Zero capacity:
    • ํ๊ฐ€ ์—†์Œ
    • ๋ฉ”์‹œ์ง€๋Š” ๋ฐ˜๋“œ์‹œ ์ฆ‰์‹œ receiver์—๊ฒŒ ์ „๋‹ฌ๋˜์–ด์•ผ ํ•จ
    • Receiver๊ฐ€ ์ค€๋น„๋˜์ง€ ์•Š์œผ๋ฉด Sender๋Š” ๊ธฐ๋‹ค๋ ค์•ผ ํ•จ = rendezvous ๋ฐฉ์‹(์™„์ „ ๋™๊ธฐ)
  2. Bounded capacity:
    • ํ์— ์ตœ๋Œ€ n๊ฐœ์˜ ๋ฉ”์‹œ์ง€๊นŒ์ง€ ์ €์žฅ ๊ฐ€๋Šฅ
    • ํ๊ฐ€ ๊ฐ€๋“ ์ฐผ์„ ๊ฒฝ์šฐ, Sender๋Š” ๊ธฐ๋‹ค๋ ค์•ผ ํ•จ
  3. Unbounded capacity:
    • ์ด๋ก ์ ์œผ๋กœ ๋ฌดํ•œํžˆ ๋งŽ์€ ๋ฉ”์‹œ์ง€๋ฅผ ์ €์žฅ ๊ฐ€๋Šฅ
    • Sender๋Š” ์ ˆ๋Œ€ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š์Œ โ†’ ์™„์ „ํ•œ ๋น„๋™๊ธฐ
    • ํ๊ฐ€ ๋„˜์น  ๊ฑฑ์ •์ด ์—†์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ ๊ณผ๋‹ค ์‚ฌ์šฉ ๊ฐ€๋Šฅ์„ฑ ์žˆ์Œ

Examples of IPC Systems - POSIX


๐Ÿ“šPOSIX Shared Memory

  • ํ”„๋กœ์„ธ์Šค๋Š” shm_open()์„ ํ†ตํ•ด ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์—ด ์ˆ˜ ์žˆ์Œ
  • shm_fd() = shm_open(name, O_CREAT | O_RDWR, 0666);
  • ftruncate()๋ฅผ ์ด์šฉํ•ด ๋ฉ”๋ชจ๋ฆฌ ๊ฐ์ฒด์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •
  • ftruncate(shm_fd, 4096)(4096์€ size)
  • mmap()์„ ํ†ตํ•ด ์ด ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‹ค์ œ ์ฃผ์†Œ ๊ณต๊ฐ„(file pointer)์— ๋งคํ•‘
  • ์ดํ›„ ์ด mmap()๋œ pointer๋ฅผ ํ†ตํ•ด ๋ฉ”๋ชจ๋ฆฌ์— ์ ‘๊ทผ(read/write) ๊ฐ€๋Šฅํ•จ.
1
2
3
4
5
6
ํ”„๋กœ์„ธ์Šค A (Producer)                   ํ”„๋กœ์„ธ์Šค B (Consumer)
----------------------                  ----------------------
1. shm_open() ์ƒ์„ฑ                      1. shm_open() ์—ด๊ธฐ
2. ftruncate() ํฌ๊ธฐ ์„ค์ •               2. mmap()์œผ๋กœ ๋งคํ•‘
3. mmap()์œผ๋กœ ์ฃผ์†Œ ๋งคํ•‘                3. ํฌ์ธํ„ฐ๋กœ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ
4. ํฌ์ธํ„ฐ๋กœ ๋ฐ์ดํ„ฐ ์“ฐ๊ธฐ                4. shm_unlink()๋กœ ์ œ๊ฑฐ
  • IPC POSIX Producer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>

int main()
{
  /* the size (in bytes) of shared memory object */
  const int SIZE = 4096;
  // name of the shared memory object
  const char *name = "OS";
  // strings written to shared memory
  const char *message_0 = "Hello";
  const char *message_1 = "World";

  // shared memory file descriptor
  int shm_fd;
  // pointer to shared memory object
  void *ptr;
  
  // create the shared memory object
  shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);

  // configure the size of the shared memory object
  ftruncate(shm_fd, SIZE);

  // memory map the shared memory object
  ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);

  // write to the shared memory object
  sprintf(ptr, "%s", message_0);
  ptr += strlen(message_0);
  sprintf(ptr, "%s", message_1);
  ptr += strlen(message_1);

  return 0;
}

shm_open์œผ๋กœ ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ ๊ฐ์ฒด ์ƒ์„ฑ
ftruncate๋กœ ํฌ๊ธฐ ์ง€์ • (4096 ๋ฐ”์ดํŠธ)
mmap์œผ๋กœ ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ž์‹ ์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ์—ฐ๊ฒฐ
์ดํ›„ sprintf๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ์— ์“ฐ๊ธฐ

alt text

Copy-on-write๋Š” ๋ณต์‚ฌ๋ฅผ ๋Œ€๊ธฐํ•˜๋‹ค๊ฐ€ write ํ• ๋•Œ ๋ณต์‚ฌํ•ด์ฃผ๋Š” ์˜ต์…˜

  • IPC POSIX Consumer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>

int main()
{
  /* the size (in bytes) of shared memory object */
  const int SIZE = 4096;
  // name of the shared memory object
  const char *name = "OS";
  // shared memory file descriptor
  int shm_fd;
  // pointer to shared memory object
  void *ptr;
  
  //  open the shared memory object
  shm_fd = shm_open(name, O_RDONLY, 0666);

  // memory map the shared memory object
  ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);

  printf("%s", (char *)ptr);

  // remove the shared memory object
  shm_unlink(name);

  return 0;
}

shm_open์œผ๋กœ ๊ธฐ์กด ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ ์—ด๊ธฐ
mmap์œผ๋กœ ์ฝ๊ธฐ ์ „์šฉ ๋งคํ•‘
๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ์—์„œ ๋ฌธ์ž์—ด์„ ์ฝ์–ด์™€ ์ถœ๋ ฅ
์‚ฌ์šฉ ํ›„ shm_unlink()๋กœ ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ ์ œ๊ฑฐ
shm_unlink()๋Š” โ€œ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ ๊ฐ์ฒด ์ž์ฒด ์ œ๊ฑฐโ€ ์—ญํ• 

Examples of IPC Systems โ€“ Windows


๐Ÿ“šALPC(Advanced Local Procedure Call)๋Š” ๋™์ผํ•œ ์‹œ์Šคํ…œ ๋‚ด ํ”„๋กœ์„ธ์Šค๋“ค๋ผ๋ฆฌ ํ†ต์‹ ํ•˜๊ธฐ ์œ„ํ•œ `message-passing`๊ธฐ๋ฐ˜ **IPC ๋ฐฉ์‹**์ด๋‹ค

  • ALPC๋Š” RPC(์›๊ฒฉ ํ”„๋กœ์‹œ์ € ํ˜ธ์ถœ)์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ, ๋กœ์ปฌ์—์„œ๋งŒ ์ž‘๋™ํ•จ
  • port(like mailbox)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ†ต์‹  ์ฑ„๋„์„ ์„ค์ •ํ•˜๊ณ  ์œ ์ง€ ๊ด€๋ฆฌ

โœ…์ž‘๋™ ์›๋ฆฌ

  1. Client๊ฐ€ ์—ฐ๊ฒฐ ์š”์ฒญ ์‹œ์ž‘
    • ํด๋ผ์ด์–ธํŠธ๋Š” subsystem์˜ connection port์— ๋Œ€ํ•œ handle์„ ์—ฐ๋‹ค
    • ์ด connection port๋Š” ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ๋ณด์ž„
  2. Client โ†’ ์—ฐ๊ฒฐ ์š”์ฒญ(connection request) ์ „์†ก
  3. Server๊ฐ€ ํ†ต์‹  ์ค€๋น„
    • ์„œ๋ฒ„๋Š” twoprivate communication ports๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
    • ์ด ์ค‘ ํ•˜๋‚˜์˜ ํฌํŠธ๋ฅผ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋‹ค์‹œ ์ „๋‹ฌ
  4. ์–‘๋ฐฉํ–ฅ ํ†ต์‹  ์‹œ์ž‘
    • ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„๋Š” ์ „๋‹ฌ๋œ port handle์„ ํ†ตํ•ด ๋ฉ”์‹œ์ง€๋ฅผ ์ฃผ๊ณ ๋ฐ›๊ฑฐ๋‚˜ callback์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

alt text

PIP


๐Ÿ“š Pipes: ํŒŒ์ดํ”„๋Š” ๋‘ ๊ฐœ์˜ ํ”„๋กœ์„ธ์Šค ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๊ธฐ ์œ„ํ•œ ํ†ต๋กœ(conduit) ์—ญํ• 

  • ํ”„๋กœ์„ธ์Šค A โ†’ ํ”„๋กœ์„ธ์Šค B ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด์ง„ ํ†ต์‹  ๊ตฌ์กฐ
  • ํŒŒ์ผ์ฒ˜๋Ÿผ ์“ฐ๊ณ  ์ฝ์„ ์ˆ˜ ์žˆ์Œ

โœ… ํ•ต์‹ฌ ์ด์Šˆ๋“ค:

  1. ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋Š”์ง€ (๋‹จ๋ฐฉํ–ฅ/unidirectional), ์–‘์ชฝ ๋ชจ๋‘ ๊ฐ€๋Šฅ (์–‘๋ฐฉํ–ฅ/bidirectional)์ธ์ง€
  2. Half-duplex์ธ์ง€, Full-duplex์ธ์ง€? (ex - Simplex: ํ‚ค๋ณด๋“œ, Half-duplex: ๋ฌด์ „๊ธฐ, Full-duplex: ์ „ํ™”๊ธฐ)
  3. ํ†ต์‹ ํ•˜๋ ค๋ฉด ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๊ฐ€ ํ•„์š”ํ•œ๊ฐ€?
  4. ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ๊ฐ€?

โœ… **๋‘ ๊ฐ€์ง€ ํŒŒ์ดํ”„ ์ข…๋ฅ˜**:

  1. Ordinary Pipe
    • ์ œํ•œ๋œ ๊ด€๊ณ„: ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    • ์™ธ๋ถ€ ์ ‘๊ทผ ๋ถˆ๊ฐ€: ํŒŒ์ดํ”„๋ฅผ ๋งŒ๋“  ํ”„๋กœ์„ธ์Šค ์™ธ๋ถ€์—์„œ ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ
    • ์˜ˆ์‹œ: ๋ถ€๋ชจ๊ฐ€ ์ž์‹ ๋งŒ๋“ค๊ณ  โ†’ ์ž์‹์—๊ฒŒ ํŒŒ์ดํ”„๋กœ ๋ช…๋ น ์ „๋‹ฌ
  2. Named Pipe
    • ๋…๋ฆฝ์ ์ธ ํ†ต์‹  ๊ฐ€๋Šฅ: ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„ ํ•„์š” ์—†์Œ
    • ํŒŒ์ผ ์‹œ์Šคํ…œ ์ƒ์— ์ด๋ฆ„์„ ๊ฐ€์ง โ†’ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์™€ ์‰ฝ๊ฒŒ ๊ณต์œ  ๊ฐ€๋Šฅ
    • ์˜ˆ์‹œ: ls | less (์œ ๋‹‰์Šค์—์„œ ๋งŽ์ด ์‚ฌ์šฉ)

Ordinary Pipe


๐Ÿ“šOrdinary Pipe: ํ•œ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์“ฐ๊ธฐ(write) ํ•˜๊ณ , ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ฝ๊ธฐ(read) ํ•˜๋Š” ๋‹จ๋ฐฉํ–ฅ(unidirectional) ํ†ต์‹ ์šฉ ํŒŒ์ดํ”„

์ƒ์„ฑํ•œ ํ”„๋กœ์„ธ์Šค(๋ถ€๋ชจ)๋Š” ์ž์‹ ๋งŒ pipe๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Œ
ํ•˜์ง€๋งŒ ์ž์‹ ํ”„๋กœ์„ธ์Šค๋Š” ์ƒ์†๋ฐ›์€ ์—ด๋ฆฐ ํŒŒ์ผ๋กœ pipe ์ ‘๊ทผ ๊ฐ€๋Šฅ

โœ… **์ž‘๋™ ๋ฐฉ์‹**:

  • Producer โ†’ ๋ฐ์ดํ„ฐ๋ฅผ ์ž‘์„ฑ(write)
  • Consumer โ†’ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์Œ(read)
  • ํ•œ ๋ฐฉํ–ฅ(unidirectional) ์œผ๋กœ๋งŒ ํ†ต์‹  ๊ฐ€๋Šฅ

alt text

  • Windows์—์„œ๋Š” anonymous pipe ๋ผ๊ณ ํ•œ๋‹ค
  • ํŒŒ์ดํ”„๋Š” ์šด์˜์ฒด์ œ ์ž…์žฅ์—์„œ๋Š” ํŠน์ˆ˜ํ•œ ํŒŒ์ผ๋กœ ์ทจ๊ธ‰๋˜๋ฏ€๋กœ read() / write()๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ด ํ†ต์‹ ์ด ๊ฐ€๋Šฅ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// ํŒŒ์ดํ”„ ์ƒ์„ฑ
if (pipe(fd) == -1) {
    fprintf(stderr, "Pipe failed");
    return 1;
}
// ์ž์‹ ํ”„๋กœ์„ธ์Šค ์ƒ์„ฑ
pid = fork(); //๋ถ€๋ชจ ํ”„๋กœ์„ธ์Šค๋Š” pid > 0,์ž์‹ ํ”„๋กœ์„ธ์Šค๋Š” pid == 0

if(pid<0){
  fprintf(stderr, "Fork Failed");
  return 1;
}

if(pid > 0){// parent process
  close(fd[READ_END]);// ์ฝ๊ธฐ์šฉ ๋‹ซ์Œ (์•ˆ ์”€)
  write(fd[WRITE_END], write_msg, strlen(write_msg) + 1);
  close(fd[WRITE_END]); // ์“ฐ๊ธฐ ๋๋‚˜๊ณ  ๋‹ซ์Œ
}

else{//child process
  close(fd[WRITE_END]); // ์“ฐ๊ธฐ์šฉ ๋‹ซ์Œ
  read(fd[READ_END], read_msg, BUFFER_SIZE); // ๋ฉ”์„ธ์ง€ ์ˆ˜์‹ 
  printf("read %s", read_msg);
  close(fd[READ_END]); // ์ฝ๊ธฐ ๋๋‚˜๊ณ  ๋‹ซ์Œ
}

Named Pipe


๐Ÿ“šNamed Pipe:์ด๋ฆ„ ์žˆ๋Š” ํŒŒ์ดํ”„. ํŒŒ์ผ ์‹œ์Šคํ…œ ์ƒ์— ์กด์žฌ

  • ๐Ÿ”„ ์–‘๋ฐฉํ–ฅ ํ†ต์‹  (Bidirectional) ๊ฐ€๋Šฅ
  • ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„ ๋ถˆํ•„์š”: ๋…๋ฆฝ๋œ ํ”„๋กœ์„ธ์Šค ๊ฐ„์—๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • Ordinary Pipes๋ณด๋‹ค ๋” ๊ฐ•๋ ฅํ•˜๊ณ  ์œ ์—ฐํ•จ โ†’ UNIX & Windows ๋ชจ๋‘์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
1
int mkfifo(const char *pathname, mode_t mode);
  • pathname: FIFO์˜ ์ด๋ฆ„
  • mode: ์ ‘๊ทผ ๊ถŒํ•œ ์„ค์ •(์˜ˆ: 0666) ์ดํ›„ open(), read(), write(), close() ๋“ฑ ์ผ๋ฐ˜ ํŒŒ์ผ์ฒ˜๋Ÿผ ๋‹ค๋ฃธ

alt text

์‰˜์ด ls | less ๋ช…๋ น์–ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ตฌ์กฐ โ†’ ls๊ฐ€ ์ถœ๋ ฅ์„ pipe์— ์“ฐ๊ณ  less๊ฐ€ ๊ทธ๊ฑธ ๋ฐ›์•„ ์ถœ๋ ฅํ•จ.

  1. pipe(fd) ํ˜ธ์ถœ๋กœ ํŒŒ์ดํ”„ ์ƒ์„ฑ
  2. fork()๋กœ ์ฒซ ๋ฒˆ์งธ ์ž์‹ ์ƒ์„ฑ โ†’ less
    • close(fd[1]): ์“ฐ๊ธฐ ์ข…๋ฃŒ
    • dup2(fd[0], 0): ํŒŒ์ดํ”„ ์ž…๋ ฅ์„ ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ์—ฐ๊ฒฐ
    • exec(): less ์‹คํ–‰
  3. ๋‹ค์‹œ fork() โ†’ ๋‘ ๋ฒˆ์งธ ์ž์‹ ์ƒ์„ฑ โ†’ ls
    • close(fd[0]): ์ฝ๊ธฐ ์ข…๋ฃŒ
    • dup2(fd[1], 1): ํŒŒ์ดํ”„ ์ถœ๋ ฅ์„ ํ‘œ์ค€ ์ถœ๋ ฅ์œผ๋กœ ์—ฐ๊ฒฐ
    • exec(): ls ์‹คํ–‰

Sokcet


๐Ÿ“šSokcet: ํ†ต์‹ ์˜ ์ถœ๋ฐœ์ /๋„์ฐฉ์  ์—ญํ• ์„ ํ•˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด ๊ตฌ์กฐ

  • IP ์ฃผ์†Œ + ํฌํŠธ ๋ฒˆํ˜ธ๋ฅผ ํ•ฉ์ณ์„œ ์†Œ์ผ“์„ ๊ตฌ๋ถ„ํ•จ
    • ์˜ˆ: 161.25.19.8:1625 โ†’ IP ์ฃผ์†Œ: 161.25.19.8, ํฌํŠธ: 1625
  • ํ†ต์‹ ์€ ํ•ญ์ƒ pair๋กœ ์ด๋ฃจ์–ด์ง (์˜ˆ: ํด๋ผ์ด์–ธํŠธ โ†” ์„œ๋ฒ„)
  • 1024๋ฒˆ ์ดํ•˜ ํฌํŠธ๋Š” ์‹œ์Šคํ…œ์—์„œ ์˜ˆ์•ฝ๋œ ํฌํŠธ๋“ค (HTTP: 80, HTTPS: 443 ๋“ฑ)
  • 127.0.0.1 (loopback): ์ž๊ธฐ ์ž์‹ ์—๊ฒŒ ๋ณด๋‚ด๋Š” IP ์ฃผ์†Œ (์˜ˆ: ์„œ๋ฒ„/ํด๋ผ์ด์–ธํŠธ๋ฅผ ํ•œ ์ปดํ“จํ„ฐ์—์„œ ํ…Œ์ŠคํŠธ)

Remote Producer calls(RPC)


๐Ÿ“š ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ์ปดํ“จํ„ฐ์˜ Producer(ํ•จ์ˆ˜)๋ฅผ ๋กœ์ปฌ์ฒ˜๋Ÿผ ํ˜ธ์ถœ

  • ์‚ฌ์šฉ์ž๋Š” ๋งˆ์น˜ ์ž์‹ ์˜ ์ปดํ“จํ„ฐ์—์„œ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” ๋‹ค๋ฅธ ์ปดํ“จํ„ฐ์—์„œ ์‹คํ–‰

__์ฃผ์š” ์š”์†Œ:__

  1. Stubs:
    • Client Stub: ํด๋ผ์ด์–ธํŠธ ์ชฝ์—์„œ ์„œ๋ฒ„ ํ•จ์ˆ˜๋ฅผ ๋Œ€์‹  ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ
    • Server Stub: ์„œ๋ฒ„ ์ชฝ์—์„œ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›์•„ ์ง„์งœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ
  2. Marshalling / Unmarshalling::
    • ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด ํฌ๋งท์„ ๋ณ€ํ™˜ํ•˜๊ณ (Marshalling), ๋ฐ˜๋Œ€์ชฝ์—์„œ ๋‹ค์‹œ ํ•ด์„ํ•˜๋Š” ๊ฒƒ(Unmarshalling)
  • Windows์—์„œ๋Š” ์ด ๊ณผ์ •์„ ์œ„ํ•œ ์ฝ”๋“œ ์ŠคํŽ™์„ ์ •์˜ํ•˜๋Š” Microsoft Interface Definition Language(MIDL)๋ฅผ ์‚ฌ์šฉ alt text

  • XDL(External Data Representation): ์„œ๋กœ ๋‹ค๋ฅธ ์‹œ์Šคํ…œ ๊ฐ„ ๋ฐ์ดํ„ฐ ํ‘œํ˜„ ๋ฐฉ์‹์„ ํ†ต์ผ
    • Big-endian and little-endian * ๋ฉ”์„ธ์ง€ ์ „๋‹ฌ ๋ฐฉ์‹(remote communication์€ local๋ณด๋‹ค ์˜ค๋ฅ˜๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ)
    • exactly once: ๋ฉ”์‹œ์ง€๊ฐ€ ํ•œ ๋ฒˆ๋งŒ ์ฒ˜๋ฆฌ๋˜๋„๋ก ๋ณด์žฅ
    • at most once: ์ค‘๋ณต ๋ฉ”์‹œ์ง€ ๋ฐฉ์ง€, ๋ฌด์‹œ ๊ฐ€๋Šฅ์„ฑ ์กด์žฌ
    • ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ์‚ฌ์ด์—๋Š” ๋„คํŠธ์›Œํฌ ์ง€์—ฐ์ด๋‚˜ ์†์‹ค์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ •ํ™•ํžˆ ํ•œ ๋ฒˆ ์ฒ˜๋ฆฌ(exactly once)๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”
    • ์ด๋ฅผ ์œ„ํ•ด ACK(์‘๋‹ต ํ™•์ธ)๋ฅผ ํ†ตํ•ด ์š”์ฒญ ์ฒ˜๋ฆฌ ์™„๋ฃŒ๋ฅผ ์•Œ๋ ค์คŒ
  • rendezvous(matchmaker):
    • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋ฅผ ์ฐพ๋„๋ก ๋„์™€์ฃผ๋Š” OS ์„œ๋น„์Šค

alt text

This post is licensed under CC BY 4.0 by the author.