Search code examples
crtl-sdr

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. 
}

i tried using system() function but it is blocking current execution, i should be able to stop the shell command if the condition doesn't satisfy and shouldn't wait until the shell command finishes execution. (the shell command i'm using is rtl_fm and it doesn't stop execution until we manually stop it). i'm writing this code specifically for linux OS. And i exactly know the time when the variable falls below 0. is there any way to execute shell command for certain period of time?

haven't tried process, threads yet. PS: This is my first question on this platform.


Solution

  • you need to create a new process with fork or clone

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

    (if you need more precise control over what pieces of execution context are shared between the calling process 'parent' and the child process. see 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