c - Program hangs connecting to FIFOs (named pipes) -


i'm trying implement pipelining in operating systems shell project creating number of fifo pipes using mkfifo(). currently, program seems hang after opening first fifo writing. additionally, fifo appears when doing ls on working directory. using freopen() wrong in 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);     (i = 0; < 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 %s.\n", commands[i]);          if (numcommands > 1)         {             int j;             (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 process complete external command.\n");             exit(exit_failure);         }         int status;         if (pid == 0) // child process         {             pmesg(1, "input: [%s] output: [%s] input: %d, output: %d backgrounding %d\n",input_file_name, output_file_name, redirect_input, redirect_output, backgrounding);             if (numcommands > 1 && == 0) // may pipelining , first process             {                 sprintf(fifo, "%s%i", "fifo", i);                 printf("initial output: %s.\n", fifo);                 freopen(fifo, "w", stdout);                 //~ unlink(fifo);             }             else if (numcommands > 1 && !=0 && (i != numcommands-1)) // pipelining , not first or last process             {                 sprintf(fifo, "%s%i", "fifo", i-1);                 printf("input process %i: %s.\n", i, fifo);                 freopen(fifo, "r", stdin);                 //~ unlink(fifo);                 sprintf(fifo, "%s%i", "fifo", i+1);                 printf("output 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 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 external command. errno: %i.\n", errno);                 exit(exit_failure);             }             else    { pmesg(2, "executed 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 child finish executing         }          pmesg(1, "the child has finished executing.\n");         free(parameters_array);     }     free(commands); } 

i've placed comments in-line code below, xader's summary worth reading first, understand big picture before detailed nitpicks.

i suggest writing out, in long form, exact code execute pipeline of 2 commands, code pipeline of 3 commands, , four.. , then place loop around duplicated code. (the fact you've got 1 fork() suggests me did code 2 commands, not three. :)

hope helps.

/* if numcommands == 1 or 0, rest useless    should guard entire routine, not routine    creates fifos */ if (numcommands > 1) {     int j;     (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 malloc(strlen()+1) "fifo", doesn't    allocate space number. don't forget    ascii nul character @ end of string. add +1. */ /* bug #2 works numcommands < 10; 10 commands, you'll need +2    2 characters, , on */ /* messy -- maybe have 'char fifo[maxfifolen];' allocation,    , set maxfifolen 255 or 4096 or something. no need allocate    , free simple little buffer dozen times. */  pid_t pid = fork();  /* should place fork() in loop; 1 new process    per command. choice write whole routine recursively    might adding many moving parts 1 program */  pmesg(2, "process forked. id = %i. \n", pid); /* note parent , child different values of 'pid', printing    value of pid here may confusing */ int status; if (fork < 0) /* bug #3 fork() function; should pid */ {     fprintf(to_write_to, "could not fork process complete external command.\n"); /* perror() wonderful routine! helps users know _why_ failed */     exit(exit_failure); }  if (pid == 0) // child process /* 1 path below ever execute; why fork()    should in loop on numcommands, each command's process    can own individual stdin , stdout hooked */ {     pmesg(1, "input: [%s] output: [%s] input: %d, output: %d backgrounding %d\n",input_file_name, output_file_name, redirect_input, redirect_output, backgrounding);     if (numcommands > 1 && == 0) // may pipelining , first process     {         char *fifo = malloc(strlen("fifo")+2);         sprintf(fifo, "%s%i", "fifo", i); /* sprintf(fifo, "fifo%i", i); */         printf("initial output: %s.\n", fifo);         freopen(fifo, "w", stdout);         //~ unlink(fifo);         free(fifo);     }     else if (numcommands > 1 && !=0 && (i != numcommands-1)) // pipelining , not first or last process     {         char *fifo = malloc(strlen("fifo")+2);         sprintf(fifo, "%s%i", "fifo", i-1);         printf("input 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 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);                  } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -