Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

main exits while running pipes...

Hi,
I am supposed to write a shell which can run command like ls |wc

I created pipe , forked a child...but when i run the program, after i execute that piece of code, the main program exits...

i think i am missing something...

if (!fork()) {
close(1);
dup(pfds[1]);
close(pfds[0]);
execlp("ls", "ls", NULL);
} else {
close(0);
dup(pfds[0]);
close(pfds[1]);
execlp("wc", "wc", "-c", NULL);
}i think i need to fork twice and run the commands in separate child...but i am confused how to do it .
[698 byte] By [sangfroid] at [2007-11-11 10:31:35]
# 1 Re: main exits while running pipes...
I'm not sure about your if condition. fork()'s return value can be positive, negative (error) or zero (when it's in the parent process). You should check each of these condition by using a more details if-else sequence:
if (fork()<0) //error
if (fork()==0) //parent
if (fork()>0) //i'm the child process
Danny at 2007-11-11 20:58:37 >
# 2 Re: main exits while running pipes...
I changed as you have mentioned, but still the problem persists..
sangfroid at 2007-11-11 20:59:32 >
# 3 Re: main exits while running pipes...
not sure how fork is like other threaded stuff but you might have some sort of "wait for threads to complete before proceeding" command you could drop into main before the return? I know you can do that in window's (both with threads or spawned apps if you choose the advanced forms of the functions). If not how about putting a cin or while !keypressed() type command to busy wait the main?
jonnin at 2007-11-11 21:00:36 >
# 4 Re: main exits while running pipes...
fork launches a child process with its own address space, local objects, stdin etc. There's very little communication between the parent and the child: when the child is launched, the fork() call in the parent returns the child's pid and when the child dies, the parent can get a signal but that's about it. In other words, fork is a multiprocessing facility. What I suspect might be the problem here is that the parent receives a signal from the child process and that signal isn't handled. Consequently, the parent is killed. Quite brutal this fork stuff, isn't it;)
Danny at 2007-11-11 21:01:37 >
# 5 Re: main exits while running pipes...
ah its like shellexecute then. Yes, you have to trap and handle the return code to know the child dies, and I think you can kill the child, but little else if they are similar?
jonnin at 2007-11-11 21:02:41 >
# 6 Re: main exits while running pipes...
Actually, I made a mistake. Here's the correct code:
if (fork()<0) //error
if (fork()==0) //child
if (fork()>0) //i'm the parent process

One more thing abou fork(): it accomplishes multiprocessing in two steps: first fork() reduplicates the current process, or clones it. Next, the child process has to detect that it's the child (it's not that trivial since it's the same executable and source file as the parent!) and replace itself by calling exec(). exec in turn launches a different program that supersedes the current program, while the pid remains the same. The effect of this two steps is creating two different tasks, where one seems to have been spawned by the parent process.
Danny at 2007-11-11 21:03:40 >