how to terminate program in windows from matlab?

2

Is there any way to terminate a program in windows from a command in matlab? Possibly resorting to a batch ... but I do not know the commands to manage programs in windows nor how to run it from matlab.

I can have multiple instances of the same program, I want to finish all but the most recent.

More details: during a cycle in matlab a program in windows (Xfoil) is started thousands of times with different input parameters. According to the paramaters the program can block or enter an infinite cycle. After a set time, the matlab cycle departs to the next iteration, opening a new instance of the same program, but the previous program continues to run, occupying the processor. Sometimes dozens accumulate by completely blocking the computer.

    
asked by anonymous 09.05.2014 / 02:56

2 answers

0

I do not know if there is any more automatic way to solve this, but I think this solves your problem.

First, you should find out what the process name is running in your code (I believe it is xfoil.exe ).

So you should keep the code below running in parallel to your code, or to adapt it in a way that runs between one run and another of xfoil on your program.

% Armazenará os IDs dos processos ativos.
pids_alive = containers.Map('KeyType','int32','ValueType','double');

% O tempo de verificação:
kill_time = 3; % 3 segundos

% O nome do processo que será encerrado.
nome_do_processo = 'xfoil.exe';

% O comando que encerrará o processo.
tasklist_cmd = sprintf('tasklist /fi "imagename eq %s" /nh /fo CSV > xfoils_running.csv', nome_do_processo);

% Loop ou Método que verificará a cada X secs o tempo
while 1 

    ids = keys(pids_alive);
    tempos = values(pids_alive);

    % Mata todos os processos que estavam rodando na passo anterior:
    for i = 1:length(ids)
        if (cputime - cell2mat(tempos(i))) > (0.1 * kill_time)
            % Comando que mata o processo.
            cmdkiller = sprintf('taskkill /f /pid %d', cell2mat(ids(i)));
            system(cmdkiller);

            % Remove o ID da lista.
            remove(pids_alive, cell2mat(ids(i)));
        end
    end

    % Lista os processos atualmente rodando.
    % Salva a lista dos processos encontrados no arquivo
    % xfoils_running.csv.
    system(tasklist_cmd)

    % Lê a lista criada.
    fid = fopen('xfoils_running.csv', 'r');

    while not(feof(fid))
        % Lê uma linha do arquivo.
        tline = fgetl(fid);

        % Lê uma linha da lista de processos.
        res = textscan(tline,'%q%q%q%q%q','delimiter',',');

        % Se não encontrar nenhum processo vivo.
        if isempty(res{2})
            continue;
        end

        % Lê ID do processo.
        pid = str2num(cell2mat(res{2}));

        % Armazena o contador tempo de vida do processo
        % se ainda não estava sendo monitorado.
        if isempty(find(cell2mat(ids) == pid))
            pids_alive(pid) = cputime;
        end
    end

    % Encerra o arquivo anterior.
    fclose(fid);

    % Espera 1 segundo
    pause(1)
end

What basically solves the problem is the commands tasklist , used to list the active processes and get the ID of the processes:

tasklist /fi "imagename eq xfoil.exe" /nh /fo CSV > xfoils_running.csv

From there, just monitor the time that these processes live. If the time is longer than what you think is the limit, you kill the process by the ID using taskkill :

taskkill /f /pid ####
    
10.05.2014 / 03:15
0

"tskill" processname "without quotes

    
09.05.2014 / 07:59