프로세스간 통신(IPC)

HJ·2021년 6월 21일
0

운영체제

목록 보기
3/3

Inter-Process Communication(IPC)


  • Cooperating processes require an IPC mechanism

  • Two fundamental models of IPC:
    - shared memory
    - message passing

  • shared memory => 응용프로그래머가 해당 로직을 직접 작성해야 함
  • message passing => OS가 다 알아서 해줌
    - direct/indirect
    - blocking/non-blocking

Example of IPC Systems


Shared Memory: POSIX Shared Memory


  • is organized using memory mapped files
    ※ POSIX: Portable Operating System Interface (for uniX)

- First, create a shared memory object:
fd = shm_open (name, O_CREAT | ORDWR, 0666);
- Configure the size of the object in bytes:
ftruncate(fd, 4096);
- Finally, establish a memory mapped file:
mmap (0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);


  • Producer process


  • Consumer process

위와 같이 작성 및 컴파일 후 생성된 Consumer 실행파일을 한번 실행하면 아래와 같이 출력됨
Hello, Shared Memory!
그리고 이어서 Consumer 실행파일을 다시 한번 실행하면 Segmentation fault가 뜸 => 이는 Consumer process 마지막에 Shared memory를 remove했기 때문임("shm_unlink(name);")

Shared Memory는 위와 같이 프로그래머가 직접 작성 및 관리해야 한다는 측면이 있음


Message Passing: Pipes


"Pipes were"

  • one of the first IPC mechanisms in early UNIX systems
  • A pipe acts as a conduit allowing two processes to communicate

"Ordinary Pipes"

  • Typically, a parent process creates a pipe and uses it to communicate with a child process that it created
  • allow two processes to communicate in producer-consumer fashion
    - the producer writes to one end of the pipe (write end)
    - the consumer reads from the other end (read end)
  • unidirectional : only one way communication is possible
  • two way communication? use two pipes!

"On UNIX systems,"

  • ordinary pipes are constructed using the function :
    - pipe(int fd[])   # 선언 시 배열의 크기는 2로 설정
    - fd[0] : the read end of the pipe
    - fd[1] : the write end
  • Ordinary pipe in UNIX

위와 같이 작성 및 컴파일 후 실행하면 아래와 같이 출력됨
read Greetings


Communication in Client-Server Systems


Sockets

  • are defined as endpoints for communication
  • 네트워크 안에서 커뮤니케이션하기 위해 등장한 개념
  • identified by an IP address concatenated with a port number


RPCs (Remote Procedure Calls)

  • abstracts procedure calls between processes on networked systems
  • one of the most common forms of remote service
  • designed as a way to abstract the procedure call mechanism
    - for use between systems with network connections
  • A client invokes a procedure on a remote host
    - as it would invoke a procedure locally

원격 프로시저 호출(영어: remote procedure call, 리모트 프로시저 콜, RPC)은 별도의 원격 제어를 위한 코딩 없이 다른 주소 공간에서 함수나 프로시저를 실행할 수 있게하는 프로세스 간 통신 기술이다. 다시 말해, 원격 프로시저 호출을 이용하면 프로그래머는 함수가 실행 프로그램에 로컬 위치에 있든 원격 위치에 있든 동일한 코드를 이용할 수 있다.

(참조 : wikipedia)

현재 유행하는 MSA(Micro Service Architecture) 구조로 서비스를 만들다보면, 다양한 언어와 프레임워크로 개발되는 경우가 잦음. 이런 Polyglot 한 구조에서는 프로토콜을 맞춰서 통신해야 하는 비용이 발생한다.

이 경우에 RPC를 이용하여 언어에 구애받지 않고, 원격에 있는 프로시저를 호출하여 조금 더 비즈니스 로직에 집중하는 개발을 할 수 있다.

(중략..)

일반적으로는 프로세스는 자신의 주소공간 안에 존재하는 함수만 호출하여 실행 가능하다. 하지만, RPC를 이용하면 다른 주소공간에서 동작하는 프로세스의 함수를 실행할 수 있게 된다. 위에도 언급했듯, 분산 컴퓨팅 환경에서 프로세스 간 상호 통신 및 컴퓨팅 자원의 효율적인 사용을 위해서 발전된 기술이다.

(참조 : https://velog.io/@jakeseo_me/RPC%EB%9E%80)

(참조 : 인프런 "운영체제 공룡책 강의")

profile
Hard & Soft

0개의 댓글