Section 6.16. Interprocess Communications


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.

6.16.1. Pipes

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 [2]); 


In both Linux and HP-UX, pipe() returns two file descriptors: fildes[0] and fildes[1], wherein fildes[0] is opened for reading and fildes[1] 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.

6.16.2. FIFOs

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,[53] whereas it is 8192 bytes on HP-UX.[54]

[53] /usr/include/linux/limits.h

[54] /usr/include/limits.h

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 


Linux includes:

#include <mqueue.h> 


Using the API sysconf within a program can also determine whether a POSIX option exists on a particular Linux installation.[55]

[55] Refer to the "Determining POSIX API Support" section of this chapter.

Table 6-23 compares message queue interfaces between HP-UX and Linux.

Table 6-23. Comparison Table of HP-UX and Linux POSIX Message Queue APIs

HP-UX

Linux

Description

mq_close

mq_close

Close a message queue.

mq_getattr

mq_getattr

Get message queue attributes.

mq_notify

mq_notify

Notify process (or thread) that a message is available on a queue.

mq_open

mq_open

Open a message queue.

mq_receive

mq_receive

Receive a message from a message queue.

mq_send

mq_send

Send a message to a message queue.

mq_setattr

mq_setattr

Set/get message queue attributes.

None

mq_timedreceive

Receive a message from a message queue; stop waiting if the specified timeout expires.

None

mq_timedsend

Send a message to a message queue; stop blocking on full message queue if the specified timeout expires.

mq_unlink

mq_unlink

Remove a message queue.


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 


Linux includes:

#include <semaphore.h> 


Table 6-24 compares POSIX semaphore interfaces in HP-UX and Linux.

Table 6-24. Comparison Table of HP-UX and Linux POSIX Semaphore APIs

HP-UX

Linux

Description

sem_close

sem_close

Close a named semaphore.

sem_destroy

sem_destroy

Destroy an unnamed semaphore.

sem_getvalue

sem_getvalue

Get the value of a semaphore.

sem_init

sem_init

Initialize an unnamed semaphore.

sem_open

sem_open

Initialize/open a named semaphore.

sem_post

sem_post

Increment the count of a semaphore.

None

sem_timedwait

Lock a semaphore, but stop waiting when the specified timeout expires.

sem_trywait

sem_trywait

Acquire or wait for a semaphore.

sem_unlink

sem_unlink

Remove a named semaphore.

sem_wait

sem_wait

Acquire or wait for a semaphore.


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.

Linux includes:

#include <sys/types> #include <sys/mman.h> 


Table 6-25 shows a comparison between HP-UX and Linux supported shared memory APIs.

Table 6-25. Comparison Table of HP-UX and Linux POSIX Shared Memory APIs

HP-UX

Linux

Description

shm_open

shm_open

Open a shared memory object.

shm_unlink

shm_unlink

Remove a shared memory object.


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:

[View full width]

sysctl -w kernel.shmmax=[number of bytes][56] echo [number of bytes] > /proc/sys/kernel/shmmax


[56] See the man page for sysctl or /etc/sysctl for more information.

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.

Linux includes:

#include <sys/types.h> #include <sys/shm.h> 


Table 6-26. Comparison Between HP-UX and Linux System Memory Interfaces

HP-UX

Linux

Description

shmat

shmat

Attach the shared memory segment to the data segment of the calling process.

shmctl

shmctl

Shared memory control.

shmdt

shmdt

Detach the shared memory segment.

shmop

shmop

Shared memory operation.

shmget

shmget

Get shared memory segment identifier.


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.

Linux includes:

#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> 


Table 6-27 compares HP-UX and Linux System V message APIs.

Table 6-27. Comparison Table of HP-UX and Linux Messages APIs

HP-UX

Linux

Description

msgctl

msgctl

Message control operation.

The msgqid_ds structure on HP-UX and Linux is not the same.

msgget

msgget

Get message queue.

msgrcv

msgrcv

Message receive operation.

msgsnd

msgsnd

Message send operation.


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.

Linux includes:

#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> 


Table 6-28 compares HP-UX and Linux semaphore APIs.

Table 6-28. Comparion Table of HP-UX and Linux Semaphore APIs

HP-UX

Linux

Description

semctl

semctl

Semaphore control operation.

  

The semid_ds structure used in semctl is different for HP-UX and Linux.

semget

semget

Get set of semaphores.

semop

semop

Semaphore operation.

semtimedop

semtimedop

Semaphore operation with time limit.


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.




UNIX to Linux Porting. A Comprehensive Reference
UNIX to Linux Porting: A Comprehensive Reference
ISBN: 0131871099
EAN: 2147483647
Year: 2004
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net