Talk C chestnuts together (94th back: C language instance -- SystemV IPC structure overview)

Source: Internet
Author: User

Talk C chestnuts together (94th back: C language instance -- SystemV IPC structure overview)

Hello, everyone. In the previous review, we talked about the critical resource of inter-process communication. The example is as follows:Overview of SystemV IPC structure. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

We usually refer to IPC as short for inter-process communication. It is just an abstract name. Today, SystemV IPC is a specific IPC, because it is widely used in the Unix SystemV version, it is called SystemV IPC. Of course, the Linux system we use also supports this type of IPC. Next we will introduce SystemV IPC in detail.

1. Definition of SystemV IPC

SystemV IPC is implemented in the system kernel. It is a structure in the kernel. This structure is an abstract concept. We can implement it as a specific object. For example, computers are an abstract concept, while desktops, laptops, and tablets are specific computer products. Related to the SystemV IPC structureSystemV IPC objects include: Shared Memory, message queue, and semaphore., We will introduce these specific SystemV IPC objects in the following chapter.

2. Use of SystemV IPC

In the kernel, the identifier is used to use the IPC structure. The identifier is similar to the file descriptor when we use the file. It is a non-negative integer value, but its value is usually a relatively large value. Identifier is something in the kernel. We usually cannot directly use it, so we need to use the key to establish a connection with the identifier, so that we can operate the structure of SystemV IPC in the kernel through the operation key, then, operate the SystemV IPC object, such as the shared memory.

In the program, we can specify an integer as the key value, but this method is not good. Generally, IPC_PRIVATE is used to create an IPC object. Or use the ftok function to generate a unique key.

3. Structure of SystemV IPC

The SystemV IPC structure has an ipc_perm structure, which is a struct that contains information such as IPC permissions and owner. The specific content is as follows (from: /linux-4.0.3/include/linux/ipc. h file)

/* used by in-kernel data structures */struct kern_ipc_perm{    spinlock_t  lock;    bool        deleted;    int     id;    key_t       key;    kuid_t      uid;  //must    kgid_t      gid;  //must    kuid_t      cuid; //must    kgid_t      cgid; //must    umode_t     mode; //must    unsigned long   seq;    void        *security;};

Different SystemV IPC objects in this structure have different definitions, but I have added the // must member that each SystemV IPC object must contain.

4. Use of SystemV IPC

The use of SystemV IPC varies with the SystemV IPC object, but the overall use process is consistent:

Obtain a SystemV IPC object, operate on the SystemV IPC object, and release the SystemV IPC object.

The detailed operation method will be introduced when we introduce the specific SystemV IPC object in the following chapter.

In addition, the SystemV IPC object in Linux is in the kernel and can be viewed like a file. Powerful Linux provides related commands to operate SystemV IPC.

Ipcs command to view all SystemV IPC objects in the systemThey are: Shared Memory, message queue, and semaphores.

The following is the result of running ipcs in the system:

Ipcs // run the ipcs command ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x00000000 163840 600 talk8 33554432 00000000 2 dest 0x3309569 talk8 600 2 dest... // other omitted ------ Semaphore Arrays -------- key semid owner perms nsems 0xcbc384f8 0 tom 600 1 // It is blank, indicates there is no shared memory. This IPC object ------ Message Queues -------- key msqid owner perms used-bytes messages // It is displayed as null, indicating that there is no Message queue. This IPC object

In addition, you can useIpcs-m/q/s separately view the shared memory, message queue, and semaphores in the system.Here is no example. You can try it on your own.

The ipcrm command can be used to delete specific IPC objects in the system.

Ipcrm-m/q/s id can be used to delete IPC objects such as shared memory, message queue, and semaphore. The command id can be viewed by running ipcs. For example, in the above example, the content in the shmid column is the shared memory id.

In addition, in Linux, you can view the files in a special directory to obtain the ipc information:

The information about the shared memory is located in the message queue information in the/proc/sysvipc/shm file. The information about the semaphores in the/proc/sysvipc/msg file is located in the/proc/sysvipc/sem file.

We can use the cat command to view the information of the ipc object. The following is an example of viewing the semaphore in the system:

Cat/proc/sysvipc/sem // run the cat command key semid perms nsems uid gid cuid cgid otime ctime-876378888 0 600 1 1000 1000 1000 1000 1452336947 1452326

Let's talk about the example about the SystemV IPC structure. I want to know what examples will be provided later, and I will try again.

 

Related Article

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.