当前位置:网站首页>UNIX Environment Programming Chapter 15 15.6 XSI IPC

UNIX Environment Programming Chapter 15 15.6 XSI IPC

2022-08-09 10:29:00 Mary Soda Meatloaf

15.6.1 Identifiers and keys

IPC structures (message queues, semaphores, or shared memory segments) in each kernel are referenced by a non-negative integer identifier.When an IPC structure is created and then deleted, the identifiers associated with the structure are incremented by 1 until the maximum positive integer value is reached, and then rolled back to 0.

Each IPC object is associated with a key that is used as the object's external name.

A key should be specified whenever an IPC structure is created.The data type of this key is the basic system data type key_t, which is usually defined as a long in the header file.This key is programmed with an identifier by the kernel.This key is transformed into an identifier by the kernel.

There are several ways to bring client and server processes together on the same IPC structure.

  1. The server process can specify the key IPC_PRIVATE to create a new IPC structure and store the returned identifier somewhere for the client process to access.The IPC_PRIVATE key can also be used for parent-child relationships.The parent process specifies IPC_PRIVATE to create a new IPC structure, and the returned identifier can be used by the child process after fork.
  2. A key recognized by both client and server processes can be included in a common header file.The server process then assigns this key to create a new IPC structure.
  3. The client process and the server process identify a pathname and project ID, and then call the function ftok to turn these two values ​​into a key.

The only service provided by ftok is to generate a key from a pathname and a project ID.

#include key_t ftok(const char* path,int id);//Success returns the key, failure returns -1

The path parameter must refer to an existing file. When generating the key, only the lower 8 bits of the id parameter are used.

The keys created by a fork are usually formed by taking the partial st_dev and st_ino fields in its stat structure by the given pathname, and then combining them with the project ID.If the two pathnames refer to two different files, then ftok will usually return different keys for the two pathnames.But because the inode number and key are usually stored in a long integer, information may be lost when the key is created.This means that for two pathnames to different files, it is possible to produce the same key if the same item ID is used.

15.6.2 Permission structure

XSI IPC associates an ipc_perm structure with each IPC structure.

When creating an IPC structure, assign initial values ​​to all fields.Later, you can call msgctl, semctl, shmctl to modify the uid, gid and mode fields.In order to modify these values, the calling process must be the creator or superuser of the IPC structure.

原网站

版权声明
本文为[Mary Soda Meatloaf]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091026200691.html