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