Click here to Skip to main content
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 12:53pm
Juuca375
Edited 23-Apr-13 14: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 Sergey Alexandrovich Kryukov 545
1 OriginalGriff 285
2 Shweta N Mishra 244
3 BillWoodruff 230
4 Mathew Soji 225
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,758
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,735


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