У моих детей отсутствуют проблемы с Fork dup2 и execvp

Этот код должен читать в

conc ls -l /bin > Dout/concBin.txt , ls -l /usr/bin > Dout/concUsrBin.txt , ls -l /etc > Dout/concEtc.txt 

и затем распечатайте ppid, который он должен перенаправить после разветвления, но перед выполнением. в настоящее время он читает в большинстве файлов, но распечатывает корзину (в примере печатаются только имена>,<и имена файлов и PPID). Кажется, что создается только concbin.txt в каталоге Dout, а затем не генерируется другие файлы. Я думаю, что-то перепутано с моим циклом for и моим execvp. Я заранее прошу прощения за массу кода. Мой профессор предоставил заголовочный файл и основной файл, которые отлично работают с моим кодом. Stdin, stdout проверка ошибок printfs, которые я вставил, работают, так что я полностью потерян. Я думаю, что я не делаю достаточно детей с моей петлей для в Аргв

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include "cs3423p8.h"
int concCmd (Cmd cmdM[], int iCmdCnt, Token tokenM[], int iTokenCnt);
//int pipeCmd (Cmd cmdM[], int iCmdCnt, Token tokenM[], int iTokenCnt);

//prints parents processID Child's ProcessID, the Command and Command arguments
//fork each of the children, Max commands is 5. If any of the commands is redirect or output
//do redirection after forking but before execing
 //redirect stdout for each child, and execvp to the particular command for child
int concCmd (Cmd cmdM[], int iCmdCnt, Token tokenM[], int iTokenCnt)
{
     int j;
     int i;
     int count = 0;
     long lForkPid;
     long lWaitPid;
     int iExitStatus = 0;
     char *execArgv[25];
     int fdin, fdout;
     //make children
     lForkPid = fork();
     //children made
     for(i = 0; i < iCmdCnt; i++)
     {             
     //count =0;
         switch(lForkPid)
         {
             case -1:
             errExit("fork failed: %s", strerror(errno));
             break;
             case 0://child

             execArgv[0] = cmdM[i].szCmdNm;
             //count = 0;
             for(j = cmdM[i].iBeginIdx; j <= cmdM[i].iEndIdx; j++)             
                 {

                     execArgv[count + 1] = tokenM[j];
                     count++;
                 }
                     execArgv[count] = NULL;

                     if (cmdM[i].iStdinRedirectIdx !=0)
                 {
                     printf("stdin redirect \n \n ");

                     fdin = open(tokenM[cmdM[i].iStdinRedirectIdx], O_RDONLY);
                     dup2(fdin, STDIN_FILENO);
                     close(fdin);
                     fprintf(stderr,"sTDIN REDIRECT Child Process: PID=%ld, PPID=%ld\n" , (long) getpid(), (long) getppid());
                     // execvp(cmdM[i].szCmdNm, execArgv);
                     //errExit("Child process failed to exec: %s", strerror(errno));
                  }

                  if (cmdM[i].iStdoutRedirectIdx !=0)
                  {
                      printf("stdout redirect \n\n");

                      fdout = open(tokenM[cmdM[i].iStdoutRedirectIdx], O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
                      dup2(fdout, STDOUT_FILENO);
                      close(fdout);
                      fprintf(stderr, "sTDOUT REDIRECT Child Process: PID=%ld, PPID=%ld\n", (long) getpid(), (long) getppid());

                  }
                  execvp(cmdM[i].szCmdNm, execArgv);
                  //errExit("Child process failed to exec: %s", strerror(errno));


                 // exit(0);
              default://parent
                  lWaitPid = wait(&iExitStatus);
                     // if(lWaitPid == -1)
                     // errExit("wait error: %s", strerror(errno));
             }
        }
         return 0;
     }

0 ответов

Другие вопросы по тегам