Thread: nanosleep() -system call does some confusing things

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    Finland
    Posts
    8

    Question 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:
    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?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, if you put a nanosleep in front of your out/usleep/out sequence, you see that the time delayed in usleep is slightly longer, yes?

    I don't know for sure the usleep implementation, but it may wait for a number of clock-ticks, so if you're unlucky, your nanosleep will shift the time another clock-tick.

    Have you looked at the variance(jitter) in your timing?

    --
    Mats

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    Finland
    Posts
    8
    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.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    Finland
    Posts
    8
    It does not change anything if I replace usleep with nanosleep.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    Finland
    Posts
    8
    For clarification, it seems like inserting nanosleep there inflicts on the system clock to make it run faster. So the computer thinks that the second is shorter..

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'd do two things:
    1. Use an oscilloscope/logic analyzer to view the waveform.
    2. Use single bits to indicate different timing parameters, e.g.
    Code:
                    // Wait a bit more. This should not make effect, but makes.
                    outb(0x02, 0x378);
                    nanosleep(&ts, NULL);
                    outb(0x00, 0x378);
    
                    outb(0x01, 0x378);
                    usleep(1000);
                    outb(0x00, 0x378);
    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...

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minix system call pls help for project
    By porvas in forum Linux Programming
    Replies: 2
    Last Post: 06-14-2009, 02:40 AM
  2. System Call Table
    By NuNn in forum C Programming
    Replies: 3
    Last Post: 03-26-2009, 01:56 PM
  3. Multiple types in lists, vectors or arrays.
    By megatron09 in forum C++ Programming
    Replies: 20
    Last Post: 08-31-2006, 01:54 PM
  4. system call with user input
    By enlinux in forum C Programming
    Replies: 7
    Last Post: 03-02-2003, 10:23 PM
  5. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 06:57 AM