fifo Linux

From Noah.org
Revision as of 17:31, 4 October 2012 by Root (Talk | contribs) (Created page with 'Category: Engineering This demonstrates how easy it is to use Linux FIFO (named pipes). First you need to create the FIFO inode. There are two commands you can use to do th…')

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


This demonstrates how easy it is to use Linux FIFO (named pipes).

First you need to create the FIFO inode. There are two commands you can use to do this. The two following commands are equivalent:

mknod --mode=666 event.fifo p
mkfifo --mode=666 event.fifo

Python Linux FIFO

#!/usr/bin/env python

# This demonstrates a Linux Named Pipe (FIFO) in Python.
# You can run the test as follows:
#     mkfifo event.fifo
#     ./fifo_test.py
#     echo "Hello 1" >> event.fifo 
#     echo "Hello 2" >> event.fifo 
#     echo "Hello 3" >> event.fifo 

while True:
    fifo_in = file ("event.fifo", "r")
    for line in fifo_in:
        print (line)

Java Linux FIFO

/* This demonstrates a Linux Named Pipe (FIFO) in Java.
 *
 *     mkfifo event.fifo
 *     javac fifo_test.java 
 *     java test
 *     echo "Hello 1" >> event.fifo 
 *     echo "Hello 2" >> event.fifo 
 *     echo "Hello 3" >> event.fifo 
 */
import java.io.*;

public class fifo_test
{
    public static void main(String args[]) throws IOException
    {
        String input_event;
        while (true){
            BufferedReader br = new BufferedReader(new FileReader("event.fifo"));
            while (true){
                input_event = br.readLine();
                if (input_event == null) break;
                System.out.println (input_event);
            }
        }
    }
}