![]() |
example for redirecting stderr,stdout in seperate files using two way pipes concept
Please give example or suggestions for redirecting stderr , stdout in separate files using two way pipes concept.
I did the following example , but I am getting crashes after running some time. I write following program for redirecting stderr , stdout in separate files while running script remotly on another linux system using plink. Here out put is redirecting to outputfile and err is redirecting to errfile.Here I should wait the process upto comlpeting the total script execution. int main(){ int pid=0; int write_stdin[2], read_stdout[2]; pipe(write_stdin); pipe(read_stdout); /* Fork the process */ pid = fork(); switch (pid) { case 0: /* Child process */ close(write_stdin[1]); close(read_stdout[0]); /* Replace stdin and stdout with the new fds */ dup2(write_stdin[0], STDIN_FILENO); dup2(read_stdout[1], STDOUT_FILENO); close(write_stdin[0]); close(read_stdout[1]); int f,ferr; f=creat(outputFilename, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)); ferr=creat(errFileName, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); dup2(f,STDOUT_FILENO); dup2(ferr, STDERR_FILENO); close(f); // original not needed after dup2 close(ferr); // original not needed after dup2 execvp(exeName,argv); _exit(0); break; case -1: /* fork() error */ printf("Fork() error for host"); return NULL; break; default: /* Parent process */ /* Close unneeded fds */ close(write_stdin[0]); close(read_stdout[1]); struct pollfd waitfordata[1]; waitfordata[0].fd=read_stdout[0]; waitfordata[0].events=POLLIN; sleep(1); //run the commands talkToPlink(waitfordata,read_stdout[0],write_stdin[1], passwd); waitWithTimeOut(pid, 300, status); close(write_stdin[1]); close(read_stdout[0]); }; } int talkToPlink(struct pollfd *waitfordata ,int read_stdout,int write_stdin,const char *passwd, const char *outputFilename,const char *errFileName) { char buf[2048]; int pollRes=0; unsigned long bread = 0; while(1) { pollRes = poll(waitfordata,1,5*1000); if(pollRes < 0) { printf("ReadWithTimeOut: Poll Error:%s",strerror(errno)); return -1; // pipe closed or other failure } memset(buf, 0, 2048); bread = 0; if(pollRes > 0) if(waitfordata[0].revents > 0 ) if(waitfordata[0].revents != POLLIN) break; } } int waitWithTimeOut(pid_t pid, int timeout,int &status) { char filebuf[64]; snprintf(filebuf, 63, "/proc/%d/status", pid); int timespent = 0; int state=0; while(timespent <= timeout){ FILE *fp = fopen(filebuf,"r"); if(!fp){ printf("no process\n\n"); return 0; } char buf[64]; while(!feof(fp)){ memset(buf, 0, 64); fgets(buf, 63, fp); if(!strstr(buf, "State")) continue; if(!strstr(buf, "zombie")) break; waitpid(pid, &state, WNOHANG); if(WIFEXITED(state)) { printf("WEXITSTATUSi1\n"); status=WEXITSTATUS(state); kill(pid, SIGKILL); waitpid(pid, 0, 0); return 1; } sleep(1); fclose(fp); fp = NULL; return 0; } // end of while -- file reading.. fclose(fp); fp = NULL; timespent++; sleep(1); } // end of while -- time wait kill(pid, SIGKILL); waitpid(pid, 0, 0); return 1; } Thanks, Nagendra R |
| All times are GMT -5. The time now is 11:59 PM. |