while studying for my finals I found a very interesting questing. This is what I desire to code.
Program read stdin into buffer (of fixed size). When buffer is full, program prints it to file. But if buffer isn't filled in fixed time (timeout), program prints to file [TIMEOUT]
and the rest of buffer (currently read)
First example:
buffer_size = 5; timeout = 4;
$ while : ; do printf 1; sleep 1; done | ./a.out
should write [TIMEOUT]1111[TIMEOUT]1111[TIMEOUT]1111
etc. because while-loop writes only 4 chars (within 4 second limit).
Second example
buffer_size = 3; timeout = 5;
$ while : ; do printf 1; sleep 1; done | ./a.out
should write 111 111 111
etc. because while-loop writes 3 chars (within 3 seconds < 5 sec limit) so timeout never happens.
I'm trying to code it using poll
but I don't know how to find out, whether all chars have been written or only one. I can't also get stuck on read(0, buffer, buffer_size)
as I would miss timeout. Is it even possible? I guess it is as our teacher pointed it out as a good excersice.
Of course, busy wait is unacceptable, only classic POSIX syscalls allowed (poll, select, read, write, open...).
Could anybody hint me, please? I have no clue how to manage this behaviour and neighter stackoverflow nor google gave me answer (or maybe I just don't know what to search for)
Thanks in advance