#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include “smr.h”

#define REGION_NAME “anony”
#define REGION_ADDR 0x001000
#define REGION_SIZE 0x100000
#define CHUNK_SIZE 4096

typedef struct {
    uint8_t* addr;
    uint32_t size;
} chunk_t;

int main() {
    smr_region region = {
        .region_name = REGION_NAME,
        .addr = REGION_ADDR,
        .len = REGION_SIZE
    };

    smr_attr smr_attr = {
        .cur_regions = &region,
        .num = 1,
        .alloc_name = “chunk”
    };

    uint32_t chunk_align = 4;
    ot_smr_alloc_attr alloc_attr = {
        .region_name = REGION_NAME,
        .len = CHUNK_SIZE,
        .align = chunk_align,
    };

    // initialize SMR
    ot_s32 ret = ot_smr_init(&smr_attr);
    if (ret != 0) {
        printf(“SMR init error[%d]!\n”, ret);
        return -1;
    }

    // allocate memory for chunk
    chunk_t chunk;
    chunk.size = CHUNK_SIZE;

    alloc_attr.len = CHUNK_SIZE;
    alloc_attr.region_name = REGION_NAME;
    alloc_attr.align = chunk_align;
    ret = ot_smr_alloc(&alloc_attr, (phys_addr*)&chunk.addr);
    if (ret != 0) {
        printf(“SMR alloc error[%d]!\n”, ret);
        ot_smr_deinit();
        return -1;
    }

    printf(“Chunk allocated at physical address 0x%08X with size %d bytes\n”, chunk.addr, chunk.size);

    // create child process
    pid_t child_pid = fork();
    if (child_pid < 0) {
        printf(“Failed to create child process!\n”);
        ot_smr_munmap(chunk.addr, CHUNK_SIZE);
        ot_smr_deinit();
        return -1;
    } else if (child_pid == 0) { // child process
        // receive physical address from parent process
        uint8_t* phys_addr;
        read(STDIN_FILENO, &phys_addr, sizeof(phys_addr));

        // map physical address to virtual address
        void* vaddr = mmap(NULL, CHUNK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(stdin), 0);
        if (vaddr == MAP_FAILED) {
            printf(“Failed to map physical address to virtual address!\n”);
            return -1;
        }

        printf(“Child process: Chunk mapped to virtual address 0x%08X\n”, vaddr);

        // add reference count to chunk
        ret = ot_smr_add_ref(vaddr);
        if (ret != 0) {
            printf(“SMR add reference error[%d]!\n”, ret);
            return -1;
        }

        // wait for parent process to terminate
        sleep(5);

        // remove chunk
        munmap(vaddr, CHUNK_SIZE);
    } else { // parent process
        // map physical address to virtual address
        void* vaddr = mmap(NULL, CHUNK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(stdin), 0);
        if (vaddr == MAP_FAILED) {
            printf(“Failed to map physical address to virtual address!\n”);
            return -1;
        }

        printf(“Parent process: Chunk mapped to virtual address 0x%08X\n”, vaddr);

        // add reference count to chunk
        ret = ot_smr_add_ref(vaddr);
        if (ret != 0) {
            printf(“SMR add reference error[%d]!\n”, ret);
            munmap(vaddr, CHUNK_SIZE);
            ot_smr_munmap(chunk.addr, CHUNK_SIZE);
            ot_smr_deinit();
            return -1;
        }

        // send physical address to child process
        write(STDOUT_FILENO, &chunk.addr, sizeof(chunk.addr));

        // wait for child process to terminate
        int status;
        wait(&status);

        // remove chunk
        munmap(vaddr, CHUNK_SIZE);
        ot_smr_munmap(chunk.addr, CHUNK_SIZE);
        ot_smr_deinit();
    }

    return 0;
}从 pid_t child_pid = fork();这一行开始解释一下每一行代码的意思