def main(): # Set up shared data shm = SharedMemory(IPC_PRIVATE, size=SHARED_MEMORY_SIZE, flags=IPC_CREAT) mutex = Semaphore(IPC_PRIVATE, initial_value=1, flags=IPC_CREAT | IPC_EXCL) empty = Semaphore(IPC_PRIVATE, initial_value=BUF_SIZE, flags=IPC_CREAT | IPC_EXCL) full = Semaphore(IPC_PRIVATE, flags=IPC_CREAT | IPC_EXCL) print('Forking producer') producer_id = fork() if not producer_id: producer(shm, mutex, empty, full) print('Forking consumer') consumer_id = fork() if not consumer_id: consumer(shm, mutex, empty, full) # Wait for children wait() wait() # Clean up. shm.detach() shm.remove() mutex.remove() empty.remove() full.remove()
class Bitmap(): def __init__(self): self.shm = SharedMemory(None, flags = IPC_EXCL|IPC_CREAT, mode= 0o600, size = bitmap_size, init_character = b'\x00') # to show we are alive self.shm.write('\x01') # print(self.bytes()) # self.shm.attach() def get_key(self): return self.shm.key def get_id(self): return self.shm.id def get_size(self): return bitmap_size def bytes(self): buffer = self.shm.read(bitmap_size) n = 0 for i in range(bitmap_size): if buffer[i] != 0: n += 1 return n def has_new_bits(self): ret = 0 buffer = self.shm.read(bitmap_size) for i in range(bitmap_size): if buffer[i] and buffer[i] & virgin_bits[i]: virgin_bits[i] &= ~buffer[i] if virgin_bits[i] == 0xff: ret = 2 elif ret < 1: ret = 1 return ret def release(self): self.shm.remove()
class FrameBufferServer: def __init__(self, size: int): self.buffer = None self.key = None self.shared_memory = None self.size = size self.offset = 0 def __enter__(self): self.start() return self def __exit__(self, exc_type, exc_value, exc_traceback): self.stop() def start(self): """ Start shared memory server """ self.shared_memory = SharedMemory(key=None, flags=IPC_CREX, size=self.size) self.buffer = memoryview(self.shared_memory) self.key = self.shared_memory.key logger.info('Started FrameBufferServer - key=%s, size=%d', self.key, self.size) def stop(self): """ Stop shared memory server """ self.shared_memory.detach() self.shared_memory.remove() logger.info('Stopped FrameBufferServer - key=%s, size=%d', self.key, self.size) def get_key(self) -> str: """ Return client key for shared memory access """ return self.key def allocate(self, size: int) -> int: """ Allocates a chunk of data and returns offset """ offset = self.offset self.offset = (self.offset + size) % self.size return offset
def run(self): time.sleep(3) shared_mem = SharedMemory(6789, flags=sysv_ipc.IPC_CREAT, size=sysv_ipc.PAGE_SIZE, init_character='\0') try: while self.work: for i in range( len(stations) ): # convert position to integer then string and fill with 4 zero to align values shared_mem.write( ' '.join( str(pos).split(".")[0].zfill(4) for pos in stations[i].params["position"]) + ' ', i * 15) else: print(shared_mem.read(45)) shared_mem.remove() except: shared_mem.remove()
#!usr/bin/env python # encoding: utf-8 # Archivo de lectura de la memoria compartida. from sysv_ipc import SharedMemory from numpy import frombuffer, float32 from subprocess import call shm_key = 0x1234; # Vinvculamos la memoria compartida a una instancia. mem = SharedMemory(shm_key) # Leemos la memoria compartida. memory_value = mem.read() # Convertimos la lectura de bytes a numpy array. y = frombuffer(memory_value, dtype=float32) print(y) # Desvinculamos la instancia. mem.detach() # Removemos la memoria. mem.remove()