I'm writing a function right now, and I want to set an alarm to avoid a timeout, here's the general idea of my code:
int amt = -2;
alarm(10);
amt = read(fd, &t->buf[tailpos], TASKBUFSIZ - tailpos); //do a read
when the alarm goes off, i want to check the value of "amt"
basically, i want to check to see that the read call was completed in under 10 seconds, however in a signal handler, i dont think you can pass in the value of amt. so is my approach possible to implement? or would i need to a fork a process and do it that way? I'd prefer not to do that, I ran into some problems with that approach..
here's the entirety of the function as well:
taskbufresult_t read_to_taskbuf(int fd, task_t *t)
{
unsigned headpos = (t->head % TASKBUFSIZ);
unsigned tailpos = (t->tail % TASKBUFSIZ);
ssize_t amt;
if (t->head == t->tail || headpos < tailpos)
amt = read(fd, &t->buf[tailpos], TASKBUFSIZ - tailpos);
else
amt = read(fd, &t->buf[tailpos], headpos - tailpos);
if (amt == -1 && (errno == EINTR || errno == EAGAIN
|| errno == EWOULDBLOCK))
return TBUF_AGAIN;
else if (amt == -1)
return TBUF_ERROR;
else if (amt == 0)
return TBUF_END;
else {
t->tail += amt;
return TBUF_OK;
}
}
help would be much appreciated, my TAs suck...