본문 바로가기
운영체제

04. Inter-Process Communication

by kwon5346 2024. 4. 18.
반응형

Inter-Process Communication(프로세스 간 통신)

시스템 내의 프로세스들은 independent 하거나 cooperative 할 수 있다.

  • Independent process
    • 다른 프로세스의 실행에 의해 영향을 받거나 영향을 주지 않는 프로세스
  • Cooperating process
    • 다른 프로세스들에 의해 영향을 받거나 줄 수 있다

프로세스 협력을 허용하는 환경을 제공하는 데에는 몇가지 이유가 있다.

  • 정보 공유
  • 계산 가속화
  • 모듈성
    • 시스템 기능을 별도의 프로세스들 또는 스레드들로 나누어 모듈식 형태로 시스템을 구성

Signal

  • 프로세스에게 전달되는 software interrupt 신호 (특정 event에 의해 생성됨)
  • 모든 signal에는 각 signal을 처리하는 signal handler가 존재한다.
  • 운영체제는 프로세스에게 signal을 전달한다.
  • $ man 7 signal(시그널 종류와 설명 확인 가능)

  • int kill(pid_t pid, int sig)
    • 프로세스가 지정한 프로세스에게 시그널을 보낼 수 있다.

Pipes

  • 파이프는 두프로세스가 통신할 수 있게 하는 전달자로서 동작한다.
  • 초창기 UNIX 시스템에서 제공하는 IPC기법의 하나였다.
  • Ordinary pipes vs. named pipes
  • 고려해야할 4가지 문제
    • 단방향 통신 또는 양방향 통신을 허용하는가?(undirectional or bidirectional)
    • 양방향 통신이라면, half-duplex방식인가 full-deplex 방식인가?
    • 통신하는 두 프로세스 간에 parent-child 같은 특정 관계가 존재해야만 하는가?
    • 네트워크를 사용하여 통신할수 있는가?

Ordinary Pipes

  • pipe(int fd[])를 호출하여 구축된다.
  • 위 함수는 fd[] 파일 디스크립터를 통해 접근되는 파이프를 생성한다.
  • fd[0]: read, fd[1]: write
  • ordinary pipe는 파이프를 생성한 프로세스 이외에는 접근할 수 없다.
  • 그래서 부모 프로세스가 fork()로 자식 프로세스를 생성하고 자식은 부모로부터 파일을 상속받는다.
  • consider as index of a file descriptor table
  • stdin:0, stdout:1, stderr:2
  • 간단한 사용 예시
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#define WRTIE_END 1
#define READ_END 0

int main()
{
    char write_msg[100] = "Hello, World!";
    char read_msg[100];

    int pipefd[2];

    if (pipe(pipefd) == -1)
    {
        return -1;
    }
    pid_t pid = fork();

    if (pid < 0)
    {
        return -1;
    }
    else if (pid > 0) // parent 
    {
        close(pipefd[READ_END]);
        write(pipefd[WRTIE_END], write_msg, strlen(write_msg) + 1);
        close(pipefd[WRTIE_END]);
    }
    else // child
    {
        close(pipefd[WRTIE_END]);
        read(pipefd[READ_END], read_msg, 100);
        printf("read from pipe: %s\n", read_msg);
        close(pipefd[READ_END]);
    }
    return 0;
}
// output : read from pipe: Hello, World!

Named Pipes(FIFO)

  • bidirectional 통신이 가능하다.
  • parent-child같은 특정 관계를 필요로 하지 않는다.
  • 프로세스가 종료되더라도 named pipe는 계속 존재하게 된다.

반응형

'운영체제' 카테고리의 다른 글

06. Process Scheduling(2)  (0) 2024.04.24
05. Process Scheduling  (0) 2024.04.24
03. System Call  (10) 2024.04.03
02. Processes  (5) 2024.04.03
01. Introduction to Operating Systems  (0) 2024.04.02