Unix™ Systems Programming [Electronic resources] : Communication, Concurrency, and Threads نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Unix™ Systems Programming [Electronic resources] : Communication, Concurrency, and Threads - نسخه متنی

Prentice Hall

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید










15.1 POSIX:XSI Interprocess Communication


The POSIX interprocess communication (IPC) is part of the POSIX:XSI Extension and has its origin in UNIX System V interprocess communication. IPC, which includes message queues, semaphore sets and shared memory, provides mechanisms for sharing information among processes on the same system. These three communication mechanisms have a similar structure, and this chapter emphasizes the common elements of their use. Table 15.1 summarizes the POSIX:XSI interprocess communication functions.

Table 15.1. POSIX:XSI interprocess communication functions.

mechanism

POSIX function

meaning

message queues

msgctl

control

msgget

create or access

msgrcv

receive message

msgsnd

send message

semaphores

semctl

control

semget

create or access

semop

execute operation (wait or post)

shared memory

shmat

attach memory to process

shmctl

control

shmdt

detach memory from process

shmget

create and initialize or access


15.1.1 Identifying and accessing IPC objects


POSIX:XSI identifies each IPC object by a unique integer that is greater than or equal to zero and is returned from the get function for the object in much the same way as the open function returns an integer representing a file descriptor. For example, msgget returns an integer identifier for message queue objects. Similarly, semget returns an integer identifier for a specified semaphore set, and shmget returns an integer identifier for a shared memory segment. These identifiers are associated with additional data structures that are defined in sys/msg.h, sys/sem.h or sys/shm.h, respectively. The integer identifiers within each IPC object type are unique, but you might well have an integer identifier 1 for two different types of objects, say, a semaphore set and a message queue.

When creating or accessing an IPC object, you must specify a key to designate the particular object to be created or accessed. Pick a key in one of these three ways.

  • Let the system pick a key (IPC_PRIVATE).

  • Pick a key directly.

  • Ask the system to generate a key from a specified path by calling ftok.


The ftok function allows independent processes to derive the same key based on a known pathname. The file corresponding to the pathname must exist and be accessible to the processes that want to access an IPC object. The combination of path and id uniquely identifies the IPC object. The id parameter allows several IPC objects of the same type to be keyed from a single pathname.


SYNOPSIS
#include <sys/ipc.h>
key_t ftok(const char *path, int id);
POSIX:XSI

If successful, ftok returns a key. If unsuccessful, ftok returns (key_t)-1 and sets errno. The following table lists the mandatory errors for ftok.

errno

cause

EACCES

search permission on a path component denied

ELOOP

a loop exists in resolution of path

ENAMETOOLONG

length of path exceeds PATH_MAX, or length of a pathname component exceeds NAME_MAX

ENOENT

a component of path is not a file or is empty

ENOTDIR

a component of path's prefix is not a directory

Example 15.1

The following code segment derives a key from the filename /tmp/trouble.c.


if ((thekey = ftok("tmp/trouble.c", 1)) == (key_t)-1))
perror("Failed to derive key from /tmp/trouble.c");


15.1.2 Accessing POSIX:XSI IPC resources from the shell


The POSIX:XSI Extension for shells and utilities defines shell commands for examining and deleting IPC resources, a convenient feature that is missing for the POSIX:SEM semaphores.

The ipcs command displays information about POSIX:XSI interprocess communication resources. If you forget which ones you created, you can list them from the shell command line.


SYNOPSIS
ipcs [-qms][-a | -bcopt]
POSIX:XSI,Shell and Utilities

If no options are given, ipcs outputs, in an abbreviated format, information about message queues, shared memory segments and semaphore sets. You can restrict the display to specific types of IPC resources with the -q, -m and -s options for message queues, shared memory and semaphores, respectively. The -a option displays a long format giving all information available. The -bcopt options specify which components of the available information to print.

Example 15.2

The following command displays all the available information about the semaphores currently allocated on the system.


ipcs -s -a

You can remove an individual resource by giving either an ID or a key. Use the ipcrm command to remove POSIX:XSI interprocess communication resources.


SYNOPSIS
ipcrm [-q msgid | -Q msgkey | -s semid | -S semkey |
-m shmid | -M shmkey] ....
POSIX:XSI,Shell and Utilities

The lower case -q, -s and -m options use the object ID to specify the removal of a message queue, semaphore set or shared memory segment, respectively. The uppercase options use the original creation key.


    / 276