#include "unpipc.h" struct shared { sem_t mutex; /* the mutex: a Posix memory-based semaphore */ int count; /* and the counter */ } shared; int main(int argc, char **argv) { int fd, i, nloop; struct shared *ptr; if (argc != 3) err_quit("usage: incr3 <#loops>"); nloop = atoi(argv[2]); /* 4open file, initialize to 0, map into memory */ fd = Open(argv[1], O_RDWR | O_CREAT, FILE_MODE); Write(fd, &shared, sizeof(struct shared)); fprintf(stderr, "before ptr-->%x\n", ptr); ptr = Mmap(NULL, sizeof(struct shared), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); Close(fd); fprintf(stderr, "after ptr-->%x\n", ptr); /* 4initialize semaphore that is shared between processes */ Sem_init(&ptr->mutex, 0, 0); fprintf(stderr, "---- 5 ----\n"); setbuf(stdout, NULL); /* stdout is unbuffered */ if (Fork() == 0) { /* child */ for (i = 0; i < nloop; i++) { Sem_wait(&ptr->mutex); printf("child: %d\n", ptr->count++); Sem_post(&ptr->mutex); } exit(0); } /* 4parent */ for (i = 0; i < nloop; i++) { Sem_wait(&ptr->mutex); printf("parent: %d\n", ptr->count++); Sem_post(&ptr->mutex); } Sem_destroy(&ptr->mutex); exit(0); }