LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2007, 12:08 AM   #1
manyoo
LQ Newbie
 
Registered: Dec 2006
Posts: 10

Rep: Reputation: 0
Problem with I/O redirection


I'm going to write a very small shell and in order to implement pipe,I use redirection.But there's some problems here:
Code:
void do_with_pipe(char **argv1,char **argv2)
{
	    int pid1,pid2;
	    int pipe1[2];
	    if(pipe(pipe1)<0)
	    {
		    perror("pipe failure");
		    exit(EXIT_FAILURE);
	    }
	    pid1=fork();
	    if(pid1<0)
	    {
		    perror("fail to fork");
		    exit(1);
	    }
	    else if(pid1==0)
	    {
		    pid2=fork();
	            if(pid2<0)
		    {
			    perror("fail to fork");
			     exit(1);
		    }
		    if(pid2==0)
		    {
		           if(dup2(pipe1[0],STDIN_FILENO)==-1)
		 		       perror("fail to dup");
			    close(pipe1[0]);
			    close(pipe1[1]);
		 	    execvp(argv2[0],argv2);
		    } 
		    else{
			    if(dup2(pipe1[1],STDOUT_FILENO)==-1)
				    perror("fail to dup");
			    close(pipe1[0]);
			    close(pipe1[1]);
			    execvp(argv1[0],argv1);
	           }
	    }
	    else{
		    if(waiting)
			    waitpid(pid1,NULL,0);
		    else
			    waiting=YES;
	    }
}
This is the function to do the work and I'm sure the arguments are right.but the result doesn't print until the program end.For instance:
loyoo:~/programming/kernel$ ./shell
loyoo>>ls|sort
loyoo>>
loyoo>>
loyoo>>
loyoo>>q
01139-1.pdf
01139-1.tar.bz2
explain.ppt
info
info.odt
Linux_schedule.doc
shell
shell.c
syllabus.doc
loyoo:~/programming/kernel$

Why???

Last edited by manyoo; 04-11-2007 at 12:10 AM.
 
Old 04-11-2007, 01:01 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I can't understand what it is you are trying to do here?
Skipping through your code it looks to me as if you are entering forking hell. When you successfully fork it looks to me as if you are immediately forking again (and again and again...)
I'm sure that there is a better way to achieve what you want.
 
Old 04-11-2007, 02:21 AM   #3
manyoo
LQ Newbie
 
Registered: Dec 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by graemef
I can't understand what it is you are trying to do here?
Skipping through your code it looks to me as if you are entering forking hell. When you successfully fork it looks to me as if you are immediately forking again (and again and again...)
I'm sure that there is a better way to achieve what you want.
I first fork a child and this process fork another. The former exec
the first argument and ouput to the pipe, the second run the later argument and read from the pipe.
 
Old 04-11-2007, 02:30 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Okay but why?
If the second process is dependent upon the completion of the first process what advantage is another child giving you?
Sorry if you think me a little slow but I can't understand what you would possibly benefit from this.
 
Old 04-11-2007, 02:56 AM   #5
manyoo
LQ Newbie
 
Registered: Dec 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by graemef
Okay but why?
If the second process is dependent upon the completion of the first process what advantage is another child giving you?
Sorry if you think me a little slow but I can't understand what you would possibly benefit from this.
I want to implement a pipe of a small shell.
So if I type in "ls|sort",the first child run "ls"
and the second run "sort". I want it to be like in a real shell.
 
Old 04-11-2007, 03:51 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
In which case I would suggest either:
Have the parent spawn all the children that will be required
or
Have the child spawn a child if another action is required

At the moment the child appears to spawn another child whether it is required or not.
 
Old 04-11-2007, 05:06 AM   #7
manyoo
LQ Newbie
 
Registered: Dec 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Other parts of the program takes the responsbility to make the decision. This function just do the work I discribed above.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirection problem studentlb Programming 18 12-04-2006 01:28 AM
Bash I/O redirection problem R00ts Programming 1 10-26-2006 07:50 PM
cpanel redirection and permission problem ddaas Linux - Software 0 03-21-2006 03:29 AM
Apache - Redirection problem... elitecodex Linux - Software 5 03-29-2004 10:12 PM
Minor output redirection problem.. Tyir Linux - General 0 01-27-2004 09:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration