/***************************************************************************
 *   Copyright (C) 2006 by Brian Lauber   *
 *   bml8@case.edu   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include <unistd.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/sem.h>

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

#include "blError.h"
#include "shared.h"


/*******************************************
Wait simply encapsulates the call to semop.
The first line is only evaulated at compile
time.  It creates a static sumbuf structure
containing the semaphore command "reduce the
semaphore count by 1".  At runtime, the
semaphore group and offset are adjusted to
Wait on the appropriate semaphore
*******************************************/
void Wait(const key_t group, const int offset)
{
  static struct sembuf acquire = {0, -1, 0};

  acquire.sem_num = offset;
  Error(   semop(group, &acquire, 1)  );
};


/*******************************************
Signal is almost identical to Wait, except
that the sumbuf structure is preformatted to
increase the semaphore count.
*******************************************/
void Signal(const key_t group, const int offset)
{
  static struct sembuf release = {0, 1, 0};

  release.sem_num = offset;
  Error(   semop(group, &release, 1)   );
}


union semun
{
  int              val;
  struct semid_ds *buf;
  unsigned short  *array;
};

enum MySems {JAR = 0, REFILL};

int main(int argc, char *argv[])
{
  int M;
  int S;

  unsigned short initSems[] = {1, 0};
  union semun evilThings;

  int i;
  int nextCookies;
  struct mem *myMem;
  M = Error(shmget(ftok(".", 's'), sizeof(struct mem), IPC_CREAT | 0666 ));
  S = Error(semget(ftok(".", 's'), 2, IPC_CREAT | 0666));

  evilThings.array = initSems;
  Error(semctl(S, 0, SETALL, evilThings  ) );

  myMem = (struct mem*) Error(shmat(M, 0, 0));

  myMem->cookies = 10;

  printf("There are %i cookies!\n", myMem->cookies);

  if(Error(fork()) == 0) // The child process!!
  {

    for(i = 0; i < 5; ++i)
    {
      Wait(S, JAR);
      printf("Child: eats a cookie\n");
      nextCookies = myMem->cookies - 1;
      sleep(1);
      myMem->cookies = nextCookies;
      printf("Child: There are %i cookies left\n", myMem->cookies);
      Signal(S, JAR);
    }
    exit(EXIT_SUCCESS);
  }
  else  // The parent process!!
  {
    for(i = 0; i < 5; ++i)
    {
      Wait(S, JAR);
      sleep(1);
      printf("Padre: eats a cookie\n");
      nextCookies = myMem->cookies - 1;
      sleep(1);
      myMem->cookies = nextCookies;
      printf("Padre: There are %i cookies left\n", myMem->cookies);
      Signal(S, JAR);
    }

    Error(wait(NULL));
  }

  Error(semctl(S, 0, IPC_RMID, NULL));
  Error(shmctl(M, IPC_RMID, NULL));
  return EXIT_SUCCESS;
}
