File: instrprof-multiprocess.c

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (89 lines) | stat: -rw-r--r-- 2,435 bytes parent folder | download | duplicates (30)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* This is a test case where the parent process forks 10 children
 * which contend to merge profile data to the same file. With
 * file locking support, the data from each child should not
 * be lost.
 */
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

void spawn_child(PROCESS_INFORMATION *pi, int child_num) {
  wchar_t child_str[10];
  _itow(child_num, child_str, 10);
  if (!SetEnvironmentVariableW(L"CHILD_NUM", child_str)) {
    printf("SetEnvironmentVariableW failed (0x%8lx).\n", GetLastError());
    fflush(stdout);
    exit(1);
  }

  STARTUPINFOW si;
  memset(&si, 0, sizeof(si));
  si.cb = sizeof(si);

  memset(pi, 0, sizeof(PROCESS_INFORMATION));

  if (!CreateProcessW(NULL,              // No module name (use command line)
                      GetCommandLineW(), // Command line
                      NULL,              // Process handle not inheritable
                      NULL,              // Thread handle not inheritable
                      TRUE,              // Set handle inheritance to TRUE
                      0,                 // No flags
                      NULL,              // Use parent's environment block
                      NULL,              // Use parent's starting directory
                      &si, pi)) {
    printf("CreateProcess failed (0x%08lx).\n", GetLastError());
    fflush(stdout);
    exit(1);
  }
}

int wait_child(PROCESS_INFORMATION *pi) {
  WaitForSingleObject(pi->hProcess, INFINITE);

  DWORD exit_code;
  if (!GetExitCodeProcess(pi->hProcess, &exit_code)) {
    printf("GetExitCodeProcess failed (0x%08lx).\n", GetLastError());
    fflush(stdout);
    exit(1);
  }

  CloseHandle(pi->hProcess);
  CloseHandle(pi->hThread);

  return exit_code;
}

#define NUM_CHILDREN 10

int foo(int num) {
  if (num < (NUM_CHILDREN / 2)) {
    return 1;
  } else if (num < NUM_CHILDREN) {
    return 2;
  }
  return 3;
}

int main(int argc, char *argv[]) {
  char *child_str = getenv("CHILD_NUM");
  if (!child_str) {
    PROCESS_INFORMATION child[NUM_CHILDREN];
    // In parent
    for (int i = 0; i < NUM_CHILDREN; i++) {
      spawn_child(&child[i], i);
    }
    for (int i = 0; i < NUM_CHILDREN; i++) {
      wait_child(&child[i]);
    }
    return 0;
  } else {
    // In child
    int child_num = atoi(child_str);
    int result = foo(child_num);
    if (result == 3) {
      fprintf(stderr, "Invalid child count!");
      return 1;
    }
    return 0;
  }
}