|
stdout 重定向已经成功了。
但 stderr 却无论如何无法定向(看不到输出),
大侠们帮忙看看啊,调了好几天也没调通。 55555~~~
另:我已经测试过, rm用stderr进行输出。rm test.txt 2>a.txt,可以看到a.txt里有rm的提示信息
如果那位大侠有完全控制子进程IO的例子那就更好了。谢谢。
- #include <sys/wait.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #define STDIN 0
- #define STDOUT 1
- #define STDERR 2
- void os_stdio_status(int s, int *readable, int *writeable, int *error)
- {
- fd_set set;
- struct timeval tv;
- tv.tv_sec = 0;
- tv.tv_usec = 0;
- if(readable)
- {
- FD_ZERO(&set);
- FD_SET(s, &set);
- select(s + 1, &set, NULL, NULL, &tv);
- *readable = FD_ISSET(s, &set);
- }
- if(writeable)
- {
- FD_ZERO(&set);
- FD_SET(s, &set);
- select(s + 1, NULL, &set, NULL, &tv);
- *writeable = FD_ISSET(s, &set);
- }
- if(error)
- {
- FD_ZERO(&set);
- FD_SET(s, &set);
- select(s + 1, NULL, NULL, &set, &tv);
- *error = FD_ISSET(s, &set);
- }
- }
- void my_run(char *command)
- {
- /* Define gints arrays to ref pipes */
- int stdin_pipe[2];
- int stdout_pipe[2];
- int stderr_pipe[2];
- /* Define buffs to copy your reads from the pipes to */
- char buffer[BUFSIZ+1];
- char buffer_err[BUFSIZ+1];
- /* Def some vars to hold your results */
- int fork_result;
- int data_processed;
- int data_processed_err;
- int nchars=0;
- int readable, writeable, error;
-
- /* Clear Your Buffers */
- memset(buffer, '\0', sizeof(buffer));
- memset(buffer_err, '\0', sizeof(buffer_err));
- buffer[0] = 0;
- buffer_err[0] = 0;
- /* Do the fork and pipe - watch closely here */
- /* Create pipes to read from and write too */
- if( ( pipe(stdin_pipe) == 0)
- && (pipe(stdout_pipe) == 0)
- && (pipe(stderr_pipe) == 0)
- )
- {
- /* Perform the fork */
- fork_result = fork();
- /* fork attempt was not successful */
- if(fork_result == -1)
- {
- //fprintf(stderr, "Fork Failure\n");
- exit(-1);
- }
- /* We're in the child process! */
- else if(fork_result == 0)
- {
- /* Close the Child process' STDIN */
- close(0);
- /* Duplicate the Child's STDIN to the stdin_pipe file descriptor */
- dup(stdin_pipe[0]);
- /* Close the read and write to for the pipe for the child. The child will now only be able to read from it's STDIN (which is our pipe). */
- close(stdin_pipe[0]);
- close(stdin_pipe[1]);
- /* Close the Child process' STDOUT */
- close(1);
- dup(stdout_pipe[1]);
- close(stdout_pipe[0]);
- close(stdout_pipe[1]);
- /* Close the Child process' STDERR */
- close(2);
- //dup(stdout_pipe[1]);
- dup(stderr_pipe[1]);
- close(stderr_pipe[0]);
- close(stderr_pipe[1]);
- /* Make the exec call to run the javac program. */
- //execlp("javac","javac", "-d", codemonkey->current->class_path, codemonkey->current->file_name, (char *)0);
- //execlp(program,program, arg1, (char *)0);
- execl("/bin/sh", "sh", "-c", command, NULL);
- /* If javac didn't take over the exec call failed. */
- exit(-1);
- }
- /* We're in the parent process. */
- else
- {
- // Close STDIN for read & write and close STDERR for write
- //close(stdin_pipe[0]);
- //close(stdin_pipe[1]);
- //close(stderr_pipe[0]);
- //close(stderr_pipe[1]);
-
-
- //close(stdin_pipe[0]);
- close(stderr_pipe[1]);
- close(stdout_pipe[1]);
-
- while(1)
- {
-
- os_stdio_status(stderr_pipe[0], &readable, NULL, &error);
- if(error)
- {
- //puts("stderr ERROR!!!!!!!!!!!!!!!!!!");
- //break;
- }
- if(readable)
- {
- //puts("stderr readable");
- data_processed_err=read(stderr_pipe[0],buffer_err,BUFSIZ);
- if(data_processed_err >0 )
- write(STDOUT, buffer_err, data_processed_err);
- //if(data_processed_err == 0) break;
- }
-
-
-
- os_stdio_status(stdout_pipe[0], &readable, NULL, &error);
- if(error)
- {
- //puts("stdout ERROR!!!!!!!!!!!!!!!!!!");
- //break;
- }
- if(readable)
- {
- //puts("stdout readable");
- data_processed=read(stdout_pipe[0],buffer,BUFSIZ);
- if(data_processed >0 )
- write(STDOUT, buffer, data_processed);
- //if(data_processed == 0) break;
- }
-
-
- /*
- os_stdio_status(stdin_pipe[1], NULL, &writeable, &error);
- if(error)
- {
- puts("stdin ERROR!!!!!!!!!!!!!!!!!!");
- break;
- }
- if(writeable)
- {
- puts("stdin writeable");
- write(stdin_pipe[1], getchar(), 1);
- //data_processed=read(stdout_pipe[0],buffer,BUFSIZ);
- //write(STDOUT, buffer, data_processed);
- //if(data_processed == 0) break;
- }
- */
- }
- /* Close the read end of STDERR */
- close(stderr_pipe[0]);
- /* Close the write end of STDOUT */
- close(stdout_pipe[1]);
- /* Close STDOUT for reading */
- close(stdout_pipe[0]);
- }
- }
- return;
- /* THE END */
- }
- int main()
- {
- system("ls > test.txt");
- my_run("rm test.txt"); //这个总是有问题。
- //my_run("ls *.gz"); //这个完全可以了
- }
复制代码 |
|