应用程序非正常退出,共享内存不能销毁,怎样销毁残留在系统中的共享内存?

应用程序非正常退出,共享内存不能销毁,怎样销毁残留在系统中的共享内存?

QNX的帮助文档,函数shm_open()里有个例子,是说怎样使用共享内存的。
注意里面 shm_unlink( “/bolts” ); 的用法了。

如果你是在开发测试,你甚至可以用以下命令行代替:
rm /dev/shmem/bolts

shm_unlink的Description最好也看看。

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>

int main( int argc, char** argv )
{
    int fd;
    unsigned* addr;

    /*
     * In case the unlink code isn't executed at the end
     */
    if( argc != 1 ) {
        shm_unlink( "/bolts" );
        return EXIT_SUCCESS;
    }

    /* Create a new memory object */
    fd = shm_open( "/bolts", O_RDWR | O_CREAT, 0777 );
    if( fd == -1 ) {
        fprintf( stderr, "Open failed:%s\n",
            strerror( errno ) );
        return EXIT_FAILURE;
    }
    
    /* Set the memory object's size */
    if( ftruncate( fd, sizeof( *addr ) ) == -1 ) {
        fprintf( stderr, "ftruncate: %s\n",
            strerror( errno ) );
        return EXIT_FAILURE;
    }

    /* Map the memory object */
    addr = mmap( 0, sizeof( *addr ),
            PROT_READ | PROT_WRITE,
            MAP_SHARED, fd, 0 );
    if( addr == MAP_FAILED ) {
        fprintf( stderr, "mmap failed: %s\n",
            strerror( errno ) );
        return EXIT_FAILURE;
    }

    printf( "Map addr is 0x%08x\n", addr );

    /* Write to shared memory */
    *addr = 1;

    /*
     * The memory object remains in
     * the system after the close
     */
    close( fd );

    /*
     * To remove a memory object
     * you must unlink it like a file.
     *
     * This may be done by another process.
     */
    shm_unlink( "/bolts" );

    return EXIT_SUCCESS;
}

当所有的进程都非正常退出后,会有共享内存遗留在内存中,当下次再启动程序时,我得判断该段内存是被使用的内存还是遗留的内存,如果是遗留的内存就可以重新初始化。但是到现在为止还不知道怎样判断内存是在使用的还是遗留的,如果有办法,请指教详细的做法,谢谢!!!

你的程序可以写成这样吗?
注意这一句和它的位置,它的用法:
/*

  • We unlink so object goes away on last close.
    */
    shm_unlink("/bolts");


    在程序可能出错前,先unlink了它。这样,你的程序出错了。它也自动消失了。
    还是建议你看看shm_unlink的说明:
    This function doesn’t affect any references to the shared memory object (i.e. file descriptors or memory mappings). If more than one reference to the shared memory object exists, then the link count is decremented, but the shared memory segment isn’t actually removed until you remove all open and map references to it.



This example uses a shared memory object to share data with a forked process: 

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>

main(int argc, char * argv[])
{
int        fd;
unsigned  *addr;

  /*
   * In case the unlink code isn't executed at the end
   */
  if (argc != 1) {
     shm_unlink("/bolts");
     return EXIT_SUCCESS;
  }

  /* Create a new memory object */
  fd = shm_open("/bolts", O_RDWR | O_CREAT, 0777);
  if (fd == -1) {
     fprintf(stderr, "Open failed : %s\n",
       strerror(errno));
     return EXIT_FAILURE;
  }

  /* Set the memory object's size */
  if (ftruncate(fd, sizeof(*addr)) == -1) {
     fprintf(stderr, "ftruncate : %s\n", strerror(errno));
     return EXIT_FAILURE;
  }

  /* Map the memory object */
  addr = mmap(0, sizeof(*addr), PROT_READ | PROT_WRITE,
               MAP_SHARED, fd, 0);
  if (addr == MAP_FAILED) {
     fprintf(stderr, "mmap failed:%s\n", strerror(errno));
     return EXIT_FAILURE;
  }

  printf("Map addr is %6.6X\n", addr);
  printf("Press break to stop.\n");
  sleep(3);    /* So you can read above message */

  /*
   * We unlink so object goes away on last close.
   */
  shm_unlink("/bolts");

  *addr = '0';
  if (fork())
     for (;;)
       if (*addr == '0')
         putc(*addr = '1', stderr);
       else
         sched_yield();
  else
    for (;;)
       if (*addr == '1')
         putc(*addr = '0', stderr);
       else
         sched_yield();
  return EXIT_SUCCESS;
}

to nakeyfish


看来你没有明白我的意思,当程序异常退出时,shm_ulink是不能执行的,又怎么能断开连接呢?