I have been looking at all the different types of Unix file: regular files, directories, device files, symbolic link, doors, socket, named pipes,…
but I couldn’t find a file type that would behave like a stack/LIFO structure.
As most of you know, the advantage of FIFO/named pipe over regular files is the following:
from https://askubuntu.com/questions/449132/why-use-a-named-pipe-instead-of-a-file
Almost everything in Linux can be considered a file, but the main difference between a regular file and a named pipe is that a named pipe is a special instance of a file that has no contents on the filesystem.
from man fifo
A FIFO special file (a named pipe) is similar to a pipe, except that it is accessed as part of the filesystem. It can be opened by multiple processes for reading or writing. When processes are exchanging data via the FIFO, the kernel passes all data internally without writing it to the filesystem. Thus, the FIFO special file has no contents on the filesystem; the filesystem entry merely serves as a reference point so that processes can access the pipe using a name in the filesystem.
The kernel maintains exactly one pipe object for each FIFO special file that is opened by at least one process. The FIFO must be opened on both ends (reading and writing) before data can be passed. Normally, opening the FIFO blocks until the other end is opened also.
So I was wondering if this type of file (LIFO/Stack) does actually exist natively in Unix? If it is not the case is there a way to simulate one?
I have been thinking about using tac <named_pipe>
but I am not sure if it is a good/viable idea?
Is there a better solution?
Thank you for your help.
Advertisement
Answer
Almost everything in Linux can be considered a file, but the main difference between a regular file and a named pipe is that a named pipe is a special instance of a file that has no contents on the filesystem.
A better way of looking at it would be to say that a named pipe is a pipe that appears in the filesystem. It doesn’t have content stored on disk, but it’s not just an empty inode either — it has particular behaviors which are specific to pipes.
So I was wondering if this type of file (LIFO/Stack) does actually exist natively in Unix?
No, it does not exist. Unlike a queue, a stack cannot be treated as a stream; and, in any case, it is not a particularly useful structure in a multiprocess environment. It doesn’t make sense to have multiple processes concurrently pushing/popping data from a stack.
I have been thinking about using
tac <named_pipe>
but I am not sure if it is a good/viable idea?
That won’t work. tac
will read the entire contents of the pipe, and there will be nothing left in the pipe afterwards.