Pipes & Forks
Jacques Parker
jp27+ at andrew.cmu.edu
Wed Dec 13 06:08:51 AEST 1989
I made the following program. When compiled & run, it will accept
input, but not act on it. It should accept commands, and the child
process should receive and execute them. This
does not happen until the program is terminated (Enter ^D). Any ideas?
How do I insure that the child receives the parents output immediately?
Thank you for any help.
Jacques Parker
#include <stdio.h>
#define MAXLEN 100
#define ST_INP 0
#define ST_OUT 1
extern char **environ;
char **arg = { "/bin/csh","-x"};
main(argc,argv,envp)
int argc;
char **argv, **envp;
{
int pd[2];
int pid;
int bak_inp = dup (ST_INP);/* Backup the input FD */
int bak_out = dup (ST_OUT);/* Backup the output FD */
if (pipe(pd) < 0)
printf("%s: Can't create pipe", cmd);
dup2(pd[1], ST_OUT); /* ST_OUT --> pipe WRITE */
dup2(pd[0], ST_INP); /* ST_INP --> pipe READ */
if ((pid = fork()) < 0)
printf("%s: Can't spawn a process.", cmd);
if (pid == 0) {
dup2(bak_out, ST_OUT); /* child out --> ST_OUT */
/* child in --> Pipe READ */
execvp(arg[0],arg,environ);
printf("Shell not started. '%s' failed.",cmd);
}
dup2(bak_inp,ST_INP); /* parent out --> Pipe WRITE */
/* parent in --> ST_INP */
while (gets(cmd) != NULL) { /* Enter ^D to end. */
printf("%s\n,cmd);
fflush(NULL);
}
}
Address: internet jp27+ at andrew.cmu.edu,
uucp ...!{harvard,uunet}!andrew.cmu.edu!jp27,
bitnet jp27%andrew at cmccvb.bitnet.
More information about the Comp.unix.wizards
mailing list