You can fake it by checking the stdin for non-blocking read. Something like this:
Code:
itsme@itsme:~/C$ cat select.c
#include <stdio.h>
#include <sys/select.h>
#include <time.h>
void do_keyboard(void)
{
char buf[128];
fgets(buf, sizeof(buf), stdin);
printf("You typed: %s", buf);
}
void do_time(void)
{
time_t now = time(NULL);
printf("Current time is: %s", ctime(&now));
}
int main(void)
{
struct timeval tv;
fd_set inpset;
tv.tv_sec = 5;
tv.tv_usec = 0;
while(1)
{
FD_ZERO(&inpset);
FD_SET(fileno(stdin), &inpset);
select(fileno(stdin)+1, &inpset, NULL, NULL, &tv);
if(FD_ISSET(fileno(stdin), &inpset))
do_keyboard();
else
{
do_time();
tv.tv_sec = 5;
tv.tv_usec = 0;
}
}
return 0;
}
itsme@itsme:~/C$ gcc -Wall select.c -o select
itsme@itsme:~/C$ ./select
Current time is: Mon Oct 11 13:20:17 2004
Current time is: Mon Oct 11 13:20:22 2004
test
You typed: test
it's running
You typed: it's running
Current time is: Mon Oct 11 13:20:27 2004
as you can see, the Current time is: Mon Oct 11 13:20:32 2004
background function runs uninterrupteCurrent time is: Mon Oct 11 13:20:37 2004
d
You typed: as you can see, the background function runs uninterrupted
Current time is: Mon Oct 11 13:20:42 2004
itsme@itsme:~/C$ ./select
test
You typed: test
Current time is: Mon Oct 11 13:20:49 2004
Current time is: Mon Oct 11 13:20:54 2004
it's kiCurrent time is: Mon Oct 11 13:20:59 2004
nd of working!
You typed: it's kind of working!
Current time is: Mon Oct 11 13:21:04 2004
Current time is: Mon Oct 11 13:21:09 2004