Message Queue, semaphore, and shared memory for IPC communication, ipc queue

Source: Internet
Author: User
Tags epoll

Message Queue, semaphore, and shared memory for IPC communication, ipc queue

Three IPC types are called xsi ipc, namely message queue, semaphore, and shared memory. Xsi ipc comes from the IPC function of System V. Xsi ipc is often criticized for not using File System namespaces But constructing their own namespaces.

Similarities: the IPC structure in each kernel is referenced with a non-negative integer identifier. For example, to send or retrieve a message to a message queue, you only need to know its queue identifier. Unlike file identifiers, IPC identifiers are not small integers. When an IPC structure is created and deleted later, the identifiers related to this structure are continuously added with 1, until the maximum positive value of an integer is reached, and then the return value is 0.

The identifier is the internal name of the IPC object. To enable multiple cooperative processes to join on the same IPC object, an external name scheme is required. For this reason, the key is used, and each IPC object is connected to a key, so the key is used as the external name of the team.

Advantage and disadvantage: the IPC structure works within the system, and there is no access count. For example, if a process creates a message queue, a few messages are put in the queue and then terminated, but the message queue and its content are not deleted, they remain in the system until the following conditions occur: A process calls msgrcv or msgctl to read or delete a message queue, or a process executes the ipcrm command to delete a message queue, or the system that is restarting deletes the message queue. Compared with the MPs queue, the MPs queue is completely deleted when the process of the last MPs queue is terminated. For FIFO, although the name of the last referenced FIFO process is retained in the system until it is explicitly deleted, the data left in the FIFO is deleted at this time, therefore, it is in vain.
Another problem with xsi ipc is that these IPC structures have no names in the file system and we cannot use functions that use the file system to access them or modify their attributes. To support them, we have to add more than a dozen new system calls (msgget, semop, shmat ). we cannot use the ls command to see IPC objects, nor use the rm command to delete them, nor use the chmod command to modify their access permissions. So I had to add new commands ipcs and ipcrm.
Because these IPC are not using file descriptors, they cannot use multiplexing I/O functions: epoll/select/poll (this is a fatal injury, because many high-performance Server programming models use epoll, these three communication mechanisms can only be used in some simple scenarios ). This makes it difficult to use multiple IPC structures at a time, and to use the IPC structure in files or device I/O. For example, without some form of busy-waiting loop, a server process cannot wait for messages to be placed in either of the two message queues.
One of the other advantages of message queue is reliability, stream control, record-oriented, and non-advanced FIFO.

Related APIs:

Semaphores:

It is a counter used by multiple processes to access Shared Memory Data Objects.
# Include <sys/sem. h>
Int semget (key_t key, int num_sems, int sem_flgs );
Int semctl (int sem_id, int sem_num, int command ...);
Int semop (int sem_id, struct sembuf * sem_ops, size_t num_sem_ops );

Shared Memory:
Allows two or more processes to share a given storage zone. Generally, semaphores are used to synchronize shared storage access.
# Include <sys/shm. h>
Int shmget (key_t key, size_t size, int shmflag );
Void * shmat (int shm_id, const void * shm_addr, int shm_flag );
Int shmctl (int shm_id, int cmd, struct shmid_ds * buf );
Int shmdt (const void * shm_addr );

Message Queue:
Provides the method to send a data block from one process to another (generally, it is not recommended for new applications)
# Include <sys/msg. h>
Int msgget (key_t key, int msgflg );
Int msgctl (int magid, int cmd, struct msgid_ds * buf );
Int msgsnd (int msgid, void * msg_ptr, size_t msg_sz, int msgflag );
Int msgrcv (int msgid, void * msg_ptr, size_t msg_sz, long int msg_type, int msgflag );

Example

Print the locations where different types of data are stored

#include <stdio.h>#include <sys/shm.h>#defineARRAY_SIZE40000#defineMALLOC_SIZE100000#defineSHM_SIZE100000#defineSHM_MODE0600/* user read/write */chararray[ARRAY_SIZE];/* uninitialized data = bss */intmain(void){intshmid;char*ptr, *shmptr;printf("array[] from %p to %p\n", (void *)&array[0],  (void *)&array[ARRAY_SIZE]);printf("stack around %p\n", (void *)&shmid);if ((ptr = malloc(MALLOC_SIZE)) == NULL)printf("malloc error");printf("malloced from %p to %p\n", (void *)ptr,  (void *)ptr+MALLOC_SIZE);if ((shmid = shmget(IPC_PRIVATE, SHM_SIZE, SHM_MODE)) < 0)printf("shmget error");if ((shmptr = shmat(shmid, 0, 0)) == (void *)-1)printf("shmat error");printf("shared memory attached from %p to %p\n", (void *)shmptr,  (void *)shmptr+SHM_SIZE);if (shmctl(shmid, IPC_RMID, 0) < 0)printf("shmctl error");exit(0);}

The mmap function maps several parts of a file to the process address space. This concept is similar to connecting a shared bucket with the shmat function. The difference between the two is that the storage segments mapped by mmap are associated with files, but the XSI shared storage segments are not associated.

For more details, refer:

Linux inter-process communication-using shared memory

Linux inter-process communication-use Message Queue

Linux inter-process communication-use semaphores


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.