13_06_server2_client2

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

 

1、mainServer2.cpp

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

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

#include <semaphore.h>

#define    MESGSIZE    256        /* max #bytes per message, incl. null at end */
#define    NMESG         16        /* max #messages */

struct shmstruct {        /* struct stored in shared memory */
  sem_t    mutex;            /* three Posix memory-based semaphores */
  sem_t    nempty;
  sem_t    nstored;
  int    nput;            /* index into msgoff[] for next put */
  long    noverflow;        /* #overflows by senders */
  sem_t    noverflowmutex;    /* mutex for noverflow counter */
  long    msgoff[NMESG];    /* offset in shared memory of each message */
  char    msgdata[NMESG * MESGSIZE];    /* the actual messages */
};

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


int main(int argc, char **argv)
{
    int        fd, index, lastnoverflow, temp;
    long    offset;
    struct shmstruct    *ptr;

    if (argc != 2)
    {
        printf("usage: server2 <name>\n");
        exit(0);
    }

        /* 4create shm, set its size, map it, close descriptor */
    shm_unlink(argv[1]);        /* OK if this fails */
    fd = shm_open(argv[1], O_RDWR | O_CREAT | O_EXCL, FILE_MODE);
    ptr = (struct shmstruct    *)mmap(NULL, sizeof(struct shmstruct), PROT_READ | PROT_WRITE,
               MAP_SHARED, fd, 0);
    ftruncate(fd, sizeof(struct shmstruct));
    close(fd);

        /* 4initialize the array of offsets */
    for (index = 0; index < NMESG; index++)
        ptr->msgoff[index] = index * MESGSIZE;

        /* 4initialize the semaphores in shared memory */
    sem_init(&ptr->mutex, 1, 1);
    sem_init(&ptr->nempty, 1, NMESG);
    sem_init(&ptr->nstored, 1, 0);
    sem_init(&ptr->noverflowmutex, 1, 1);

        /* 4this program is the consumer */
    index = 0;
    lastnoverflow = 0;
    for ( ; ; ) {
        sem_wait(&ptr->nstored);
        sem_wait(&ptr->mutex);
        offset = ptr->msgoff[index];
        printf("index = %d: %s\n", index, &ptr->msgdata[offset]);
        if (++index >= NMESG)
            index = 0;                /* circular buffer */
        sem_post(&ptr->mutex);
        sem_post(&ptr->nempty);

        sem_wait(&ptr->noverflowmutex);
        temp = ptr->noverflow;        /* don't printf while mutex held */
        sem_post(&ptr->noverflowmutex);
        if (temp != lastnoverflow) {
            printf("noverflow = %d\n", temp);
            lastnoverflow = temp;
        }
    }

    exit(0);
}

 

2、mainClient2.cpp

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

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

#include <semaphore.h>
#include <string.h>

#define    MESGSIZE    256        /* max #bytes per message, incl. null at end */
#define    NMESG         16        /* max #messages */

struct shmstruct {        /* struct stored in shared memory */
  sem_t    mutex;            /* three Posix memory-based semaphores */
  sem_t    nempty;
  sem_t    nstored;
  int    nput;            /* index into msgoff[] for next put */
  long    noverflow;        /* #overflows by senders */
  sem_t    noverflowmutex;    /* mutex for noverflow counter */
  long    msgoff[NMESG];    /* offset in shared memory of each message */
  char    msgdata[NMESG * MESGSIZE];    /* the actual messages */
};

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


int main(int argc, char **argv)
{
    int        fd, i, nloop, nusec;
    pid_t    pid;
    char    mesg[MESGSIZE];
    long    offset;
    struct shmstruct    *ptr;

    if (argc != 4)
    {
        printf("usage: client2 <name> <#loops> <#usec>\n");
        exit(0);
    }
    nloop = atoi(argv[2]);
    nusec = atoi(argv[3]); // microsecond

        /* 4open and map shared memory that server must create */
    fd = shm_open(argv[1], O_RDWR, FILE_MODE);
    ptr = (struct shmstruct *)mmap(NULL, sizeof(struct shmstruct), PROT_READ | PROT_WRITE,
               MAP_SHARED, fd, 0);
    close(fd);

    pid = getpid();
    for (i = 0; i < nloop; i++) {
        usleep(nusec);
        snprintf(mesg, MESGSIZE, "pid %ld: message %d", (long) pid, i);

        if (sem_trywait(&ptr->nempty) == -1) {
            if (errno == EAGAIN) {
                sem_wait(&ptr->noverflowmutex);
                ptr->noverflow++;
                sem_post(&ptr->noverflowmutex);
                continue;
            } else
                printf("sem_trywait error\n");
        }
        sem_wait(&ptr->mutex);
        offset = ptr->msgoff[ptr->nput];
        if (++(ptr->nput) >= NMESG)
            ptr->nput = 0;        /* circular buffer */
        sem_post(&ptr->mutex);
        strcpy(&ptr->msgdata[offset], mesg);
        sem_post(&ptr->nstored);
    }
    exit(0);
}

 

3、

.pro 文件里面都要加上 "LIBS += -lrt"

 

4、

5、

 

转载于:https://www.cnblogs.com/LinuxCode/p/5455441.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值