본문 바로가기
Study/운영체제

3. Processes - Example of IPC system

by 이미뇨 2023. 4. 11.

IPC : Inter-Process Communication의 약자로, 다른 프로세스와 데이터를 주고 받는 방법

세그먼트(segment): 프로그램 실행 시 메모리를 분할하여 관리하는 방식 중 하나

(일반적으로 프로그램은 코드, 데이터, 스택 등으로 나누어져 각각의 세그먼트로 구성)

Shared-memory (System-V, POSIX)

1.[System V] Shared-Memory

shared memory 만들기

int shmget ( key_t key, int size, int shmflg); //shared memory block을 만든다.

shmget() 함수는 새로운 공유 메모리 세그먼트를 생성하거나 이미 생성된 공유 메모리 세그먼트를 찾는다.

  • key: 생성하거나 찾으려는 공유 메모리 세그먼트의 식별자로 사용
  • size: 공유 메모리 세그먼트의 크기를 지정
  • shmflg: 공유 메모리 세그먼트의 퍼미션을 설정. 보통 0666을 사용

Ex) seg_id = shmget(IPC_PRIVATE, size, S_IRUSR|S_IWUSR); // 아이디를 리턴받는다. 

(S_IRUSR|S_IWUSR : read, write 가능하게 만들어라)

 

프로세스의 주소 공간에 shared memory 연결

void* shmat ( int shmid, char *shmaddr, int shmflg); //메모리를 매핑하는 명령어
  • shmid: shmget() 함수를 통해 반환된 공유 메모리 세그먼트 식별자
  • shmaddr: 공유 메모리 세그먼트를 연결할 가상 주소 공간의 주소. NULL로 설정하면 커널이 알아서 가상 주소 공간을 지정.
  • shmflg: 공유 메모리 세그먼트에 대한 접근 권한을 설정. 대부분의 경우 0을 사용.

Ex) shared_mem = (char *) shmat(seg_id, NULL, 0) // 그냥 메모리처럼 쓰면 된다.

 

 

더보기

공부를 하다가 갑자기 궁금해진점

 

왜 세그먼트 기법을 사용해야하는가?

이유

: 프로그램이 사용하는 메모리 공간을 효율적으로 관리할수 있다.

예시

: 예를들어 데이터 세그먼트와 스택 세그먼트는 프로그램 실행 중 크기가 동적으로 변할 수 있다

세그먼트 기법을 사용하면 이러한 동적인 메모리 할당 및 해제를 쉽게 처리할수 있다고 한다.

 

 

더보기

Reading Assignment

 

System V shared memory

shmget() - 공유 메모리 블록 할당

shmat() - 공유 메모리 블록을 프로세스에 연결합니다

shmdt() - 프로세스에서 공유 메모리 블록 분리

shmctl() - 공유 메모리 블록 제어 및 할당 해제

 

2.POSIX Shared Memory

IPC POSIX Consumer

// IPC POSIX Producer


#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 obect */ 
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); //sprintf : 자동으로 NULL을 넣어줌
ptr += strlen(message_0); 
sprintf (ptr, "%s",message_1); 
ptr += strlen(message_1);

return 0;
}
// IPC POSIX Consumer


#include <stdio.h> 
#include <stdlib.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 obect */ 
void *ptr;
/* open the shared memory object */ 
shm_fd = shm_open(name, O_RDONLY, 0666); //O_RDONLY : 읽어오라
/* memory map the shared memory object */ 
ptr = mmap(0, SIZE, PROT_READ, MAP SHARED, shm fd, 0); //메모리 매핑
/* read from the shared memory object */ 
printf("%s", (char *)ptr);
/* remove the shared memory object */ 
shm unlink(name);

return 0;
}

 

더보기

Reading Assignment

 

POSIX shared memory (컴파일 옵션 '-lrt' 필요)

  • shm_open(): POSIX shared memory의 생성 및 열기를 수행하는 함수
  • shm_unlink(): shm_open() 함수로 열린 POSIX shared memory 객체를 삭제하는 함수. 해당 객체를 더 이상 사용하지 않을 때 호출
  • ftruncate(): 파일 디스크립터를 이용하여 파일의 크기를 변경하는 함수
  • mmap(): 파일이나 파일 디스크립터를 메모리에 매핑하는 함수
  • munmap() : mmap() 함수로 매핑된 메모리를 해제하는 함수

 

POSIX message passing

  • msgget() - message queue 생성
  • msgsnd() - message queue로 메시지 보내기
  • msgrcv() - message queue로 부터 메시지 받기
  • msgctl() – control/deallocate message queue (eg: msgctl(msgq, IPC_RMID, NULL);)

 

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

4. Threads - Multithreading models  (0) 2023.04.12
4. Threads - Overview  (0) 2023.04.12
3. Processes - Inter-process communication  (0) 2023.04.11
3. Processes - Operations on processes  (0) 2023.04.11
3. Processes - Process scheduling  (0) 2023.04.11