c - What is the purpose of pthread and the select () function? -
our professor gave code in class:
st = select(max+1, &rs, null, null, &timeinterval); if(st){ for(int i=0; i<workers; i++) { if(fd_isset(channels[i]->read_fd(), &rs))
and have hard time trying understand it's doing. i've tried research more pthreads nothing seems show explain doing. says has file descriptors don't how being in code.
the purpose of select
wait on multiple file descriptors, possibly time out, when returns positive number, means @ least 1 fd in rs
set becomes ready reading, in loop, check fd it, , performs read on it.
note, should check bigger 0 instead, because -1 returned in case of error, should not check fd_set handle error:
if(st > 0) { for(int i=0; i<workers; i++) { if(fd_isset(channels[i]->read_fd(), &rs)) { // perform read on channels[i]->read_fd } } } else if (st == 0) { // handle time out } else { // handle error }
Comments
Post a Comment