PatientRht 2013-11-14
代码如下:
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
代码如下:
struct timeval{ long tv_sec;/*secons* long tv_usec;/*microseconds*/ }
代码如下:
void seconds_sleep(unsigned seconds){ struct timeval tv; tv.tv_sec=seconds; tv.tv_usec=0; int err; do{ err=select(0,NULL,NULL,NULL,&tv); }while(err<0 && errno==EINTR); }
代码如下:
void milliseconds_sleep(unsigned long mSec){ struct timeval tv; tv.tv_sec=mSec/1000; tv.tv_usec=(mSec%1000)*1000; int err; do{ err=select(0,NULL,NULL,NULL,&tv); }while(err<0 && errno==EINTR); }
代码如下:
void microseconds_sleep(unsigned long uSec){ struct timeval tv; tv.tv_sec=uSec/1000000; tv.tv_usec=uSec%1000000; int err; do{ err=select(0,NULL,NULL,NULL,&tv); }while(err<0 && errno==EINTR); }
代码如下:
#include <stdio.h> #include <sys/time.h> #include <errno.h> int main() { int i; for(i=0;i<5;++i){ printf("%d\n",i); //seconds_sleep(1); //milliseconds_sleep(1500); microseconds_sleep(1900000); } }