IPC: Difference between revisions

From Control Systems Technology Group
Jump to navigation Jump to search
(Created page with '== Inter Process Communication == For our project, the [http://cstwiki.wtb.tue.nl/index.php?title=Football_Table Football Table] a synchronized high speed inter process communic…')
 
Line 1: Line 1:
== Inter Process Communication ==
= Inter Process Communication Using Shared Memory =


For our project, the [http://cstwiki.wtb.tue.nl/index.php?title=Football_Table Football Table] a synchronized high speed inter process communication(IPC) was necessary. For us to allow communication between our [http://cstwiki.wtb.tue.nl/index.php?title=Football_Table_Motion Motion Loop] (in Simulink) and [http://gazebosim.org/ Gazebo]. For simulation purposes we ideally want to run as fast possible, whilst retaining a causal link.
For our project, the [http://cstwiki.wtb.tue.nl/index.php?title=Football_Table Football Table] a synchronized high speed inter process communication(IPC) was necessary. For us to allow communication between our [http://cstwiki.wtb.tue.nl/index.php?title=Football_Table_Motion Motion Loop] (in Simulink) and [http://gazebosim.org/ Gazebo]. For simulation purposes we ideally want to run as fast possible, whilst retaining a causal link.
 
The library/communication makes use of the POSIX pthread library. Pthread was chosen for its compatibility with Matlab. For this work a ''very'' basic wrapper library was made for this project to deal clean up recurring parts of code. This library basically dumbs pthreads down a bit, making it more accesible. For advanced use i do not recommend using this. In our 'library' we used shared memory in combination with a mutex and a condition. For the sake of usability/adaptability this article will very shortly first discuss the basic principles used.
 
== Shared Memory, Mutexes and Conditions ==
 
=== Shared Memory ===
 
Shared memory is one of the fastest methods available for inter process communication. Basically a segment of memory gets allocated to which multiple threads/programs have access. This way the processes can exchange data and synchronize with each other. Using shared memory however, is slightly tricky because a simultaneous read/write will cause undefined behavior (most likely a crash). To manage this, mutexes are used.
 
=== Mutexes ===
 
A mutex, or ''mutual exclusion'', does exactly what its name describes. It provides a thread/program with exclusive access to a shared resource. It attains this exclusive access by 'locking' the mutex. In our case, we use it to control access to shared memory. As long as one process has a lock, no other process can access the data (using a basic mutex), hence ensuring [http://en.wikipedia.org/wiki/Thread_safety thread-save] behavior. There are other types of mutexes which operate slightly different (such as a [http://en.wikipedia.org/wiki/Readers-writer_lock readers-writers lock]). By default if a process cannot get a lock, it will wait until it does. If you do not carefully programs you can easily get deadlocks.
 
=== Conditions ===
 
Synchronizing programs/threads one program might not be able to continue without data provided by the other. Conditions are a wonderful tool to perform this type of synchronization. They are always used in combination with a mutex and some condition variable (which can be anything). It's use is best described with an short example:
 
Process 1 will lock the shared memory, check if a condition is met, find out is not (process 2 has not updated data yet). It will wait (on a condition_signal) and automatically unlock the mutex. Once process 2 is ready to update it: locks the mutex, updates the data, signals process 1 (waking it) and unlocks the mutex.
 
== Usage in a feedback loop ==
 
Now, how do we use all this? Our goal is to place gazebo as the plant in our feedback loop in simulink and run as fast as possible.

Revision as of 16:22, 6 August 2012

Inter Process Communication Using Shared Memory

For our project, the Football Table a synchronized high speed inter process communication(IPC) was necessary. For us to allow communication between our Motion Loop (in Simulink) and Gazebo. For simulation purposes we ideally want to run as fast possible, whilst retaining a causal link.

The library/communication makes use of the POSIX pthread library. Pthread was chosen for its compatibility with Matlab. For this work a very basic wrapper library was made for this project to deal clean up recurring parts of code. This library basically dumbs pthreads down a bit, making it more accesible. For advanced use i do not recommend using this. In our 'library' we used shared memory in combination with a mutex and a condition. For the sake of usability/adaptability this article will very shortly first discuss the basic principles used.

Shared Memory, Mutexes and Conditions

Shared Memory

Shared memory is one of the fastest methods available for inter process communication. Basically a segment of memory gets allocated to which multiple threads/programs have access. This way the processes can exchange data and synchronize with each other. Using shared memory however, is slightly tricky because a simultaneous read/write will cause undefined behavior (most likely a crash). To manage this, mutexes are used.

Mutexes

A mutex, or mutual exclusion, does exactly what its name describes. It provides a thread/program with exclusive access to a shared resource. It attains this exclusive access by 'locking' the mutex. In our case, we use it to control access to shared memory. As long as one process has a lock, no other process can access the data (using a basic mutex), hence ensuring thread-save behavior. There are other types of mutexes which operate slightly different (such as a readers-writers lock). By default if a process cannot get a lock, it will wait until it does. If you do not carefully programs you can easily get deadlocks.

Conditions

Synchronizing programs/threads one program might not be able to continue without data provided by the other. Conditions are a wonderful tool to perform this type of synchronization. They are always used in combination with a mutex and some condition variable (which can be anything). It's use is best described with an short example:

Process 1 will lock the shared memory, check if a condition is met, find out is not (process 2 has not updated data yet). It will wait (on a condition_signal) and automatically unlock the mutex. Once process 2 is ready to update it: locks the mutex, updates the data, signals process 1 (waking it) and unlocks the mutex.

Usage in a feedback loop

Now, how do we use all this? Our goal is to place gazebo as the plant in our feedback loop in simulink and run as fast as possible.