Rate This Document
Findability
Accuracy
Completeness
Readability

kupl_shm_attach

Allocate memory and map the allocated memory to the memory of another process for shared memory.

Interface Definition

void* kupl_shm_attach(struct kupl_shm_addr_t addr, size_t size)

Parameters

Table 1 Parameter definition

Parameter

Type

Description

Input/Output

addr

struct kupl_shm_addr_t

Information about the memory to be mapped.

Input

size

size_t

Size of the memory to be mapped.

Input

Table 2 kupl_shm_addr_t structure (memory mapping information)

Parameter

Type

Description

Input/Output

src_addr

void*

Virtual address of the memory to be mapped.

Input

src_pid

int

ID of the process to which the memory to be mapped belongs.

Input

dst_pid

int

ID of the current process.

Input

Return Value

  • Success: pointer to the memory allocated and mapped by the current process.
  • Failure: nullptr

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
Process A
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "kupl.h"
#define SHM_NAME "/pid_shm_example"
typedef struct {
    pid_t pid;
    int ready;
    void* addr;
} shared_data_t;
int main() {
    // Create a shared memory object and fill in data.
    int shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0666);
    ftruncate(shm_fd, sizeof(shared_data_t);
    shared_data_t *shared_data = mmap(NULL, sizeof(shared_data_t),
                                      PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);         
    // Write the PID to the shared memory.
    shared_data->pid = getpid();
    printf("Process A: PID %d\n", shared_data->pid);
    shared_data->ready = 1;
    shared_data->addr = kupl_malloc(KUPL_MEM_DEFAULT, sizeof(shared_data_t));
    memset(shared_data->addr, 0, sizeof(shared_data_t));
    data_addr->pid = getpid();
    // Keep running and wait for process B to read the data.
    printf("Press Enter to exit...\n");
    getchar();
    // Clear data.
    kupl_free(KUPL_MEM_DEFAULT, shared_data->addr);
    munmap(shared_data, sizeof(shared_data_t));
    shm_unlink(SHM_NAME);
    return 0;
}

Process B
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "kupl.h"
#define SHM_NAME "/pid_shm_example"
typedef struct {
    pid_t pid;
    int ready;
    void* addr;
} shared_data_t;
int main() {
    // Open the shared memory object.
    int shm_fd = shm_open(SHM_NAME, O_RDONLY, 0666);
    shared_data_t *shared_data = mmap(NULL, sizeof(shared_data_t),
                                     PROT_READ, MAP_SHARED, shm_fd, 0);
    shared_data_t *attach_data = (shared_data_t*)calloc(1, sizeof(shared_data_t));
    // Wait until the data is ready.
    while (shared_data->ready == 0) {
        usleep(100000); // Wait for 100 ms.
    }
    // Read information.
    kupl_shm_addr_t kupl_shm_addr_info;
    kupl_shm_addr_info.src_addr = shared_data->addr;
    kupl_shm_addr_info.src_pid  = shared_data->pid;
    kupl_shm_addr_info.dst_pid  = getpid();
    // Perform the mapping.
    attach_data->addr = kupl_shm_attach(kupl_shm_addr_info, sizeof(shared_data_t));
    if (attach_data->addr == NULL) {
        printf("attach wrong!\n");
    } else {
        printf("attach OK!");
    }
    // Wait until the data is ready.
    shared_data_t* data_addr = (shared_data_t*)attach_data->addr;
    while (data_addr->ready != 3) {
        usleep(100000); // Wait for 100 ms.
    }
    printf("Process B: Read PID %d from shared memory\n", data_addr->pid);
    // Clear data.
    munmap(shared_data, sizeof(shared_data_t));
    kupl_shm_detach(attach_data->addr);
    free(attach_data);
    close(shm_fd);
    return 0;
}

The execution result is as follows:

Process A
Process A: PID xxxx
Press Enter to exit...

Process B
attach OK!
Process B: Read PID xxxx from shared memory // The PID must be the same as that printed by process A.

The preceding example demonstrates how to use kupl_shm_attach to allocate memory and map the allocated memory to the memory of another process, cancel the mapping, and release the allocated memory. After processes A and B are executed in sequence, process A allocates memory and writes data. After the data is written, process A keeps running and waits for process B to read data. After any key is pressed, process A exits. After process B is executed, kupl_shm_attach is called to attempt memory mapping. After the memory is mapped successfully, process B attempts to read the content written by process A. The read content should be the PID of process A. After the reading is complete, process B unbinds using detach and terminates.