Output redirection using fork() and execl()

前端 未结 2 1368
谎友^
谎友^ 2020-12-01 15:09

I want to use fork() to spawn off a new process in my program. The new process will have only one task: To redirect the mouse input to a serial port. I have tested the follo

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 15:48

    You cannot perform redirection in this way.

    If you want to redirect stdout for a process you're about to exec, you need to open the path and dup2 it onto the appropriate file descriptor.

    Example:

     if (!(pid = fork()) 
     {
         int fd = open("/dev/ttyS0", O_WRONLY);
         dup2(fd, 1);  // redirect stdout
         execl("/usr/bin/hexdump", "hexdump", "/dev/input/mice", NULL);
     }
    

提交回复
热议问题