Professional Documents
Culture Documents
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);
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