| 1 | /* This is a test case where the parent process forks 10 |
| 2 | * children which contend to write to the same file. With |
| 3 | * file locking support, the data from each child should not |
| 4 | * be lost. |
| 5 | */ |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <unistd.h> |
| 9 | #include <sys/wait.h> |
| 10 | |
| 11 | extern FILE *lprofOpenFileEx(const char *); |
| 12 | int 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, format: "Requires one argument \n" ); |
| 22 | exit(status: 1); |
| 23 | } |
| 24 | FN = argv[1]; |
| 25 | truncate(file: FN, length: 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, format: "Can not open file %s from child\n" , FN); |
| 34 | exit(status: 1); |
| 35 | } |
| 36 | fseek(stream: F, off: 0, SEEK_END); |
| 37 | fprintf(stream: F, format: "Dump from Child %d\n" , i); |
| 38 | fclose(stream: F); |
| 39 | exit(status: 0); |
| 40 | } else { |
| 41 | child[i] = c; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // In parent |
| 46 | for (i = 0; i < 10; i++) { |
| 47 | int child_status; |
| 48 | if ((tid = waitpid(pid: child[i], stat_loc: &child_status, options: 0)) == -1) |
| 49 | break; |
| 50 | } |
| 51 | F = lprofOpenFileEx(FN); |
| 52 | if (!F) { |
| 53 | fprintf(stderr, format: "Can not open file %s from parent\n" , FN); |
| 54 | exit(status: 1); |
| 55 | } |
| 56 | fseek(stream: F, off: 0, SEEK_END); |
| 57 | fprintf(stream: F, format: "Dump from parent %d\n" , i); |
| 58 | return 0; |
| 59 | } |
| 60 | |