On 15-Dec-2004, Quentin Spencer <address@hidden> wrote:
| I want to run a command using the "system" function that starts an
| external program that performs some computations. I would like to be
| able to start it in the background, and then run a loop that periocially
| checks the status of the program, loads the results, and kills the
| external process if certain conditions are met, using a loop something
| like this:
|
| PID = system(command, 1, 'async');
| while(1)
| sleep(5)
| if( PID has completed)
| break
| else
| load results
| perform computations
| if(some condition)
| kill(PID);
| break;
| end
| end
| end
|
| The problem is that I don't know how to test to see whether PID has
| completed running. Is there any way to do this using built in octave
| commands?
Yes, you can use waitpid. Something like
page_screen_output = 0;
pid = system ("xterm", 1, "async");
while (1)
sleep (5);
if (waitpid (pid, 1) == pid)
printf ("PID = %d exited\n", pid);
break;
else
printf ("PID = %d still running\n", pid);
[s, err, msg] = stat ("~/killit");
if (! err)
kill (pid, SIG.QUIT);
printf ("PID = %d killed\n", pid);
break;
endif
endif
endwhile
seems to work for me. I see messages every 5 seconds when the xterm
process is running. If I close the xterm window, the next time
through the loop, waitpid notices and breaks out of the loop. If I
touch a file called ~/killit while the loop is running, the stat call
notices the new file and the kill sends SIGQUIT to the xterm process,
which then exits.
jwe