D0430@PUCC.BITNET (Paul Lansky) (11/12/86)
Can someone tell me why the following short program keeps such lousy time? (metronome emulator) I tried it under Ultrix 1.1 and 4.2. I would expect some granularity but not to the extent I saw. With an argument of 1, for example, there are sometimes errors of as much as 50% /* beats time according to command line argument */ #include <sys/time.h> #include <sys/signal.h> #include <stdio.h> float tick,tval=0; struct itimerval *value; main(argc,argv) char *argv[]; { int beatit(); char *malloc(); double atof(); tick = atof(*++argv); value = (struct itimerval *)malloc(sizeof(value)); value->it_value.tv_sec = value->it_interval.tv_sec = (int)tick; value->it_value.tv_usec = value->it_interval.tv_usec = (int)( 1000000. * (tick - (float)value->it_value.tv_sec)); (void) signal(SIGALRM,beatit); setitimer(ITIMER_REAL,value,value); while(1); } beatit() { getitimer(ITIMER_REAL,value); tval += value->it_value.tv_sec + (float)value->it_value.tv_usec/1000000; printf("%f"\n",tval); } thanks Paul Lansky bitnet == d0430@pucc Music Department uucp == princeton!winnie!paul Princeton University
greg@xios.UUCP (Greg Franks) (11/18/86)
In article <1346@PUCC.BITNET> D0430@PUCC.BITNET writes: >Can someone tell me why the following short program keeps such lousy >time? (metronome emulator) I tried it under Ultrix 1.1 and 4.2. >I would expect some granularity but not to the extent I saw. With an >argument of 1, for example, there are sometimes errors of as much as 50% I am no Berserkeley expert, but I have written some real time operating system kernels, so I can tell you that multiuser computer systems make lousy interval timers. You are the victim of scheduling delay. The signal goes off - your process gets scheduled. Now you wait for every one else in front of your process in the run queue to execute. Once they are finished you get to run. You also have to wait for all other interrupt processing to complete. So if the computer is being beaten to death by TTY's, disks and what have you, you wait. The more users, the worse it gets. The infinite loop at the bottom of the program will probably cause the scheduler to lower your process's priority probably to the point where you run dead last (just before the machine goes to IDLE). I must admit that I don't know what effects signals have on priority though. For accurate interval timing, use a real-time operating system designed for the task (gasp - VAX/VMS might be useful :-), or use real hardware. The latter is probably the best (and cheapest) bet! Happy hacking! Greg Franks