Hi, I ve been working on my assignment. Having problems. Please solve them for me. I will be thankful to you all.
What I am doing, or want to do is, read a file in a PROCESS, read it character by character, send each character, one by one through pipe, to the child of that PROCESS and convert it to Capital letter if it small lettered and wirte to another file. The code which I ve written so far is,
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
int main ()
{
int ascii_value = 0; /*used to get the ASCII value of each character*/
char character = '0'; /*used to read the input file, then used to write to output file, character by character*/
int in; /*used for getting reading file info*/
int out; /*used for getting writing file info*/
int file_pipes [2];
pid_t child1;
char junk_character = '0';
int i = 0;
int stat_val;
int exit_code;
pid_t child_exit;
in = open ("file.in", O_RDONLY);
out = open ("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
if (pipe (file_pipes) == 0)
{
child1 = fork ();
for (i = 0; i < 8; i++){
if (child1 == -1)
{
printf ("Child not created.\n");
exit (1);
}
else if (child1 == 0)
{
read (file_pipes [0], &junk_character, 1);
printf ("Child process %c \n", junk_character);
}
else
{
read (in, &junk_character, 1);
write (file_pipes [1], &junk_character, 1);
printf ("Parent process %c \n", junk_character);
// child_exit = wait (&stat_val);
// if (WIFSTOPPED (stat_val));
}
}
}
exit (1);
}
The problem is I want the parent Process to "wait" till child process reads the character from the pipe, convert it, and write it to file. and I dont know how to do it.
"WAIT", not taught in the class, studied by myself in a book, but unable to use it correctly, or as I want it to be.
This code is not properly on my algorithm, as I am a newbie in LINUX programming, I ve taken some arbitrary values to see what is really happening, like in for loop I ve set i < 8. My arbitrary program is supposed to read 8 characters from the file, character by character, send it to child through pipe, where child prints on screen showing the child process got the character. The wait calls are commented, because they were not working right.