I'm tying to make a simple shell, but I'm stuck implementing pipes. Here's the part of my code that pertains to the pipe. There's more I can put if you think it'll help, but the rest of it works exactly as expected.
args[i] = NULL;
p_flag = 1;
int stop = i;
// we have to fork again so we can write to a buffer file.
// First we creat the pipe.
if (pipe(p_file) == -1) {
//check if the pipe failed
fprintf(stderr, "There was an error creating the pipe\n");
}
switch (pid = fork()) {
case 0:
//Open up the input end of the pipe for writing.
printf("test1\n");
close(p_file[0]);
printf("test2\n");
dup2(p_file[1], 1);
printf("test3\n");
execvp(args[0], args);
fprintf(stderr, "ERROR %s no such program.\n", line);
exit(1);
break;
case -1:
fprintf(stderr, "ERROR can't create child process!\n");
break;
default:
wait();
break;
}
/*
at this point only the first child should still be running
and since we waited we know that the buffer file has our input.
Now we have to redirect our input to read from the buffer and
run the rest of the commands normally. first the args array has
to be cleaned up we want the commands past the pipe symbol to be
in front.
*/
int j = 0;
stop++;
while (args[stop] != NULL) {
args[j] = args[stop];
j++;
stop++;
}
args[j] = NULL;
close(p_file[1]);
dup2(p_file[0], 0);
As you can see, I've put in some print statements to try and figure out where exactly it was getting stuck, and I get test1
and test2
output before the program sits processing forever spinning its wheels. I have to ctrl+c to get the prompt back. I don't know why it's stuck doing the dup2
command. The only important thing left out of this code snippet is initialization of p_file
and it's an array of size two (i.e. int p_file[2];
).