Home  >  Article  >  Backend Development  >  Using shared memory and message queues in C++

Using shared memory and message queues in C++

WBOY
WBOYOriginal
2023-08-22 16:21:441265browse

Using shared memory and message queues in C++

In C, shared memory and message queue are two commonly used inter-process communication methods. They can help us share data and information between different processes, allowing for more efficient programming.

Shared memory is a special memory area that can be shared by multiple processes. Using shared memory avoids the overhead of copying data and reduces the delay in transferring data between processes.

To use shared memory in C, you need to include the header file and use the shmget, shmat, shmdt, and shmctl functions to operate. The following is a simple shared memory example:

#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int shmid;
    char *shmaddr;

    shmid = shmget((key_t)1234, sizeof(char)*100, 0666 | IPC_CREAT);
    if(shmid == -1){
        perror("shmget failed");
        exit(EXIT_FAILURE);
    }

    shmaddr = (char*) shmat(shmid, (void*)0, 0);
    if(shmaddr == (char*)-1){
        perror("shmat failed");
        exit(EXIT_FAILURE);
    }

    sprintf(shmaddr, "Hello shared memory!");

    printf("Message is written in shared memory: %s
", shmaddr);

    shmdt(shmaddr);

    return 0;
}

In the above example, we use the shmget function to create shared memory, and the shmat function connects the shared memory to the address space of the current process, and then can use it like a normal variable. its operation. Finally, use the shmdt function to disconnect from the shared memory. The shmctl function can be used to control the behavior of the shared memory.

Message queue is an inter-process communication mechanism that can deliver messages between processes. Its advantage is that it can transmit messages asynchronously, unlike shared memory that requires locking to ensure data synchronization. To use the message queue in C, you need to include the header file and use the msgget, msgsnd, msgrcv, and msgctl functions to operate. The following is a simple message queue example:

#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct{
    long msg_type;
    char msg_text[100];
} msg_buf;

int main()
{
    int msgid;
    msg_buf msg;

    msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
    if(msgid == -1){
        perror("msgget failed");
        exit(EXIT_FAILURE);
    }

    msg.msg_type = 1;
    strcpy(msg.msg_text, "Hello message queue!");

    if(msgsnd(msgid, (void*)&msg, sizeof(msg_buf), 0) == -1){
        perror("msgsnd failed");
        exit(EXIT_FAILURE);
    }

    memset(&msg, 0, sizeof(msg_buf));

    if(msgrcv(msgid, (void*)&msg, sizeof(msg_buf), 0, 0) == -1){
        perror("msgrcv failed");
        exit(EXIT_FAILURE);
    }

    printf("Message received from message queue: %s
", msg.msg_text);

    if(msgctl(msgid, IPC_RMID, 0) == -1){
        perror("msgctl failed");
        exit(EXIT_FAILURE);
    }

    return 0;
}

In the above example, we use the msgget function to create a message queue, the msgrcv function to receive messages, and the msgsnd function to send messages. The msg_buf structure is used to define the type and content of the message. Finally, use the msgctl function to delete the message queue.

In general, shared memory and message queues are very practical inter-process communication methods. Using these techniques in multi-process and multi-threaded applications can increase program concurrency and efficiency and reduce complex synchronization and asynchronous issues.

The above is the detailed content of Using shared memory and message queues in C++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn