1*7c3d14c8STreehugger Robot /* This is a test case where the parent process forks 10
2*7c3d14c8STreehugger Robot * children which contend to write to the same file. With
3*7c3d14c8STreehugger Robot * file locking support, the data from each child should not
4*7c3d14c8STreehugger Robot * be lost.
5*7c3d14c8STreehugger Robot */
6*7c3d14c8STreehugger Robot #include <stdio.h>
7*7c3d14c8STreehugger Robot #include <stdlib.h>
8*7c3d14c8STreehugger Robot #include <unistd.h>
9*7c3d14c8STreehugger Robot #include <sys/wait.h>
10*7c3d14c8STreehugger Robot
11*7c3d14c8STreehugger Robot extern FILE *lprofOpenFileEx(const char *);
main(int argc,char * argv[])12*7c3d14c8STreehugger Robot int main(int argc, char *argv[]) {
13*7c3d14c8STreehugger Robot pid_t tid;
14*7c3d14c8STreehugger Robot FILE *F;
15*7c3d14c8STreehugger Robot const char *FN;
16*7c3d14c8STreehugger Robot int child[10];
17*7c3d14c8STreehugger Robot int c;
18*7c3d14c8STreehugger Robot int i;
19*7c3d14c8STreehugger Robot
20*7c3d14c8STreehugger Robot if (argc < 2) {
21*7c3d14c8STreehugger Robot fprintf(stderr, "Requires one argument \n");
22*7c3d14c8STreehugger Robot exit(1);
23*7c3d14c8STreehugger Robot }
24*7c3d14c8STreehugger Robot FN = argv[1];
25*7c3d14c8STreehugger Robot truncate(FN, 0);
26*7c3d14c8STreehugger Robot
27*7c3d14c8STreehugger Robot for (i = 0; i < 10; i++) {
28*7c3d14c8STreehugger Robot c = fork();
29*7c3d14c8STreehugger Robot // in child:
30*7c3d14c8STreehugger Robot if (c == 0) {
31*7c3d14c8STreehugger Robot FILE *F = lprofOpenFileEx(FN);
32*7c3d14c8STreehugger Robot if (!F) {
33*7c3d14c8STreehugger Robot fprintf(stderr, "Can not open file %s from child\n", FN);
34*7c3d14c8STreehugger Robot exit(1);
35*7c3d14c8STreehugger Robot }
36*7c3d14c8STreehugger Robot fseek(F, 0, SEEK_END);
37*7c3d14c8STreehugger Robot fprintf(F, "Dump from Child %d\n", i);
38*7c3d14c8STreehugger Robot fclose(F);
39*7c3d14c8STreehugger Robot exit(0);
40*7c3d14c8STreehugger Robot } else {
41*7c3d14c8STreehugger Robot child[i] = c;
42*7c3d14c8STreehugger Robot }
43*7c3d14c8STreehugger Robot }
44*7c3d14c8STreehugger Robot
45*7c3d14c8STreehugger Robot // In parent
46*7c3d14c8STreehugger Robot for (i = 0; i < 10; i++) {
47*7c3d14c8STreehugger Robot int child_status;
48*7c3d14c8STreehugger Robot if ((tid = waitpid(child[i], &child_status, 0)) == -1)
49*7c3d14c8STreehugger Robot break;
50*7c3d14c8STreehugger Robot }
51*7c3d14c8STreehugger Robot F = lprofOpenFileEx(FN);
52*7c3d14c8STreehugger Robot if (!F) {
53*7c3d14c8STreehugger Robot fprintf(stderr, "Can not open file %s from parent\n", FN);
54*7c3d14c8STreehugger Robot exit(1);
55*7c3d14c8STreehugger Robot }
56*7c3d14c8STreehugger Robot fseek(F, 0, SEEK_END);
57*7c3d14c8STreehugger Robot fprintf(F, "Dump from parent %d\n", i);
58*7c3d14c8STreehugger Robot return 0;
59*7c3d14c8STreehugger Robot }
60