Click here to Skip to main content
12,998,759 members (70,765 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi all,

It's the second time I'm coming to you guys in aid, and I'm almost completely lost. I've read every 'good source' that I know of about the subject, and I'm still lost on what's not working.

void execute_commands(COMMAND* commlist) {
    COMMAND* next = commlist;
    const int count = count_commlist(commlist) - 1;
    pid_t child_pid[count+1];
    int pipeline[count][2]; int p = 0,status;
    for(;p<count;++p){pipe(pipeline[p]);};p = 0;
    for(;p <= count;++p)
      COMMAND* current = next;
      next = current->next;
	if((child_pid[p] = fork())==-1)printf("Error creating new process");
	else if(child_pid[p] == 0)
	    if (p == 0){dup2(pipeline[p][WRITE],1);}
	    else if (p == count) {dup2(pipeline[p-1][READ],0);}
	    int ic = 0;
	    for (;ic <count ; ++ic){close(pipeline[ic][READ]);close(pipeline[ic][WRITE]);}
	    execvp(current->cmd, current->argv);
    int ic = 0;
    for (;ic &lt;count ; ++ic){close(pipeline[ic][READ]);close(pipeline[ic][WRITE]);}//added solution code

Basically I'm implementing a prompt with basic OS operation. This function is supposed to implement command pipelines funcionality. The pipeline is working, as the supposed output comes out, but I'm having trouble with the child processes. As test I did "ls | cat" and the ls stays zombified, and cat in sleep. Also with "ls | cat | more", ls again zombifies, and cat and more stay asleep.

I've tried with WUNTRACED | WCONTINUED as options, and the ls is reaped after termination, but cat and more stay asleep.

I've also tried doing differently, and put each of the consequent processes to wait for theirs previous brother, but that worked even worse.

Hope you guys could help me in this.

Thankz in advance

In a more detailed ps -el, I gazed upon the stoping point of the cat, WCHAN = pipe_w. Not sure it is helpful.

Actually didn't know this, but was kind of lame mistake. I didn't close the pipe ends for the bash process, resulting in device waiting for the child processes that require in-stream.
Posted 23-Apr-13 11:53am
Updated 23-Apr-13 13:10pm
SoMad 23-Apr-13 19:22pm
Thank you for not posting your fix as an answer. That in itself almost deserves an up-vote on your question. :)

Kidding aside, I am sure there will be developers out there that will stumble across this and thank you for helping them out.

Soren Madsen
Sergey Alexandrovich Kryukov 23-Apr-13 19:50pm
Right! I voted 5 for this question.

Actually, I would really like to have answers to the questions of the same member prohibited. This is one of the worst abuse in this forum. I don't mean nice answers as this one, I mean the real fake. A number of inquirers just have no shame.
H.Brydon 23-Apr-13 23:10pm
Likewise I owe SoMad and SA a +5 - watch for it...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100