Here is the whole program that I'd like to modify. I reposted this here from C-programming because it might be more appropriate.

Code:


// Header files
#include <stdio.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>

#define READ 0
#define WRITE 1

/*pip1 = Pipe 1
 *pip2 = Pipe 2
 *pip3 = Pipe 3
 *pip4 = Pipe 4
 */

// Main
int main()
{
  int i, i_tot, pip1[2], pip2[2], pip3[2], pip4[2], noMsgs, ClNu; 
							  

  pid_t pid1, pid2, pid3; 
  char inbuf[1024];
  
  char *buffer[1024], string[1024], prebuffer[1024], *bufferpointer; 
  int nread, nread2;
  int flag = 1;

  
  pipe(pip1);
  pipe(pip2);
  pipe(pip3);
  pipe(pip4);


  
  pid1=fork();
  if(pid1==0) 
  {
	//child(1, pip);
	close(pip1[WRITE]);
	close(pip2[READ]);
	close(pip2[WRITE]);
	close(pip3[READ]);
	close(pip3[WRITE]);
	close(pip4[READ]);
	close(pip4[WRITE]);

	
	i=0;
	while((nread=read(pip1[READ],prebuffer,1024))>0)
	{
		bufferpointer=(char*)malloc(sizeof(char));
		strcpy(bufferpointer,prebuffer);
		buffer[i]=bufferpointer;
		i++;
	}

	i_tot=i;
	for(i=0;i<i_tot;i++)
	{
		if(i==(i_tot-1))
			printf("- Child 1 read: %s. C1 exiting...\n",buffer[i]);
		else
			printf("- Child 1 read: %s.\n",buffer[i]);
	}
	exit(0);
  }
  else 
  {
	pid2=fork();
	if(pid2==0) // in child 2 

	{	
		close(pip2[WRITE]);
		close(pip1[READ]);
		close(pip1[WRITE]);
		close(pip3[READ]);	
		close(pip4[WRITE]);	
	
		//child(2, pip);
		fcntl(pip2[READ],F_SETFL, O_NONBLOCK);
		fcntl(pip4[READ],F_SETFL, O_NONBLOCK);	
	
		i=0;		
		while(flag)
		{
			nread=read(pip2[READ],prebuffer,1024);
			nread2=read(pip4[READ],string,1024);
			if(nread==-1||nread2==-1)
				
			if(nread>0)
			{
				if(prebuffer[strlen(prebuffer)-1]=='3')  
				{
					printf("  * Forwarding message along Pipe P3.\n");
					prebuffer[strlen(prebuffer)-1]='\0';
					write(pip3[WRITE],prebuffer,strlen(prebuffer)+1);  
				}
				else
				{
					prebuffer[strlen(prebuffer)-1]='\0';
					bufferpointer=(char*)malloc(sizeof(char));
					strcpy(bufferpointer,prebuffer);
					buffer[i]=bufferpointer;
					i++;
				}
			}
			if(nread2>0) // message forwarded from C3 to C2 through P4
			{
				bufferpointer=(char*)malloc(sizeof(char));
				strcpy(bufferpointer,string);
				buffer[i]=bufferpointer;
				i++;
			}		
			if(nread==0)
				flag=0;
		}
	
		i_tot=i;
		for(i=0;i<i_tot;i++)
		{
			if(i==(i_tot-1))
				printf("- Child 2 read: %s. C2 exiting...\n",buffer[i]);
			else
				printf("- Child 2 read: %s.\n",buffer[i]);
		}
		exit(0);
	}	
	else // in parent again
	{
		pid3=fork();
		if(pid3==0) // in child 3
		{
			 
		  	close(pip2[WRITE]);
			close(pip1[READ]);
			close(pip1[WRITE]);
			close(pip3[WRITE]);	 
			close(pip4[READ]);	 

	//continue to check messages of pipes 2 and 3
			fcntl(pip2[READ],F_SETFL, O_NONBLOCK);
			fcntl(pip3[READ],F_SETFL, O_NONBLOCK);	
	
			i=0;	
			while(flag)
			{
				nread=read(pip2[READ],prebuffer,1024);
				nread2=read(pip3[READ],string,1024);
				if(nread==-1||nread2==-1)
					 /*pipe empty do nothing*/;
				if(nread>0)
				{
					if(prebuffer[strlen(prebuffer)-1]=='2') 
					{
						printf("  * Forwarding message along Pipe P4.\n");						
						prebuffer[strlen(prebuffer)-1]='\0';
						write(pip4[WRITE],prebuffer,strlen(prebuffer)+1); 
					}
					else
					{
						prebuffer[strlen(prebuffer)-1]='\0';
						bufferpointer=(char*)malloc(sizeof(char));
						strcpy(bufferpointer,prebuffer);
						buffer[i]=bufferpointer;
						i++;
					}
				}
				if(nread2>0) // message forwarded from C2 to C3 through P3
				{
					bufferpointer=(char*)malloc(sizeof(char));
					strcpy(bufferpointer,string);
					buffer[i]=bufferpointer;
					i++;
				}		
				if(nread==0)
					flag=0;
			}

			i_tot=i;
			for(i=0;i<i_tot;i++)
			{
				if(i==(i_tot-1))
					printf("- Child 3 read: %s. C3 exiting...\n",buffer[i]);
				else
					printf("- Child 3 read: %s.\n",buffer[i]);
			}
			exit(0); 
		}
		else 
		{
			
			close(pip3[READ]);  
			close(pip3[WRITE]);
			close(pip4[READ]);  
			close(pip4[WRITE]);

			close(pip1[READ]);
			close(pip2[READ]);

			printf("- Hello, I am the parent. How many messages should I send? ");
			scanf("%d",&noMsgs);
			for(i=1;i<=noMsgs;i++)
			{
				sleep(1);
				printf("- Message %d (<msg> <child>): ", i);
				scanf("%s %d",inbuf,&ClNu);
				if(ClNu==1)
				{
					nread=write(pip1[WRITE],inbuf,strlen(inbuf)+1);
					printf("  + Message has been sent along Pipe P1.\n");
				}
				if(ClNu==2)
				{
					strcat(inbuf,"2");
					write(pip2[WRITE],inbuf,strlen(inbuf)+1);
					printf("  + Message has been sent along Pipe P2.\n");
				}
				if(ClNu==3)
				{
					strcat(inbuf,"3");
					write(pip2[WRITE],inbuf,strlen(inbuf)+1);
					printf("  + Message has been sent along Pipe P2.\n");
				}
			}
			close(pip1[WRITE]);
			close(pip2[WRITE]);

			waitpid(pid1, NULL, 0);
			waitpid(pid2, NULL, 0);
			waitpid(pid3, NULL, 0);
			printf("- Parent exiting...\n");
		}
	}
  }
  exit(0);
}

How could I go about modifying this program so the Nonblocks aren't needed, but the program does the same thing, i.e., the parent uses four pipes to send messages to three children in the same way as before?
Code:
fcntl(pip2[READ],F_SETFL, O_NONBLOCK);
fcntl(pip3[READ],F_SETFL, O_NONBLOCK);