Output redirection using fork() and execl()

Viewed 9984

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 following command with success in a terminal window: hexdump /dev/input/mice > /dev/ttyS0

So far I have managed to use fork to create a child process, but my problem is that I can't seem to get my execl() method to work properly:

execl("/usr/bin/hexdump", "hexdump", "/dev/input/mice > /dev/ttyS0", (char*) NULL);

I have tried other variants also, like this:

execl("/usr/bin/hexdump", "hexdump", "/dev/input/mice", ">", "/dev/ttyS0", (char*) NULL);

But always with the same result, an exit value of 1 (general error).

It's also worth mentioning that I have managed to make it work by using the popen() method, where you can type the command exactly like you would have done in a terminal. The problem with popen() is that I have not found a good way for terminating the process. With fork() I get the PID, and can terminate the process by using:

kill(pid, SIGKILL);

This is a requirement, as I have to be able to stop and restart the output redirection as desired while the program is running.

2 Answers
Related