Talk C together (95th back: C language instance -- use shared memory for inter-process communication I)

Source: Internet
Author: User

Talk C together (95th back: C language instance -- use shared memory for inter-process communication I)

Hello, everyone. We talked about the SystemV IPC structure in the previous review. The example is as follows:Use shared memory for inter-process communication. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

Shared Memory is a specific object of the abstract concept of SystemV IPC structure. Just like its name, it provides a memory space for different processes to use, through which data can be transferred between processes to implement inter-process communication.

Before introducing how to use the shared memory, we will introduce several functions that are used to operate the shared memory.

Shmget Function
int shmget(key_t key, size_t size,int shmflag)

This function is used to obtain the shared memory. The function returns the identifier of the shared memory. We can use this identifier to use the shared memory;

The first parameter is the key value. It is used to operate the structure of IPC in the kernel, that is, the structure of the shared kernel exists. (This is described in the previous article) the second parameter is the shared memory capacity, in bytes. The third parameter is the shared memory permission tag, which is the same as the File Permission. The shared memory identifier is returned when the function runs successfully, otherwise,-1 is returned;

When using this function, we need to define a shared memory type and calculate the memory space of this type. The shared memory type can be defined according to the program's needs. It is common to define a struct type.

Shmat Function
void * shmat(int shm_id, const void *shm_addr,int shmflg)

This function is used to connect the shared memory to the address space of the process so that the process can use the shared memory;

The first parameter is the identifier of the shared memory, which can be obtained through the shmget function. The second parameter is an address, which indicates the location of the shared memory connected to the process. The third parameter is a single-bit mark, there are only three values for use: SHM_RND, SHM_RDONLY, and 0. If the function runs successfully, a pointer to the first byte of the shared memory is returned; otherwise,-1 is returned;

When using this function, the second parameter usually uses a null pointer, which allows the system to select the location where the shared memory is connected to the process address space, in this case, the third parameter can be SHM_RDONLY or 0. If an address is specified for the second parameter, the third parameter must use SHM_RND.

Shmdt Function
int shmdt(const void *shm_addr)

This function is used to separate the shared memory from the address space of the process. After the separation, the process will not be able to use the shared memory;

The first parameter is an address, which is the pointer to the first byte of the shared memory, that is, the return value of the shmat function. If the function runs successfully, 0 is returned; otherwise,-1 is returned.
int shmctl(int shm_id, int cmd,struct shmid_ds *buf)

This function is used to connect the shared memory to the address space of the process so that the process can use the shared memory;

The first parameter is the identifier of the shared memory, which can be obtained through the shmget function. The second parameter is a command that indicates the operation on the shared memory. There are only three commands for use: IPC_STAT, IPC_SET and IPC_RMID; the third parameter is a struct pointer, which contains information such as the permission and owner of the shared memory. If the function runs successfully, 0 is returned; otherwise,-1 is returned;

We usually use this function to delete the shared memory. In this case, we need to assign IPC_RMID to the second parameter, which means to delete the shared memory. The third parameter can be a null pointer. The other two commands of the second parameter:

IPC_STAT indicates that the content in the third parameter is associated with the shared memory; IPC_SET indicates that the content in the third parameter is set to the value of shared memory.

The type of the third parameter, which we mentioned in the previous article, is similar to the SystemV IPC structure. Besides the required members, it also has its own unique members.

I found the type of the third parameter from the source code, detailed definitions are as follows :( IN THE linux-4.0.3/include/linux/shm. h file)

struct shmid_kernel /* private to the kernel */{               struct kern_ipc_perm    shm_perm;        struct file             *shm_file;        unsigned long           shm_nattch;        unsigned long           shm_segsz;        time_t                  shm_atim;        time_t                  shm_dtim;        time_t                  shm_ctim;        pid_t                   shm_cprid;        pid_t                   shm_lprid;        struct user_struct      *mlock_user;        /* The task created the shm object.  NULL if the task is dead. */        struct task_struct      *shm_creator;        struct list_head        shm_clist;      /* list by creator */};

For more information, see the example of using shared memory for inter-process communication. 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.