Following this SO question & answer
Connecting n commands with pipes in a shell?
I tried executing yes | head
but it runs in an infinite loop or it never responds back. What is the issue.
I did made some changes and here is the running code
#include <unistd.h>
struct command
{
const char **string;
};
Helper Function
pid_t start(command* command, pid_t pid, int* status, int in, int out) {
(void) pid;
pid_t cpid;
int childInt;
cpid = fork();
if (cpid == 0) {
if (in != 0)
{
dup2(in, 0);
close(in);
}
if (out != 1)
{
dup2(out, 1);
close(out);
}
execvp(c->string[0], c->string);
_exit(1);
}
waitpid(cpid, &childInt, 0);
}
*status = childInt;
return c->pid;
}
and in my main function
for(int i = 0; i < n; i++)
//New command every loop
int p = pipe(fd);
if (p == 0)
{
start_command(c, 0, &status, in, fd[1]);
close(fd[1]);
in = fd[0];
}
continue;
}
dup2(in, 0);