I have a weird issue. I'm trying get a purebasic program to use shared memory. I've looked in apifunctions.txt to see if that was there and I didn't see it, so doesn't seem that its built in. I have code that works, but only run via sudo. So at least I know the code itself can work. I've tried a bit of C make sure its not a system level permissions issue and its not. I Garuda Linux which is Arch based. I think this has to do with purebasic not setting the permissions octal properly.
Code: Select all
ImportC "-lc"
shmget(key.l, size.l, shmflg.l)
shmat(shmid.l, shmaddr.i, shmflg.l)
shmdt(shmaddr.i)
EndImport
#IPC_CREAT = $200
#IPC_EXCL = $400
#IPC_PRIVATE = 0
#SHM_RDONLY = $1000
Procedure SharedMemoryExample()
; Generate a unique key using an integer
key = 125654
; Create or access a shared memory segment (1024 bytes)
shmid = shmget(key, 2048, #IPC_CREAT | 0666)
If shmid = -1
MessageRequester("Error", "Failed to create shared memory segment!")
ProcedureReturn
EndIf
; Attach to the shared memory segment
*shmaddr = shmat(shmid, 0, 0)
If *shmaddr = -1
MessageRequester("Error", "Failed to attach to shared memory!")
ProcedureReturn
EndIf
; Write a string to shared memory
PokeS(*shmaddr, "Hello, shared memory!", -1, #PB_UTF8)
; Read back the string from shared memory
SharedMemoryString$ = PeekS(*shmaddr, -1, #PB_UTF8)
MessageRequester("Shared Memory", "Data in shared memory: " + SharedMemoryString$)
; Detach from the shared memory
If shmdt(*shmaddr) = -1
MessageRequester("Error", "Failed to detach from shared memory!")
EndIf
EndProcedure
SharedMemoryExample()
I run a icps -m and I see the shared memory all 2 KB does get created.
Perms read as 232, that is the odd part because in the code it should have requested 666. So this has to do with the requested permissions not getting set properly from purebasic, because 232 is not the 666 requested. Anyone got a clue?╭─kake26@kake26 in ~/projects/purebasic/snippets took 3ms
╰─λ ipcs -m (base)
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 14 kake26 606 11496960 2 dest
0x00000000 15 kake26 606 11496960 2 dest
0x00000000 32811 kake26 600 4194304 2 dest
0x0001ead6 32814 kake26 232 2048 0