I think you are going about this entirely the wrong way.
There are two propper ways, that I can see:
- Rewrite your program to work as an event loop:
call select($rbits,$wbits,$ebits,0) to find FDs
ready for I/O, and process them
- Write two threads: One does select($rbits,$wbits,$ebits,00.1) </code>(Note the non-zero timeout), which only makes note of which filehandles are ready for I/O, and leaves the information somewhere for the main process to find.
If the "process I/O buffer" is an operation with a guaranteed upper time limit, the first option is probably preferable. | [reply] [d/l] |
Event loop is of course good thing... when it possible. But it isn't always possible: sometimes I need to do very long calculations inside event loop and this will result in FDs timeout and error.
Also event loop isn't good idea if "process IO buffer" isn't main task of this program.
Threads... hmm... not in perl. At least - not current iThreads. They are too slow and use too much memory.
So I wonna to make safe ASYNC IO without threads using SIGNALS as was described before.
| [reply] |
As far as I know none of perl's functions are reentrant for signal handlers, because pretty much any perl function can allocate memory, and perl's malloc is not reentrant.
If you have a system malloc that is reentrant, and you've built perl to use it (configure option -Uusemymalloc if it isn't the default for your system), you may have better luck. But I'm not aware that anyone's done the analysis of what within perl is or isn't safe in that context.
I would normally look to do async i/o using nonblocking reads and a select() loop, as suggested by matija in his response.
Hugo
| [reply] |
IIRC, read(2) isn't safe to be called in a signal handler, but I'd have to look at one of Richard Stevens' books to be sure.
Can you just block signals or set a flag while you're doing the sysread? Or could you implement this as two processes communicating through shared memory?
| [reply] [d/l] [select] |
read(2) is safe to call in a signal handler, I've checked W. Richard Stevens' Advanced Programming in the Unix Environment for confirmation. Of course, you would need the buffer to be allocated beforehand to use read in a perl signal handler to avoid a malloc.
| [reply] |
Agreed. I just verified that in my copy of APUE.
| [reply] |