43 lines · c
1// A simple fork results in two processes writing to the same file2// RUN: rm -fr %t.profdir3// RUN: %clang_pgogen=%t.profdir -o %t -O2 %s4// RUN: %run %t5// RUN: llvm-profdata show --all-functions --counts %t.profdir/default_*.profraw | FileCheck %s6// RUN: rm -fr %t.profdir7// RUN: env LLVM_PROFILE_NO_MMAP=1 %run %t8// RUN: llvm-profdata show --all-functions --counts %t.profdir/default_*.profraw | FileCheck %s9 10//11// CHECK: func1:12// CHECK: Block counts: [21]13// CHECK: func2:14// CHECK: Block counts: [10]15 16#include <sys/wait.h>17#include <unistd.h>18 19__attribute__((noinline)) void func1() {}20__attribute__((noinline)) void func2() {}21 22int main(void) {23 // child | parent24 // func1 func2 | func1 func225 func1(); // +10 | +1 (*)26 int i = 10; // |27 while (i-- > 0) { // |28 pid_t pid = fork(); // |29 if (pid == -1) // |30 return 1; // |31 if (pid == 0) { // |32 func2(); // +10 |33 func1(); // +10 |34 return 0; // |35 } // |36 } // ------------+------------37 int status; // 20 10 | 1 038 i = 10; // (*) the child inherits counter values prior to fork39 while (i-- > 0) // from the parent in non-continuous mode.40 wait(&status);41 return 0;42}43