Find the answer to your Linux question:
Results 1 to 4 of 4
Hi ! I want to simulate a serial protocol in user space program to test reception algorithm. The protocol is similar to UART protocol but frame format is different. The ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2010
    Posts
    7

    [C] threads and timers is this possible ?


    Hi !

    I want to simulate a serial protocol in user space program to test reception algorithm. The protocol is similar to UART protocol but frame format is different. The duration of 1 bit is 114,46 us.

    In my main function I'm creating two threads. Tx for simulating transmitting data and Rx for reception data. For measure time I use create_timer() function and signals. Each thread has its own timer and signal handler. Tx timer uses SIGUSR1 and Rx timer uses SIGUSR2. Tx thread sets the global variable and Rx reads it.

    The timers are set to sample with 114,46 us period. When they reach this value signal handler should set the flag. Flag is check in thread and some operation are made.
    When I set the timers period for longer time (100 ms) everything works fine but when I change it to default value (114,46 us) the Rx thread doesn't receive the proper data.
    I am wondering if the 114,46 timer period is possible to simulate in user space? Whether the time will give me the proper resolution ? Theoretically it should provide 1 ns resolution.

    What do you think about this ??

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,572
    There are a couple of problems doing it this way to get tight timing on Linux. First, you didn't say if you are using the realtime scheduler. Second, using interrupts/signals triggered by system timers is not going to provide much accuracy. Myself, I would use a timing loop in each thread, and check during the loop for the desired elapsed time, then set whatever flags or do whatever is appropriate. Running two tight system timers (100us or so) in one process (even in different threads), is problematic and not recommended.

    BTW, I assume you mean timer_create() and not create_timer()? FWIW, Linux timers do not support such short periods, even though the sturctures allow for them. The system does not. How short the period may be in reality depends upon the kernel and other implementation details.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Jun 2010
    Posts
    7
    Thanks for the reply !

    Of course I mean timer_create(). Sorry for the mistake.

    I've changed the timers to nanosleep() function. The program is now more precise. I can now change the period for smaller values. I've also changed the scheduling to round robin. But still I can't get desired accuracy.

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,572
    Some options. Try one or more of them.

    1. Decrease niceness of the process (gives it higher priority.
    2. Use the real-time scheduler for your process.
    3. Get CPU with higher clock speed and faster memory i/o bus. BTW, what is your CPU family and speed?
    4. Make sure you have enough ram to avoid swapping.
    5. Related to #4, make sure no other processes are writing to disc.
    6. Shut down all unneeded services and processes.
    7. Run without GUI (console only).

    Ones you should do first off are #s 1, 4, 5, 6, and 7. X-windows GUI's use up a LOT of CPU and memory.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •