I'm trying to implement pipelining in an Operating Systems shell project by creating a number of FIFO pipes using mkfifo(). Currently, the program seems to hang after opening the first FIFO for writing. Additionally, the FIFO appears when doing an ls on the working directory. Am I using freopen() wrong in this instance?
void execute_external()
{
int backgrounding = 0;
if (raw_command[strlen(raw_command)-1] == '&')
{
pmesg(2, "Backgrounding requested.\n");
raw_command[strlen(raw_command)-1] = '\0';
raw_command = realloc(raw_command, strlen(raw_command)-1);
backgrounding = 1;
}
int numCommands = 1;
char **commands;
commands = malloc(sizeof(char *));
if(strstr(raw_command, "|") != NULL)
{
numCommands = separate_pipeline_commands(commands);
}
else
{
commands[0] = malloc(strlen(raw_command) * sizeof(char));
commands[0] = raw_command;
}
int i;
char *fifo = malloc(MAXFIFOLEN);
for (i = 0; i < numCommands; i++)
{
char **parameters_array = malloc(strlen(commands[i]) * sizeof(char *));
int num_params;
num_params = str_to_str_array(commands[i], parameters_array);
pmesg(2, "Command is %s.\n", commands[i]);
if (numCommands > 1)
{
int j;
for (j = 0; j < numCommands - 1; j++)
{
sprintf(fifo, "fifo%i", j);
mkfifo(fifo, S_IWUSR | S_IRUSR );
}
}
pid_t pid = fork();
pmesg(2, "Process forked. ID = %i. \n", pid);
if (pid < 0)
{
fprintf(to_write_to, "Could not fork a process to complete the external command.\n");
exit(EXIT_FAILURE);
}
int status;
if (pid == 0) // This is the child process
{
pmesg(1, "input: [%s] output: [%s] input: %d, output: %d backgrounding is %d\n",input_file_name, output_file_name, redirect_input, redirect_output, backgrounding);
if (numCommands > 1 && i == 0) // we may be pipelining and this is the first process
{
sprintf(fifo, "%s%i", "fifo", i);
printf("Initial output: %s.\n", fifo);
freopen(fifo, "w", stdout);
//~ unlink(fifo);
}
else if (numCommands > 1 && i !=0 && (i != numCommands-1)) // we are pipelining and this is not the first or last process
{
sprintf(fifo, "%s%i", "fifo", i-1);
printf("Input for process %i: %s.\n", i, fifo);
freopen(fifo, "r", stdin);
//~ unlink(fifo);
sprintf(fifo, "%s%i", "fifo", i+1);
printf("Output for process %i: %s.\n", i, fifo);
freopen(fifo, "w", stdout);
//~ unlink(fifo_2);
}
else if (numCommands != 1 &&i == numCommands)
{
char *fifo = malloc(strlen("fifo")+2);
sprintf(fifo, "%s%i", "fifo", i);
freopen(fifo, "r", stdin);
free(fifo);
}
if(redirect_output == 1)
{
freopen(output_file_name, "w", stdout);
}
if(redirect_input == 1)
{
freopen(input_file_name, "r", stdin);
}
if (backgrounding != 0)
{
freopen("/dev/null", "w", stdout);
}
pmesg(2, "This is the child process, running execlp.\n");
pmesg(1, "Command: [%s]\n", parameters_array[0]);
if (execvp(parameters_array[0], parameters_array) < 0)
{
fprintf(to_write_to, "Could not execute the external command. errno: %i.\n", errno);
exit(EXIT_FAILURE);
}
else { pmesg(2, "Executed the child process.\n");}
}
else
{
if (backgrounding != 0)
{
enqueue(&process_queue, pid, clock(), 0, 0);
printQueue(process_queue);
}
if (backgrounding == 0) { while(wait(&status) != pid); }// Wait for the child to finish executing
}
pmesg(1, "The child has finished executing.\n");
free(parameters_array);
}
free(commands);
}
I've placed some comments in-line to your code below, but XAder's summary is worth reading first, to help understand the big picture before my detailed nitpicks.
I suggest writing out, in very long form, the exact code you would execute for a pipeline of two commands, then the code for a pipeline of three commands, and then four.. and then place a loop around the duplicated code. (The fact that you've only got one fork() suggests to me that you did the code for two commands, but not three. :)
Hope this helps.
/* if numCommands == 1 or 0, the rest is probably useless too
so this should guard the entire routine, not just the routine
that creates FIFOs */
if (numCommands > 1)
{
int j;
for (j = 0; j < numCommands - 1; j++)
{
char *fifo = malloc(strlen("fifo")+1); /* BUG #1 */
sprintf(fifo, "%s%i", "fifo", j); /* BUG #2 */
mkfifo(fifo, S_IWUSR | S_IRUSR );
free(fifo); /* messy */
}
}
/* Bug #1 The malloc(strlen()+1) is only good for "fifo", doesn't
actually allocate space for your number. Don't forget the
ascii NUL character at the end of the string. Add another +1. */
/* Bug #2 works for numCommands < 10; with 10 commands, you'll need to +2
for two characters, and so on */
/* messy -- maybe just have a 'char fifo[MAXFIFOLEN];' allocation,
and set MAXFIFOLEN to 255 or 4096 or something. No need to allocate
and free a simple little buffer a dozen times. */
pid_t pid = fork();
/* You should probably place fork() in a for loop; one new process
per command. Another choice is to write this whole routine recursively
which might be adding too many moving parts into one program */
pmesg(2, "Process forked. ID = %i. \n", pid);
/* Note that parent and child get different values of 'pid', so printing
the value of pid here may be very confusing */
int status;
if (fork < 0) /* BUG #3 fork() is a function; should be pid */
{
fprintf(to_write_to, "Could not fork a process to complete the external command.\n");
/* perror() is a wonderful routine! it helps users know _why_ something failed */
exit(EXIT_FAILURE);
}
if (pid == 0) // This is the child process
/* only ONE path below will ever execute; this is why your fork()
should be in a loop over numCommands, so each command's process
can get its own individual stdin and stdout hooked together */
{
pmesg(1, "input: [%s] output: [%s] input: %d, output: %d backgrounding is %d\n",input_file_name, output_file_name, redirect_input, redirect_output, backgrounding);
if (numCommands > 1 && i == 0) // we may be pipelining and this is the first process
{
char *fifo = malloc(strlen("fifo")+2);
sprintf(fifo, "%s%i", "fifo", i); /* could just be sprintf(fifo, "fifo%i", i); */
printf("Initial output: %s.\n", fifo);
freopen(fifo, "w", stdout);
//~ unlink(fifo);
free(fifo);
}
else if (numCommands > 1 && i !=0 && (i != numCommands-1)) // we are pipelining and this is not the first or last process
{
char *fifo = malloc(strlen("fifo")+2);
sprintf(fifo, "%s%i", "fifo", i-1);
printf("Input for process %i: %s.\n", i, fifo);
freopen(fifo, "r", stdin);
//~ unlink(fifo);
char *fifo_2 = malloc(strlen("fifo")+2);
sprintf(fifo_2, "%s%i", "fifo", i+1);
printf("Output for process %i: %s.\n", i, fifo);
freopen(fifo_2, "w", stdout);
//~ unlink(fifo_2);
free(fifo);
free(fifo_2);
}
else if (numCommands != 1 &&i == numCommands)
{
char *fifo = malloc(strlen("fifo")+2);
sprintf(fifo, "%s%i", "fifo", i);
freopen(fifo, "r", stdin);
free(fifo);
}
In general:
bad memory allocation for mkfifo filename
according to the code there's logic only for the child process and it can go only by one of 3 paths so always only one process opens file and according to the man pages it shall be blocked until two parties will open fh.
i, numofcommands and logic are very unclear - Try this approach
Related
I am trying to use multiple processes with the fork and pipe functions to modify a program that reads through a file and return the total number of lines, words, and characters. The program compiles and runs fine, but currently the output is only correct when the user inputs 1 for the number of child processes. For every other positive integer the program returns the correct value multiplied by the number of processes. (ex: if number of lines in file is 4 and user inputs 2 for number of processes, the returned value is 8) Does anyone know how I can fix this so that each process divides the work of reading through the file instead of each reading through the entire thing? The user enters the file name and the number of child processes they want when they run the program. I do not care about efficiency at the moment, only that the output is correct. Here is my code:
//wc.h
#ifndef WC_H
#define WC_H
#include <stdio.h>
typedef struct count_t {
int linecount;
int wordcount;
int charcount;
} count_t;
count_t word_count(FILE* fp, long offset, long size);
extern int crashRate;
#endif
//wc_mul.c
#include "wc.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#define MAX_PROC 100
#define MAX_FORK 100
int crashRate = 0;
count_t word_count(FILE* fp, long offset, long size)
{
char ch;
long rbytes = 0;
count_t count;
// Initialize counter variables
count.linecount = 0;
count.wordcount = 0;
count.charcount = 0;
printf("[pid %d] reading %ld bytes from offset %ld\n", getpid(), size, offset);
if(fseek(fp, offset, SEEK_SET) < 0) {
printf("[pid %d] fseek error!\n", getpid());
}
while ((ch=getc(fp)) != EOF && rbytes < size) {
// Increment character count if NOT new line or space
if (ch != ' ' && ch != '\n') { ++count.charcount; }
// Increment word count if new line or space character
if (ch == ' ' || ch == '\n') { ++count.wordcount; }
// Increment line count if new line character
if (ch == '\n') { ++count.linecount; }
rbytes++;
}
srand(getpid());
if(crashRate > 0 && (rand()%100 < crashRate))
{
printf("[pid %d] crashed.\n", getpid());
abort();
}
return count;
}
int main(int argc, char **argv)
{
long fsize;
FILE *fp;
int numJobs;
//plist_t plist[MAX_PROC];
count_t total, count, buf;
int i, j, pid, status, p[2];
int nFork = 0;
if(argc < 3) {
printf("usage: wc <# of processes> <filname>\n");
return 0;
}
if(argc > 3) {
crashRate = atoi(argv[3]);
if(crashRate < 0) crashRate = 0;
if(crashRate > 50) crashRate = 50;
}
printf("crashRate RATE: %d\n", crashRate);
numJobs = atoi(argv[1]);
if(numJobs > MAX_PROC) numJobs = MAX_PROC;
total.linecount = 0;
total.wordcount = 0;
total.charcount = 0;
// Open file in read-only mode
fp = fopen(argv[2], "r");
if(fp == NULL) {
printf("File open error: %s\n", argv[2]);
printf("usage: wc <# of processes> <filname>\n");
return 0;
}
fseek(fp, 0L, SEEK_END);
fsize = ftell(fp);
fclose(fp);
// calculate file offset and size to read for each child
for(i = 0; i < numJobs; i++) {
if(nFork++ > MAX_FORK) return 0;
if (pipe(p) != 0)
exit(1);
pid = fork();
if(pid < 0) {
printf("Fork failed.\n");
} else if(pid == 0) {
// Child
fp = fopen(argv[2], "r");
count = word_count(fp, 0, fsize);
write(p[1], &count, sizeof(count));
close(p[0]);
close(p[1]);
// send the result to the parent through the message queue
fclose(fp);
return 0;
}
}
waitpid(pid, &status, 0);
close(p[1]);
for (j=0; j < numJobs; j++) {
read(p[0], &buf, sizeof(count));
total.linecount += buf.linecount;
total.wordcount += buf.wordcount;
total.charcount += buf.charcount;
}
// Parent
// wait for all children
// check their exit status
// read the result from normalliy terminated child
// re-crete new child if there is one or more failed child
printf("\n========== Final Results ================\n");
printf("Total Lines : %d \n", total.linecount);
printf("Total Words : %d \n", total.wordcount);
printf("Total Characters : %d \n", total.charcount);
printf("=========================================\n");
return(0);
}
The pipe must be opened before you fork. Otherwise it's not available in the parent.
Pass a pointer to count, not count itself, to read and write:
write(p[1], &count, sizeof(count));
..
read(p[0], &buf, sizeof(count));
Each child needs to close the write-end of the pipe after the write via close(p[1]). Now, you're only closing the read-end.
PS: And adding the usual result checking for read and write would be advisable too.
If you use a single pipe for multiple processes, once the first child closes the pipe, it's closed and can't be read anymore.
You need an array of pipes, one for each process:
int p[numJobs][2];
Below code worked for me. I added some printf for better understanding.
int main(int argc, char **argv)
{
long fsize;
FILE *fp;
int numJobs;
count_t total, count, buf;
int i, j, pid, status;
if(argc < 3) {
printf("usage: wc <# of processes> <filname>\n");
return 0;
}
if(argc > 3) {
crashRate = atoi(argv[3]);
if(crashRate < 0) crashRate = 0;
if(crashRate > 50) crashRate = 50;
}
printf("crashRate RATE: %d\n", crashRate);
numJobs = atoi(argv[1]);
if(numJobs > MAX_PROC) numJobs = MAX_PROC;
int p[numJobs][2];
total.linecount = 0;
total.wordcount = 0;
total.charcount = 0;
// Open file in read-only mode
fp = fopen(argv[2], "r");
if(fp == NULL) {
printf("File open error: %s\n", argv[2]);
printf("usage: wc <# of processes> <filname>\n");
return 0;
}
fseek(fp, 0L, SEEK_END);
fsize = ftell(fp);
fclose(fp);
// calculate file offset and size to read for each child
for(i = 0; i < numJobs; i++) {
if (pipe(p[i]) != 0) exit(1);
pid = fork();
if(pid < 0) {
printf("Fork failed.\n");
exit(1);
} else if(pid == 0) {
// Child
fp = fopen(argv[2], "r");
count = word_count(fp, 0, fsize);
fclose(fp);
close(p[i][0]);
// send the result to the parent through the message queue
long bytes_sent;
if ( (bytes_sent = write(p[i][1], &count, sizeof(count)) ) ==-1) {
printf("Writing into pipe failed.\n");
exit(1);
};
printf("Child process %d sent %ld bytes (%'d lines, %'d words, %'d chars) \n",getpid(), bytes_sent, count.linecount, count.wordcount, count.charcount);
close(p[i][1]);
_exit(0);
}
}
// wait for all child processes to close
while(wait(NULL) != -1){};
long bytes_read;
for (j=0; j < numJobs; j++) {
if ((bytes_read = read(p[j][0], &buf, sizeof(buf)) ) ==-1) {
printf("Reading from pipe failed.\n");
exit(1);
};
if (bytes_read){
printf("Parent process %d read %ld bytes (%'d lines, %'d words, %'d chars) \n",getpid(), bytes_read, buf.linecount, buf.wordcount, buf.charcount);
total.linecount += buf.linecount;
total.wordcount += buf.wordcount;
total.charcount += buf.charcount;
}
close(p[j][0]);
close(p[j][1]);
}
// Parent
// wait for all children
// check their exit status
// read the result from normalliy terminated child
// re-create new child if there is one or more failed child
printf("\n========== Final Results ================\n");
printf("Total Lines : %d \n", total.linecount);
printf("Total Words : %d \n", total.wordcount);
printf("Total Characters : %d \n", total.charcount);
printf("=========================================\n");
return(0);
}
Would this code be considered concurrent or sequential? I think it is concurrent because there a for-loop and it creates processes and for each process there a child,parent, etc... so they all run in parallel. Would this be right?
Code:
int main(int argc, char *argv[])
{
// ATTRIBUTES //
int freq[argc-1], ipc[argc][2], nbytes, i;// freq of words, pipes, counters
ssize_t errorfi;
char readbuffer[9999];
char** k = malloc(50);
char** op = malloc(50);
if(argc == ONE) { // there are no files given, throw error and close
fprintf(stderr, "There are no files found from %s\n", argv[0]);
exit(0);
}
for(i = 1; i < argc; i++) // creates pipes for ipc
pipe(ipc[i]);
for(i = 1; i < argc; i++) { // reads input after position 0(a.out)
pid_t pid = fork(); // creates process
if( pid < 0 ) { // bad fork process: error
perror("bad fork");
exit(0);
}
else if(pid > 0) { //parent process
close(ipc[i][1]);
wait(NULL);
nbytes = read(ipc[i][0], readbuffer, sizeof(readbuffer));
if(nbytes > 0)
printf("%s\n", readbuffer);
}
else if(pid == 0) { // child process
close(ipc[i][0]);
k = inputReader(argv[i]); // finds filename,w1,w2,w3,uniqueWords
char info[50];
strcat(info, k[0]);
strcat(info, " ");
strcat(info, k[1]);
strcat(info, " ");
strcat(info, k[2]);
strcat(info, " ");
strcat(info, k[3]);
strcat(info, " ");
strcat(info, k[4]);
int uniqueWordint = atoi(k[4]);
freq[i-1] = uniqueWordint; // freq of all uniqueWords
errorfi = write(ipc[i][1], info, strlen(info)+1); // writes info to pipe i
if (errorfi < 0 ) {
fprintf(stderr, "error found when writing in pipe errofi: %d\n", errorfi);
exit(0);
}
exit(0); // close process
}// closes child process
} // closes for-loop for each process
return(0); // close main
} // closes main
I have looked all over the internet and die.net and can't see to make my code work. My problem is that I am able to redirect the output to a file, but have trouble bringing it back to standard out, I have tried using dup, dup2 and close, but maybe I am using them wrong. Any help would be appreciated, thank you
. My problem begins at the if(myargc >= 3) block when I am trying to redirect the output.
main()
{
int i, myargc =0, background, newfile, file, stdout2, read = 0, write = 0;
pid_t pid;
char input[512], *myargv[60];
while(1)
{
background = 1;
printf("Myshell>");
gets(input);
//scanf("%s", input);
myargc = parser(input, myargv);
if(strcmp(*myargv, "exit") == 0)
{
exit(0);
}
if(strcmp(myargv[myargc-1], "&") == 0)
{
background = 0;
myargv[myargc-1] = '\0';
myargc--;
}
if(myargc >= 3)
{
if(strcmp(myargv[myargc-2], ">") == 0)
{
write = 1;
file = creat(myargv[myargc-1], S_IWUSR);
myargv[myargc-2] = '\0';
if(file < 0)
{
printf("File could not be created.\n");
}
printf("Redirecting output to file %s.\n", myargv[myargc-1]);
fflush(stdout);
stdout2 = dup(STDOUT_FILENO);
//fclose(stdout); // fclose() for type FILE*
newfile = dup2(file, 1); // uses lowest number descriptor (1, since just
// closed stdout)
close(file); // closes old file descriptor duplicate, close() uses int
myargc = myargc-2;
}
}
if ((pid = fork()) == -1 )
{
// if fork fails, print error and exit
perror("Fork failed");
exit(-1);
}
else if (pid == 0) { // child process
if (read == 0 && write == 0)
{
printf("This is the child ready to execute: ");
fflush(stdout);
for (i =0; i < myargc; i++)
{
printf("%s ", myargv[i]);
fflush(stdout);
}
printf("\n");
}
if (execvp(*myargv,myargv) < 0);
{
printf("Execution failed.");
}/* error exit - exec returned */
close(newfile);
dup2(stdout2, STDIN_FILENO);
//close(file);
//if (close(file) == 0)
//{
//dup2(newfile, STDOUT_FILENO);
//close(newfile);
//close(stdout2);
// printf("Reopened stdout\n");
// }
perror("Exec returned");
exit(-1);
}
close(newfile);
if (background == 1) { /* this is the parent -- wait for child to terminate */
wait(pid,0,0);
printf("The parent is exiting now\n");
}else{
waitpid(pid, NULL, WNOHANG); //returns immediately, no wait
}
//test for correct parsing
printf("myargv:\n");
for (i = 0; i < myargc; i++)
{
printf("%s\n", myargv[i]);
}
printf("myargc: %d\n", myargc);
// clear out buffers
memset(&myargv[0], 0, sizeof(myargv));
memset(&input[0], 0, sizeof(input));
}
}
I'm working on a mini shell for a college assignment. We have to read in the command, find the binary to execute from the path var, and execute command, both with and without pipes. I have everything working (I think) except for the pipe.
Through web searches I've been able to build a test program that use two hard coded commands and pipes one to the other, with the expected results. Now when I copy and paste that code into my actual program, the first command outputs fine (actually outputs the command as if there were no pipe), while the second I don't think actually does anything (the output from the first is not piped through to the second).
Here is the entire code:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define BUFFSIZE 1024
#define MAXWORDS 17
#define MAXCHAR 64
static char *path;
extern char **environ;
//split cmd "string" on pipe (|) symbol
void split(char **pipe, char **left, char **right, int n)
{
int i, x;
for(i = 0; i < n; i++)
{
if (strchr(&pipe[i][0], '|') != 0)
{
for(x = 0; x < i; x++)
strcpy(left[x], pipe[x]);
left[x++] = 0;
break;
}
}
i++;
for(x = 0; i < n; x++)
strcpy(right[x], pipe[i++]);
right[x++] = 0;
}
//Find directory where cmd can be executed from (PATH or direct access)
char *finddir(char *s)
{
char *pp;
char *pf;
int ok;
strcpy(path, getenv("PATH"));
pp = strtok(path, ":");
while (pp != NULL)
{
pf = (char *)malloc(strlen(pp) + strlen(s) + 2);
if (pf == NULL)
{
fprintf(stderr, "Out of memory in finddir\n");
return NULL;
}
strcpy(pf,pp);
strcat(pf,"/");
strcat(pf,s);
ok = !access(pf, X_OK);
free(pf);
if (ok)
return pp;
pp = strtok(NULL, ":");
}
return NULL;
}
int cmdcheck(char *cmd, char *p)
{
char *dir;
if (strchr(p, '/') != NULL)
sprintf(cmd, "%s\0", p);
else
{
dir = finddir(p);
if (dir == NULL)
return 1;
else
sprintf(cmd, "%s/%s\0", dir, p);
}
return 0;
}
void runpipe(int pfd[], char *cmd1, char *p1[], char *cmd2, char *p2[])
{
int pid;
int status;
switch (pid = fork())
{
case 0: //Child
dup(pfd[0]);
close(pfd[1]); //the child does not need this end of the pipe
execve(cmd2, p2, environ);
perror(cmd2);
default: //Parent
dup(pfd[1]);
close(pfd[0]); //the parent does not need this end of the pipe
execve(cmd1, p1, environ);
perror(cmd1);
case -1: //ERROR
perror("fork-RP");
exit(1);
}
}
int main(void)
{
int status; //read status when reading cmd in
char ch; //character currently reading
int n, i, x; //(n) count of chars read; (i) cmd args iter; (x) cmd arg iter in cmd array
char buffer[BUFFSIZE]; //read buffer
char *token; //token var when splitting buffer
int pid0, pid1, pid2; //return ID from fork call
int which; //return value from wait (child pID that just ended)
char msg[100]; //messages to print out
char *cmd1, *cmd2; //cmds when piping
char *params[MAXWORDS]; //cmd parameters to send to execve
int fd[2]; //pipe file descriptors
char *pparam1[MAXWORDS]; //cmd "string" on left side of pipe
char *pparam2[MAXWORDS]; //cmd on right side of pipe
for(;;)
{
for (i = 0; i < MAXWORDS; i++)
params[i] = malloc(MAXCHAR);
n = 0;
write(1, "# ", 2);
for(;;)
{
status = read(0, &ch, 1);
if (status == 0)
return 0; //End of file
if (status == -1)
return 1; //Error
if(n == BUFFSIZE)
{
write(1, "Line too long\n", 14);
return 1;
}
buffer[n++] = ch;
if(ch == '\n')
break;
}
buffer[n] = '\0';
x = 0;
token = strtok(buffer, " \t\n\0");
while(token != NULL)
{
strcpy(params[x++], token);
token = strtok(NULL, " \t\n\0");
}
params[x] = 0;
path = getenv("PATH");
if (path == NULL)
{
fprintf(stderr, "PATH environment variable not found.\n");
return 1;
}
n = strlen(path);
path = (char *)malloc(n+1);
if (path == NULL)
{
fprintf(stderr, "Unable to allocate space for copy of PATH.\n");
return 1;
}
cmd1 = malloc(MAXCHAR);
cmd2 = malloc(MAXCHAR);
for (i = 0; i < MAXWORDS; i++)
pparam1[i] = malloc(MAXCHAR);
for (i = 0; i < MAXWORDS; i++)
pparam2[i] = malloc(MAXCHAR);
split(params, pparam1, pparam2, x);
//Check first cmd
if(cmdcheck(cmd1, pparam1[0]))
{
sprintf(msg, "cmd '%s' is not executable\n", pparam1[0]);
write(1, msg, strlen(msg));
break;
}
//Check second cmd
if(cmdcheck(cmd2, pparam2[0]))
{
sprintf(msg, "cmd '%s' is not executable\n", pparam2[0]);
write(1, msg, strlen(msg));
break;
}
pipe(fd);
switch (pid0 = fork())
{
case 0: //Child
switch (pid1 = fork())
{
case 0: //Child
runpipe(fd, cmd1, pparam1, cmd2, pparam2);
exit(0);
default:
exit(0);
//break;
case -1: //ERROR
perror("fork-2");
exit(1);
}
default: //Parent
which = wait(&status);
if (which == -1)
{
write(1, "wait failed\n", 12);
exit(1);
}
if (status & 0xff)
sprintf(msg, "process %d terminated abnormally for reason %d\n", which, status & 0xff);
else
sprintf(msg, "process %d terminated normally with status %d\n", which, (status >> 8) & 0xff);
write(1, msg, strlen(msg));
break;
case -1: //ERROR
perror("fork-1");
exit(1);
}
free(cmd1);
free(cmd2);
for (i = 0; i < MAXWORDS; i++)
free(pparam1[i]);
for (i = 0; i < MAXWORDS; i++)
free(pparam2[i]);
free(path);
for (i = 0; i < MAXWORDS; i++)
free(params[i]);
}
return 0;
}
Typing echo one | wc -l at the prompt will only output one with the respective wait print statement following. It has been a few years since I've used C, so am I on the right track?
Thanks.
EDIT:
Here is the runpipe function as it stands now. But the only thing that is printed is the wait statement.
void runpipe(int pfd[], char *cmd1, char *p1[], char *cmd2, char *p2[])
{
const int READ = 0;
const int WRITE = 1;
int pid;
int status;
switch (pid = fork())
{
case 0: //Child
close(pfd[WRITE]);
dup2(pfd[READ], STDIN_FILENO);
close(pfd[READ]);
execve(cmd2, p2, environ);
perror(cmd2);
default: //Parent
close(pfd[READ]);
dup2(pfd[WRITE], STDOUT_FILENO);
close(pfd[WRITE]);
execve(cmd1, p1, environ);
perror(cmd1);
case -1: //ERROR
perror("fork-RP");
exit(1);
}
}
There are a couple of things going on there that are contributing to the unexpected behavior.
The first is that you're forking too much. If you unroll your runpipe() function call into the switch statement in main(), you'll see that you reach the great-grandchild level:
switch (pid0 = fork())
{
case 0: // Child
switch (pid1 = fork())
{
case 0: // GRAND-Child
// function call to runpipe()
switch (pid = fork())
{
case 0: // GREAT-GRAND-Child
close(pfd[WRITE]);
dup2(pfd[READ], STDIN_FILENO);
close(pfd[READ]);
execve(cmd2, p2, environ);
perror(cmd2);
default: // GRAND-Child
close(pfd[READ]);
dup2(pfd[WRITE], STDOUT_FILENO);
close(pfd[WRITE]);
execve(cmd1, p1, environ);
perror(cmd1);
Which is not necessary. Fork once in main() and then call your runpipe() function.
Related to this issue is where you're creating your pipe. When you fork, the newly created child process inherits all of the parent process's open files (among many other things). This includes the default descriptors 0, 1, and 2 (stdin, stdout, and stderr), as well as any other open files, including the pipe you created called fd. This means that the parent, child, grandchild, and great-grandchild are all inheriting a copy of both ends of the pipe. You correctly close the unused ends inside the runpipe() function (the grandchild's and great-grandchild's copies), but the parent and child in your main() function also have copies!
Since the only pair of processes using the pipe are those created in runpipe(), you can move the declaration of fd and the call to pipe(2) into that function.
These two modifications will resolve your issues.
A completely unrelated issue that just relates to the flow of your shell is that your main() ends up doing its wait(2) on the "parent" process of the runpipe() function. Since that parent is the one running cmd1, your shell is going to return its prompt as soon as cmd1 finishes, instead of when the last command (cmd2 in this case) in the pipeline finishes. You can see the behavioral difference by running something like echo | sleep 10 into your shell and a real shell.
The dup function duplicates a file descriptor, and returns the new duplicate. However, this will not work, as stdin in the child still exists, and the new file descriptor will not be put in place of the standard input.
You must close the standard input file descriptor first, before doing dup. Or use dup2 which will close the destination file descriptor automatically first before doing the duplication:
dup2(pfd[0], STDIN_FILENO);
So I am at the very end of a project to create a basic UNIX shell using C. I have finished a lot of different pieces of the program, but now I would like to conquer piping. I would specifically like to create a program that can handle any number of pipes.
For some reason my code get to s certain line (labeled: //DIES HERE) and then stops and I can't figure out why.
Here is the code that I have so far:
//the contents of args[0] is {"ls","-l","-o"}
//the contents of args[1] is {"wc","-l"}
int pipefd[2];
pipe(&pipefd[0]); // Error check!
fflush(stdout);
for (i = 0; i < commands; i++){
int pid = fork();
if (pid == 0){
int command_no = i;
int prev_pipe = ((command_no - 1) % 2) * 2;
int current_pipe = (command_no % 2) * 2;
printf("\ncmd %d: prev pipe %d, curr pipe %d\n\n", i, prev_pipe, current_pipe);
fflush(stdout);
// If current command is the first command, close the
// read end, else read from the last command's pipe
if (command_no == 0){
close(pipefd[0]);
}
else{
dup2(pipefd[prev_pipe], 0);
close(pipefd[current_pipe]);
}
// If current command is the last command, close the
// write end, else write to the pipe
if (command_no == commands - 1){
close(pipefd[current_pipe + 1]);
}
else{
dup2(pipefd[current_pipe + 1], 1); //DIES HERE
}
// printf("Here?\n\n");
execvp(*args[i], args[i]);
fprintf(stderr, "Failed to exec: %s (%d: %s)\n", arrayOfCommands[i], errno, strerror(errno));
_exit(1);
}
}
Any help is appreciated! :)
The primary issue I see is that pipe() is outside the loop. You're going to need a new pipe() between every pair of processes. The comments on your question make some good points as well.
I wrote a shell many years ago in college and here's the similar loop from my code. I'm sure I'd do it much differently now, but it may be of use to you:
for (i = 0; i < iNumPipes; ++i) {
if (i == iNumPipes - 1) {
/* this is the last command
*/
p[1] = fdOutput;
p[0] = -1;
} else if (-1 == pipe(p)) {
perror("pipe");
exit(1);
}
switch (iPid = fork()) {
case -1:
perror("fork");
exit(1);
case 0:
close(0);
dup2(fdInput, 0);
close(fdInput);
close(1);
dup2(p[1], 1);
close(p[1]);
if (-1 != fdErr) {
close(2);
dup2(fdErr, 2);
close(fdErr);
}
pc = SearchPath(pppcAvs[i][0]);
execve(pc, pppcAvs[i], ppcEnv);
perror(pc);
_exit(-1);
default:
close(fdInput);
close(p[1]);
fdInput = p[0];
}
}