Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
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.

C++
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);}
	    else
	      {
		dup2(pipeline[p-1][READ],0);
		dup2(pipeline[p][WRITE],1);
	      }
	    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
    for(p=0;p<=count;p++){waitpid(child_pid[p],&status,WEXITED);}
}


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

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

edit2[SOLVED]:
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
Updated 23-Apr-13 13:10pm
v4
Comments
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.
—SA
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900