{"id":438,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:17","modified_gmt":"2020-08-22T20:26:17","slug":"this-is-the-page-title-toplevel-271","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=438","title":{"rendered":"Pipes"},"content":{"rendered":"\n<title>Pipes<\/title>\n<p>\nThe common Linux shells all allow redirection.\nFor example\n<pre>\n$ ls | pr | lpr\n<\/pre>\n<p>\npipes the output from the <font face=\"helvetica\">ls<\/font> command listing the\ndirectory&#8217;s files into the standard input of the <font face=\"helvetica\">pr<\/font>\ncommand which paginates them. Finally the standard output from the\n<font face=\"helvetica\">pr<\/font> command is piped into the standard\ninput of the <font face=\"helvetica\">lpr<\/font> command which prints the results on\nthe default printer.\nPipes then are unidirectional byte streams which connect the standard output from\none process into the standard input of another process.\nNeither process is aware of this redirection and behaves just as it would normally.\nIt is the shell that sets up these temporary pipes between the processes.\n<p>\n<img decoding=\"async\" src=\"pipes.gif\"><br \/>\nFigure: Pipes\n<p>\n<p>In Linux, a pipe is implemented using two <tt>file<\/tt> data structures which both\npoint at the same temporary VFS inode which, in turn, points at a physical page within\nmemory.\nFigure&nbsp;<a href=\"#pipes-figure\"\n> 5.1<\/a> shows that each <tt>file<\/tt> data structure contains\npointers to different file operation routine vectors: one for writing to\nthe pipe, the other for reading from the pipe.\n<p>\nThis hides the underlying differences from the generic system calls which read and\nwrite to ordinary files.\nAs the writing process writes to the pipe, bytes are copied into the shared data page\nand when the reading process reads from the pipe, bytes are copied from the shared data\npage.\nLinux must synchronize access to the pipe.\nIt must make sure that the reader and the writer of the pipe are in step and to do this\nit uses locks, wait queues and signals.\n<p>\nWhen the writer wants to write to the pipe it uses the standard\nwrite library functions.\nThese all pass file descriptors that are indices into  the process&#8217; set of\n<tt>file<\/tt> data structures, each one representing an open file or, as in  this case, an open\npipe.\nThe Linux system call uses the write routine pointed at by the <tt>file<\/tt> data structure\ndescribing this pipe.\nThat write routine uses information held in the VFS inode representing the pipe to\nmanage the write request.\n<p>\nIf there is enough room to write all of the bytes into the pipe and, so long\nas the pipe is not locked by its reader, Linux locks it for the writer and copies the bytes\nto  be written from the process&#8217; address space into the shared data page.\nIf the pipe is locked by the reader or if there is not enough room for the data then\nthe current process is made to sleep on the pipe inode&#8217;s wait queue and the scheduler\nis called so that another process can run.\nIt is interruptible, so it can receive signals and it will be awakened by the reader\nwhen there is enough room for the write data or when the pipe is unlocked.\nWhen the data has been written, the pipe&#8217;s VFS inode is unlocked and any waiting\nreaders sleeping on the inode&#8217;s wait queue will themselves be awakened.\n<p>\nReading data from the pipe is a very similar process to writing to it.\n<p>\nProcesses are allowed to do non-blocking reads (depending on the mode in\nwhich they opened the file or pipe and if there is no data\nto be read or if the pipe is locked, an error will be returned,as in this case).\nThis means that the process <i>can<\/i> continue to run.\nThe alternative is to wait on the\npipe inode&#8217;s wait queue until the write process has finished.\nWhen both processes have finished with the pipe, the pipe inode is discarded along\nwith the shared data page.\n<p>\nLinux also supports <em>named<\/em> pipes, also known as FIFOs because pipes operate\non a First In, First Out principle.\nThe first data written into the pipe is the first data read from the pipe.\nUnlike pipes, FIFOs are not temporary objects, they are entities in the file system\nand can be created using the <font face=\"helvetica\">mkfifo<\/font> command.\nProcesses are free to use a FIFO so long as they have appropriate access rights to it.\nThe way that FIFOs are opened is a little different from pipes.\nA pipe (its two <tt>file<\/tt> data structures, its VFS inode and the shared data page) is\ncreated in one go whereas a FIFO already exists and is opened and closed by its users.\nLinux must handle readers opening the FIFO before writers open it as well as readers\nreading before any writers have written to it.\nThat aside, FIFOs are handled almost exactly the same way as pipes and they use the\nsame data structures and operations.\n<p>\n","protected":false},"excerpt":{"rendered":"<p>Pipes The common Linux shells all allow redirection. For example $ ls | pr | lpr pipes the output from the ls command listing the directory&#8217;s files into the standard input of the pr command which paginates them. Finally the &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=438\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-438","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=438"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/438\/revisions"}],"predecessor-version":[{"id":657,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/438\/revisions\/657"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}