{"id":592,"date":"2012-10-18T15:31:48","date_gmt":"2012-10-18T15:31:48","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=592"},"modified":"2017-04-10T15:56:52","modified_gmt":"2017-04-10T15:56:52","slug":"named-pipes-fifos","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/pipes\/named-pipes-fifos","title":{"rendered":"Named Pipes \/ FIFO&#8217;s"},"content":{"rendered":"<p>\nIf you need unrelated processes to be able to exchange data you can do this using FIFOs, often referred to as named pipes. A named pipe is a special type of file (everything is a file in linux!) that exists as a name in the file system but behaves like unnamed pipes. &nbsp; Once created data can be passed through it, i.e. to pass data to your application or read data from it.\n<\/p>\n<h4>\nCreating A Named Pipe \/ FIFO For Receiving Input<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n#include &lt;unistd.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;fcntl.h&gt;\r\n#include &lt;sys\/types.h&gt;\r\n#include &lt;sys\/stat.h&gt;\r\n\r\n#define OUR_INPUT_FIFO_NAME &quot;\/tmp\/my_fifo&quot;\r\n\r\n\r\n\tint our_input_fifo_filestream = -1;\r\n\r\n\r\n\t\/\/--------------------------------------\r\n\t\/\/----- CREATE A FIFO \/ NAMED PIPE -----\r\n\t\/\/--------------------------------------\r\n\tint result;\r\n\r\n\tprintf(&quot;Making FIFO...\\n&quot;);\r\n\tresult = mkfifo(OUR_INPUT_FIFO_NAME, 0777);\t\t\/\/(This will fail if the fifo already exists in the system from the app previously running, this is fine)\r\n\tif (result == 0)\r\n\t{\r\n\t\t\/\/FIFO CREATED\r\n\t\tprintf(&quot;New FIFO created: %s\\n&quot;, OUR_INPUT_FIFO_NAME);\r\n\t}\r\n\r\n\tprintf(&quot;Process %d opening FIFO %s\\n&quot;, getpid(), OUR_INPUT_FIFO_NAME);\r\n\tour_input_fifo_filestream = open(OUR_INPUT_FIFO_NAME, (O_RDONLY | O_NONBLOCK));\r\n\t\t\t\t\t\/\/Possible flags:\r\n\t\t\t\t\t\/\/\tO_RDONLY - Open for reading only.\r\n\t\t\t\t\t\/\/\tO_WRONLY - Open for writing only.\r\n\t\t\t\t\t\/\/\tO_NDELAY \/ O_NONBLOCK (same function) - Enables nonblocking mode. When set read requests on the file can return immediately with a failure status\r\n\t\t\t\t\t\/\/\t\t\t\t\t\t\t\t\t\t\tif there is no input immediately available (instead of blocking). Likewise, write requests can also return\r\n\t\t\t\t\t\/\/\t\t\t\t\t\t\t\t\t\t\timmediately with a failure status if the output can&#39;t be written immediately.\r\n\tif (our_input_fifo_filestream != -1)\r\n\t\tprintf(&quot;Opened FIFO: %i\\n&quot;, our_input_fifo_filestream);\r\n\r\n<\/code><\/pre>\n<p>\nNote that although this code asks for a mode of 0777, it will be altered by the user mask (umask) setting just as in normal file creation.\n<\/p>\n<p>\nA program may not open a FIFO for reading and writing with the mode O_RDWR. &nbsp;Use seperate pipes if you want to read and write.\n<\/p>\n<h4>\nChecking For Received Bytes<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\t\/\/---------------------------------------------\r\n\t\t\/\/----- CHECK FIFO FOR ANY RECEIVED BYTES -----\r\n\t\t\/\/---------------------------------------------\r\n\t\t\/\/ Read up to 255 characters from the port if they are there\r\n\t\tif (our_input_fifo_filestream != -1)\r\n\t\t{\r\n\t\t\tunsigned char rx_buffer[256];\r\n\t\t\tint rx_length = read(our_input_fifo_filestream, (void*)rx_buffer, 255);\t\t\/\/Filestream, buffer to store in, number of bytes to read (max)\r\n\t\t\tif (rx_length &lt; 0)\r\n\t\t\t{\r\n\t\t\t\t\/\/An error occured (this can happen)\r\n\t\t\t\t\/\/printf(&quot;FIFO Read error\\n&quot;);\r\n\t\t\t}\r\n\t\t\telse if (rx_length == 0)\r\n\t\t\t{\r\n\t\t\t\t\/\/No data waiting\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\t\/\/Bytes received\r\n\t\t\t\trx_buffer[rx_length] = &#39;\\0&#39;;\r\n\t\t\t\tprintf(&quot;FIFO %i bytes read : %s\\n&quot;, rx_length, rx_buffer);\r\n\t\t\t}\r\n\t\t}\r\n<\/code><\/pre>\n<h4>\nClosing The FIFO<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/----- CLOSE THE FIFO -----\r\n\t(void)close(our_input_fifo_filestream);\r\n<\/code><\/pre>\n<h4>\nClose FIFO&nbsp;From Command Line<br \/>\n<\/h4>\n<p>\nThis can be needed when debugging. &nbsp;Normally you don&#39;t have to close a fifo&nbsp;to be able to use it again&nbsp;but it is possible to sometimes cause a fifo to not work properly when used again (&nbsp;maybe by not having emptied it before your application closes?), but using this will kill it completely ready for the next time your app is run to re-create it.\n<\/p>\n<pre>\r\n<code>\r\nsudo rm \/tmp\/my_fifo\r\n<\/code><\/pre>\n<h4>\nSending data&nbsp;to your applications FIFO&nbsp;<br \/>\n<\/h4>\n<h5>\nSending from a command prompt<br \/>\n<\/h5>\n<pre>\r\n<code>\r\necho &quot;ABCD&quot; &gt; \/tmp\/my_fifo\r\n<\/code><\/pre>\n<h5>\nSending from C++<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tchar buffer[] = &quot;ABCD&quot;;\r\n\twrite(our_input_fifo_filestream, (void*)buffer, strlen(buffer));\r\n<\/code><\/pre>\n<h5>\nSending bytes from a PHP web page<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\t&lt;?php\r\n\tsystem(&quot;sudo sh -c &#39;echo \\&quot;ABCD\\&quot; &gt; tmp\/my_fifo&#39;&quot;);\r\n\t?&gt;\r\n<\/code><\/pre>\n<h4>\nPHP Web page Access<br \/>\n<\/h4>\n<p>\nSee our PHP web page example <a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/tcpip\/web-interfaces\">here<\/a>\n<\/p>\n<h4>\nUsing&nbsp;pipes between main application and child threads<br \/>\n<\/h4>\n<p>\nSee <a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/threads\/running-a-command-line-tool-on-a-child-thread\">this example here<\/a>.\n<\/p>\n<h4>\nOther Methods Of Passing Data Between Processes<br \/>\n<\/h4>\n<p>\nSee <a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/memory\/shared-memory\">Shared Memory here<\/a>.\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you need unrelated processes to be able to exchange data you can do this using FIFOs, often referred to as named pipes. A named pipe is a special type of file (everything is a file in linux!) that exists as a name in the file system but behaves like unnamed pipes. &nbsp; Once created [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-592","post","type-post","status-publish","format-standard","hentry","category-pipes"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=592"}],"version-history":[{"count":19,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/592\/revisions"}],"predecessor-version":[{"id":2765,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/592\/revisions\/2765"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}