Linux Shared Memory

Linux specific forum
kake26
Enthusiast
Enthusiast
Posts: 157
Joined: Sun Jan 25, 2004 7:21 pm
Contact:

Linux Shared Memory

Post by kake26 »

Hi all,

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()
So upon running it I failed to attach memory error.

I run a icps -m and I see the shared memory all 2 KB does get created.
╭─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
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
Enthusiast
Enthusiast
Posts: 157
Joined: Sun Jan 25, 2004 7:21 pm
Contact:

Re: Linux Shared Memory

Post by kake26 »

Hi all,

Okay so I did another deep dive in the forum. After a few searches and many pages I did find this viewtopic.php?p=268227&hilit=Linux+shar ... ry#p268227 a random post from 2008 about what I am trying to do. I copied the code and just attempted to run it. It ran to my surprise and it worked. I guess this time I got lucky after at least a day and many hours of running commands from a terminal few would ever understand. This thing was buried way down in the results so not surprised my initial searches missed this. Well, this is a wrap and I feel like a little silly.
Post Reply