#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>

#define LLEGIR 0
#define ESCRIU 1

/*Carles Pina i Estany  -  carles@pinux.info
 * Febrer 2004*/


/*exempe PIPE -AMB UN "ERROR"*/

void fill (int p[2]);


int main () {
	int p[2];
	pid_t pid;
	char buf[100];
	int status;
	int ret;

	pipe(p);

	pid=fork();

	if (pid==0) {
		/*Sóc el fill*/
		fill (p);
	}
	/*Sóc el pare*/
	close (p[LLEGIR]);

	strcpy(buf,"Hola Bon Dia\n");

	printf("P: Vaig a escriure a fill\n");
	ret=write (p[ESCRIU],buf,strlen(buf)+1);

	strcpy(buf,"Hola Bon Dia2\n");
	printf("P: Vaig a escriure a fill\n");
	ret=write (p[ESCRIU],buf,strlen(buf)+1);

	
	status=0;
	printf("P: Estic esperant a fill\n");
	wait (&status);

	return (0);	
}

void fill (int p[2]) {
	char buf[100];
	char *seg;

	close (p[ESCRIU]);
	
	read(p[LLEGIR],buf,sizeof(buf));
	printf("F: he llegit: %s\n",buf);
	
	read(p[LLEGIR],buf,sizeof(buf));
	printf("F: he llegit: %s\n",seg);

	exit(0);
}

