By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,953 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,953 IT Pros & Developers. It's quick & easy.

Problem with simpe unixshell attempt

P: n/a

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
}
Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Seamoon wrote:
I'm trying to do a simple unixshell and have some problems with the
pipeprocedure.


Wrong newsgroup. Try comp.unix.programmer Here we talk about standard C

Allin Cottrell
Nov 14 '05 #2

P: n/a
> Wrong newsgroup. Try comp.unix.programmer Here we talk about standard C

Oki, my bad. I was to quick to ask anyway. Case solved now.
Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.