Hi,
Is it possible to access same peripheral from multiple containers?
Say using can0 I want to transmit data from one container.
I want to receive data in another container.
Hi,
Is it possible to access same peripheral from multiple containers?
Say using can0 I want to transmit data from one container.
I want to receive data in another container.
Hi, I’m the Toradex AI Assistant.
I might make mistakes. Our support team will step in if needed. If I answered your question, please tick the “Solution” checkbox. For follow-up questions to me, please use @ToradexAI in your post.
Hello!
Yes, it is possible to access the same peripheral, such as can0, from multiple containers. To enable this, you need to:
Here is a basic example for your Docker Compose file:
services:
can-tx:
image: your-tx-image
devices:
- /dev/can0
privileged: true
# Add other needed configurations
can-rx:
image: your-rx-image
devices:
- /dev/can0
privileged: true
# Add other needed configurations
Ensure both containers are granted access to /dev/can0 and use the privileged flag if your application requires it. You may need to manage device access control through udev rules or the device_cgroup_rules property, depending on your security requirements.
For more details, see:
Issue with multiple USB mount points on Toradex iMX7D
Let me know if you need a more detailed example or further clarification.
Best regards,
Toradex AI Assistant.
Hi,
It is showing resource busy.
It does not fix the problem.
Greetings @khaled.hassan,
Please reference the following article when it comes to using the CAN bus in a container: How to Use CAN on Torizon OS | Toradex Developer Center
I was able to start two containers and see the can0
interface in both of them.
Best Regards,
Jeremias
Yes. I can also see can0 from two containers. Question is, is it safe and will be there any race condition.
I can transmit and receive messages in two different containers.
I am using socket can. I can’t set bitrate programmatically now.
Question is, is it safe and will be there any race condition.
I don’t believe there is a definitive answer. It would largely depend on your exact use-case and how you orchestrate the two containers I would think. If both containers try to access the bus at the same time, for a transmit for example. I imagine there could be possible issues.
You would need a way to make sure only one process works on the bus at a time if you fear this possibility.
Best Regards,
Jeremias
@jeremias.tx I can ensure only on container will transmit and other container will receive. Both containers will not do the same task simultaneously.
I can ensure only on container will transmit and other container will receive. Both containers will not do the same task simultaneously.
That seems like it should be okay, at least from an initial impression. Though of course you should test your use-case just to be sure.
Best Regards,
Jeremias
Adding to the reply: this is not a container specific problem, it’s an user-space synchronization issue, simply two processes accessing the same file descriptor. Mutual exclusivity is not enforced by the kernel, I think in the can example you can even have unordered interleaved messages being written to the fd.
Depending on your use-case you can either use file status flags with things like fcntl(2) - Linux manual page or use an IPC-mechanism between the two processes to coordinate access, the latter is specially good if you already need IPC between the two processes in the first place.