6.16. Interprocess Communications
Both HP-UX and Linux support System V and POSIX 1003.1 forms of interprocess communications and synchronization. This section covers pipes, FIFOs, POSIX message queues, POSIX semaphores, POSIX shared memory, System V shared memory, System V messages, and System V semaphores.
A pipe is a technique used to communicate between two threads in a process or between parent and child processes. When a process calls fork, file descriptors are copied to its new child process. As a result, the parent can communication with the child. A pipe can be created by calling the pipe function:
#include <unistd.h> int pipe (int fildes );
In both Linux and HP-UX, pipe() returns two file descriptors: fildes and fildes, wherein fildes is opened for reading and fildes is opened for writing.
The popen and pclose functions are both supported in HP-UX and Linux:
#include <stdio.h> FILE *popen(const char *command, const char *mode); int pclose(FILE *stream);
The call to the popen function creates a pipe between the calling process and the program specified by command in the child process. The calling process can either read from or write to the pipe, as specified by the mode argument. The return value is a stream pointer such that you can write to the standard input of the command, if the mode is w, by writing to the file stream, and you can read from the standard output of the command, if the mode is r, by reading from the file stream.
A first-in, first-out (FIFO) file (also known as named pipes) is a pipe that has a name in the filesystem associated with it. This enables the pipe to be open or closed by any processes. The processes on either end of the pipe need not be related to each other.
You can create a FIFO programmatically by calling the mkfifo function:
#include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode);
This function works the same way on both HP-UX and Linux. A FIFO can have multiple readers and writers. A write request of PIPE_BUF bytes or less from a writer is guaranteed not to be interleaved with data from other processes. On Linux, PIPE_BUF is 4096 bytes, whereas it is 8192 bytes on HP-UX.
6.16.3. POSIX Message Queues
Messages enable multiple processes to send formatted data streams to arbitrary processes. On Linux, POSIX Message is supported only when the option _POSIX_MESSAGE_PASSING is defined. To determine whether the Linux system has the option _POSIX_MESSAGE_PASSING, issue the following command:
$ getconf _POSIX_MESSAGE_PASSING 200112
Using the API sysconf within a program can also determine whether a POSIX option exists on a particular Linux installation.
Table 6-23 compares message queue interfaces between HP-UX and Linux.
6.16.4. POSIX Semaphores
You can determine POSIX semaphore support on a Linux system by using the genconf command or the sysconf API. To determine whether POSIX semaphore support is present, use the getconf command to get the following output:
$ getconf _POSIX_SEMAPHORES 200112
Table 6-24 compares POSIX semaphore interfaces in HP-UX and Linux.
6.16.5. POSIX Shared Memory
HP-UX supports both shm_open and shm_unlink shared memory functions. On Linux, these two functions are available in glibc version 2.2 and later. To use these functions, specify the -lrt flag with the compiler.
#include <sys/types> #include <sys/mman.h>
Table 6-25 shows a comparison between HP-UX and Linux supported shared memory APIs.
Sometimes the default shared memory configured on a Linux system may not be enough for applications. To modify shared memory configuration in Linux, use the following methods:
6.16.6. System V Shared Memory
Both HP-UX and Linux support System V shared memory interfaces. Table 6-26 compares HP-UX and Linux interfaces.
#include <sys/types.h> #include <sys/shm.h>
Note that some flags (shmflags) used in shmget and shmat may have arguments that differ slightly between implementations.
6.16.7. System V Messages
Both HP-UX and Linux support System V messages. The one difference noted is the msgqid_ds structure used in the msgctl API, which is different on each platform.
#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h>
Table 6-27 compares HP-UX and Linux System V message APIs.
The default maximum size of a message queue (msg_qbytes) on Linux is set to the system parameter MSGMNB (16384 bytes). You can raise the msg_qbytes value beyond MSGMNB by using the msgctl function with the appropriate privileges. On Linux, the maximum size for message text is set to MSGMAX (8192 bytes).
6.16.8. System V Semaphores
Both HP-UX and Linux support System V semaphores. The one difference noted is the semid_ds structure used in the semctl API, which is different on each platform.
#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h>
Table 6-28 compares HP-UX and Linux semaphore APIs.
As you can see, Linux provides most if not all interprocess communication facilities available from HP-UX. This makes porting to Linux as easy as just recompiling source code.