如何共享现有的char *与其他进程? [英] How to share existing char * with an other process?

查看:133
本文介绍了如何共享现有的char *与其他进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想与大家分享使用 shmget的的shmat 的其他叉+ execed过程中的一些记忆:

I'm trying to share some memory with an other forked+execed process using shmget and shmat:

char test[]="test";
int shID;
char *shptr;
key_t shkey = 2404;

shID = shmget(shkey, sizeof(char)*(strlen(test)+1), IPC_CREAT | 0666);
if (shID >= 0) {
    shptr = shmat(shID, 0, 0);
    if (shptr==(char *)-1) {
        perror("shmat");
    } else {
        memcpy(shptr, &test, strlen(test)+1);
        ....
        //forking and execing
        ....
        shmdt(shptr);
    }
} else {
    perror("shmget");
}

这工作得很好。

问题是,测试[] 将是一个巨大的的char * 。所以,我喜欢轻松共享文本[] ,而不是复制it.Is有没有更好的方式来处理呢?

The thing is that test[] is going to be a huge char*. So I liked easy to share text[] instead of copying it.Is there any better way to handle this?

推荐答案

如果你可以阅读你想从文件和位置读取比你可以使用mmap映射文件到内存中的一部分文件大小或确切的内存。

if you can read file size or exact memory which you want to read from file and location than you can use mmap to map that part of file to memory.

这篇关于如何共享现有的char *与其他进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆