본문 바로가기
COMPUTER SCIENCE/OS

[OS] Process - IPC (Interprocess Communication)

by 민트맛녹차 2023. 7. 10.

Independent Process & Cooperating Process

OS 에서 실행되는 프로세스들은 Independent process 나 Cooperating process 중 하나이다.

Independent process 는 다른 프로세스에게 영향을 주거나 받을 수 없다. 즉, 다른 프로세스와 data 를 공유하지 않는 프로세스는 Independent process 이다.

Cooperating process 는 다른 프로세스에게 영향을 주거나 받을 수 있다. 즉, 다른 프로세스와 data 를 공유하는 프로세스는 Cooperating process 이다. 

 

다른 프로세스와 협력을 하는 이유는 다음과 같다.

  • Information sharing : 동일한 정보에 접근이 필요할 수 잇음
  • Computation speedup : 특정 작업을 빠르게 실행하려면, 하위 작업으로 나누고 다른 작업과 병렬로 실행해야 한다.
  • Modularity 
  • Convenience

 

IPC (Interprocess Communication)

Cooperating process 들은 data 를 주고받기 위해 IPC 메커니즘이 필요하다.

IPC 에는 Shared memory 와 Message Passing 이라는 두 가지 기본 모델이 있다.

  • Shared memory : Cooperating process 들에 의해 공유되는 메모리 영역이 설정됨
  • Message Passing : 공유 변수들에 의존하지 않고 cooperating process 간 교환되는 메세지를 통해 통신 

Message Passing 적은 양의 데이커 교환에 유리, 분산시스템에서의 구현이 비교적 쉬움

Shared Memory 는 비교적 빠름( Message Passing 은 system call 사용해 구현되므로)

 

Shared Memory

Shared memory 를 통해 프로세스를 교환하기 위해선, 프로세스가 shared memory 영역을 만들어야 한다. 

일반적으로, shared memeory 영역은 shared memory segment 를 생성하는 프로세스의 address space 에 있다, 해당 shared memeory segment 를 사용해 통신하려는 다른 프로세스들은 해당 segment 를 자신의 address space 에 연결해야 한다. 즉, process 는 다른 프로세스들의 주소공간에 shared memory 의 system call 을 통해 접근할 수 있다.

 

일반적으로 OS 는 한 프로세스가 다른 프로세스의 메모리에 엑세스하는 것을 허용하지 않는다. shared memory 를 사용하려면 두 개 이상의 프로세스가 이 제한을 제거하는데 동의해야 한다.

프로세스들은 shared memory 에 데이터를 읽고 쓰면서 정보를 교환할 수 있다. 데이터의 형식과 위치는 OS 가 아닌 프로세스에 의해 결정된다. 또한 프로세스는 데이터가 동시에 같은 위치에 쓰지 않도록 한다.

 

Producer-Consumer Problem

  • producer : information 을 생성
  • consumer : information 을 소비

producer-consumer problem 의 해결책 중 하나는 shared memory 를 사용하는 것이다. producer 과 consumer process 가 동시에 실행되도록 하려면, producer 가 채우고 consumer 가 비우는 items 의 buffer 가 필요하다. producer 와 consumer 는 consumer 가 item 이 생성되지 않을 때 소비하지 않도록 동기화 되어야 한다.

 

buffer 의 종류는 다음과 같다.

  • unbounded buffer : buffer size 에 제한이 없음. consumer 는 새로운 item 을 기다리지만, producer 는 항상 새로운 item 을 생성 가능
  • bounded buffer : buffer size 가 고정되어 있음. consumer 는 buffer 가 비면 기다려야하고, producer 는 buffer 가 가득 차면 기다려야함.

 

shared memory 를 사용할 때에는 데이터의 동기화와 상호 배제 등의 문제에 대한 적절한 처리가 필요하다.

 

 

Message Passing

message passing  은 프로세스들이 동일한 address space 를 고유하지 않고도 통신하고 동기화할 수 있다. 통신하는 프로세스들이 네트워크로 연결된 다른 컴퓨터에 위치하는 분산 환경에서 특히 유용하다.

 

message passing 은 두 가지 operations 를 제공한다.

  • send(message) : message size 는 고정, 가변 모두 가능
  • receive(message)

프로세스 P 와 Q 가 통신하려면, P 와 Q 사이에 communication link 를 만들어야 하고, send 와 receive 를 통해 message 를 교환해야 한다. 

 

Naming

프로세스의 통신 방법에는 direct communication 과 indirect communication 가 있다.

Direct Communication

프로세스끼리 직접 message 를 주고 받는다.

프로세스 name 이 명시되어야 함

  • send(P, message) : send a message to process P
  • receive(Q, message) : receive a message from process Q

통신하는 프로세스 사이에 link 가 자동으로 생성되며, 정확히 두 프로세스 당 하나의 링크가 존재한다.

 

Indirect Communication

message 를 mail boxes (ports) 를 통해 주고 받는다.

mailbox 이름이 명시되어야 함

  • send(A, message) : send a message to mailbox A
  • receive(A, message) : receive a message from mailbox A

 

Synchronization

message passing 은 blocking 또는 nonblocking 이다.

Blocking 은 synchronous 라고도 한다.

  • Blocking send : message 가 received 될 때까지 sender 가 block 되거나 queue 에 message 가 가득 찰 때 sender 가 block
  • Blocking receive : message 가 전송될 때 까지 receiver 가 block

Non-blocking 은 asynchronous 라고도 한다.

  • Non-blocking send : sender 는 계속 message 를 send 한다.
  • Non-blocking receive : receiver 는 valid message 나 null 을 receive 한다.

 

Buffering

프로세스간 메세지는 큐를 통해 교환되는데, 세 가지 방법으로 구현된다.

  • Zero capcity : queue 의 크기가 0. sender 는 receiver 가 message 를 받을 때 까지 기다려야 한다. 
  • Bounded capcity : queue 의 크기가 유한 길이 n. link 가 가득 차면 sender 는 기다려야 한다.
  • Unbounded capcity : queue 의 크기가 무한 길이. sender 는 기다리지 않는다.

 

 

IPC 예시...

 

참조
Silberschatz_Operating_System_Concepts_10e_2018
광운대 운영체제 강의(안우현 교수, 2021)

'COMPUTER SCIENCE > OS' 카테고리의 다른 글

[OS] Synchronization  (0) 2023.08.16
[OS] Threads  (0) 2023.07.27
[OS] CPU Scheduling  (0) 2023.07.14
[OS] Process - Operations on Processes  (0) 2023.07.10
[OS] Process - Process Concept & Scheduling  (0) 2023.07.03

댓글