Skip to content

Instantly share code, notes, and snippets.

@heiner
Created November 26, 2019 00:58
Show Gist options
  • Select an option

  • Save heiner/f63d63714250f81f42e5fdd4d62aedd7 to your computer and use it in GitHub Desktop.

Select an option

Save heiner/f63d63714250f81f42e5fdd4d62aedd7 to your computer and use it in GitHub Desktop.
import array
import os
import socket
import mmap
import time
def send_fds(sock, msg, fds): # not yet used
return sock.sendmsg(
[msg], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", fds))]
)
def main():
size = 1024
path = "/tmp/fd-test"
fd = os.open(path, flags=os.O_EXCL | os.O_RDWR | os.O_CREAT, mode=0o600)
os.ftruncate(fd, size)
os.unlink(path)
child = os.fork()
if child == 0:
time.sleep(2)
mm = mmap.mmap(fd, 0)
print("child read", mm.readline())
return
mm = mmap.mmap(fd, 0, prot=mmap.PROT_READ | mmap.PROT_WRITE, flags=mmap.MAP_SHARED)
mm.write(b"oh yes")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment