Linux下使用python读取共享内存

在Python中没有直接支持Linux共享内存的库,但可以通过ctypes模块调用系统API来实现。本文介绍了如何使用C++创建共享内存,并展示了一个Python示例,该示例读取共享内存内容并将其保存到文件httpd_cdorked_config.bin,文件内容显示为'hello word!this is a test.'。
摘要由CSDN通过智能技术生成

python没有独立的库可以读取linux下的共享内存,下面使用ctypes调用系统的API读取共享内存的内容

使用C++创建共享内存

#include <stdio.h>
#include <iostream>
#include <unistd.h>  
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <errno.h>

#define MY_SHM_ID 67483

void get_buf(char *buf)
{
    int i=0;
    while((buf[i]=getchar())!='\n'&&i<1024)
        i++;
}


int main(  )
{
    printf("page size=%d\n", getpagesize());
    int shmid=0, ret=0;
    shmid = shmget(MY_SHM_ID, 4096, 0666|IPC_CREAT);
    
    if (shmid > 0)
    {
        printf("Create a shared memory segment %d\n", shmid);
    }
    struct shmid_ds shmds;
    ret = shmctl( shmid, IPC_STAT, &shmds );

    if (ret == 0 )
    {
        printf( "Size of memory segment is %d \n", shmds.sh
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值