Linux IPC------System V

Source: Internet
Author: User
Tags message queue semaphore

System V IPC refers to the three types of interprocess communication tools that are introduced in the system V.2 release:
(1) semaphore, which is used to manage access to shared resources; (2) shared memory, which can efficiently realize the data sharing between processes; (3) Message Queuing, which is used to implement inter-process data transfer.
we collectively refer to these three tools as System VIPC Objects, each object has a uniqueIPC Identifiers(identifier).
IPC Key        How to enable multiple processes to access a specific IPC object Span style= "Color:rgb (51,51,51); Font-weight:normal "> requires an IPC keyword ( IPC key The problem of convergence on an IPC object. When creating an IPC object, you need to specify a key value, type key_t. The kernel is responsible for converting the IPC keyword to the IPC identifier. a struct is defined in the Linux kernel:
<span style= "Font-weight:normal;" ><span style= "FONT-SIZE:14PX;" >struct ipc_perm{key_t key, keyword uid_t uid;/* Valid user ID */gid_t gid for Shared memory owner,/* Valid group id*/uid_t for the group to which the shared memory owner belongs; */* Shared memory Creator Effective user id*/gid_t Cgid; /* Valid group for the group to which the creator of the shared memory belongs id*/unsigned short mode; /* Permissions + shm_dest and shm_locked logo */unsignedshort seq; /* Serial number */};</span></span>
key_t key is the IPC key, which is the key to identify this shared channel. Also called the key.
1., IPC Key (IPC key)key_t This data type is defined in <sys/types.h>, and is usually a 32-bit integer at least. We usually use the Ftok () function (which we can remember: file to key) converts an existing pathname and an integer identifier into a key_t value called the IPC key. There are three ways to choose the IPC keyword:
a) Ipc_private. The kernel is responsible for selecting a keyword and then generating an IPC object and passing the IPC identifier directly to another process. b) Select a keyword directly. c) Use the Ftok () function to generate a keyword.
2. The Ftok prototype is as follows:
key_t Ftok (char * fname, int id)

Header file:

#include <sys/types.h>  #include <sys/ipc.h>  
Parameters:
fname The file name you specify (the file must be present and accessible), the ID is a sub-ordinal, although it is an int, but only 8 bits are used (0-255).
return Value:
When executed successfully, a key_t value is returned, otherwise-1 is returned.

System V IPC Object Summary

1. Signal Volume (semaphores)
The set of signal volumes for System v represents a collection of one or more semaphores. The kernel maintains a SEMID_DS data structure for each semaphore set, and each semaphore in the semaphore set is represented by a nameless struct that contains at least the following members:
struct{
Unsigned short semval;//semaphore value, always >=0
pid_t Sempid; PID of the last operation
...
};
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

< br> (1) Create or access semaphore
* int semget (key_t key,int nsems,int flag); The
Nsems specifies the number of semaphores in the semaphore set, and if you just get the identifier of the semaphore sets (rather than the new ones), then Nsems can be 0. The low 9 bits of flag are the access bits of the semaphore, similar to the access rights of the file, and if Ipc_creat and IPC_EXCL are specified in flag, the function returns a eexist error if the key is already associated with an existing IPC object. For example, flag can be ipc_creat|0666.

(2) control semaphore set
* int semctl (int semid,int semnum,int cmd,union semun arg);
Performs a CMD operation on the Semid semaphore set; the two values that cmd commonly use are: Setval initializes the value of the Semnum semaphore to arg.val;ipc_rmid delete the semaphore.

(3) operates on one or more semaphores
* int semop (int semid,struct sembuf *sops,unsigned nsops);
* struct sembuf{
unsigned short sem_num;//Semaphore index
Short sem_op;//operation on semaphore, two commonly used values are-1 and +1, respectively, for P, v Operation
Short sem_ Flag The more important value is Sem_undo: When the process ends, the corresponding action is canceled, and
} is automatically freed if the resource is not freed when the process ends;

2, shared memory
Shared memory allows two or more processes to share a certain store because no data is required to be copied. So this is the fastest kind of IPC.
#include <sys/ipc.h>
#include <sys/shm.h>


(1) Create or access shared memory
* int Shmget (key_t key,size_t size,int shmflg);

(2) Append shared memory to the address space of the process
* void *shmat (int shmid,const void *shmaddr,int shmflg),//shmaddr is usually null, the system chooses the address to which the shared memory is attached; SHMFLG can detach shared memory
* int SHMDT (const void *SHMADDR) from the address space of the process for Shm_rdonly

(3),//SHMADDR is the return value of the Shmat () function

(4 ) Control shared Memory
* int shmctl (int shmid,int cmd,struct shmid_ds *buf);
* struct shmid_ds{
struct ipc_perm shm_perm;
...
}; Common values for
cmd are: (a) Ipc_stat gets the SHMID_DS structure of the current shared memory and saves it in BUF (2) Ipc_set use the value in BUF to set the SHMID_DS structure of the current shared memory (3) Ipc_rmid Delete the current shared memory

3, Message Queuing
Message Queuing is stored in the kernel and is a linked list of messages.
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>


(1) Create or access Message Queuing
* int Msgget (key_t key,int MSGFLG);

(2) Manipulating Message Queuing
* int msgsnd (int msqid,const void *msg,size_t nbytes,int MSGFLG);
The struct that the MSG points to must start with a long int member, as a message type of MSGRCV () and must be greater than 0. Nbytes refers to the size of the MSG pointing to the struct, but not the size of the long int part
* ssize_t MSGRCV (int msqid,void *msg,size_t nbytes,long msgtype,int msgflg);
If Msgtype is 0, the first message in the message queue is returned, and if it is a positive integer, the first message of that type in the queue is returned, and if it is a negative number, the first message with the lowest value in the queue is returned, and the minimum value is less than or equal to the absolute value of Msgtype.

(3) Control Message Queuing
* int msgctl (int msqid,int cmd,struct msqid_ds *buf);
* struct msqid_ds{
struct Ipc_perm msg_perm;
...
};


4. Summary

(1) Use the Semget ()/shmget ()/msgget () function to create or access IPC objects according to the IPC keyword key and one flag flag. If key is ipc_private, or key is not already associated with an existing IPC object and the flag contains the IPC_CREAT flag, a completely new IPC object is created.

(2) Use the Semctl ()/shmctl ()/msgctl () function to modify the properties of the IPC object.

(3) Destroy the IPC instance using the Semctl ()/shmctl ()/msgctl () function and the IPC_RMID flag.





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux IPC------System V

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.