0

I want the rest of my c program to keep running instead of waiting for the multiple system() calls in Area - C1 to finish executing.

I have added a '&' to the end of the coommand to run in the background but while running this program, Area - C2 does not run until Area - C1 is finished running.

  1. Is there any non blocking version of system() to do this?
  2. Why "&" added at end is not working?

#include <stdio.h>

#define N1 0
#define N2 1000

int main()
{
    int i = 0, j = 0;

    /* Area - C1 */
    for (i = N1; i < N2; i++)
    {
        system("command name &");
    }

    /* Other codes */
    {
        /*Area - C2;*/
        /*Area - C3;*/
    }
    return 0;
}
Stargateur
  • 24,473
  • 8
  • 65
  • 91
Ritu
  • 11
  • 4
  • What do you want to do with the `system` calls? You might want to spawn threads. – Gaurav Sehgal Jan 04 '18 at 11:42
  • This is for Area - C1 `char command[50];sprintf( command, "ip addr add %s dev %s &",carrIPAddress, carrInterface );system(command);` I was going to add some ip addresses – Ritu Jan 04 '18 at 11:46
  • 1
    Time to open up your C book and learn about `fork` and the `exec` family of functions. – Chris Turner Jan 04 '18 at 11:49
  • 2
    @ChrisTurner That won't be in a standard C book I think. – meaning-matters Jan 04 '18 at 11:51
  • @meaning-matters pretty rubbish book on C if it doesn't teach you how to do that kind of thing IMHO – Chris Turner Jan 04 '18 at 12:01
  • @Nebril The solutions given there is not working for me – Ritu Jan 04 '18 at 12:02
  • Is there no way doing this except creating thread to do system() job ? – Ritu Jan 04 '18 at 12:04
  • @ChrisTurner It's a UNIX API so books that purely talk about C should not discuss it. My K&R doesn't mention `fork()` nor `exec()` for example; and that's **the** C book. – meaning-matters Jan 04 '18 at 12:05
  • @meaning-matters: K&R C is now the ancestor of current C and many things have changed. But you are right on one point, neither `fork()` nor `exec()` are members of the C standard library and should not be discussed in in C book. They cannot be used for example in Windows C implementations... – Serge Ballesta Jan 04 '18 at 12:27
  • The answer will depend on the OS. You will use fork/exec on Linux or Unix, and CreateProcess on Windows. So it is indeed possible but not portable. – Serge Ballesta Jan 04 '18 at 12:32
  • @alk: Are you sure this is a duplicate? As OP says that the ampersand *did not work*, I suppose he does not use Linux... But I still won't vote to reopen unless OP says what is his OS – Serge Ballesta Jan 04 '18 at 13:08

0 Answers0