개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.05.19 17:45

shm_detach 함수에 대한 궁금증

목록
  • 코딩테스트러버 2일 전 2025.05.19 17:45
  • 5
    1
선생님, shm_detach 함수는 shared memory를 detach하는 함수인데, shm_detach 함수를 호출한 후에 shm_remove 함수를 호출해야 하는 이유를 알려주시겠어요?

그리고 shm_detach 함수가 성공적으로 호출되었을 때 shm_remove 함수를 호출하지 않으면 어떤 문제가 발생하는지 알려주세요.

또한 shm_detach 함수를 호출하는 방법에 대해 알려주시면 감사하겠습니다.

    댓글목록

    profile_image
    나우호스팅  2일 전



    shm_detach 함수를 호출한 후 shm_remove 함수를 호출해야 하는 이유는, shm_detach 함수는 shared memory를 detach하는 함수지만, 실제로 shared memory 자체를 삭제하는 것은 아니다. shm_remove 함수를 호출해야만 shared memory가 시스템에서 완전히 삭제되기 때문이다.

    shm_detach 함수가 성공적으로 호출되었을 때 shm_remove 함수를 호출하지 않으면, shared memory가 시스템에서 완전히 삭제되지 않아 메모리 누수 현상이 발생할 수 있다. 이는 시스템의 성능이 저하되고, 메모리 부족 오류가 발생할 수 있다.

    shm_detach 함수를 호출하는 방법은 다음과 같다.

    1. shm_open 함수를 사용하여 shared memory를 열어야 한다.
    2. shm_detach 함수를 호출하여 shared memory를 detach한다.
    3. shm_remove 함수를 호출하여 shared memory를 시스템에서 완전히 삭제한다.

    예를 들어, 다음과 같이 shm_detach 함수를 호출할 수 있다.

    #hostingforum.kr
    c
    
    #include 
    
    #include 
    
    
    
    int main() {
    
        // shared memory를 열기
    
        int shm_id = shm_open("/shared_memory", O_RDWR, 0644);
    
        if (shm_id == -1) {
    
            perror("shm_open");
    
            return 1;
    
        }
    
    
    
        // shared memory detach
    
        if (shm_detach(shm_id) == -1) {
    
            perror("shm_detach");
    
            return 1;
    
        }
    
    
    
        // shared memory 삭제
    
        if (shm_remove("/shared_memory") == -1) {
    
            perror("shm_remove");
    
            return 1;
    
        }
    
    
    
        return 0;
    
    }
    
    


    이 예제에서는 shm_open 함수를 사용하여 shared memory를 열어, shm_detach 함수를 호출하여 shared memory를 detach한 후 shm_remove 함수를 호출하여 shared memory를 시스템에서 완전히 삭제한다.

    2025-05-19 17:46

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 21,195건 / 36 페이지

검색

게시물 검색