LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-06-2009, 06:08 AM   #1
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 141

Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirecting stdout, stderr to pty0? Rostfrei Linux - General 4 03-20-2007 03:15 AM
copying stdout and stderr to seperate files dasy2k1 Linux - General 2 02-09-2007 06:41 AM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
redirecting stdout and stderr to a file Avatar33 Programming 4 03-12-2005 07:55 AM
redirecting stdin and stdout with pipes: convincing apps they're for a terminal? prell Programming 1 09-02-2004 06:38 AM


All times are GMT -5. The time now is 02:06 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration