Note that there are some explanatory texts on larger screens.

plurals
  1. POExecuting consecutive processes with execl()
    primarykey
    data
    text
    <p>I have what is hopefully a simple question. I'd like to execute three programs called "brute_seq", "brute_pth" and "brute_omp" consecutively; basically execute "brute_seq", wait until it's finished, then execute "brute_pth" and same thing for "brute_omp".</p> <p>I've tried doing this several different ways and so far, the best solution I've come up with is this, although it's not quite what I need.</p> <pre><code>#include &lt;unistd.h&gt; int main() { if(fork() == 0) execl("brute_seq", (const char*)NULL, (char*)NULL); if(fork() == 0) execl("brute_pth", (const char*)NULL, (char*)NULL); if(fork() == 0) execl("brute_omp", (const char*)NULL, (char*)NULL); return 0; } </code></pre> <p>From what I understand, the forked process will run the execl() command, which in turn will replace the forked process with the called process - this insures that my forked process terminates when the called process ends. </p> <p>I have two problems with this implementation:</p> <ol> <li>The processes do not execute sequentially, but in parallel, which I don't want.</li> <li>Once they're all done executing, my terminal blocks in a similar way that it would block when encountering a deadlock due to poor synchronization, except worse, because I can't CTRL-Z out of it; I have to exit from the terminal and open it again.</li> </ol> <p>Any ideas?</p> <hr> <p>Thanks to Joachim's helpful tip, here's the working code that does exactly what I want:</p> <pre><code>#include &lt;sys/wait.h&gt; #include &lt;unistd.h&gt; int main() { pid_t child_pid; int status; if((child_pid = fork()) == 0) execl("brute_seq", (const char*)NULL, (char*)NULL); else { waitpid(child_pid, &amp;status, 0); if((child_pid = fork()) == 0) { execl("brute_pth", (const char*)NULL, (char*)NULL); } waitpid(child_pid, &amp;status, 0); execl("brute_omp", (const char*)NULL, (char*)NULL); } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload