Re: оффтоп
От: The Passenger СССР  
Дата: 11.04.14 13:23
Оценка:
Здравствуйте, Кодт, Вы писали:

К>

К>    signal(SIGINT,   halt_zuruck);
К>    signal(SIGTERM,  halt_zuruck);
К>    signal(SIGBREAK, halt_zuruck); // это чисто виндовское - Ctrl+Break

К>


а винда вообще ловит эти сигналы? ... а то у меня чета не очень получилось — толи не ловит совсем, толи на бряке не стопорится
по крайней мере на первых двух
Re[2]: оффтоп
От: Кодт Россия  
Дата: 11.04.14 13:42
Оценка:
Здравствуйте, The Passenger, Вы писали:

TP>а винда вообще ловит эти сигналы? ... а то у меня чета не очень получилось — толи не ловит совсем, толи на бряке не стопорится

TP>по крайней мере на первых двух

Я опытным путём выяснил, что ловит. SIGINT — это Ctrl+C, SIGBREAK — Ctrl+Break и закрывании консоли по [x], SIGTERM — не помню, как-то прилетал.
Просто в виндах есть два способа прервать программу
Перекуём баги на фичи!
Re: Как прибивать программу с OpenMP ?
От: lxa http://aliakseis.livejournal.com
Дата: 23.04.14 12:42
Оценка: 68 (1)
Не знаю, в тему ли, но вдруг...
http://www.cplusplus.com/reference/cstdlib/quick_exit/
Re[2]: Как прибивать программу с OpenMP ?
От: Vain Россия google.ru
Дата: 23.04.14 13:25
Оценка:
Здравствуйте, lxa, Вы писали:

lxa>Не знаю, в тему ли, но вдруг...

lxa>http://www.cplusplus.com/reference/cstdlib/quick_exit/
Что только не придумают, чтобы не писать правильный клинап.
[In theory there is no difference between theory and practice. In
practice there is.]
[Даю очевидные ответы на риторические вопросы]
Re: Как прибивать программу с OpenMP ?
От: Vain Россия google.ru
Дата: 23.04.14 13:32
Оценка:
Здравствуйте, Кодт, Вы писали:

К>Дано: консольная программа, портируемая под винды и линукс.

может попробывать что-нибудь такое: http://www.uni-kassel.de/eecs/fileadmin/datas/fb16/Fachgebiete/PLM/Dokumente/Research/OpenMP/thread-cancellation.html

Specification

The following new directives to support thread cancellation in
OpenMP are proposed:

#pragma omp cancelregion:
Asks all threads in the team to stop their parallel work and go
to the end of the parallel region, where only the master thread
will continue execution as usual. The emphasis here is on "asks".
The threads in the team are not cancelled immediately, but merely
a cancel flag is set for each of them (cooperative cancellation).
An exception is the thread that called the directive: it is
cancelled immediately by an implicit call of the exitregion
directive (explained below). It is the task of the programmer to
check if the cancel flag has been set, using a new OpenMP runtime
library function:

int omp_get_cancelled (void):
This function returns 1 (true), if the cancellation of the
enclosing parallel region was requested and 0 (false) otherwise.

#pragma omp exitregion:
This directive is not only useful for thread cancellation, but
can be used at any point in a parallel region to immediately end
the execution of the calling thread. This is accomplished by
jumping to the end of the present parallel region, right into its
closing implicit barrier (which is of course honored). Together,
both new directives can be used in the following way to achieve
thread cancellation:

cancelling thread:
#pragma omp cancelregion

all other threads in the team:
if (omp_get_cancelled ()) {

/* this jumps directly into the closing, implicit barrier
* at the end of the parallel region
*/
#pragma omp exitregion
}

...

[In theory there is no difference between theory and practice. In
practice there is.]
[Даю очевидные ответы на риторические вопросы]
Re: Как прибивать программу с OpenMP ?
От: McQwerty Россия  
Дата: 09.07.14 10:26
Оценка:
Здравствуйте, Кодт, Вы писали:

К>Дано: консольная программа, портируемая под винды и линукс.


К>Добавляю #pragma omp parallel for...


К>Вопрос: как грамотно и портабельно убивать программу с omp, — при том, что нет задачи аккуратно джойнить потоки. Просто чтобы она молча останавливалась.


Не совсем про OMP, но недавно по нужде придумали как убивать числодробительный поток не внося изменений в его код.
Идея — отнять доступ к памяти над которой происходит числодробление.

Используется __try/__except от Микрософта. Но, думаю, можно и портировать на что-нибудь другое.

#include <windows.h>

struct thread_params_t
{
    DWORD  data_numbers;
    DWORD* data_area;
    DWORD  data_area_size;
}; // thread_params_t

void calc_func (thread_params_t& params)
{
    while (1)
    {
        for (DWORD i = 0; i < params. data_numbers; ++ i)
        {
            params. data_area [i] = GetTickCount ();
        }
    }
} // calc_func

DWORD WINAPI calc_thread_func (LPVOID param)
{
    thread_params_t* params = (thread_params_t*) param;

    __try
    {
        calc_func (* params);
    }
    __except (EXCEPTION_EXECUTE_HANDLER)
    {
    }

    return 0;
} // calc_thread_func

DWORD round_up_to_page_size (DWORD size)
{
    SYSTEM_INFO si = { 0 };
    GetSystemInfo (&si);

    return (size + si. dwPageSize - 1) & ~(si. dwPageSize - 1);
} // round_up_to_page_size

int main (int /* argc */, char* /* argv */ [])
{
    thread_params_t thread_params = { 0 };
    thread_params. data_numbers = 10000;
    thread_params. data_area_size = round_up_to_page_size (thread_params. data_numbers * sizeof (DWORD));
    thread_params. data_area = (DWORD*) VirtualAlloc (NULL, thread_params. data_area_size, MEM_COMMIT, PAGE_READWRITE);

    if (thread_params. data_area == NULL)
    {
        return 1;
    }

    DWORD id = 0;
    HANDLE thread = CreateThread (NULL, 0, calc_thread_func, & thread_params, 0, & id);

    Sleep (1000);

    DWORD prev_access = 0;
    VirtualProtect (thread_params. data_area, thread_params. data_area_size, PAGE_NOACCESS, & prev_access);

    WaitForSingleObject (thread, INFINITE);

    CloseHandle (thread);

    VirtualFree (thread_params. data_area, thread_params. data_area_size, MEM_RELEASE);

    return 0;
} // main
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.