调用shmat时出错

时间:2015-08-15 16:00:11

标签: c++ shared-memory

尝试通过将一大块用户缓冲区转换为共享内存来对共享内存执行某些操作,但shmat()仍然失败。

# ./a.out
shmid=89260087 0x7fbab055c000
shmat failed: : Invalid argument

这是源代码。不知道是什么原因。感谢。

#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <malloc.h>

char *output;
int n;

int main (int argc, char *argv[]) {
    void *p = NULL;
    int i;
    double startTS;
    double tmp;
    output = (char*) valloc(0x1000000);
    sprintf(output, "hi you!!");
    unsigned int size = 0x1000000;
    int shmid = shmget(12348, 0x1000000, IPC_CREAT); //SHM_RDONLY);
    printf("shmid=%d %p\n", shmid, &output[0]);
    char *bigBuf = (char*)shmat(shmid, &output[0], 0);
    if (bigBuf == (void*)-1) {
        perror("shmat failed: "); exit(-1);
    }
    bigBuf[0] = 'x';
    printf("%p enter:\n", bigBuf);
    scanf("%d\n", &i);
    return 0;
}

0 个答案:

没有答案