创建过程不起作用

Posted

技术标签:

【中文标题】创建过程不起作用【英文标题】:Create Process doesn't work 【发布时间】:2009-12-01 14:35:45 【问题描述】:

我正在创建一个进程并向系统请求大量内核对象。我的代码是:

int main()
    //Cria processo para o Data Viewer Unit
        LPSTARTUPINFOA si;
    PROCESS_INFORMATION pi;

    // Start the child process. 
    if(!CreateProcessA( 
                "E:\\Documents\\Faculdade\\Matérias\\Automação em Tempo Real\\TP 3\\DataViewerUnit\\Debug\\DataViewerUnit.exe", // Module name
                NULL,                                   // Command line
                NULL,                                   // Process handle not inheritable
                NULL,                                   // Thread handle not inheritable
                FALSE,                              // Set handle inheritance to FALSE
                CREATE_NEW_CONSOLE,   // No creation flags
                NULL,                                   // Use parent's environment block
                NULL,                                   // Use parent's starting directory 
                si,                                     // Pointer to STARTUPINFO structure
                &pi )                                   // Pointer to PROCESS_INFORMATION structure
    ) 
    
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return 0;
    

  // Wait until child process exits.
  WaitForSingleObject( pi.hProcess, INFINITE );

    LOG("Traffic System initiating...");
    LOG("Keyboard commands:\n"
        "0              - Changes the system mode to manual.\n"
        "1              - Changes the system mode to automatic.\n"
        "Spacebar - Swaps between buffer lock and unlock.\n"
        "ESC            - Terminates the program."
        );

    system("PAUSE");
    isAutoMode = TRUE;
    terminate_progam = FALSE;
    terminate_TOU = FALSE;
    bufferSpace = BUFFER_SIZE;
    InitializeCriticalSection(&bufferCS);
    system("PAUSE");
    hMailslot = CreateFile(MAILSLOT, GENERIC_WRITE, FILE_SHARE_READ, 
        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    ASSERT(hMailslot, "Mailslot couldn't be created");

    hHasPosition = CreateSemaphore(NULL, BUFFER_SIZE, BUFFER_SIZE, 
        (LPCWSTR)"HAS_POSITION");
    ASSERT(hHasPosition, "Positions to write semaphore couldn't be created.");

    hHasMsg = CreateSemaphore(NULL, 0, BUFFER_SIZE, (LPCWSTR)"HAS_MSG");
    ASSERT(hHasMsg, "Messages to read semaphore couldn't be created.");

    hBufferLockStatus = CreateEvent(NULL, true, true, 
        (LPCWSTR)"BUFFER_LOCK_STATUS");
    ASSERT(hBufferLockStatus, "Lock status semaphore couldn't be created.");

    hTimer = CreateSemaphore(NULL, 0, 1, (LPCWSTR)"REMOTE_UNITS_TIMER");
    ASSERT(hTimer, "Timer semaphore couldn't be created.");

    nextMsgToRead = 0;
    nextPositionToWrite = 0;

    for (int i = 0;i < RU_QUANTITY;i++) 
        hRUs[i] = (HANDLE) _beginthreadex(NULL, 0, (CAST_FUNCTION)RUAction, (LPVOID)i, 0, NULL);
        ASSERT(hRUs[i], "RU " << i << "could not be created.");
        DEBUG("RU " << i << " created.");
        RUTime[i] = generateTime(100, 200);
    DEBUG("Remote Unit " << i << " interval: " <<
        RUTime[i]);
    

    HANDLE hTOU = (HANDLE) _beginthreadex(NULL, 0, (CAST_FUNCTION)TOUAction, NULL, 0, NULL);
    ASSERT(hTOU, "Traffic Optimization Unit couldn't be created.");

    bool isLocked = false;
    int typed;
    do
        typed = _getch();
        switch(typed)
        case CHAR0:         
            if(isAutoMode)
                LOG("You changed the mode to manual.")
            else
                LOG("Mode is already manual.")
                isAutoMode = false;
            break;
        case CHAR1:         
            if(!isAutoMode)
                LOG("You changed the mode to automatic.")
            else
                LOG("Mode is already automatic.")
            isAutoMode = true;
            break;
        case SPACEBAR :
            if(isLocked)
                isLocked = false;
                SetEvent(hBufferLockStatus);
                LOG("You unlocked the buffer.");
             else 
                isLocked = true;
                ResetEvent(hBufferLockStatus);
                LOG("You locked the buffer.");
            
            break;
        Default:
            break;
        
     while (typed!=ESC);
    LOG("You typed ESC, the program will be finished.");
    //Pede o fim das threads

    terminate_progam = true;
    // Importante para que as threads não fiquem 
    // travadas sem poder terminar.
    SetEvent(hBufferLockStatus);
    WaitForMultipleObjects(RU_QUANTITY, hRUs, TRUE, INFINITE);
    terminate_TOU = true;
    WaitForSingleObject(hTOU, INFINITE);


    // Close process and thread handles. 
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
    CloseHandle(hHasMsg);
    CloseHandle(hHasPosition);
    CloseHandle(hBufferLockStatus);
    CloseHandle(hTimer);

    return 0;

我已经注释了几乎所有这些代码,并且创建过程运行正常。当我留下超过 1 个内核对象时,程序停止运行,windows vista 显示“此程序停止运行...”的消息。另一个进程只是一个 cout 说了些什么……

我想知道我的代码有什么问题。 问候, 莱安德罗·利马

【问题讨论】:

编译器没有警告您未初始化的si 变量吗?永远不要忽略编译器警告。 【参考方案1】:

您将未初始化的指针传递给 STARTUPINFO 结构。您需要声明一个 STARTUPINFO 结构,初始化它的字段,并传递一个指向它的指针。

【讨论】:

【参考方案2】:

使用 zeromemory 或 memset 并初始化您的 ProcessInfo 和 StartupInfo

这是声明一个指向启动信息结构的指针,声明该结构并通过引用传递它 LPSTARTUPINFOA si

设置 si.cb = sizeof(STARTUPINFO)

类似的东西

PROCESS_INFORMATION PI;
STARTUPINFO SI;   
SI.dwFlags =  STARTF_USESTDHANDLES;
SI.cb = sizeof(STARTUPINFO);
char * sCommandLine = _strdup(sCommand.c_str());
if(!CreateProcess(NULL,sCommandLine,NULL,NULL,true,CREATE_NO_WINDOW,NULL,NULL,&SI,&PI))

【讨论】:

它仍然停止工作。 sCommandLine,我需要使用 (LPCWSTR) 进行投射。 现在我把它放在我的代码中: //Cria processo para o Data Viewer Unit PROCESS_INFORMATION pi; STARTUPINFO si; si.dwFlags = STARTF_USESTDHANDLES; si.cb = sizeof(STARTUPINFO); char * sCommandLine = _strdup("E:\\Documents\\Faculdade\\Matérias\\Automação em Tempo Real\\TP 3\\DataViewerUnit\\Debug\\DataViewerUnit.exe"); // 启动子进程。 if(!CreateProcess( (LPCWSTR)sCommandLine, // Module ... 但还是错了。 我倾向于更简洁地写成STARTUPINFO SI = sizeof STARTUPINFO ;。这会将所有其他字段清零。适用于所有以长度为前缀但应为 0 的 Windows 结构。

以上是关于创建过程不起作用的主要内容,如果未能解决你的问题,请参考以下文章

AUTHID DEFINER 参数不起作用

将存储过程数据插入临时表不起作用

插入带有参数MYSQL的存储过程不起作用

插入选择存储过程不起作用插入NULL

过程中的 PL/SQL 游标不起作用

for循环后的最后一个过程语句在pl sql中不起作用