UNIX network programming 2 Reading Notes Chapter 2-System V IPC

Source: Internet
Author: User
1. Overview
  • Three types of System v ipc: System V message queue, System V semaphore, and System V shared memory Zone
  • System v ipc shares many similar points between the functions that access them and the information maintained by the kernel for them. This chapter describes all these common attributes.
  • Summarizes all System v ipc functions.
2. key_t key and ftok Function
  • The header file SYS/types. h defines the data type key_t as an integer, usually a 32-bit integer.
  1. #include <sys/ipc.h>
  2. key_t ftok (const char *pathname, int id);
  3. Returns: IPC key if OK, -1 on error
  • The ftok function converts an existing path name and an integer identifier into a key_t value, which is called the IPC key.
  • If the pathname does not exist or is inaccessible to the calling process, ftok returns-1
  • There is no guarantee that the combination of two different pathnames and the same ID produces different keys.
3. ipc_perm Structure
  • The kernel maintains an information structure for each IPC object, which is similar to the information maintained by the kernel for files.
    1. struct ipcqerm {
    2. uid_t uid; /* owner‘s user id */
    3. gid_t gid; /* owner‘s group id */
    4. uid_t cuid; /* creator‘s user id */
    5. gid_t cgid; /* creator‘s group id */
    6. mode_t mode; /* read - write permissions */
    7. ulong_t seq; /* slot usage sequence number */
    8. key_t key; /* IPC key */
    9. };
4. Create and open an IPC Channel
  • The application has two options for the key value.
    1. Call ftok and pass the pathname and ID to it.
    2. Specify the key as ipc_private. This ensures that a new and unique IPC object is created.

  • All three xxxget functions have a parameter named Oflag, which specifies the read/write permission bit of the IPC object (the mode Member of the ipc_perm structure) and selects to create a new IPC object, or access an existing IPC object.


  • System v ipc defines its own ipc_xxx constant value, instead of using the o_creat and o_excl constant values as standard open functions and posix ipc functions do.
  • Note: The System v ipc function combines the common root permission bits of ipc_xxx to a single Oflag parameter. The open function has a parameter named Oflag and mode.
5. IPC permission
  • Each time you use a xxxget function to create a new IPC object, the following information is saved to the ipc_perm structure of the object.
    1. Some bits in the Oflag parameter initialize the mode Member of the ipc_perm structure. The suffix A of sem_a indicates alter change.
    2. In the ipc_perm structure, the cuid and cgid members are set to the valid user ID and valid group ID of the calling process. These two members are collectively called the Creator ID.
    3. The UID and GID members in the ipc_perm structure are also set to the valid user ID and the valid group ID of the calling process.
  • Although a process can call the xxxctl function to modify the owner ID, the Creator Id never changes. The xxxctl function also allows a process to modify the mode member of an IPC object.
  • Every time a process accesses an IPC object, IPC performs two-level checks. When the object is opened (xxxget function), it is executed once, and will be executed once every time this object is used.
6. identifier Reuse
  • The ipc_perm structure contains a seq variable, which is a slot usage serial number. This variable is a counter maintained by the kernel for each potential IPC object in the system. Each time an IPC object is deleted, the kernel increments the corresponding slot number. If it overflows, the cycle is 0.
  • There are two reasons for this counter:
    1. Consider the file descriptor maintained by the kernel for opening files. They are small integers that only make sense within a single process and are process-specific values. However, the system v ipc identifier is system-specific, not process-specific.
    2. To avoid reusing the System v ipc identifier in a short time
7. IPCS and ipcrm programs
  • It can only be used for System v ipc, but not for POSIX IPC
  • IPCS-Q-m-S
  • Ipcrm -- all = msg
8. kernel restrictions
  • Sysctl command

UNIX network programming 2 Reading Notes Chapter 2-System V IPC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.