简体   繁体   English

向子进程发送信号的问题

[英]Problem sending signals to child processes

I have a server that sets up a console handler with the SetConsoleCtrlHandler() function at startup and creates a number of child processes.我有一个服务器,它在启动时使用SetConsoleCtrlHandler()函数设置控制台处理程序并创建许多子进程。
My goal is this:我的目标是这样的:

  • the server receives the CTRL+C signal服务器收到CTRL+C信号

  • the server generates the same signal to all active child processes of the same group服务器向同一group所有活动子进程生成相同的信号

  • the child processes manage the signal and correctly complete their execution子进程管理信号并正确完成它们的执行

This is my code:这是我的代码:

//handler for server.c
BOOL WINAPI CtrlHandler( DWORD fdwCtrlType ) 
{ 

    //CTRL_C_EVENT
    if(fdwCtrlType == CTRL_BREAK_EVENT)
    {
        newConf=1;
        return TRUE;
    }
    else if(fdwCtrlType == CTRL_C_EVENT)
    {
        //send CTRL+C to all process with the same groupId of calling process
        GenerateConsoleCtrlEvent(0,0);//
        return FALSE;     
    }
}

// server.c main
int main(int argc, char *argv[]) {
    ...
    ...
    if(!SetConsoleCtrlHandler(CtrlHandler, TRUE))
    {
        getLastError("SetConsoleCtrlHandler failed");//function written by me
        exit(EXIT_FAILURE);
    }
    ... 
    ...
    //This is how I create child processes (in a for loop)
    STARTUPINFO info;
    GetStartupInfo(&info);
    WSAPROTOCOL_INFO protInfo;
    PROCESS_INFORMATION processInfo;
    if(!CreateProcess("workerProcess.exe",command, NULL, NULL,0,CREATE_NEW_PROCESS_GROUP, NULL, NULL, &info, &processInfo))
    {
        fprintf(stderr, "could not create process.\n");
        return -1;
    }
    ...
    ...
}


//handler for child process(workerProcess.c)
BOOL WINAPI CtrlWorkerHandler( DWORD fdwCtrlType ) 
{ 

    //CTRL_C_EVENT
    if(fdwCtrlType == CTRL_C_EVENT)
    {
        printf("\nCLOSING WORKER\n");
        return FALSE;  
    }
    return TRUE; 
}

//workerProcess.c main
int main(int argc, char *argv[]) {
    ...
    ...
    if(!SetConsoleCtrlHandler(CtrlWorkerHandler, TRUE))
    {
        fprintf(stderr,"setConsoleCtrlHandler failed\n");
        exit(EXIT_FAILURE);
    }
    ...
    ...
}

The server correctly handles the CTRL + C signal, the GenerateConsoleCtrlEvent function does not fail so I assume it can send the signal to all processes that have the same groupid as the calling process, but unfortunately the workerProcess.exe process does not seem to handle the CTRL+C signal received from the parent process服务器正确处理CTRL + C信号, GenerateConsoleCtrlEvent函数不会失败,所以我假设它可以将信号发送到与调用进程具有相同 groupid 的所有进程,但不幸的是workerProcess.exe进程似乎没有处理从父进程接收到的CTRL+C信号

Not sure and not able test right now, but I give it shot: by specifying CREATE_NEW_PROCESS_GROUP ( The new process is the root process of a new process group. ) for each worker process you basically create a new process group for each worker process.现在不确定也不能测试,但我试了一下:通过为每个工作进程指定CREATE_NEW_PROCESS_GROUP新进程是新进程组的根进程。 ),您基本上为每个工作进程创建了一个新进程组。

Then, however, the call to GenerateConsoleCtrlEvent(0, 0) , where the second 0 says the signal is generated in all processes that share the console of the calling process.但是,然后调用GenerateConsoleCtrlEvent(0, 0) ,其中第二个0表示在共享调用进程控制台的所有进程中生成信号。 seems to be futile.似乎是徒劳的。 All the worker processes are not even in the same process group as the calling process.所有工作进程甚至都不与调用进程在同一个进程组中。

Probably, you should remove the CREATE_NEW_PROCESS_GROUP flag when creating the worker processes, or remember the lpProcessInformation.dwProcessId value for each worker process and invoke GenerateConsoleCtrlEvent for each.可能,您应该在创建工作进程时删除CREATE_NEW_PROCESS_GROUP标志,或者记住每个工作进程的lpProcessInformation.dwProcessId值并为每个调用GenerateConsoleCtrlEvent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM