The task I have to do is something along the lines "I receive some input and based on the first character I send it through pipe to one of the children to print".
The scheme it is based on is 1->2; 1->3; 1->4; 2 will print all the input that starts with a letter, 3 will print all the input that starts with a digit and 4 will print everything else. (Example: 12, Anne, **, *a will be printed by 3, 2, 4, 4)
I have quite understood how pipes work between one parent process and one child, but can't figure it out how to make this work, but I have tried, so I'mma attach the code down below. Any tip would be much appreciated.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{ int pipefd[2];
int pipefe[2];
int pipeff[2];
pipe(pipefd);
pipe(pipefe);
pipe(pipeff);
char arr[80];
scanf("%s", arr);
int f1 = fork();
if(f1>0)
{close(pipefd[0]);
close(pipefe[0]);
close(pipeff[0]);
if (arr[0]>='A'&&arr[0]<='z')
{
write(pipefd[1], arr, strlen(arr)+1);
close(pipefd[1]);
}
if(arr[0]>='0' && arr[0]<='9')
{
write(pipefe[1], arr, strlen(arr)+1);
close(pipefe[1]);
}
else
{
write(pipeff[1], arr, strlen(arr)+1);
close(pipeff[1]);
}
exit(0);
}
else if(fork()==0)
{ wait(NULL);
close(pipefd[1]);
read(pipefd[0], arr, strlen(arr)+1);
printf("%s\n", arr);
close(pipefd[0]);
exit(0);
}
else if(fork()==0)
{ wait(NULL);
close(pipefe[1]);
read(pipefe[0], arr, strlen(arr)+1);
printf("%s\n", arr);
close(pipefe[0]);
exit(0);
}
else if(fork()==0)
{ wait(NULL);
close(pipeff[1]);
read(pipeff[0], arr, strlen(arr)+1);
printf("%s\n", arr);
exit(0);
return 0;
}
}