Linux shared memory with luajit

Source: Internet
Author: User
Tags ffi

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.luaFfi = 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) endprint (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. ManagementIpcs command to view ipcrm to delete shared memory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.