[9] | 1 | /* test 10 */
|
---|
| 2 |
|
---|
| 3 | #include <sys/types.h>
|
---|
| 4 | #include <sys/wait.h>
|
---|
| 5 | #include <errno.h>
|
---|
| 6 | #include <fcntl.h>
|
---|
| 7 | #include <stdlib.h>
|
---|
| 8 | #include <unistd.h>
|
---|
| 9 | #include <stdio.h>
|
---|
| 10 |
|
---|
| 11 | char *name[] = {"t10a", "t10b", "t10c", "t10d", "t10e", "t10f", "t10g",
|
---|
| 12 | "t10h", "t10i", "t10j"};
|
---|
| 13 | int errct;
|
---|
| 14 | long prog[300];
|
---|
| 15 | int psize;
|
---|
| 16 |
|
---|
| 17 | _PROTOTYPE(int main, (void));
|
---|
| 18 | _PROTOTYPE(void spawn, (int n));
|
---|
| 19 | _PROTOTYPE(void mkfiles, (void));
|
---|
| 20 | _PROTOTYPE(void cr_file, (char *name, int size));
|
---|
| 21 | _PROTOTYPE(void rmfiles, (void));
|
---|
| 22 | _PROTOTYPE(void quit, (void));
|
---|
| 23 |
|
---|
| 24 | int main()
|
---|
| 25 | {
|
---|
| 26 | int i, n, pid, r;
|
---|
| 27 |
|
---|
| 28 | printf("Test 10 ");
|
---|
| 29 | fflush(stdout); /* have to flush for child's benefit */
|
---|
| 30 |
|
---|
| 31 | system("rm -rf DIR_10; mkdir DIR_10; cp t10a DIR_10");
|
---|
| 32 | chdir("DIR_10");
|
---|
| 33 |
|
---|
| 34 | pid = getpid();
|
---|
| 35 |
|
---|
| 36 | /* Create files t10b ... t10h */
|
---|
| 37 | mkfiles();
|
---|
| 38 |
|
---|
| 39 | if (getpid() == pid)
|
---|
| 40 | if (fork() == 0) {
|
---|
| 41 | execl("t10a", (char *) 0);
|
---|
| 42 | exit(0);
|
---|
| 43 | }
|
---|
| 44 | if (getpid() == pid)
|
---|
| 45 | if (fork() == 0) {
|
---|
| 46 | execl("t10b", (char *) 0);
|
---|
| 47 | exit(0);
|
---|
| 48 | }
|
---|
| 49 | if (getpid() == pid)
|
---|
| 50 | if (fork() == 0) {
|
---|
| 51 | execl("t10c", (char *) 0);
|
---|
| 52 | exit(0);
|
---|
| 53 | }
|
---|
| 54 | if (getpid() == pid)
|
---|
| 55 | if (fork() == 0) {
|
---|
| 56 | execl("t10d", (char *) 0);
|
---|
| 57 | exit(0);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | srand(100);
|
---|
| 61 | for (i = 0; i < 60; i++) {
|
---|
| 62 | r = rand() & 07;
|
---|
| 63 | spawn(r);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | for (i = 0; i < 4; i++) wait(&n);
|
---|
| 67 | rmfiles();
|
---|
| 68 | quit();
|
---|
| 69 | return(-1); /* impossible */
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | void spawn(n)
|
---|
| 73 | int n;
|
---|
| 74 | {
|
---|
| 75 | int pid, k;
|
---|
| 76 |
|
---|
| 77 | if (pid = fork()) {
|
---|
| 78 | wait(&n); /* wait for some child (any one) */
|
---|
| 79 | } else {
|
---|
| 80 | k = execl(name[n], (char *) 0);
|
---|
| 81 | errct++;
|
---|
| 82 | printf("Child execl didn't take. file=%s errno=%d\n", name[n], errno);
|
---|
| 83 | rmfiles();
|
---|
| 84 | exit(1);
|
---|
| 85 | printf("Worse yet, EXIT didn't exit\n");
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | void mkfiles()
|
---|
| 90 | {
|
---|
| 91 | int fd;
|
---|
| 92 | fd = open("t10a", 0);
|
---|
| 93 | if (fd < 0) {
|
---|
| 94 | printf("Can't open t10a\n");
|
---|
| 95 | exit(1);
|
---|
| 96 | }
|
---|
| 97 | psize = read(fd, (char *) prog, 300 * 4);
|
---|
| 98 | cr_file("t10b", 1600);
|
---|
| 99 | cr_file("t10c", 1400);
|
---|
| 100 | cr_file("t10d", 2300);
|
---|
| 101 | cr_file("t10e", 3100);
|
---|
| 102 | cr_file("t10f", 2400);
|
---|
| 103 | cr_file("t10g", 1700);
|
---|
| 104 | cr_file("t10h", 1500);
|
---|
| 105 | cr_file("t10i", 4000);
|
---|
| 106 | cr_file("t10j", 2250);
|
---|
| 107 | close(fd);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | void cr_file(name, size)
|
---|
| 111 | char *name;
|
---|
| 112 | int size;
|
---|
| 113 |
|
---|
| 114 | {
|
---|
| 115 | int fd;
|
---|
| 116 |
|
---|
| 117 | #if (CHIP == SPARC)
|
---|
| 118 | size += 4000;
|
---|
| 119 | #endif
|
---|
| 120 | prog[6] = (long) size;
|
---|
| 121 | fd = creat(name, 0755);
|
---|
| 122 | write(fd, (char *) prog, psize);
|
---|
| 123 | close(fd);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | void rmfiles()
|
---|
| 127 | {
|
---|
| 128 | unlink("t10b");
|
---|
| 129 | unlink("t10c");
|
---|
| 130 | unlink("t10d");
|
---|
| 131 | unlink("t10e");
|
---|
| 132 | unlink("t10f");
|
---|
| 133 | unlink("t10g");
|
---|
| 134 | unlink("t10h");
|
---|
| 135 | unlink("t10i");
|
---|
| 136 | unlink("t10j");
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | void quit()
|
---|
| 140 | {
|
---|
| 141 |
|
---|
| 142 | chdir("..");
|
---|
| 143 | system("rm -rf DIR*");
|
---|
| 144 |
|
---|
| 145 | if (errct == 0) {
|
---|
| 146 | printf("ok\n");
|
---|
| 147 | exit(0);
|
---|
| 148 | } else {
|
---|
| 149 | printf("%d errors\n", errct);
|
---|
| 150 | exit(1);
|
---|
| 151 | }
|
---|
| 152 | }
|
---|