Skip to content

Loading…

Only first call to shm_file:write works #14

Open
stuta opened this Issue · 1 comment

1 participant

@stuta

Only first call to shm_file:write works. Workaround is to use atached memory and detach+attach again before writes.

Run following program with argumets 0, 1 and 2.


local apr = require 'apr'

print("\n --- apr-lua shm test - start")
local tmp_path = "/tmp/ma_apr_shm"

local attach = tonumber( arg[1] ) or 0
local msg_size = 10

print("create: " .. tmp_path)
apr.file_remove(tmp_path) -- file may not exist before shm_create

shm_file_created = assert(apr.shm_create( tmp_path, msg_size ))
if( attach >= 1 ) then
print("attach: " .. tmp_path)
shm_file = assert(apr.shm_attach(tmp_path))
else
shm_file = shm_file_created
end

print(". Write 1: " .. string.char(65) .. ", " .. 65)
shm_file:seek("set", 0) -- set file postition to 0
shm_file:write( string.char(65) ) -- 65 == char A

shm_file:seek("set", 0) -- set file postition to 0
read_status_char1 = shm_file:read(1) -- read 1 char
read_status1 = string.byte( read_status_char1 )
print(". Read 1: " .. read_status_char1 .. ", " .. read_status1)

if( attach >= 2 ) then
print(".. detach and attach again")
assert(shm_file:detach())
shm_file = assert(apr.shm_attach(tmp_path))
end

print(". Write 2: " .. string.char(66) .. ", " .. 66)
shm_file:seek("set", 0) -- set file postition to 0
shm_file:write( string.char(66) ) -- 65 == char B

shm_file:seek("set", 0) -- set file postition to 0
read_status_char2 = shm_file:read(1) -- read 1 char
read_status2 = string.byte( read_status_char2 )
print(". Read 2: " .. read_status_char2 .. ", " .. read_status2)

if( attach >= 1 ) then
print("detach : " .. tmp_path)
assert(shm_file:detach())
end
print("destroy: " .. tmp_path)
assert(shm_file_created:destroy())

print(" --- apr-lua shm test - end\n")

@stuta

And theer is also segmentation fault when looping code (about 230 loops in OSX). My other code did not use attach/detach at all and it had segmentation faults also.


for loop = 1, 20000 do
print("-loop: " .. loop )
print(". Write 1: " .. string.char(65) .. ", " .. 65)
shm_file:seek("set", 0) -- set file postition to 0
shm_file:write( string.char(65) ) -- 65 == char A

shm_file:seek("set", 0) -- set file postition to 0
read_status_char1 = shm_file:read(1) -- read 1 char
read_status1 = string.byte( read_status_char1 )
print(". Read 1: " .. read_status_char1 .. ", " .. read_status1)

if( attach >= 2 ) then
    print(".. detach and attach again")
    assert(shm_file:detach())
    shm_file = assert(apr.shm_attach(tmp_path))
end

print(". Write 2: " .. string.char(66) .. ", " .. 66)
shm_file:seek("set", 0) -- set file postition to 0
shm_file:write( string.char(66) ) -- 65 == char B

shm_file:seek("set", 0) -- set file postition to 0
read_status_char2 = shm_file:read(1) -- read 1 char
read_status2 = string.byte( read_status_char2 )
print(". Read 2: " .. read_status_char2 .. ", " .. read_status2)

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.