此代码应该在
中读取conc ls -l /bin > Dout/concBin.txt , ls -l /usr/bin > Dout/concUsrBin.txt , ls -l /etc > Dout/concEtc.txt
然后打印出它应该在分叉之后但在执行之前重定向的ppid。目前它正在读取大多数文件,但它打印出bin(该示例仅打印>,<和文件名和PPID)它似乎只生成concout.txt到Dout目录然后不生成任何其他文件。我觉得有些东西搞砸了我的for循环和我的execvp。 我提前为大量代码道歉。我的教授提供了一个头文件和一个主文件,这些代码正常。我投入的stdin,stdout错误检查printfs正在工作,所以我完全迷失了。我想我在Argv中没有为我的for循环制作足够的孩子
#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;
}