次の方法で共有


_cwait

ほかのプロセスが終了するまで待機します。

重要

この API は、Windows ランタイムで実行するアプリケーションでは使用できません。 詳細については、「ユニバーサル Windows プラットフォーム アプリでサポートされていない CRT 関数」を参照してください。

構文

intptr_t _cwait(
   int *termstat,
   intptr_t procHandle,
   int action
);

パラメーター

termstat
指定されたプロセスの結果コードを格納するバッファーへのポインターまたは NULL

procHandle
待機するプロセス (つまり、_cwait が処理を戻す前に終了する必要のあるプロセス) へのハンドル。

action
NULL: Windows オペレーティング システムのアプリケーションでは無視されます。それ以外のアプリケーションでは、procHandle に対して実行されるアクション コードです。

戻り値

指定されたプロセスが正常に完了すると、プロセスのハンドルを返し、termstat にプロセスの結果コードを設定します。 それ以外の場合は、–1 を返し、errno に以下を設定します。

errno 説明
ECHILD 指定されたプロセスが存在しない、procHandle が無効である、または GetExitCodeProcess あるいは WaitForSingleObject API への呼び出しが失敗しました。
EINVAL action が無効です。

これらのリターン コードとその他のリターン コードについては、「errno_doserrno_sys_errlist_sys_nerr」を参照してください。

解説

_cwait 関数は、procHandle で指定されたプロセスのプロセス ID が終了するまで待機します。 _cwait に渡された procHandle の値は、指定されたプロセスを作成した _spawn 関数の呼び出しによって返される値にする必要があります。 プロセス ID が _cwait の呼び出し前に終了した場合、_cwait はすぐに処理を戻します。 プロセスで _cwait を使用すると、有効なハンドル (procHandle) を持つ別のプロセスを待機できます。

termstat は、指定されたプロセスのリターン コードを格納するバッファーへのポインターです。 termstat の値は、指定されたプロセスが Windows ExitProcess API の呼び出しによって正常に終了したかどうかを示します。 指定されたプロセスが ExitProcess または exit を呼び出した場合、_exit から戻った場合、main の終端に達した場合のいずれかで、main が内部的に呼び出されます。 渡される termstat値の詳細については、次を参照してください GetExitCodeProcess_cwaitNULL 値を指定して termstat を呼び出すと、指定したプロセスのリターン コードは格納されません。

Windows オペレーティング システムの環境では、親子関係が実装されていないため、action パラメーターは無視されます。

procHandle が -1 または -2 (現在のプロセスまたはスレッドへのハンドル) でない限り、ハンドルは閉じられます。 このような状況では、返されたハンドルは使用しないでください。

既定では、この関数のグローバル状態の適用対象は、アプリケーションになります。 この動作を変更するには、「CRT のグローバル状態」を参照してください

必要条件

ルーチンによって返される値 必須ヘッダー オプション ヘッダー
_cwait <process.h> <errno.h>

互換性の詳細については、「 Compatibility」を参照してください。

// crt_cwait.c
// compile with: /c
// This program launches several processes and waits
// for a specified process to finish.

#define _CRT_RAND_S

#include <windows.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

// Macro to get a random integer within a specified range
#define getrandom( min, max ) (( (rand_s (&number), number) % (int)((( max ) + 1 ) - ( min ))) + ( min ))

struct PROCESS
{
    intptr_t hProcess;
    char    name[40];
} process[4] = { { 0, "Ann" }, { 0, "Beth" }, { 0, "Carl" }, { 0, "Dave" } };

int main(int argc, char* argv[])
{
    int termstat, c;
    unsigned int number;

    srand((unsigned)time(NULL));    // Seed randomizer

    // If no arguments, this is the calling process
    if (argc == 1)
    {
        // Spawn processes in numeric order
        for (c = 0; c < 4; c++) {
            _flushall();
            process[c].hProcess = _spawnl(_P_NOWAIT, argv[0], argv[0],
                process[c].name, NULL);
        }

        // Wait for randomly specified process, and respond when done
        c = getrandom(0, 3);
        printf("Come here, %s.\n", process[c].name);
        _cwait(&termstat, process[c].hProcess, _WAIT_CHILD);
        printf("Thank you, %s.\n", process[c].name);

    }
    // If there are arguments, this must be a spawned process
    else
    {
        // Delay for a period that's determined by process number
        Sleep((argv[1][0] - 'A' + 1) * 1000L);
        printf("Hi, Dad. It's %s.\n", argv[1]);
    }
}

出力の順序は実行によって異なります。

Hi, Dad. It's Ann.
Come here, Ann.
Thank you, Ann.
Hi, Dad. It's Beth.
Hi, Dad. It's Carl.
Hi, Dad. It's Dave.

関連項目

プロセスと環境の制御
_spawn_wspawn 関数