/*
 * pipe01.c
 * 
 * Copyright 2025 osboxes <osboxes@osboxes>
 * 
 * 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.
 * 
 * 
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>

// matriz con totales de ventas
double totales[4][12];

void cargoVentas(double *,int);
void muestroVentas();
void cierro(int[]);

void sigusr1(int);

int main(int argc, char **argv)
{
	signal(SIGUSR1,sigusr1);
	printf("(%d) soy proceso padre!\n",getpid());
	// lleno la matriz con totales de ventas
	cargoVentas(&totales[0][0],4*12);
	muestroVentas();
	int p1[2],p2[2],p3[2],p4[2];
	int rp1 = pipe(p1);
	int rp2 = pipe(p2);
	int rp3 = pipe(p3);
	int rp4 = pipe(p4);
	printf("Pipes creados rp1=%d rp2=%d rp3=%d rp4=%d\n",rp1,rp2,rp3,rp4);
	pid_t pids[4];
	int i;
	for(i=0;i<4;i++) {
		pid_t pid = fork();
		if ( pid == 0 ) {
			// proceso hijo:
			int suc=0,mes,nb=0;
			double total=0.0;
			printf("(%d): i=%d voy a leer del pipe!\n",getpid(),i);
			switch(i) {
				case 0:
					cierro(p2);cierro(p3);cierro(p4);
					printf("(%d): i=%d voy a leer del pipe!\n",getpid(),i);
					nb = read(p1[0],&suc,sizeof(int));
					close(p1[0]);
					break;
				case 1:
					cierro(p1);cierro(p3);cierro(p4);
					printf("(%d): i=%d voy a leer del pipe!\n",getpid(),i);
					nb = read(p2[0],&suc,sizeof(int));
					close(p2[0]);
					break;
				case 2:
					cierro(p1);cierro(p2);cierro(p4);
					printf("(%d): i=%d voy a leer del pipe!\n",getpid(),i);
					nb = read(p3[0],&suc,sizeof(int));
					close(p3[0]);
					break;
				case 3:
					cierro(p1);cierro(p2);cierro(p3);
					printf("(%d): i=%d voy a leer del pipe!\n",getpid(),i);
					nb = read(p4[0],&suc,sizeof(int));
					close(p4[0]);
					break;
				default:
					printf("Valor erroneo de pipe! %d\n",i);
					suc = -1;
					break;
			}
			printf("(%d): nb=%d suc=%d\n",getpid(),nb,suc);
			if ( suc != -1 ) {
				nb=0;
				for(mes=0;mes<12;mes++)
					total+=totales[suc][mes];
				switch(suc) {
					case 0:
						nb = write(p1[1],&total,sizeof(double));
						close(p1[1]);
						break;
					case 1:
						nb = write(p2[1],&total,sizeof(double));
						close(p2[1]);
						break;
					case 2:
						nb = write(p3[1],&total,sizeof(double));
						close(p3[1]);
						break;
					case 3:
						nb = write(p4[1],&total,sizeof(double));
						close(p4[1]);
						break;
					default:
						printf("Valor erroneo de suc! %d\n",suc);
						break;
				}
				printf("(%d): grabe nb=%d bytes en pipe!\n",getpid(),nb);
			}
			printf("(%d) retorno rc=%d\n",getpid(),i);
			exit(i);
			break;
		} else {
			pids[i] = pid;
		}
	}
	printf("(%d) voy a escribir pipes\n",getpid());
	int nbytes[4];
	memset(&nbytes[0],0,sizeof(int)*4);
	i=0;nbytes[0]=write(p1[1],&i,sizeof(int));
	i=1;nbytes[1]=write(p2[1],&i,sizeof(int));
	i=2;nbytes[2]=write(p3[1],&i,sizeof(int));
	i=3;nbytes[3]=write(p4[1],&i,sizeof(int));
	//close(p1[1]);close(p2[1]);close(p3[1]);close(p4[1]);
	printf("(%d) escribi pipes %d %d %d %d\n",getpid(),
		nbytes[0],nbytes[1],nbytes[2],nbytes[3]);
	
	double t[4];
	memset(&t[0],0,sizeof(double)*4);
	memset(&nbytes[0],0,sizeof(int)*4);
	printf("(%d): envie signal SIGUSR1\n",getpid());
	pause();
	printf("(%d) voy a leer de los pipes\n",getpid());
	nbytes[0]=read(p1[0],&t[0],sizeof(double));
	nbytes[1]=read(p2[0],&t[1],sizeof(double));
	nbytes[2]=read(p3[0],&t[2],sizeof(double));
	nbytes[3]=read(p4[0],&t[3],sizeof(double));
	//close(p1[0]);close(p2[0]);close(p3[0]);close(p4[0]);
	printf("(%d) lei pipes %d %d %d %d\n",getpid(),
		nbytes[0],nbytes[1],nbytes[2],nbytes[3]);	
	int s,m;
	double tot;
	for(s=0;s<4;s++) {
		tot=0.0;
		for(m=0;m<12;m++) {
			tot+=totales[s][m];
		}
		printf("Total Suc %d (sumo padre) tot=%lf (sumo hijo) t1=%lf\n",
			s,tot,t[s]);
	}
	//waitpid por hijos
	int status=0;
	pid_t p;
	for(i=0;i<4;i++)
		if ( (p = waitpid(pids[i],&status,0)) > 0 )
			printf("pid %d retorno %d\n",p,status/256);

	cierro(p1);cierro(p2);cierro(p3);cierro(p4);
	return 0;
}

void cargoVentas(double *p,int n) {
	srand(time(NULL));
	int i;
	for(i=0;i<n;i++,p++) *p = (double) (rand()/100000.0);
}

void muestroVentas() {
	int s,m;
	for(s=0;s<4;s++) {
		printf("%4d ",s);
		for(m=0;m<12;m++) {
			printf("%10.2lf",totales[s][m]);
		}
		printf("\n");
	}
}

void cierro(int p[]) {
	close(p[0]);close(p[1]);
}

void sigusr1(int signo) {
	printf("(%d) recibi signal(%d)\n",getpid(),signo);
}
