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:
Code:
ts.tv_nsec = 6e3

        while(1)
        {

                // Wait for system clock second change
                while(second1 == second2)
                {
                        time(&aclock);
                        test_time = localtime(&aclock);
                        second2 = test_time->tm_sec;
                }

                time(&aclock);
                test_time = localtime(&aclock);
                second1 = test_time->tm_sec;
                second2 = test_time->tm_sec;

                // Wait a bit more. This should not make effect, but makes.
                nanosleep(&ts, NULL);

                outb(0xff, 0x378);
                usleep(1000);
                outb(0x00, 0x378);
	}
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?