当前位置:   article > 正文

QNX Share Memory Sample code_qnx shmem

qnx shmem

Process A

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>
int main(int argc, char *argv[]) {
    int fd;
    unsigned char version[20];
    unsigned char * addr;


    /* Create a new memory object */
    fd = shm_open( "/version", O_RDONLY, 0777 );
    if( fd == -1 ) {
        printf("Open failed:%s\n","/version");
        return EXIT_FAILURE;
    }

    /* Map the memory object */
    addr = mmap( 0, 20,
            PROT_READ,
            MAP_SHARED, fd, 0 );
    if( addr == MAP_FAILED ) {
        printf("mmap failed\n");
        return EXIT_FAILURE;
    }

    printf( "Map addr is 0x%08x\n", addr);

    /* READ shared memory */
    memcpy(version, addr, 20);
    

    printf("version: %c%c%c%c%c%c\n", version[0], version[1], \
            version[2], version[3], version[4], version[5]);
    /*
     * The memory object remains in
     * the system after the close
     */
    close( fd );

    return EXIT_SUCCESS;
}


Process B:


#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>
int main(int argc, char *argv[]) {
    int fd;
    unsigned char version[20];
    unsigned char * addr;


    /* Create a new memory object */
    fd = shm_open( "/version", O_RDONLY, 0777 );
    if( fd == -1 ) {
        printf("Open failed:%s\n","/version");
        return EXIT_FAILURE;
    }

    /* Map the memory object */
    addr = mmap( 0, 20,
            PROT_READ,
            MAP_SHARED, fd, 0 );
    if( addr == MAP_FAILED ) {
        printf("mmap failed\n");
        return EXIT_FAILURE;
    }

    printf( "Map addr is 0x%08x\n", addr);

    /* READ shared memory */
    memcpy(version, addr, 20);

    version[19]='\0';


    printf("version: %c%c%c%c%c%c\n", version[0], version[1], \
            version[2], version[3], version[4], version[5]);

    printf("%s\n",version);
    /*
     * The memory object remains in
     * the system after the close
     */
    close( fd );

    return EXIT_SUCCESS;
}

运行结果

标题

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/964099
推荐阅读
相关标签
  

闽ICP备14008679号