java如何创建共享内存_如何使用共享内存共享char **变量

我正在使用共享内存在两个进程之间进行通信 . 我使用char **附加到共享地址空间 . 问题是当我在 生产环境 者上填充数据然后将消费者附加到地址空间时,没有数据被传输 . 我只是得到空值

这是我的制片人的片段

// create shared memory

shm_handle = shmget(key, BUFF_SIZE * PAGE_SIZE, IPC_CREAT | 0644);

printf("\nhandle is %d\n", shm_handle);

// valid handle?

if (shm_handle == -1) {

printf("shared memory creation failed\n");

exit(0);

}

// attach to shared block, see man pages for detail

buf = (char**) shmat(shm_handle, 0, 0);

if (buf == (char **) -1) {

printf("Shared memory attach failed");

exit(0);

}

int a = 0;

buf = malloc(sizeof(char*) * BUFF_SIZE);

for (a = 0; a < BUFF_SIZE; a++) {

buf[a] = malloc(sizeof(char) * PAGE_SIZE);

}

和消费者

// create shared memory

shm_handle = shmget(key, BUFF_SIZE * PAGE_SIZE, IPC_CREAT | 0644);

printf("handle is %d", shm_handle);

// valid handle?

if (shm_handle == -1) {

printf("shared memory creation failed\n");

exit(0);

}

char ** buft;

int a = 0;

// attach to shared block

buf = (char**) shmat(shm_handle, 0, 0);

if (buf == (char **) -1) {

printf("Shared memory attach failed");

exit(0);

}

buf = malloc(sizeof(char*) * BUFF_SIZE);

buft = malloc(sizeof(char*) * PAGE_SIZE);

for (a = 0; a < BUFF_SIZE; a++) {

buf[a] = malloc(sizeof(char) * PAGE_SIZE);

buft[a] = malloc(sizeof(char) * PAGE_SIZE);

}

printf("%s", buf[0]);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值