簡體   English   中英

C將stdout重定向到子進程中exec調用的進程的文件

[英]C Redirecting stdout to a file for process called by exec in child process

我無法將STDOUT重定向到文件。

我有兩個過程。 這些應運行5秒鍾,然后殺死。

流程1:將其STDOUT重定向到管道,然后打印隨機數字。

流程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);
    }
}

這是代碼。

感謝大伙們。

我只是根據您的代碼運行了以下代碼,它對我有用。 我建議您可能在運行appc1時遇到問題,並且應該在execl()之后添加一個printf(),除非execl()無法正確處理,否則它永遠都不會打印。

appc1.c

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

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

#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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM