sections in this module City College of San Francisco - CS260A
Linux System Administration

Module: Processes
module list

pipes and sockets

Named pipes

We have all been using pipes since our early days of using Linux. We understand conceptually what a pipe is - a connection between two processes. The first process writes to the pipe, usually via standard output, and the second process reads from the pipe, usually via standard input. The pipe connection is created while the processes run. If one of the processes exits, the pipe breaks and is destroyed.

You can create a filesystem object that has the same qualities as a pipe. Just like a regular pipe, one process writes to the pipe, and another process reads from it. Just like a regular pipe, the data comes out of the pipe in the same order it was written. The only difference is the pipe persists even if the process exits. This filesystem object is called a named pipe or a fifo. You create a fifo using mkfifo

Let's take a look at a simple example. (Hint: DO THIS EXERCISE YOURSELF, and remember it when we cover systemd later!)

In window #1

$ cd /tmp
$ mkfifo mypipe
$ echo "hellloooo down therrrre!" > mypipe
bash: !": event not found
$ echo 'hellllloooo down therrre!' > mypipe

At this point, the echo command is blocked. When you write to a named pipe your process is blocked until someone reads the data. So, in window #2:

$ cd /tmp
$ cat < mypipe
hellllloooo down therrre!
$

Once the pipe has been read , the echo command in window #1 exits and you get back to the shell prompt.

Note the filetype on named pipes, type 'p'

$ ls -l mypipe
prw-r--r--. 1 gboyd gboyd 0 Jul 14 17:26 mypipe
$ rm mypipe

The named pipe can be removed just like a file, with rm.

Sockets

Named pipes aren't used very often, but sockets are. Similar in function to a named pipe, a socket has one or more writers, and a reader.

There are two types of sockets - internet sockets and Unix domain sockets. Internet domain sockets are basically a filesystem object that has been connected on the other side to an ip address/port pair. On the local side (the filesystem on which the socket resides), it is connected to a port. This is used to communicate with a remote network process.

Unix domain sockets are used to provide a location for a local daemon process to listen. Other processes that want to communicate with the daemon then simply write to the socket. It behaves similarly to a named pipe, except the Unix domain socket also allows the transmission of file descriptors and process information. This allows a client to identify (or authenticate) itself and even to pass a file of data (using an open file descriptor).

Several programs that you are familiar with (and many that you are not) use Unix domain sockets: rsyslogd uses /dev/log, for example. (You can see the 's' type, which identifies a socket, below):

$ ls -ld /dev/log
srw-rw-rw-. 1 root root 0 Jul 15 08:46 /dev/log

Probably the most important process that uses sockets for communication is the system message bus, dbus. It, and its daemon, messagebus (dbus-daemon) is worth mentioning here:

The system message bus is used for desktop process intercommunication. Without it, a desktop process that managed the panel, for example, would not know when the mouse was clicked on it. Many processes connect to the messagebus daemon. It then sifts through the data on the desktop busses (there are two of them) and sends the appropriate messages to the processes that service them. If messagebus dies, so does your desktop.

Other IPC mechanisms

Pipes, named pipes, signals and sockets are examples of interprocess communication mechanisms. There are several others including shared memory and semaphores. The /proc filesystem, in fact, is a sort of IPC mechanism, except for the fact that one of the 'processes' in this case is the kernel.

Prev This page was made entirely with free software on linux:  
Kompozer
and Openoffice.org    
Next

Copyright 2014 Greg Boyd - All Rights Reserved.

Document made with Kompozer