Welcome to the CHICKEN Scheme pasting service

pipes-socketpair.scm added by gahr on Wed May 29 09:24:02 2024

(import (scheme) (chicken foreign) (chicken base) (chicken file posix) (chicken process))

(foreign-declare "#include <sys/socket.h>")
(define socketpair 
  (foreign-primitive ()
    "
    int ends[2] = { -1, -1 };
    socketpair(PF_LOCAL, SOCK_STREAM, 0, ends);
    C_word av[4] = { C_SCHEME_UNDEFINED, C_k, C_fix(ends[0]), C_fix(ends[1]) };
    C_values(4, av);
    "))

(let-values (((p c) (socketpair)))
  (define (child)
    (let ((out (open-output-file* c))
          (in  (open-input-file* c)))
      (let ((r (read in)))
        (print "child: got " r)
        (write "pong" out)
        (flush-output out)  )))

  (let ((pid (process-fork child #t)))
    (if (eq? pid 0)
      (exit)
      (begin
        (let ((out (open-output-file* p))
              (in  (open-input-file* p)))
          (write "ping" out)
          (flush-output out)
          (let ((r (read in)))
            (print "parent: got " r)))))))

Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
Which module provides `foreign-declare'?
Visually impaired? Let me spell it for you (wav file) download WAV