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.

The wait() function monitors the state of a process, not its I/O streams. What you really want is the poll() or select() function.

It also seems that you want unbuffered input on your end of the pipe. See termios(3) for more.

See also poll(3) and select(3) for good reading on how these functions are used.

Hope this helps.

Comments
Some good old fashioned RTM - excellent

I ve solved the problem myself. I ve used sleep and changed the position of pipe function and child2 fork. Used "sleep" for some time delay so that the output on file.out is right.

The problem was to get all the characters from a file in a process, send that data/characters to 2nd process through pipe. The 2nd process converts the lower case letters to upper case ones and send them to 3rd process through pipe. The 3rd process outputs the data to a file.

/*****************************************************
The input file is file.in, and output file is file.out.
*****************************************************/

#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.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*/
	char junk_character = '0';
	int in;						/*used for getting reading file info*/
	int out;					/*used for getting writing file info*/
	int file_pipes [2];
	int file_pipes2 [2];
	int i = 0;
	pid_t child1;
	pid_t child2;

	FILE *read_ptr = fopen ("file.in", "r");
	
	if (read_ptr == NULL)
	{
		printf ("File not opened.\n");
		
		exit (1);
	}
	
	while (character != EOF)
	{
		character = fgetc (read_ptr);
		
		if (character != EOF)
		{
			i++;
		}
	}
	
	fclose (read_ptr);
        
    character = '0';
    
    in = open ("file.in", O_RDONLY);
    
    out = open ("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
    
    if (pipe (file_pipes) == 0 && pipe (file_pipes2) == 0)
    {
        child1 = fork ();		/*process 2*/

    	child2 = fork ();		/*process 3*/
       
       	for (; i > 0; i--)
       	{
			if (child1 == -1 || child2 == -1)
			{
				printf ("Child not created.\n");
				
				exit (1);
			}
	
			else if (child1 == 0)
			{
				read (file_pipes [0], &junk_character, 1);			/*gets the character from the parent*/
		
				ascii_value = junk_character;
		
				if (ascii_value >= 97 && ascii_value <= 122)
				{
					ascii_value = ascii_value - 32;
			
					junk_character = ascii_value;
				}
							
				write (file_pipes2 [1], &junk_character, 1);		/*writes the character in pipe b/w child1 and child2*/
			}
			
			else if (child2 == 0)
			{				
				read (file_pipes2 [0], &character, 1);				/*gets the capitalized character from child1*/
				
				write (out, &character, 1);							/*writes the capitalized character to file.out*/
			}
				
	
			else
			{
				read (in, &junk_character, 1);						/*gets the character from file.in*/
			
		   		write (file_pipes [1], &junk_character, 1);			/*writes the character in the pipe b/w parent and child1*/
		
				sleep (0.1);					/*waits for a while (0.1s), so that the child1 and child2 are synchronized, otherwise child2 will wrongly output to file.out. Increasing this will improve the output of child2 but the program will get slower and slower. It may give a distorted output at 0.1s*/
			}
    	}
    }
    
    close (in);
    close (out);
    
    exit (1);
}

The EOF was not working in the way I wanted, so I first counted all the characters in the file. Then used that counter afterwards.

This question has already been answered. Start a new discussion instead.