본문 바로가기
보안과정/참고

공유 메모리 관련 함수에 대하여

by Luuii 2017. 11. 30.

shmget() 함수

shmat() 함수

shmdt() 함수


○ shmget()

NAME

       shmget - allocates a shared memory segment


 SYNOPSIS

       #include <sys/ipc.h>


       #include <sys/shm.h>


       int shmget(key_t key, size_t size, int shmflg);


 DESCRIPTION

       shmget()  returns  the  identifier of the shared memory segment associated with the

       value of the argument key.  A new shared memory segment, with  size  equal  to  the

       value  of  size  rounded  up  to a multiple of PAGE_SIZE, is created if key has the

       value IPC_PRIVATE or key isn’t IPC_PRIVATE, no shared memory segment  corresponding

       to key exists, and IPC_CREAT is specified in shmflg.


       If shmflg specifies both IPC_CREAT and IPC_EXCL and a shared memory segment already

       exists for key, then shmget() fails with errno set to EEXIST.  (This  is  analogous

       to the effect of the combination O_CREAT | O_EXCL for open(2).)


       The value shmflg is composed of:


       SHM_HUGETLB used for allocating HUGETLB pages for shared memory.  IPC_CREAT to cre-

                   ate a new segment. If this flag is not used, then  shmget()  will  find

                   the  segment  associated with key and check to see if the user has per-

                   mission to access the segment.


       IPC_EXCL    used with IPC_CREAT to ensure failure if the segment already exists.


○ shmget() 함수

> 프로세서가 여기에 접근을 할 수 있도록 attach라는 작업이 필요하고 떼어낼 때는 dettach 작업이 필요하다.


○ shmat(), shmdt()

NAME

       shmop - shared memory operations


 SYNOPSIS

       #include <sys/types.h>

       #include <sys/shm.h>


       void *shmat(int shmid, const void *shmaddr, int shmflg);


       int shmdt(const void *shmaddr);


 DESCRIPTION

       shmat() attaches the shared memory segment identified by shmid to the address space

       of the calling process.  The attaching address is specified by shmaddr with one  of

       the following criteria:


       If  shmaddr  is  NULL,  the  system chooses a suitable (unused) address at which to

       attach the segment.


       If shmaddr isn’t NULL and SHM_RND is specified in shmflg, the attach occurs at  the

       address equal to shmaddr rounded down to the nearest multiple of SHMLBA.  Otherwise

       shmaddr must be a page-aligned address at which the attach occurs.


       If SHM_RDONLY is specified in shmflg, the segment is attached for reading  and  the

       process  must  have  read  permission  for  the  segment.  Otherwise the segment is

       attached for read and write and the process must have read and write permission for

       the segment.  There is no notion of a write-only shared memory segment.


○ shmat() 함수

> 이미 할당된 공유 메모리 공간을 다른 프로세스에서 사용할 수 있게 권한을 부여하는 함수

> 생성된 공유 메모리를 프로세스에 연결(Attach)

형식 : void *shmat(int shmid, const void *shmaddr, int shmflg);

> int shmid : 공유 메모리를 생성할 때 만들어진 공유 메모리 ID

> const void *shmaddr : 공유 메모리가 할당된 주소

> int shmflg : 공유 메모리 사용 옵션을 지정

>> SHM_RND : 공유 메모리 주소를 프로세스에 맞게 따로 할당

>> SHM_RDONLY : 공유 메모리를 읽기 전용으로 설정


○ shmdt() 함수

> 다른 프로세스에 연결된 공유 메모리 공간의 사용을 끝낸 후 프로세스와 공유 메모리 공간의 연결을 끊는다.

> 프로세스에 연결된 공유 메모리를 분리(Dettach)

형식 : int shmdt(const void *shmaddr);

>> const void *shmaddr : 공유 메모리가 할당된 주소


반응형

'보안과정 > 참고' 카테고리의 다른 글

SAN(Storage Area Network)  (0) 2017.12.29
변수의 메모리 배치 확인과 GDB 사용법  (0) 2017.11.30
아스키 코드표  (0) 2017.11.29
SIGNAL에 대하여  (0) 2017.11.29
ln 명령어  (0) 2017.11.29