I know I've been posting a lot into this forum with questions that might seem....dumb, but I am learning a lot actually from those questions and the help I've been given. Many thanks( mostly to Rashakil Fol. I salute you sir!)
The latest and greatest problem, is that I'm not sure how to use piping correctly. I am under the impression it is possible to fork off multiple processes from one parent, and if that be the case, then this code I have here was designed with the "intention" of that. It however does not work, and my program will freeze up after running just one pipe (I want to be handling as many pipes as they input) also I don't know Linux commands well, so I was wondering if whoever helps me could give me some pointers on different linux commands to try out to test that this is working
( right now I'm using ls -l | grep stats, stats is the name of some files in my directory , and ps | more, but I need something that will test:
command | command | command
too. I just don't know enough commands...heh)
But first, the question. What am I really doing here? I don't think it's doing what I think it's doing or want it to be doing.
My thought was that I was closing std out of the child, then putting the piped value filedescriptor for stdout into the child. Then in parent. I'm closing the stdin file descriptor and putting the std out into the parent. Shouldnt this then send the stdout of the parent into the stdin of the child? and close up the pipe on the child's end?
Here is the code
void runPipeProc(command_t cmds[], lnkLst* tL, int cmNm, char* prNm)
{
struct tms theTime;
clock_t uT,sT;
int pidNum = cmNm;
pid_t pids[pidNum];
char* fpaths[pidNum];
char* pNames[pidNum];
//check all commands to see if they are valid commands
for(int i=0; i < pidNum; i++)
{
fpaths[i] = checkExe(cmds[i].file);
if(fpaths[i] == NULL)
{
return;
}
}
int fdes[2];
for(int j=0; j<pidNum; j++)
{
if( pipe(fdes) == -1)
{
perror("Pipe failure");
return;
}
times(&theTime);
uT = theTime.tms_cutime;
sT = theTime.tms_cstime;
pids[j] = fork();
if(pids[j] < 0)
{
cerr<< "Fork " << j << " Failed" << endl;
return;
}
else if(pids[j] == 0)
{
close(fdes[1]); //close child's stdout
dup2(fdes[0], 0); //dup ino stdin
execvp(cmds[j].file,cmds[j].args);
cerr<< cmds[j].file << ":Command not found" << endl;
exit(-5);
}
else
{
//close(fdes[0]); //close parent's stdin
dup2(fdes[1], 1); //dup into stdout
while(wait(&status) == pids[j]);
times(&theTime);
double uTime = double(theTime.tms_cutime-uT)/sysconf(_SC_CLK_TCK);
double sTime = double(theTime.tms_cstime-sT)/sysconf(_SC_CLK_TCK);
tL = adde(tL,pNames[j],uTime,sTime);
}
}
}
A little explanation about variables passed in. cmNm is hte command number. Number of different items they wanted execed. cmds is the array of commands. flpaths are the file paths. These are checked with checkExe to see if the command is a valid unix command. prNm is hte process name. when reporting something like a history, I use prNm to tell which process was running. I think that explains the outer items.
If anyone could point me in the right direction for this I would much appreciate. Thanks again for the continued help.