Notice
Recent Posts
Recent Comments
Link
관리 메뉴

look-forest

프로세스 간 통신 : IPC 본문

Computer Science/Operating System

프로세스 간 통신 : IPC

studyHub 2021. 6. 6. 22:45

지난 시간에 concurrently executing되는 멀티프로세싱에 대해서 배웠다.

이때 프로세스들은 independent할 수도 있고, cooperating할 수도 있다.

cooperating할 때는 프로세스 간에 서로 영향을 주고받는데, 이는 데이터를 교환하고 공유함을 의미한다.

 

그런데 프로세스들은 서로 독립적이고, 메모리를 공유하지 않는다. 서로의 메모리 영역을 침범해선 안된다.

이번 시간에는 프로세스들이 동시 실행되면서 데이터를 주고받는 프로세스 간 통신에 대해서 알아보겠다.

 

※ 참고로 프로세스 관련 통신 종류는 다음과 같이 3 종류가 있는데,

그 중 한 컴퓨터 내에서 concurrently executing되는 프로세스간 통신에 대해서 알아보는 것이다.

 

프로세스 간 데이터 통신에는 사용자 직접 구현 방식과 OS 제공 통신 방식이 있다

 


IPC: Inter-Process Communication

cooperating한 (동시에 실행되며 데이터를 교환하는) 프로세스들은 IPC 메카니즘이 필요하다.

즉, 데이터를 보내고 받을 수 있어야 한다.

이러한 Communication model에는 크게 2가지 방법이 있다.

  • shared memory (사용자 직접 구현)
  • message passing (OS 제공)

Communication models. (a) Shared memory. and (b) Message Passing

 

IPC in Shared-Memory Systems

Producer-Consumer Problem를 예로 들어보겠다.

생산자는 공유 버퍼에 메시지를 write()하고, 소비자는 공유 버퍼로부터 read()한다.

Producer는 정보 생산, Consumer는 정보 소비.
ex) web server와 web browser

1. Producer와 Consumer가 둘 다 접근할 수 있는 shared memory(buffer)를 정의한다.

#define BUFFER_SIZE 10

typedef struct{
	...
} item;

item buffer[BUFFER_SIZE];
int in = 0;
int out =0;

 

2. Producer가 write할 수 있는 로직을 구현한다.

item next_produced;

while(true){
	/* produce an item in next_produced */
    
    while ((in+1) % BUFFER_SIZE) == out)
    	; /* do nothing. waiting. */
    
    buffer[in] = next_produced;
    in = (in+1) % BUFFER_SIZE;
}

 

3. Consumer가 read할 수 있는 로직을 구현한다.

item next_consumed;

while (true) {
	while (in == out)
    	; /* do nothing. 버퍼가 채워질 때까지 대기 */
    
    next_consumed= buffer[out];
    out = (out+1) % BUFFER_SIZE;
    
    /* consume the item in next_consumed */
}

단점

shared memory 할당 및 회수, 복잡한 read-write 로직을 개발자가 작성해야 한다..

게다가 Producer-Consumer 1:1 관계가 아니라 많은 Producer와 Consumer가 있다면 구현하기가 더 어렵다.

 

IPC in Message-Passing Systems 

데이터 교환을 위한 수단(API)을 OS가 제공해준다!

ex) pipe

  • send(message)
  • receive(message)

개발자는 그저 메시지를 던지면 된다.

 

 

Producer는 OS가 제공하는 massage passing System call을 사용한다.

message next_produced;

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

Consumer는 OS가 제공하는 massage passing System call을 사용한다.

message next_produced;

while (true) {
	receive(next_produced);
    /* consume an item in next_produced */
}

 

이러한 Communication Link(send to and receive messages)는 다양한 방식으로 구현될 수 있다.

  • direct or indirect communication
  • synchronous and asynchronous communication
  • automatic or explicit buffering

direct or indirect communication

1. direct communication: recipient와 sender를 명시하여, 1:1 link 생성

  • send(𝑃, message) – send a message to process 𝑃
  • receive(𝑄, message) – receive a message from process 𝑄

2. indirect communication: mailbox(port)를 통하여, m:n 통신 가능

  • send(𝐴, message) – send a message to mailbox 𝐴
  • receive(𝐴, message) – receive a message from mailbox

mailbox(또는 port)를 사용한 message-passing은 indirect 통신을 가능하게 한다

이때 OS는 다음과 같은 기능을 제공한다.

  • Create a new mailbox
  • Send and Receive messages through the mailbox
  • Delete a mailbox

blocking or non-blocking: synchronous or asynchronous

blocking: sender는 message가 received될 때까지 blocking, receiver는 sender가 message를 보낼 때까지 blocking.

non-blocking: sender는 message를 보내고 다음 로직 진행, receiver는 message가 있으면 수신, 없으면 null 수신.

 

메시지의 전송이 완료될 때까지 block되는 send를 사용하면 synchronous 통신을 할 수 있다

blocking은 결국 sender와 receiver가 같은 상태를 유지하게 된다(동기화). //대신 기다리느라 느리다.

데이터를 수신하면 요금을 부과하는 등 synchronous해야 되는 서비스는 blocking I/O를 써야한다.

 

동기 비동기의 차이

더보기

동기 비동기의 차이: 기다리느냐 안 기다리느냐
동기적 처리: 빨래를 하고 설거지를 하고 청소를 한다. 하나의 작업이 끝날 때까지 기다린다.
비동기적 처리: 각각 업체에 시킨다. 그동안 다른 작업을 하고 일을 마친 업체는 내게 알려준다.

메소드를 실행시킴과 동시에 반환 값이 기대되는 경우를 동기, 그렇지 않은 경우 비동기라 한다.

 

동시에  실행되었을 때 값이 반환되기 전까지는 blocking되어 있다.

결국 상태를 일치시키면서 진행하기 때문에 동기화라고 한다.


비동기의 경우, blocking 되지 않고 이벤트 큐에 넣거나 백그라운드 스레드에게 해당 task를 위임하고 바로 다음 코드를 실행하기 때문에 기대되는 값이 바로 반환되지 않는다.

 

RPC: Remote Procedure Call
원격 컴퓨터의 함수를 호출 ▷ 원격 프로세스간 통신

예를 들어 1000만개의 배열 원소의 합을 구할 때,
10개의 원격 컴퓨터에 각각 100만개씩 더해서 반환하라고 요청하는 상황.
이때 원격지 컴퓨터의 sum() 함수를 호출하는 것.

 

 

 


 

참고 자료 & 이미지 출처
운영체제 공룡책 강의 (주니온 님)
Operating System Concepts, 10th Ed (Silberschatz et al)
쉽게 배우는 운영체제 (조성호 님)

 

'Computer Science > Operating System' 카테고리의 다른 글

Critical Section Problem: 프로세스 동기화가 필요한 상황  (0) 2021.06.09
CPU Scheduling  (0) 2021.06.08
Thread  (0) 2021.06.07
프로세스의 이해  (0) 2021.06.06
OS 란?  (0) 2021.06.05