Optimal sharing of a ring buffer between processes
Clash Royale CLAN TAG#URR8PPP
We're implementing an embedded Linux system and have to provide a live updating graph on a website that shows data coming from a background process in the system.
The question is how to optimally share data between:
The data generating process that regularly updates a ring buffer with new values, written in C.
The webserver CGI function that must fetch the latest contents of the buffer. We use Python for CGI's (nginx+wsgi+flask).
I currently lean towards making a unix socket solution for this, but I gather this would require multithreading in the C program to make sure the background process isn't disturbed.
I wonder if there isn't a simpler way. Could we map the buffer memory directly to be a virtual file? What about using a normal file on a RAM disk as the buffer and just seeking and writing to it?
linux python embedded c ipc
add a comment |
We're implementing an embedded Linux system and have to provide a live updating graph on a website that shows data coming from a background process in the system.
The question is how to optimally share data between:
The data generating process that regularly updates a ring buffer with new values, written in C.
The webserver CGI function that must fetch the latest contents of the buffer. We use Python for CGI's (nginx+wsgi+flask).
I currently lean towards making a unix socket solution for this, but I gather this would require multithreading in the C program to make sure the background process isn't disturbed.
I wonder if there isn't a simpler way. Could we map the buffer memory directly to be a virtual file? What about using a normal file on a RAM disk as the buffer and just seeking and writing to it?
linux python embedded c ipc
add a comment |
We're implementing an embedded Linux system and have to provide a live updating graph on a website that shows data coming from a background process in the system.
The question is how to optimally share data between:
The data generating process that regularly updates a ring buffer with new values, written in C.
The webserver CGI function that must fetch the latest contents of the buffer. We use Python for CGI's (nginx+wsgi+flask).
I currently lean towards making a unix socket solution for this, but I gather this would require multithreading in the C program to make sure the background process isn't disturbed.
I wonder if there isn't a simpler way. Could we map the buffer memory directly to be a virtual file? What about using a normal file on a RAM disk as the buffer and just seeking and writing to it?
linux python embedded c ipc
We're implementing an embedded Linux system and have to provide a live updating graph on a website that shows data coming from a background process in the system.
The question is how to optimally share data between:
The data generating process that regularly updates a ring buffer with new values, written in C.
The webserver CGI function that must fetch the latest contents of the buffer. We use Python for CGI's (nginx+wsgi+flask).
I currently lean towards making a unix socket solution for this, but I gather this would require multithreading in the C program to make sure the background process isn't disturbed.
I wonder if there isn't a simpler way. Could we map the buffer memory directly to be a virtual file? What about using a normal file on a RAM disk as the buffer and just seeking and writing to it?
linux python embedded c ipc
linux python embedded c ipc
asked Feb 22 at 13:59
LarspLarsp
61
61
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Simple solution using files and atomic rename which can be easily implemented using scripting languages.
Sender
- write data into file
A
- rename file
A
toB
The rename is atomic, and it can be done while the receiver is processing the old file.
The sender will not block if the receiver does not read the data.
Receiver
If it is no problem to process the same data twice:
- open file
B
- read data
- close file
This option also works when there is more than one receiver, e.g. multiple CGI processes of the web server.
- open file
If the receiver should not process the same data twice:
- rename file
B
toC
- if successful, read and process file
C
- optionally delete file
C
This option works with a single receiver only.
- rename file
In both cases old data will be lost if the sender writes data faster than the receiver can read it.
Of course there are other options.
One of the other options is POSIX IPC, which offers shared memory, message queues, and semaphores. Google is you friend.
– Johan Myréen
Feb 22 at 16:14
Check out zeromq ...
– Murray Jensen
Feb 23 at 4:26
Thanks for the answer, but since the data generating process will append the ring buffer with new values multiple times a second, it doesn't seem like a good fit. I'd prefer not to have to save the whole buffer in a new file on updates.
– Larsp
Feb 25 at 9:23
Thanks for the tip about zeromq, very interesting. I'll also give POSIX IPC a look.
– Larsp
Feb 25 at 9:23
@Larsp Of course you can choose a different solution. If you think this would be a performance problem I suggest to test and measure first. You should add your timing requirements to the question. How often does the sender update the data? How much data is one set? How often is the receiver expected to display the data?
– Bodo
Feb 25 at 9:27
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502309%2foptimal-sharing-of-a-ring-buffer-between-processes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Simple solution using files and atomic rename which can be easily implemented using scripting languages.
Sender
- write data into file
A
- rename file
A
toB
The rename is atomic, and it can be done while the receiver is processing the old file.
The sender will not block if the receiver does not read the data.
Receiver
If it is no problem to process the same data twice:
- open file
B
- read data
- close file
This option also works when there is more than one receiver, e.g. multiple CGI processes of the web server.
- open file
If the receiver should not process the same data twice:
- rename file
B
toC
- if successful, read and process file
C
- optionally delete file
C
This option works with a single receiver only.
- rename file
In both cases old data will be lost if the sender writes data faster than the receiver can read it.
Of course there are other options.
One of the other options is POSIX IPC, which offers shared memory, message queues, and semaphores. Google is you friend.
– Johan Myréen
Feb 22 at 16:14
Check out zeromq ...
– Murray Jensen
Feb 23 at 4:26
Thanks for the answer, but since the data generating process will append the ring buffer with new values multiple times a second, it doesn't seem like a good fit. I'd prefer not to have to save the whole buffer in a new file on updates.
– Larsp
Feb 25 at 9:23
Thanks for the tip about zeromq, very interesting. I'll also give POSIX IPC a look.
– Larsp
Feb 25 at 9:23
@Larsp Of course you can choose a different solution. If you think this would be a performance problem I suggest to test and measure first. You should add your timing requirements to the question. How often does the sender update the data? How much data is one set? How often is the receiver expected to display the data?
– Bodo
Feb 25 at 9:27
add a comment |
Simple solution using files and atomic rename which can be easily implemented using scripting languages.
Sender
- write data into file
A
- rename file
A
toB
The rename is atomic, and it can be done while the receiver is processing the old file.
The sender will not block if the receiver does not read the data.
Receiver
If it is no problem to process the same data twice:
- open file
B
- read data
- close file
This option also works when there is more than one receiver, e.g. multiple CGI processes of the web server.
- open file
If the receiver should not process the same data twice:
- rename file
B
toC
- if successful, read and process file
C
- optionally delete file
C
This option works with a single receiver only.
- rename file
In both cases old data will be lost if the sender writes data faster than the receiver can read it.
Of course there are other options.
One of the other options is POSIX IPC, which offers shared memory, message queues, and semaphores. Google is you friend.
– Johan Myréen
Feb 22 at 16:14
Check out zeromq ...
– Murray Jensen
Feb 23 at 4:26
Thanks for the answer, but since the data generating process will append the ring buffer with new values multiple times a second, it doesn't seem like a good fit. I'd prefer not to have to save the whole buffer in a new file on updates.
– Larsp
Feb 25 at 9:23
Thanks for the tip about zeromq, very interesting. I'll also give POSIX IPC a look.
– Larsp
Feb 25 at 9:23
@Larsp Of course you can choose a different solution. If you think this would be a performance problem I suggest to test and measure first. You should add your timing requirements to the question. How often does the sender update the data? How much data is one set? How often is the receiver expected to display the data?
– Bodo
Feb 25 at 9:27
add a comment |
Simple solution using files and atomic rename which can be easily implemented using scripting languages.
Sender
- write data into file
A
- rename file
A
toB
The rename is atomic, and it can be done while the receiver is processing the old file.
The sender will not block if the receiver does not read the data.
Receiver
If it is no problem to process the same data twice:
- open file
B
- read data
- close file
This option also works when there is more than one receiver, e.g. multiple CGI processes of the web server.
- open file
If the receiver should not process the same data twice:
- rename file
B
toC
- if successful, read and process file
C
- optionally delete file
C
This option works with a single receiver only.
- rename file
In both cases old data will be lost if the sender writes data faster than the receiver can read it.
Of course there are other options.
Simple solution using files and atomic rename which can be easily implemented using scripting languages.
Sender
- write data into file
A
- rename file
A
toB
The rename is atomic, and it can be done while the receiver is processing the old file.
The sender will not block if the receiver does not read the data.
Receiver
If it is no problem to process the same data twice:
- open file
B
- read data
- close file
This option also works when there is more than one receiver, e.g. multiple CGI processes of the web server.
- open file
If the receiver should not process the same data twice:
- rename file
B
toC
- if successful, read and process file
C
- optionally delete file
C
This option works with a single receiver only.
- rename file
In both cases old data will be lost if the sender writes data faster than the receiver can read it.
Of course there are other options.
edited Feb 22 at 16:19
answered Feb 22 at 15:14
BodoBodo
2,251618
2,251618
One of the other options is POSIX IPC, which offers shared memory, message queues, and semaphores. Google is you friend.
– Johan Myréen
Feb 22 at 16:14
Check out zeromq ...
– Murray Jensen
Feb 23 at 4:26
Thanks for the answer, but since the data generating process will append the ring buffer with new values multiple times a second, it doesn't seem like a good fit. I'd prefer not to have to save the whole buffer in a new file on updates.
– Larsp
Feb 25 at 9:23
Thanks for the tip about zeromq, very interesting. I'll also give POSIX IPC a look.
– Larsp
Feb 25 at 9:23
@Larsp Of course you can choose a different solution. If you think this would be a performance problem I suggest to test and measure first. You should add your timing requirements to the question. How often does the sender update the data? How much data is one set? How often is the receiver expected to display the data?
– Bodo
Feb 25 at 9:27
add a comment |
One of the other options is POSIX IPC, which offers shared memory, message queues, and semaphores. Google is you friend.
– Johan Myréen
Feb 22 at 16:14
Check out zeromq ...
– Murray Jensen
Feb 23 at 4:26
Thanks for the answer, but since the data generating process will append the ring buffer with new values multiple times a second, it doesn't seem like a good fit. I'd prefer not to have to save the whole buffer in a new file on updates.
– Larsp
Feb 25 at 9:23
Thanks for the tip about zeromq, very interesting. I'll also give POSIX IPC a look.
– Larsp
Feb 25 at 9:23
@Larsp Of course you can choose a different solution. If you think this would be a performance problem I suggest to test and measure first. You should add your timing requirements to the question. How often does the sender update the data? How much data is one set? How often is the receiver expected to display the data?
– Bodo
Feb 25 at 9:27
One of the other options is POSIX IPC, which offers shared memory, message queues, and semaphores. Google is you friend.
– Johan Myréen
Feb 22 at 16:14
One of the other options is POSIX IPC, which offers shared memory, message queues, and semaphores. Google is you friend.
– Johan Myréen
Feb 22 at 16:14
Check out zeromq ...
– Murray Jensen
Feb 23 at 4:26
Check out zeromq ...
– Murray Jensen
Feb 23 at 4:26
Thanks for the answer, but since the data generating process will append the ring buffer with new values multiple times a second, it doesn't seem like a good fit. I'd prefer not to have to save the whole buffer in a new file on updates.
– Larsp
Feb 25 at 9:23
Thanks for the answer, but since the data generating process will append the ring buffer with new values multiple times a second, it doesn't seem like a good fit. I'd prefer not to have to save the whole buffer in a new file on updates.
– Larsp
Feb 25 at 9:23
Thanks for the tip about zeromq, very interesting. I'll also give POSIX IPC a look.
– Larsp
Feb 25 at 9:23
Thanks for the tip about zeromq, very interesting. I'll also give POSIX IPC a look.
– Larsp
Feb 25 at 9:23
@Larsp Of course you can choose a different solution. If you think this would be a performance problem I suggest to test and measure first. You should add your timing requirements to the question. How often does the sender update the data? How much data is one set? How often is the receiver expected to display the data?
– Bodo
Feb 25 at 9:27
@Larsp Of course you can choose a different solution. If you think this would be a performance problem I suggest to test and measure first. You should add your timing requirements to the question. How often does the sender update the data? How much data is one set? How often is the receiver expected to display the data?
– Bodo
Feb 25 at 9:27
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502309%2foptimal-sharing-of-a-ring-buffer-between-processes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown