Click here to Skip to main content
11,647,600 members (64,193 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Linux
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);}
	    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 23-Apr-13 11:53am
Juuca377
Edited 23-Apr-13 13:10pm
v4
Comments
SoMad at 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 at 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 at 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
0 jyo.net 500
1 F-ES Sitecore 370
2 DamithSL 335
3 OriginalGriff 322
4 CPallini 290
0 OriginalGriff 1,342
1 jyo.net 994
2 DamithSL 971
3 Sergey Alexandrovich Kryukov 873
4 CPallini 795


Advertise | Privacy | Mobile
Web03 | 2.8.150804.2 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2015
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