January 18th, 2011, 12:11 PM
Problem while execv for a program using PIPES
I am making a program for my class assignment related to PIPES.
In the project i need to implement a remote shell server which can take 'piping' commands form the user.
ls | cat => as expected this will take output from ls into cat.
The problem is that i did all the piping etc. But when i finally run the code and 'ls' is executed first and it pipes its data into 'PIPE' (fd=1) and in the next iteration, the forked process which executes CAT also has its stdin as PIPE's fd=0(i used dup2 to do copy the file descriptors).
Now when finally the command is executed, CAT sends back the output as desired to the client but doesnot return for some reason(it just sleeps after sending the last line of output). The task manager in Ubuntu shows that its sleeping.
Because I use wait() for the child process to exit...so my program gets stuck there.
If I kill it from the task manager then the process returns to accept the next command but this problem has other effects which are not letting me finish my F##$#ing assigment.
I am really REALLY stuck and all my work will go down the drain....your help will be very much appreciated.
Where could I have been wrong :|
Below is the main function I am using(hope its not that messy )
the struct command *comm -> this has the commands like LS and CAT stored in an array format.
sub_comm_count = number of commands in a single like e.g ls | cat =>2 commands so its value= 1
current_count = -1(at the first call)
Also the program takes care of commands like
ls > output.txt
or cat < input.txt
but these are not a problem right now
void recursive_call(struct command *comm, int sub_comm_count,int current_count)
current_count++; // if current_count==sub_comm_count then no more commands to process
if( (childpid=fork()) <0)
else if (childpid==0)
*child process - Recursive_call()
read_pipe=read_from_pipe(comm_count);// checks and copies the pipe fd to read pending data from
//No PENDING pipe to read from
/** check for '<' **/
errlog("error opening 'read' file \n");
// copy the file descriptor to the STDIN_FILENO
errlog("error in dup2\n");
/** check for '>' **/
errlog("error opening 'write' file \n");
// copy the file descriptor to the STDOUT_FILENO
/** OUTPUT TO client SOCKET ==default output **/
///CHANGE HERE SOCKET VALUE ---enter it from the funciton
if( dup2(newsockfd,STDOUT_FILENO)!= STDOUT_FILENO )
errlog("dup 2 error");
close(pipe_from[comm[current_count].pipe_level + comm_count ]);
/** handle '|' --> STDOUT DATA piped into some PIPE **/
if( dup2(pipe_from[comm[current_count].pipe_level + comm_count ],STDOUT_FILENO)!= STDOUT_FILENO )
errlog("dup 2 error pipe out\n");
/** execute the command **/
errlog("execv did not work \n");
*Parent process - Recursive_call()
January 18th, 2011, 01:17 PM
Re: Problem while execv for a program using PIPES
Thanks but my problem has been solved now.
I was not able to get the EOF from the reading process as i had not closed all the write ends of my pipes.
Click Here to Expand Forum to Full Width