注册 登录
编程论坛 Linux教室

获取到的共享内存地址无法访问

w906414 发布于 2019-06-01 17:44, 6087 次点击
程序代码:
{
    unsigned int iShmId = 0;
    unsigned int uiShmKey = 0x10724;
    unsigned int *ptest = RRM_PNULL;

    iShmId = shmget_wrapper(uiShmKey, 0, (IPC_CREAT | 0666));
    if (iShmId >= 0)
    {
        fprintf(stderr, "iShmId(%d) exist\n", iShmId);
        /* IF key exist then remove SHM*/
        shmctl_wrapper_ipc_rmid(iShmId);
    }

    /* Get the shared memory ID */
    iShmId = shmget_wrapper(uiShmKey, sizeof(unsigned int),(IPC_CREAT | 0666));
    if (0 > iShmId)
    {
        fprintf(stderr, "SHM(%d) get failed\n", uiShmKey);
        return;
    }

    /* Map Shared memory */
    ptest = (unsigned int *)shmat_wrapper(iShmId, RRM_PNULL, 0);   
    fprintf(stderr,"ptest(%p)", ptest);
    if ((void *)-1 == ptest)
    {
        fprintf(stderr,"SHM(%d) attach failed", uiShmKey);
        shmctl_wrapper_ipc_rmid(iShmId);
        iShmId = -1;
        return;
    }

    /* Initialize shared memory */
    memset(ptest, 0, sizeof(unsigned int));   
    fprintf(stderr,"SHMend", uiShmKey);
}
2 回复
#2
w9064142019-06-01 17:46
程序没有返回错误,但是运行到最后metset就会挂死
#3
w9064142019-06-01 17:51
上面的代码有问题,使用的是unsigned int,但后续全部改为int之后,依然是memset挂死
1