13_01__shmCreateZ

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

 

1、mainCreateZ.cpp

//#include <iostream>
//using namespace std;

#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("*** shmCreate *** Z ***\n");

    int c, fd, flags;
    char *ptr;
    off_t length;

    flags = O_RDWR | O_CREAT;
    while( (c=getopt(_argc, _argv, "e")) != -1 )
    {
        switch(c)
        {
        case 'e':
            flags |= O_EXCL;
            break;
        }
    }
    if (optind != _argc-2)
    {
        printf("usage : shmcreate [-e] <name> <length>");
        exit(0);
    }

    length = atol(_argv[optind + 1]);

    fd = shm_open(_argv[optind], flags, FILE_MODE);
    ftruncate(fd, length);

    ptr = (char*)mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

    exit(0);
    return 0;
}

 

2、mainCreateZ.pro

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

SOURCES += \
    mainCreateZ.cpp

LIBS += -lrt

include(deployment.pri)
qtcAddDeployment()

 

3、

 

posted @ 2016-05-03 16:13  LinuxCode  阅读(263)  评论(0)    收藏  举报