Here's the story:
Write a program in C that creates a child process and a granchild process (child of a child process!). The father process should read the contents of a file which name will take as an argument when you run the program. The father should send the text read to the child, which takes to convert those characters 'a' found in 'o'. The changed text will send the child to grandchild that (the grandchild) will take to count the number of rows and number of 'o' characters in the text and print the result on the screen (and optionally a file output.txt ).
So i create a file with some 'a' characters inside and i cant really convert those characters to 'o' .
The Question is what kind of command should i use for character converting ?
Any help much appreciated.
Here's what i have done so far :
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
int main() {
int fd_1[2],fd_2[2],pointer,pointer2,pid1,pid2,i,n;
char filename[200];
char buffer[500000];
printf("Input filename:");
fflush(stdout);
fflush(stdin);
scanf("%s", filename);
pointer = open(filename, O_RDONLY);
if (pointer == -1) {
printf("file does not exist\n");
}
pipe(fd_1);
if ( pipe(fd_1) == -1) {
perror("pipe");
exit(1);
}
pid1=fork();
switch(pid1){
case -1: {
perror("fork");
exit(10);
break;
}
case 0: { //child process
pipe(fd_2);
if ( pipe(fd_2) == -1) {
perror("pipe");
exit(1);
}
pid2=fork();
switch(pid2) {
case -1: {
perror("fork");
exit(10);
break;
}
case 0: { //grandchild process
close (fd_2[1]);
break;
}
default: { //child process
close (fd_1[1]);
dup2(fd_1[0],1);
pointer2 = open(buffer, O_WRONLY);
for (i=0; i< sizeof(buffer) ; i++)
if (buffer[i] == 'a' ) {
buffer[i] = 'o';
}
write(fd_1[1], buffer, sizeof(buffer));
wait(&pid2);
break;
}
}
}
default: { //parent process
close (fd_1[0]);
dup2(fd_1[1],0);
n = read(pointer, buffer, 200);
write(fd_1[1], buffer, n);
wait(&pid1);
break;
}
}
return 0;
}