Project/app_code/pipe.c
void pipeline(char **argv)
{
	/*ret is for parent and l_pid is for child*/
	pid_t ret;
	pid_t l_pid;

	/*status to be used if u want the child termination status*/
	int status;
	
	/*this variable is aptly named because it tells me that the pipes are over 
	  and therefore the result should be placed on terminal or redirected if required*/
	int last = 0;
	
	/*this static variable is used to keep track of number of times pipes is encountered*/
	/*its use is already told in logic section above*/
	
	static int count=1;

	/*creat pipe and check for errors*/
	if(pipe(pipefd)<0)
	{
		perror("pipe");
		exit(1);
	}

	if(pipe(pipefd2)<0)
	{
		perror("pipe");
		exit(2);
	}

	/*loop to continue till last command*/
	while(!last)
	{
		/*refresh pipefd to use it again as explained in logic*/
		if((count > 2) && (count % 2)!=0)
		{
			close(pipefd[0]);
			close(pipefd[1]);
			if(pipe(pipefd) < 0)
			{
				perror("pipe");
				exit(7);
			}
		}
		/*refresh pipefd2 for even continuation as exp. in logic*/
		else if((count > 2)&&(count % 2)==0)
		{
			close(pipefd2[0]);
			close(pipefd2[1]);
			if(pipe(pipefd2) < 0)
                        {
                                perror("pipe");
                                exit(7);
                        }
		}

		/*parse_pipe separates the arguments for pipe*/
		parse_pipe(argv,&l_argv,&last);

		/*create child process*/
		l_pid = fork();

		/*check for pipe error*/
		if(l_pid < 0)
		{
			perror("fork");
			exit(3);
		}

		/*in child call left child*/
		else if(l_pid == 0)
		{
			left_child(l_argv,last,count);
		
		}
		else
		/*in parent increase the count*/
		count++;
		        
	}
	/*once we r done,we reset count to 1, and close all the open file
	  descriptors in parent*/
	count = 1;
	close(pipefd[0]);
	close(pipefd[1]);
	close(pipefd2[0]);
	close(pipefd2[1]);
}
/*main function for pipe implementation.it is called by main and it handles all 
  the other functions to finish the pipe implementation*/
main function for pipe implementation.it is called by main and it handles all
the other functions to finish the pipe implementation
main function for pipe implementation . it is called by main and it handles all the other functions to finish the pipe implementation
void pipeline(char **argv)
{
	pid_t ret;
	pid_t l_pid;
	int status;
	int last = 0;
	static int count=1;
	if(pipe(pipefd)<0)
	{
		perror("pipe");
		exit(1);
	}
	if(pipe(pipefd2)<0)
	{
		perror("pipe");
		exit(2);
	}
	while(!last)
	{
		if((count > 2) && (count % 2)!=0)
		{
			close(pipefd[0]);
			close(pipefd[1]);
			if(pipe(pipefd) < 0)
			{
				perror("pipe");
				exit(7);
			}
		}
		else if((count > 2)&&(count % 2)==0)
		{
			close(pipefd2[0]);
			close(pipefd2[1]);
			if(pipe(pipefd2) < 0)
                        {
                                perror("pipe");
                                exit(7);
                        }
		}
		parse_pipe(argv,&l_argv,&last);
		l_pid = fork();
		if(l_pid < 0)
		{
			perror("fork");
			exit(3);
		}
		else if(l_pid == 0)
		{
			left_child(l_argv,last,count);
		}
		else
		count++;
	}
	count = 1;
	close(pipefd[0]);
	close(pipefd[1]);
	close(pipefd2[0]);
	close(pipefd2[1]);
}
void pipeline ( char * * argv ) { pid_t ret ; pid_t l_pid ; int status ; int last = 0 ; static int count = 1 ; if ( pipe ( pipefd ) < 0 ) { perror ( " " ) ; exit ( 1 ) ; } if ( pipe ( pipefd2 ) < 0 ) { perror ( " " ) ; exit ( 2 ) ; } while ( ! last ) { if ( ( count > 2 ) && ( count % 2 ) != 0 ) { close ( pipefd [ 0 ] ) ; close ( pipefd [ 1 ] ) ; if ( pipe ( pipefd ) < 0 ) { perror ( " " ) ; exit ( 7 ) ; } } else if ( ( count > 2 ) && ( count % 2 ) == 0 ) { close ( pipefd2 [ 0 ] ) ; close ( pipefd2 [ 1 ] ) ; if ( pipe ( pipefd2 ) < 0 ) { perror ( " " ) ; exit ( 7 ) ; } } parse_pipe ( argv , & l_argv , & last ) ; l_pid = fork ( ) ; if ( l_pid < 0 ) { perror ( " " ) ; exit ( 3 ) ; } else if ( l_pid == 0 ) { left_child ( l_argv , last , count ) ; } else count ++ ; } count = 1 ; close ( pipefd [ 0 ] ) ; close ( pipefd [ 1 ] ) ; close ( pipefd2 [ 0 ] ) ; close ( pipefd2 [ 1 ] ) ; }
main function for pipe implementation.it is called by main and it handles all
the other functions to finish the pipe implementation
main function for pipe implementation . it is called by main and it handles all the other functions to finish the pipe implementation
/*ret is for parent and l_pid is for child*/ /*status to be used if u want the child termination status*/ /*this variable is aptly named because it tells me that the pipes are over 
	  and therefore the result should be placed on terminal or redirected if required*/ /*this static variable is used to keep track of number of times pipes is encountered*/ /*its use is already told in logic section above*/ /*creat pipe and check for errors*/ /*loop to continue till last command*/ /*refresh pipefd to use it again as explained in logic*/ /*refresh pipefd2 for even continuation as exp. in logic*/ /*parse_pipe separates the arguments for pipe*/ /*create child process*/ /*check for pipe error*/ /*in child call left child*/ /*in parent increase the count*/ /*once we r done,we reset count to 1, and close all the open file
	  descriptors in parent*/