-2

I am trying to run multiple command in ubuntu using c++ code at the same time. I used system() call to run multiple command but the problem with system() call is it invoke only one command at a time and rest commands are in waiting. below I wrote my sample code, may this help you to get what I am trying to do. major thing is I want to run all these command at a time not one by one. Please help me.

Thanks in advance.

main()
{   
   string command[3];
   command[0]= "ls -l";
   command[1]="ls";
   command[2]="cat main.cpp";  

   for(int i=0;i<3;i++){
       system(command[i].c_str());
   }   
}
dlmeetei
  • 9,905
  • 3
  • 31
  • 38
  • 2
    In whatever book/etc you're learning C++ on UNIX from, find the section on `fork()`. – slim Jul 18 '17 at 08:04
  • (more specifially, on `fork()` and `exec()` and process-management in general. – slim Jul 18 '17 at 08:29
  • 1
    You can use `int main(){return 0;}` You are not checking the return value of `system` so you don't care if commands you run succeed or not. Might just as well not run them. – n. m. could be an AI Jul 18 '17 at 08:57

2 Answers2

2

You should read Advanced Linux Programming (a bit old, but freely available). You probably want (in the traditional way, like most shells do):

  • perhaps catch SIGCHLD (set the signal handler before fork, see signal(7) & signal-safety(7)...)

  • call fork(2) to create a new process. Be sure to check all three cases (failure with a negative returned pid_t, child with a 0 pid_t, parent with a positive pid_t). If you want to communicate with that process, use pipe(2) (read about pipe(7)...) before the fork.

  • in the child process, close some useless file descriptors, then run some exec function (or the underlying execve(2)) to run the needed program (e.g. /bin/ls)

  • call (in the parent, perhaps after having got a SIGCHLD) wait(2) or waitpid(2) or related functions.

This is very usual. Several chapters of Advanced Linux Programming are explaining it better.

There is no need to use threads in your case.

However, notice that the role of ls and cat could be accomplished with various system calls (listed in syscalls(2)...), notably read(2) & stat(2). You might not even need to run other processes. See also opendir(3) & readdir(3)

Perhaps (notably if you communicate with several processes thru several pipe(7)-s) you might want to have some event loop using poll(2) (or the older select(2)). Some libraries provide an event loop (notably all GUI widget libraries).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
-1

You have a few options (as always):

  • Use threads (C++ standard library implementation is good) to spawn multiple threads which each perform a system call then terminate. join on the thread list to wait for them all to terminate.
  • Use the *NIX fork command to spawn a new process, then within each child process use exec to execute the desired command (see here for an example of "getting the right string to the right child"). Parent process can use waitpid to determine when all children have finished running, in order to move on with the program.
  • Append "&" to each of your commands, which'll tell the shell to run each one in the background (specifically, system will start the process in the background then return, without waiting for the result). Not tried this, don't know if it'll work. You can't then wait for the call to terminate though (thanks PSkocik).

Just pointing out - if you run those 3 specific commands at the same time, you're unlikely to be able to read the output as they'll all print text to the terminal at the same time.

If you do require reading the output from within the program (though not mentioned in your question), this is relevant (although it doesn't use system).

hnefatl
  • 5,860
  • 2
  • 27
  • 49