nanosleep() -system call does some confusing things

This is a discussion on nanosleep() -system call does some confusing things within the Linux Programming forums, part of the Platform Specific Boards category; I made a program that sends pulse-per-second to serial port. I measured the frequency of the pulses and noticed some ...

nanosleep() -system call does some confusing things

I made a program that sends pulse-per-second to serial port. I measured the frequency of the pulses and noticed some surprising effects caused by involving a nanosleep() system call to my program. Here is my program:

In my code, the function of nanosleep() is to change the phase of pulses relative to system clock second ticks. So it should not affect to the frequency of the pulses. Nevertheless, the frequency of pulses without nanosleep() is ~1.000001 Hz, but with nanosleep the frequency is ~1.0001 Hz. I tried to change the amount of nanosleeping, but it doesnt make effect if it was 1 ns or 1 ms. So my conclusion from that is that involving a nanosleep to my loop raises the frequency of pulse-per-second. Does anyone know any explanations to this?

I don't think the problem is in usleep. My frequency meter detects the pulse from the rising edge, i.e. when I do outb(0xff, 0x378);. usleep is there just to keep the output level up for a while to create short output pulse. The problem is that when I insert nanosleep before the pulse, the pulse happens a bit earlier relative to previous pulse than without inserting nanosleep. Hence, the frequency of pulses is higher with nanosleep. I cannot see any sense in that.

This assumes that your freqeuncy analyzer is on Data0 of the parallell port (from memory, that's pin 2, but I could be completely wrong on that). Connect a scope to the Data1 (which would be pin 3 if above Pin 2 is correct) and see what it does and how it varies.

I would also look at the variation on data0, as I suspect you'll find that _WITH_ the nanosleep, it will be more variation than without - because it calls schedule, which means that your current process gives up the rest of the timeslice, and only wakes up whenever the scheduler decides it has to run...