C将stdout重定向到子进程中exec调用的进程的文件

时间:2016-05-18 15:07:16

标签: c redirect exec stdout dup

我无法将STDOUT重定向到文件。

我有两个程序。这些程序应该运行5秒然后被杀死。

进程1:将其STDOUT重定向到管道,然后打印随机的nubers。

进程2:将其STDIN重定向到管道,将其STDOUT重定向到文件。然后它通过execl()调用过程appc1。此过程将打印到STDOUT。

问题:文件out.txt包含读者进程完成的测试打印,但appc1程序没有其他任何内容。如果我跳过重定向并打印到控制台,一切正常。

这可能是杀死进程导致的问题吗?我太快关闭文件了吗?非常感谢您的任何建议,我很长一段时间都无法解决这个问题。

ret = pipe(pipefd);

if (ret == -1) {
    printf("Error creating pipe");
    exit(1);
}

reader = fork();

if (reader == 0) {  
    dup2(pipefd[0], STDIN_FILENO);

    file = fopen("out.txt", "w");
    if(file == NULL) fputs("Could not open file",stderr);

    dup2(fileno(file), STDOUT_FILENO);
    fclose(file);

    printf("Test values: %d %d\n\n", 234, 598);

    execl("appc1","appc1", NULL);
}

else {
    printf("PARENT: forking writer\n");
    writer = fork();

    if (writer == 0) {
        sleep(1);
        dup2(pipefd[1], STDOUT_FILENO);

        while (1) {
            num1 = rand() % 1000 + 1;
            num2 = rand() % 1000 + 1;
            printf("%d %d\n", num1, num2);
            sleep(1);
        }   
    }
    else {
        sleep(5);
        kill(reader, SIGUSR1);
        kill(writer, SIGUSR1);
        wait(NULL);
        exit(0);
    }
}

这是代码。

谢谢你们。

1 个答案:

答案 0 :(得分:0)

我刚刚根据您的代码运行了以下代码,它对我有用。我建议您运行appc1可能有问题,并且应该在execl()之后添加一个printf(),除非execl()无法正确处理,否则应该永远不会打印。

appc1.c

#include <stdlib.h>
#include <stdio.h>

int main()
{
   printf("Output from appc1\n" );
}

pipes.c

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

int main ()
{
  int ret;
  int num1, num2;
  pid_t reader;
  pid_t writer;
  int pipefd[2];
  FILE *file;

  printf( "PARENT: creating pipe\n" );

  ret = pipe( pipefd );
  if ( ret == -1 ) {
      printf( "Error creating pipe - %s", strerror( errno ) );
      exit (1);
  }

  printf( "PARENT: forking reader\n" );

  reader = fork();

  if ( reader == -1 ) {
      printf( "failed to fork reader\n" );
      exit (1);

  } else if ( reader == 0 ) {
      printf( "READER CHILD\n" );
      ret = dup2( pipefd[0], STDIN_FILENO );
      if ( ret == -1 ) {
         printf( "dup2 failed on pipefd[0] %s" , strerror( errno ) );
         exit (1);
      }

      file = fopen( "out.txt", "w" );
      if ( file == NULL ) {
         fputs( "Could not open file", stderr );
         exit (1);
      }

      ret = dup2( fileno( file ), STDOUT_FILENO );
      if ( ret == -1 ) {
         printf( "dup2 failed on fileno() %s" , strerror( errno ) );
         exit (1);
      }

      fclose( file );

      printf( "Test values: %d %d\n\n", 234, 598 );

      execl( "appc1", "appc1", NULL );

      printf( "execl failed - %s\n", strerror( errno ) );

  } else {
      printf( "PARENT: forking writer\n" );

      writer = fork();

      if ( writer == -1 ) {
         printf( "Failed forking writer\n" );
         exit (1);

      } else if ( writer == 0 ) {
          printf( "WRITER CHILD\n" );
          sleep( 1 );
          ret = dup2( pipefd[1], STDOUT_FILENO );
          if ( ret == -1 ) {
             printf( "dup2 failed on pipefd[1] %s" , strerror( errno ) );
             exit (1);
          }

          while( 1 ) {
              num1 = rand () % 1000 + 1;
              num2 = rand () % 1000 + 1;
              printf( "WRITER CHILD %d %d\n", num1, num2 );
              sleep( 1 );
          }

      } else {
          printf( "PARENT: sleeping\n" );
          sleep( 5 );
          printf( "PARENT: sending SIGUSR1 to reader\n" );
          kill( reader, SIGUSR1 );
          printf( "PARENT: sending SIGUSR1 to writer\n" );
          kill( writer, SIGUSR1 );
          wait( NULL );
          exit( 0 );
        }
    }
}

控制台输出:

pipes
PARENT: creating pipe
PARENT: forking reader
PARENT: forking writer
READER CHILD
PARENT: sleeping
WRITER CHILD
PARENT: sending SIGUSR1 to reader
PARENT: sending SIGUSR1 to writer

out.txt内容:

Test values: 234 598

Output from appc1

当我将appc1 execl()调用更改为appc1x(不存在)时,out.txt包含:

Test values: 234 598

execl failed - No such file or directory