13_04__shmReadZ

ZC: Unix网络编程第2版 第2卷 第13章

 

1、mainReadZ.cpp

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>

#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

int main(int _argc, char** _argv)
{
    printf("*** shmRead *** Z ***\n");
    int i,fd;
    struct stat stat01;
    unsigned char c, *ptr;

    if (_argc != 2)
    {
        printf("usage : shmread <name>\n");
        exit(0);
    }
    fd = shm_open(_argv[1], O_RDWR, FILE_MODE);
    fstat(fd, &stat01);
    ptr = (unsigned char *)mmap(NULL, stat01.st_size, PROT_READ, MAP_SHARED, fd, 0);
    close(fd);

    for (i=0; i<stat01.st_size; i++)
        if ( (c=*ptr++) != (i % 256) )
            printf("ptr[%d] = %d\n", i, c);

    exit(0);
    return 0;
}

 

2、mainReadZ.pro

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += \
    mainReadZ.cpp

LIBS += -lrt

include(deployment.pri)
qtcAddDeployment()

 

3、

 

posted @ 2016-05-03 16:17  LinuxCode  阅读(124)  评论(0编辑  收藏  举报