//#include <sys/types.h>
//#include <sys/ipc.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.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) 
    {
    fprintf(stderr,"usage:incr3 <pathname> <#loops>");
    exit(1);
    }
  nloop= atoi(argv[2]); 
  /* open file, initialize to 0, map into memory */ 
  fd =open(argv[1], O_RDWR | O_CREAT, 0x666); 
  write(fd,&shared, sizeof(struct shared)); 
  ptr=mmap(NULL, sizeof(struct shared), PROT_READ | PROT_WRITE, 
           MAP_SHARED,fd, 0); 
  close(fd); 
  /* initialize semaphore that is shared between processes */ 
  sem_init(&ptr->mutex,1, 1); 
  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); 
  } 
  /* parent */ 
  for(i = 0; i < nloop; i++) { 
    sem_wait(&ptr->mutex); 
    printf("parent:%d\n", ptr->count++); 
    sem_post(&ptr->mutex); 
  } 
  exit(0); 
} 

