Optimal sharing of a ring buffer between processes

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1















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:



  1. The data generating process that regularly updates a ring buffer with new values, written in C.


  2. 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?










share|improve this question


























    1















    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:



    1. The data generating process that regularly updates a ring buffer with new values, written in C.


    2. 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?










    share|improve this question
























      1












      1








      1








      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:



      1. The data generating process that regularly updates a ring buffer with new values, written in C.


      2. 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?










      share|improve this question














      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:



      1. The data generating process that regularly updates a ring buffer with new values, written in C.


      2. 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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 22 at 13:59









      LarspLarsp

      61




      61




















          1 Answer
          1






          active

          oldest

          votes


















          0














          Simple solution using files and atomic rename which can be easily implemented using scripting languages.



          Sender



          • write data into file A

          • rename file A to B

          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




          1. 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.




          2. If the receiver should not process the same data twice:



            • rename file B to C

            • if successful, read and process file C

            • optionally delete file C

            This option works with a single receiver only.



          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.






          share|improve this answer

























          • 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










          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
          );



          );













          draft saved

          draft discarded


















          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









          0














          Simple solution using files and atomic rename which can be easily implemented using scripting languages.



          Sender



          • write data into file A

          • rename file A to B

          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




          1. 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.




          2. If the receiver should not process the same data twice:



            • rename file B to C

            • if successful, read and process file C

            • optionally delete file C

            This option works with a single receiver only.



          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.






          share|improve this answer

























          • 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















          0














          Simple solution using files and atomic rename which can be easily implemented using scripting languages.



          Sender



          • write data into file A

          • rename file A to B

          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




          1. 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.




          2. If the receiver should not process the same data twice:



            • rename file B to C

            • if successful, read and process file C

            • optionally delete file C

            This option works with a single receiver only.



          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.






          share|improve this answer

























          • 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













          0












          0








          0







          Simple solution using files and atomic rename which can be easily implemented using scripting languages.



          Sender



          • write data into file A

          • rename file A to B

          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




          1. 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.




          2. If the receiver should not process the same data twice:



            • rename file B to C

            • if successful, read and process file C

            • optionally delete file C

            This option works with a single receiver only.



          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.






          share|improve this answer















          Simple solution using files and atomic rename which can be easily implemented using scripting languages.



          Sender



          • write data into file A

          • rename file A to B

          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




          1. 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.




          2. If the receiver should not process the same data twice:



            • rename file B to C

            • if successful, read and process file C

            • optionally delete file C

            This option works with a single receiver only.



          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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

















          • 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

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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






          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?