S
Seamoon
I'm trying to do a simple unixshell and have some problems with the
pipeprocedure.
The procedure will generate a correct result when for example executing
rwho|grep nnn|wc,
but if bground is set to 0, I want the pipe to execute in the foreground
such that the prompter will be displayed after the
result is printed, but the pipe still executes in the background so that the
result is displayed after the prompter. I suppose I'm doing something wrong
when waiting for the first child to terminate (executing the last command in
the pipe)?
cmdList is a linked list of arrays each holding the command (pgms[0]) and
accompanying switches pgms[1..n] for each process in the pipe..
Perhaps it can be immediately seen if I'm doing something conceptually
wrong?
Best regards,
Simon
void execPipe(Pgm *cmdList, int bground, Command info) {
pid_t childPid;
char **pgms;
int status;
childPid = fork();
if (childPid == 0) {
pgms = cmdList->pgmlist;
if (info.rstdout) {
close(1);
creat(info.rstdout, O_RDWR|O_CREAT);
}
while (1) {
int p[2];
pipe(p);
if (!fork()) {
close(0);
dup(p[0]);
close(p[0]);
close(p[1]);
execvp(pgms[0], pgms);
perror("Unknown command");
_exit(1);
}
cmdList = cmdList->next;
pgms = cmdList->pgmlist;
close(1);
dup(p[1]);
close(p[1]);
close(p[0]);
if (!cmdList->next) {
if (info.rstdin) {
close(0);
open(info.rstdin, O_RDONLY);
}
execvp(pgms[0], pgms);
perror("Unknown command");
_exit(1);
}
}
}
else
if (!bground)
waitpid(childPid, &status, 0); //Doesn't seem to block til the
last process in the pipe is terminated
}
pipeprocedure.
The procedure will generate a correct result when for example executing
rwho|grep nnn|wc,
but if bground is set to 0, I want the pipe to execute in the foreground
such that the prompter will be displayed after the
result is printed, but the pipe still executes in the background so that the
result is displayed after the prompter. I suppose I'm doing something wrong
when waiting for the first child to terminate (executing the last command in
the pipe)?
cmdList is a linked list of arrays each holding the command (pgms[0]) and
accompanying switches pgms[1..n] for each process in the pipe..
Perhaps it can be immediately seen if I'm doing something conceptually
wrong?
Best regards,
Simon
void execPipe(Pgm *cmdList, int bground, Command info) {
pid_t childPid;
char **pgms;
int status;
childPid = fork();
if (childPid == 0) {
pgms = cmdList->pgmlist;
if (info.rstdout) {
close(1);
creat(info.rstdout, O_RDWR|O_CREAT);
}
while (1) {
int p[2];
pipe(p);
if (!fork()) {
close(0);
dup(p[0]);
close(p[0]);
close(p[1]);
execvp(pgms[0], pgms);
perror("Unknown command");
_exit(1);
}
cmdList = cmdList->next;
pgms = cmdList->pgmlist;
close(1);
dup(p[1]);
close(p[1]);
close(p[0]);
if (!cmdList->next) {
if (info.rstdin) {
close(0);
open(info.rstdin, O_RDONLY);
}
execvp(pgms[0], pgms);
perror("Unknown command");
_exit(1);
}
}
}
else
if (!bground)
waitpid(childPid, &status, 0); //Doesn't seem to block til the
last process in the pipe is terminated
}