I/O multiplexing on pipes
Posted: Sat May 24, 2014 3:09 pm
Had to perform some I/O multiplexing on pipes (read multiple pipes in parallel
until they're closed), and realized that newlisp does not provide a function like
"select" in C to monitor file descriptors.
I've looked in the documentation but didn't find any solution. I've ended doing a C
implementation to get the desired result, something like that (select_readable is a
wrapper calling my c implementation, taking a list of file descriptors and returning
the first one that is ready to read):
If you have any 100% newlisp solution, I would be interested. The peek function
might have been a candidate, but is not enough, as I need to know in a non
blocking way when a pipe has been closed.
PS: as a side note, while coding my C extension, I realized I wasn't able to get a signed
integer values as a return value, even if I mention "int" as the str-return-type during import:
Any idea of what I'm doing wrong? Thanks in advance.
until they're closed), and realized that newlisp does not provide a function like
"select" in C to monitor file descriptors.
I've looked in the documentation but didn't find any solution. I've ended doing a C
implementation to get the desired result, something like that (select_readable is a
wrapper calling my c implementation, taking a list of file descriptors and returning
the first one that is ready to read):
Code: Select all
(import "./mylib.so" "c_select_readable")
...
; fd1 and fd2, two filedescriptors I'd like to read
(setq fds (list fd1 fd2))
(while (and fds (setq fd (select_readable fds)))
(if (nil? (read-line fd))
(replace fd fds)
(println current-line)))
might have been a candidate, but is not enough, as I need to know in a non
blocking way when a pipe has been closed.
PS: as a side note, while coding my C extension, I realized I wasn't able to get a signed
integer values as a return value, even if I mention "int" as the str-return-type during import:
Code: Select all
$ cat returnneg.c
int returnneg()
{
return -1;
}
$ gcc -Wall -fPIC returnneg.c -shared -o returnneg.so
> (import "./returnneg.so" "returnneg" "int")
returnneg@7F1E4B1D2645
> (returnneg)
4294967295