I need to write a shell that can trigger channels. For example, commands such as ls -l | wc -l". I successfully analyzed the command given by the user, as shown below:
"ls" = firstcmd
"- l" = frsarg
"wc" = scmd
"- l" = secarg
Now I need to use two forks, since the teams are two and the channel. The code block that I wrote to execute the command is as follows:
pid_t pid;
int fd[2];
pipe(fd);
pid = fork();
if(pid==0)
{
dup2(fd[WRITE_END], STDOUT_FILENO);
close(fd[READ_END]);
execlp(firstcmd, firstcmd, frsarg, (char*) NULL);
}
else
{
pid=fork();
if(pid==0)
{
dup2(fd[READ_END], STDIN_FILENO);
close(fd[WRITE_END]);
execlp(scmd, scmd, secarg, (char*) NULL);
}
}
Therefore, when I run my shell and I enter the command ls -l | wc -l(for example), the result from execs does not appear, but the shell continues to work normally.
It is strange that the result of the command is displayed only when I complete my shell with "exit" or "^ C".
? ?