Chinaunix

标题: shmdt报错,请大侠帮忙 [打印本页]

作者: xiaoruoax    时间: 2012-12-17 22:23
标题: shmdt报错,请大侠帮忙
GUN/Linux编程指南中的例子,源代码如下:
/*
* atshm.c - Attaching a shared memory segment
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int shmid; /* Segment ID */
    char *shmbuf; /* Address in process */

    /* Expect an segment id on the command line */
    if (argc != 2) {
        puts("USAGE: atshm <identifier>");
        exit(EXIT_FAILURE);
    }
    shmid = atoi(argv[1]);

    /* Attach the segment */
    if ((shmbuf = shmat(shmid, 0, 0)) < (char *)0) {
        perror("shmat");
        exit(EXIT_FAILURE);
    }
    /* Where is it attached? */
    printf("segment attached at %p\n", shmbuf);

    /* See, we really are attached! */
    system("ipcs -m");

    /* Detach */
    if ((shmdt(shmbuf)) < 0) {
        perror("shmdt");
        exit(EXIT_FAILURE);
    }
    puts("segment detached");

    /* Yep, we really did detach it */
    system("ipcs -m");

    exit(EXIT_SUCCESS);
}

编译:gcc atshm.c -o atshm
执行:./atshm 11137就报错了如图:

作者: crazyhadoop    时间: 2012-12-17 23:43
shmid z这个参数可以随意指定么?  shmat 那返回的就是 -1 吧,貌似已经出错了




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2