从 c 程序启动和停止 linux shell 命令而不阻止当前执行

start and stop a linux shell command from a c program without blocking current execution

while(variable > 0){
  updatevariable(); //variable gets updated from UDP serevr.
  // i want to execute a shell command here without blocking current execution. 
}

我尝试使用 system() 函数,但它阻止了当前执行,如果条件不满足我应该能够停止 shell 命令并且不应该等到 shell 命令结束执行。 (我正在使用的 shell 命令是 rtl_fm 并且在我们手动停止它之前它不会停止执行)。我正在专门为 linux OS 编写此代码。 我确切地知道变量低于 0 的时间。有什么方法可以在特定时间段内执行 shell 命令吗?

还没有尝试过进程、线程。 PS: 这是我在这个平台上的第一个问题

您需要使用 fork 或 clone 创建一个新进程

int clone(int (*fn)(void *), void *stack, int flags, void *arg, ...
             /* pid_t *parent_tid, void *tls, pid_t *child_tid */ );

(如果您需要更精确地控制在调用进程 'parent' 和子进程之间共享哪些执行上下文。请参阅 clone_man_pages then you can send your newly created child to run some other code (without waiting or blocking your current shell). this is done with the exec system calls family exec_family_man_pages