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 <count ; ++ic){close(pipeline[ic][READ]);close(pipeline[ic][WRITE]);} 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.