设为首页 加入收藏

TOP

用Luajit玩Linux共享内存
2014-11-24 08:22:41 来源: 作者: 【 】 浏览:0
Tags:Luajit Linux 共享 内存

1. shmtest_wirte.lua


ffi = require 'ffi'


ffi.cdef[[


int shmget(int key,int size,int flag);


void * shmat(int shmid,const void* ptr,int flag);


void perror(const char* msg);


char * strcpy(char * dest,const char* src);


]]


local IPC_CREAT = 01000


local shmid = ffi.C.shmget(798,30000000,IPC_CREAT)


print(shmid)


ffi.C.perror("creat shm")


local ptr = ffi.C.shmat(shmid,nil,0)


local str = ffi.cast("char *",ptr)


ffi.C.strcpy(str,"Hello World")


ffi.C.perror("str cpy")


2. shmtest_read.lua
ffi = require 'ffi'
ffi.cdef[[
int shmget(int key,int size,int flag);
void * shmat(int shmid,const void* ptr,int flag);
void perror(const char* msg);
char * strcpy(char * dest,const char* src);
]]
local shmid = ffi.C.shmget(798,30000000,0)
if shmid==-1 then
ffi.C.perror('')
os.exit(0)
end
print(shmid)
ffi.C.perror("get shm by key")
local ptr = ffi.C.shmat(shmid,nil,0)
local str = ffi.cast('char *',ptr)
print(ffi.string(str,30))
3. 管理
ipcs命令查看
ipcrm删除共享内存


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android中有用的Item选中和按下去.. 下一篇移植Linux内核到mini2440常见问题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)