On Thu, 30 Oct 2003, Ami Chayun wrote: > Hi, > I have a question regarding short sleeps (under 10 millisec). > I require to implement sleep with about 1-10 microsec accuracy (that's no > problem), but I require to sleep for times ranging between 1 microsec to 1 > millisec. > From my understanding I have two options: > 1) CPU tight loops, and count CPU cycles. > 2) Use some sort of realtime clock. > > (Since my code uses a lot of CPU already, I would really like to avoid method > 1.) > Can anyone point me to some examples on any (or both...) of the methods, pros > and cons of each will be most welcome :-) > > Thanks, > Ami >
I once got the following answer (as to sleep for less than 30 n) from Iftach Hyams, who deals with RT systems (he also gave a lecture about it in Haifux: http://www.haifux.org/lectures/78/ I admit I do not fully understand the answer, but maybe it will help you: <ANSWER BY IFTACH> The solution is as follow (I implemented it in VxWorks 5.4 but the idea is, if you just implement pipes correctly and they support select) : (It is a part of code that waits for an acknowledge on in a socket, but you can only "delay" a short time). My function: ... fd_set rdfs; int myPipe; /* Linux need an array - man pipe */ createPipe (myPipe); FD_ZERO (&rdfs); FD_SET (acknowledgeSocket, &rdfs); /* Register socket */ FD_SET (myPipe, &rdfs); /* Register pipe */ ioctl (pipeFile, FIOFLUSH, 0); /* Discard previous data */ send (...) /* Send something which cause an acknowledge */ armTimer (time ....); socketStatus = select (FD_SETSIZE,&rdfs,NULL,NULL,NULL); if (FD_ISSET (acknowledgeSocket,&rdfs)) ... ISR service : void timerExpired (int pipeFile) { /* Write anything to the pipe so the select will be unlocked */ write (pipeFile,"Something",1); } Register an ISR: isrRegister ((void*)timerExpired,*pipeFile); Emphasizes : - Make sure the pipes have the select feature. - Timers are not portable. Each system has its own implementation. - The context switch & delay has its own latency. You must measure it if the delays are very small (milliseconds are not small). - Do everything before the "send" or after the "select" so the waiting time will be consistent and minimal (beyond the timers delay). </ANSWER BY IFTACH> ================================================================= To unsubscribe, send mail to [EMAIL PROTECTED] with the word "unsubscribe" in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED]
