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、