2

I am writing a program that uses streaming data from the terminal. If you are interested the following command:

 system("candump slcan0 > sources/file.txt").

I want the system to run that command for x seconds. But a normal stop of the system call would be fine, I'll write some code that will execute the stop after x seconds.

The program will process this data and, so an exit(EXIT_SUCCESS) won't help. That will let the whole program stop instead of only the system call.

Kenavera
  • 83
  • 1
  • 8

1 Answers1

0

Instead of using system(), use fork() and exec(). fork() will give you the child process's PID.

At this point there are several, fairly standard, ways to wait for the child process to exit for some period of time. And if it doesn't, SIGKILL the child process. There should be plenty of examples in most books on UNIX/POSIX.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Thanks for your answer. This solved my problem. I want to add for people who are finding this topic, in order to let the outputing work: the "> sources/files.txt" part. I had to look at [this](http://stackoverflow.com/questions/20488574/output-redirection-using-fork-and-execl) stackoverflow page. – Kenavera Nov 17 '15 at 11:01