/*
 * prod.c
 * 
 * Copyright 2025 grchere <grchere@grci3>
 * 
 * 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., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * Proceso productor con espera activa y exclusion mutua en 
 * incremento/decremento de variable contador
 * 
 * $ gcc -Wall -o prod prod.c operIPC.c -I .
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <string.h>
#include <operIPC.h>

int main(int argc, char **argv) {
	
	int shmid1 = shmget(0xa,0,0);
	int shmid2 = shmget(0xb,0,0);
	int semid = semget(0xa,0,0);
	char *pbuffer = (char *) shmat(shmid1,0,0); 
	int *pcontador = (int *) shmat(shmid2,0,0);
	int in = 0;
	char letra = 'a';
	while(1) {
		while(*pcontador == BUFFERSIZE);
		*(pbuffer+in) = letra;
		P(semid,0);
			(*pcontador)++;
		V(semid,0);
		in = (in + 1) % BUFFERSIZE;
		letra++;
		if ( letra > 'z' ) letra = 'a';
	}
	// este codigo no se ejecuta
	// detach de la memoria compartida
	return 0;
}

