简体   繁体   English

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

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

I am unable to redirect STDOUT to a file. 我无法将STDOUT重定向到文件。

I have two processes. 我有两个过程。 These should run for 5 seconds and then be killed. 这些应运行5秒钟,然后杀死。

Process 1: redirects its STDOUT to a pipe and then prints random nubers. 流程1:将其STDOUT重定向到管道,然后打印随机数字。

Process 2: redirects its STDIN to the pipe and its STDOUT to file. 流程2:将其STDIN重定向到管道,并将其STDOUT重定向到文件。 Then it calls procedure appc1 by execl(). 然后,它通过execl()调用过程appc1。 This procedure prints to STDOUT. 此过程将打印到STDOUT。

Problem: File out.txt contains the test print done by reader process, but nothing else from the appc1 procedure. 问题:文件out.txt包含读取器进程完成的测试打印,但是appc1过程中没有其他内容。 If I skip the redirection and print to console, everything works fine. 如果我跳过重定向并打印到控制台,一切正常。

Could it be an issue caused by killing the processes? 可能是由于终止进程引起的问题吗? Do I close the file too soon? 我是否过早关闭文件? Thank you very much for any advice, I am unable to solve this for quite some time. 非常感谢您的任何建议,我已经有一段时间无法解决此问题了。

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

This is the code. 这是代码。

Thank you guys. 感谢大伙们。

I just ran the following code based upon your code and it worked for me. 我只是根据您的代码运行了以下代码,它对我有用。 I would suggest that you may have a problem with running appc1 and you should add a printf() after the execl() that should never print unless the execl() fails to process properly. 我建议您可能在运行appc1时遇到问题,并且应该在execl()之后添加一个printf(),除非execl()无法正确处理,否则它永远都不会打印。

appc1.c 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 );
        }
    }
}

console output: 控制台输出:

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 content: out.txt内容:

Test values: 234 598

Output from appc1

When I changed appc1 execl() call to a appc1x (which does not exist) then out.txt contains: 当我将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