//CSE 660
//Jason Kim
//Lab 3

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include <signal.h>
#include <wait.h>
#include "sshm.h"
#include "ssem.h"

using namespace std;

//code of procZ

int main()
{
	int i;
	int full,mutex,empty;


	/* here create and initialize all semaphores */
	full = sem_create(1,0);
	mutex = sem_create(2,NULL);
	empty = sem_create(3,1);


	/* here created: shared memory array BufferA of size 20 */
	char *BufferA;
	shm_get(123,(void**)&BufferA,20*sizeof(char));

   	/* here created: shared memory array BufferB of size 30 */
	char *BufferB;
	shm_get(456,(void**)&BufferB,20*sizeof(char));


	/* synchronize with ProcX and ProcY Consumer Producer*/

	
	
	for(i=0;i<500;i++)
	{
		if(i%100==0)
		{
			sleep(1);
		}
		sem_wait(full); //wait(full)
		sem_wait(mutex);
		cout << BufferA << endl; //use buffer
		cout << BufferB << endl;
		sem_signal(mutex);//signal empty
		sem_signal(empty);
	}
 
	return 0;
}

