1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

Synchronizing user space threads with kernel space in linux

Discussion in 'Embedded' started by Mad@Spammers, Apr 29, 2004.

  1. Mad@Spammers

    Mad@Spammers Guest

    I want to synchronize a user space thread to an external event that
    generates an interrupt. I thought of using the following approach: the
    ISR that treats the interrupt does a quick processing (such as data
    capture and buffering) and then sends data to the user space thread
    through a message queue or signals to the thread waiting on a
    semaphore and it gets the buffered data from shared memory or some
    similar mechanism. So far I haven't succeded in finding a way to do
    it. I've tryed to write a module which uses semaphores (<linux/sem.h>)
    but aparently there is no way to use SysV IPC primitives in kernel
    modules.

    I would appreciate having hints on how to do it or pointers to
    documentation and example code if available.

    Thank you very much in advance for your help.

    Elder.
     
    Mad@Spammers, Apr 29, 2004
    #1
    1. Advertising

  2. Mad@Spammers wrote:
    > I want to synchronize a user space thread to an external event that
    > generates an interrupt. I thought of using the following approach: the
    > ISR that treats the interrupt does a quick processing (such as data
    > capture and buffering) and then sends data to the user space thread
    > through a message queue or signals to the thread waiting on a
    > semaphore and it gets the buffered data from shared memory or some
    > similar mechanism. So far I haven't succeded in finding a way to do
    > it. I've tryed to write a module which uses semaphores (<linux/sem.h>)
    > but aparently there is no way to use SysV IPC primitives in kernel
    > modules.
    >
    > I would appreciate having hints on how to do it or pointers to
    > documentation and example code if available.
    >
    > Thank you very much in advance for your help.
    >
    > Elder.


    A user-space signal is the equivalent of an interrupt.
    When user-space drivers are involved, I like to use
    "send_sig_info()" to send a signal to the user-space
    task to indicate changes in the state of the driver.

    I like to model the user/kernel space interface to
    look like a hardware device in terms of interrupt/signal
    handling, with ioctl's to acknowledge/enable/disable
    signals/interrupts from the driver. However, I'm an
    embedded type that is used to that kind of thing ;-)

    Getting the data to/from kernel space is another
    issue, typically done by the read/write driver
    interface, or by using mmap().

    --
    Michael N. Moran (h) 770 516 7918
    5009 Old Field Ct. (c) 678 521 5460
    Kennesaw, GA, USA 30144 http://mnmoran.org

    "... abstractions save us time working, but they don't
    save us time learning."
    Joel Spolsky, The Law of Leaky Abstractions

    The Beatles were wrong: 1 & 1 & 1 is 1
     
    Michael N. Moran, Apr 29, 2004
    #2
    1. Advertising

  3. On 29 Apr 2004, Mad@Spammers wrote:

    > I want to synchronize a user space thread to an external event that
    > generates an interrupt.


    Maybe the ongoing work about D-Bus can give you inspiration:
    <http://freedesktop.org/Software/dbus>
    One of its motivations was/is exactly to be able to do more driver stuff in
    user space, and I got the impression that that was what you are looking
    for...

    Herman
    --
    K.U.Leuven, Mechanical Eng., Mechatronics & Robotics Research Group
    <http://people.mech.kuleuven.ac.be/~bruyninc> Tel: +32 16 322480
     
    Herman Bruyninckx, Apr 29, 2004
    #3
  4. Mad@Spammers

    Dan Kegel Guest

    Michael N. Moran wrote:
    >> I want to synchronize a user space thread to an external event that
    >> generates an interrupt. I thought of using the following approach: the
    >> ISR that treats the interrupt does a quick processing (such as data
    >> capture and buffering) and then sends data to the user space thread
    >> through a message queue or signals to the thread waiting on a
    >> semaphore and it gets the buffered data from shared memory or some
    >> similar mechanism. So far I haven't succeded in finding a way to do
    >> it. I've tryed to write a module which uses semaphores (<linux/sem.h>)
    >> but aparently there is no way to use SysV IPC primitives in kernel
    >> modules.

    >
    > A user-space signal is the equivalent of an interrupt.
    > When user-space drivers are involved, I like to use
    > "send_sig_info()" to send a signal to the user-space
    > task to indicate changes in the state of the driver. ...
    >
    > Getting the data to/from kernel space is another
    > issue, typically done by the read/write driver
    > interface, or by using mmap().


    What kind of time resolution do you need?

    One other option is to use netlink sockets. They're
    pretty easy. One drawback is netlink socket addresses
    are somewhat precious, a little like signal numbers.

    - Dan
     
    Dan Kegel, Apr 29, 2004
    #4
  5. Mad@Spammers

    Gary Kato Guest

    You could use wait queues. Have your user process sleep on a queue and have
    your ISR wake processes on that queue.
     
    Gary Kato, Apr 29, 2004
    #5
  6. Hello Elder,

    > I want to synchronize a user space thread to an external event that
    > generates an interrupt. I thought of using the following approach: the
    > ISR that treats the interrupt does a quick processing (such as data
    > capture and buffering) and then sends data to the user space thread


    - Let your driver register a character interface
    - from your user space thread, do a synchronous read.
    - in your interrupt routine, copy the data and wakeup the
    waiting thread.

    Very common situation. Works without any problem for me.

    best regards

    Wolfgang Mües
     
    Wolfgang =?ISO-8859-15?Q?M=FCes?=, Apr 29, 2004
    #6
  7. Wolfgang Mües wrote:

    > Hello Elder,
    >
    >> I want to synchronize a user space thread to an external event that
    >> generates an interrupt. I thought of using the following approach: the
    >> ISR that treats the interrupt does a quick processing (such as data
    >> capture and buffering) and then sends data to the user space thread

    >
    > - Let your driver register a character interface
    > - from your user space thread, do a synchronous read.
    > - in your interrupt routine, copy the data and wakeup the
    > waiting thread.
    >
    > Very common situation. Works without any problem for me.
    >
    > best regards
    >
    > Wolfgang Mües


    Look twice at this approach! This is the UNIX way - everything is a file.

    Read about the (now) old OSS audio drivers for block devices - same
    data size on every read or write.
    Or serial ports for character devices.

    /RogerL

    --
    Roger Larsson
    Skellefteå
    Sweden
     
    Roger Larsson, Apr 30, 2004
    #7
  8. Mad@Spammers

    Mad@Spammers Guest

    Roger Larsson <> wrote in message

    > Look twice at this approach! This is the UNIX way - everything is a file.


    Is it a good or a bad thing?

    Cheers.

    Elder.
     
    Mad@Spammers, Apr 30, 2004
    #8
  9. Mad@Spammers

    Mad@Spammers Guest

    Dan Kegel <> wrote in message

    > What kind of time resolution do you need?
    >

    I presume this question was asked to me.

    I am just evaluating how well would Linux (either 2.4 with preemption
    and low latency patches or 2.6 with preemption enabled) perform for my
    application. Worst case interrupt latency of 400us and thread
    activation latency of 4ms are more than acceptable in my application.

    Regards.

    Elder.
     
    Mad@Spammers, Apr 30, 2004
    #9
  10. Mad@Spammers

    Mad@Spammers Guest

    Herman Bruyninckx <> wrote in message news:<>...
    > On 29 Apr 2004, Mad@Spammers wrote:
    >
    > > I want to synchronize a user space thread to an external event that
    > > generates an interrupt.

    >
    > Maybe the ongoing work about D-Bus can give you inspiration:
    > <http://freedesktop.org/Software/dbus>
    > One of its motivations was/is exactly to be able to do more driver stuff in
    > user space, and I got the impression that that was what you are looking
    > for...
    >
    > Herman


    I had a quick look at it. My requirements are much more modest and I'd
    rather use native linux resources instead. Still I will look at its
    code for some tips.

    Elder.
     
    Mad@Spammers, Apr 30, 2004
    #10
  11. Wolfgang Mües wrote:

    > - Let your driver register a character interface
    > - from your user space thread, do a synchronous read.
    > - in your interrupt routine, copy the data and wakeup the
    > waiting thread.


    This method would get my vote. The "send a signal" approach is
    workable, but it can be difficult to get right. This is especially true
    if your application is already using a number of signals for other things.

    YMMV...

    John
    --
    John W. Linville
     
    John W. Linville, Apr 30, 2004
    #11
  12. X-No-Archive:yes

    A certain Mad@Spammers, of comp.arch.embedded "fame", writes :

    >I would appreciate having hints on how to do it or pointers to
    >documentation and example code if available.


    What you'll need to do is create a device driver with an entry in /dev,
    and then set up a poll()/select() call within it. These can be used to
    inform a user space application that some data has arrived to be
    processed.

    Your user space code can open the device and use the above functions
    which among other things provide the ability to block a thread until an
    interrupt is received, etc. Much more elegant than signal processing.

    The free book "Linux Device Drivers" covers this subject in detail.

    --

    "Jokes mentioning ducks were considered particularly funny." - cnn.com
     
    Chesney Christ, Apr 30, 2004
    #12
  13. X-No-Archive:yes

    A certain Mad@Spammers, of comp.arch.embedded "fame", writes :

    >> What kind of time resolution do you need?
    >>

    >I presume this question was asked to me.
    >
    >I am just evaluating how well would Linux (either 2.4 with preemption
    >and low latency patches or 2.6 with preemption enabled) perform for my
    >application. Worst case interrupt latency of 400us and thread
    >activation latency of 4ms are more than acceptable in my application.


    Supposedly the low-latency patch can give worst-case latency of around
    150us, but you'd need to check out how this fits on your architecture.

    --

    "Jokes mentioning ducks were considered particularly funny." - cnn.com
     
    Chesney Christ, Apr 30, 2004
    #13
  14. X-No-Archive:yes

    A certain Mad@Spammers, of comp.arch.embedded "fame", writes :
    >Roger Larsson <> wrote in message
    >
    >> Look twice at this approach! This is the UNIX way - everything is a file.

    >
    >Is it a good or a bad thing?


    Keeps everything nice and simple. There is a consistent way of dealing
    with hardware.

    --

    "Jokes mentioning ducks were considered particularly funny." - cnn.com
     
    Chesney Christ, Apr 30, 2004
    #14
  15. "Mad@Spammers" schrieb:
    >
    > I want to synchronize a user space thread to an external event that
    > generates an interrupt.


    Look at rtc.c in the Kernel source. They provide a blocking read to the
    application to have it wait for an event. Her some data is provided to
    the application, too.

    -Michael
     
    Michael Schnell, May 3, 2004
    #15
  16. > Supposedly the low-latency patch can give worst-case latency of around
    > 150us, but you'd need to check out how this fits on your architecture.
    >



    Nope. Linux can't guarantee any worst-case latency at all (I did find
    more than 100 msek with the low-latency patch in very rare instances).

    -Michael
     
    Michael Schnell, May 3, 2004
    #16
  17. X-No-Archive:yes

    A certain Michael Schnell, of comp.arch.embedded "fame", writes :
    >> Supposedly the low-latency patch can give worst-case latency of around
    >> 150us, but you'd need to check out how this fits on your architecture.

    >
    >Nope. Linux can't guarantee any worst-case latency at all (I did find
    >more than 100 msek with the low-latency patch in very rare instances).


    That's why I was quite careful not to use the word "guarantee".

    Clearly if you need hard real time performance, you need to look at
    another OS.

    --

    "Jokes mentioning ducks were considered particularly funny." - cnn.com
     
    Chesney Christ, May 3, 2004
    #17
  18. Mad@Spammers

    Guy Macon Guest

    Chesney Christ <> says...
    >
    >A certain Michael Schnell, of comp.arch.embedded "fame", writes :
    >
    >>> Supposedly the low-latency patch can give worst-case latency of around
    >>> 150us, but you'd need to check out how this fits on your architecture.

    >>
    >>Nope. Linux can't guarantee any worst-case latency at all (I did find
    >>more than 100 msek with the low-latency patch in very rare instances).

    >
    >That's why I was quite careful not to use the word "guarantee".
    >
    >Clearly if you need hard real time performance, you need to look at
    >another OS.


    "Hard real-time means no surprises or silent failures as system
    configuration changes or load increases. Deadlines will still
    be met. For example, the worst case delay on a 1 millisecond
    thread on the HP Pavilion Notebook (AMD K7) is 12 microseconds.
    -http://www.fsmlabs.com/products/rtlinuxpro/


    "RTAI's microkernel approach guarantees that the data-acquisition
    task will take place on schedule, even while the previously acquired
    and calculated result is written to disk."
    -http://www.elecdesign.com/Articles/Index.cfm?ArticleID=3816&pg=2


    "Lineo Solutions, Inc. (Lineo) demonstrated ... hard real-time
    support with Linux .... Deterministic time metrics of the systems
    demonstrated ... is as follows.

    Interrupt Response Time 5 microseconds

    Task Latency Time 4 microseconds

    Periodic Task Latency Time 20 microseconds

    Timer Clock Accuracy 1 microsecond, Jitter 1 microsecond)"

    - http://www.lineo.co.jp/eng/news-events/press-release/20040109.html


    "RTAI - the Realtime Linux Application Interface for Linux ... lets
    you write applications with strict timing constraints for your
    favourite operating system."
    -http://www.aero.polimi.it/~rtai/

    "KURT-Linux: Kansas University Real-Time Linux: Microsecond timing
    resolution and event-driven real-time scheduling..."
    -http://www.ittc.ukans.edu/kurt/documents-noframes.html



    --
    Guy Macon, Electronics Engineer & Project Manager for hire.
    Remember Doc Brown from the _Back to the Future_ movies? Do you
    have an "impossible" engineering project that only someone like
    Doc Brown can solve? My resume is at http://www.guymacon.com/
     
    Guy Macon, May 3, 2004
    #18
  19. X-No-Archive:yes

    A certain Guy Macon, of comp.arch.embedded "fame", writes :

    >"RTAI's microkernel approach guarantees that the data-acquisition
    >task will take place on schedule, even while the previously acquired
    > and calculated result is written to disk."
    > -http://www.elecdesign.com/Articles/Index.cfm?ArticleID=3816&pg=2


    http://www.aero.polimi.it/~rtai/documentation/articles/guide.html

    "What is RTAI?

    RTAI means Real Time Application Interface. Strictly speaking, it is not
    a real time operating system, such as VXworks or QNX. It is based on the
    Linux kernel, providing the ability to make it fully pre-emptable. "

    Your serve.

    [BTW, isn't RTAI a kernel - a microkernel - all by itself ? You could
    say that this isn't actually Linux. Linux runs essentially as a process
    above RTAI. This isn't necessary in an OS such as, for example,
    Greenhills Integrity, which provides guaranteed latency and other
    goodies.]

    --

    "Jokes mentioning ducks were considered particularly funny." - cnn.com
     
    Chesney Christ, May 3, 2004
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Damaeus
    Replies:
    3
    Views:
    400
    Damaeus
    Jul 25, 2003
  2. Martin
    Replies:
    1
    Views:
    309
    Juhan Leemet
    Jul 21, 2004
  3. Like2Learn
    Replies:
    6
    Views:
    814
    Like2Learn
    Dec 9, 2010
  4. Siri Cruz
    Replies:
    9
    Views:
    169
    James K. Lowden
    Nov 17, 2013
  5. t_palani
    Replies:
    2
    Views:
    193
Loading...

Share This Page