All pastes #806823 Raw Edit

Someone

public c v1 · immutable
·published 2007-12-05 23:19 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include <fcntl.h>int pfd[2];int pfe[2];void child (int processNum);int main(){	int i;	int ret;	char buf;		if (pipe(pfd) < 0) { //an to pipe epistrepsei < 0 tote yphrkse provlima dimiourgias tou		printf("Pipe allocation error!");		exit(1);	}		fcntl(pfd[0], F_SETFL, O_NONBLOCK); //kanoume tin pipe non blocking		for (i = 0; i < 2; i++) {		ret = fork();		if (ret == 0) { //diergasies paidia mono, oi diergasies patera exoun ret != 0 kai eksetazontai sto telos			child(i); //kaleitai h synarthsh child, pou kanei ta dio paidia na grapsoun 'a' to prwto kai 'b' sto deftero mesa stin pipe			exit(0);		}	}	wait(NULL); //perimenoume tis 2 diergasies-paidia na epistrepsoun kai meta synexizei o pateras	wait(NULL);		if (pipe(pfe) < 0) { //an to pipe epistrepsei < 0 tote yphrkse provlima dimiourgias tou		printf("Pipe allocation error!");		exit(1);	}	//	printf("test");	fcntl(pfe[0], F_SETFL, O_NONBLOCK); //kanoume tin pipe non blocking	while (read(pfd[0], &buf, sizeof(char)) > 0) {		write(pfe[1], &buf, sizeof(char));	}	ret = fork();	if (ret == 0) {		child(3);		exit(0);	}	close(pfd[0]); //kleinei i eksodos tis pipe	close(pfe[0]);	return 0;}void child(int processNum){	close(pfd[0]); //kleinoume tin eisodo tou 1ou pipe	close(pfe[1]); //kleinoume tin eksodo tou 2ou pipe	char ch;	int i;	if (processNum == 0) {		ch = 'a';		for (i = 0; i < 1000; i++) 			write(pfd[1], &ch, sizeof(char));	}	else if (processNum == 1) {		ch = 'b';		for (i = 0; i < 1000; i++)			write(pfd[1], &ch, sizeof(char));	}	else {		while (read(pfe[0], &ch, sizeof(char)) > 0)			putchar(ch);	}	close(pfd[1]); //kleinoume tin eksodo tou 1ou pipe	close(pfe[0]); //kleinoume tin eisodo tou 2ou pipe}