You are on page 1of 2

#include <unistd.

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

int main(void)
{
int pid, nr, poz[2], neg[2];
FILE *fin_poz,*fout_poz, *fin_neg, *fout_neg;
pipe(poz);
pipe(neg);

fin_poz = fdopen(poz[0],"r"); /* capatul de citire */


fout_poz = fdopen(poz[1],"w"); /* capatul de scriere */

fin_neg = fdopen(neg[0],"r"); /* capatul de citire */


fout_neg = fdopen(neg[1],"w"); /* capatul de scriere */

if(fork()==0)
{
int negative[2];
pipe(negative);
FILE* negs_fin = fdopen(negative[0],"r");
FILE* negs_fout = fdopen(negative[1],"w");
fclose(fout_neg);
while( fscanf(fin_neg,"%d",&nr) != EOF)
{
fprintf(negs_fout,"%d\n",nr);
fflush(stdout);
}
while( fscanf(negs_fin,"%d",&nr) != EOF)
printf("%d ",nr);

fflush(stdout);
fclose(fin_neg);

}
else
{
// if(fork()==0)
// {
// fclose(fout_poz);
// while( fscanf(fin_poz,"%d",&nr) != EOF)
// {
// printf("Pozitive: %d\n",nr);
// fflush(stdout);
// }
// printf("Pozitive: am citit EOF din canal!\n");
// fclose(fin_poz);

// }
// else
{
//parinte
fclose(fin_poz);
fclose(fin_neg);
printf("Introduceti o secventa de numere diverse, terminata prin
CTRL+D.\n");
while(EOF != scanf("%d",&nr) )
{
if (nr>=0)
{
fprintf(fout_poz,"%d\n",nr);
fflush(fout_poz);
}
else
{
fprintf(fout_neg,"%d\n",nr);
fflush(fout_neg);
}
}
fclose(fout_poz);
fclose(fout_neg);

//wait(0);
wait(0);
}
}
return 0;
}

https://pastebin.com/pDpJZCPK

You might also like