| 1 | /* $Id: fork-1.c 2290 2005-08-20 22:32:05Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * Massive forking.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2005 knut st. osmundsen <[email protected]>
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of InnoTek LIBC.
|
|---|
| 10 | *
|
|---|
| 11 | * InnoTek LIBC is free software; you can redistribute it and/or modify
|
|---|
| 12 | * it under the terms of the GNU General Public License as published by
|
|---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 14 | * (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * InnoTek LIBC is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 19 | * GNU General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU General Public License
|
|---|
| 22 | * along with InnoTek LIBC; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | /*******************************************************************************
|
|---|
| 28 | * Header Files *
|
|---|
| 29 | *******************************************************************************/
|
|---|
| 30 | #include <unistd.h>
|
|---|
| 31 | #include <stdlib.h>
|
|---|
| 32 | #include <stdio.h>
|
|---|
| 33 | #include <string.h>
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include <sys/wait.h>
|
|---|
| 36 | #include <sys/time.h>
|
|---|
| 37 |
|
|---|
| 38 | /*******************************************************************************
|
|---|
| 39 | * Global Variables *
|
|---|
| 40 | *******************************************************************************/
|
|---|
| 41 | static pid_t s_apidChildren[75];
|
|---|
| 42 | static unsigned s_cChildren = 0;
|
|---|
| 43 | static int s_cErrors = 0;
|
|---|
| 44 |
|
|---|
| 45 | /*char ach[1024*1024*8] = {0}; - test memcpy */
|
|---|
| 46 |
|
|---|
| 47 | static void reap(void)
|
|---|
| 48 | {
|
|---|
| 49 | /*printf("fork-1: info: reaping s_cChildren=%d...\n", s_cChildren);*/
|
|---|
| 50 | int iStatus = -1;
|
|---|
| 51 | pid_t pid;
|
|---|
| 52 | while ((pid = wait4(-1, &iStatus, WNOHANG, NULL)) > 0)
|
|---|
| 53 | {
|
|---|
| 54 | if (!WIFEXITED(iStatus) || WEXITSTATUS(iStatus))
|
|---|
| 55 | {
|
|---|
| 56 | printf("fork-1: child %d -> iStatus=%#x\n", pid, iStatus);
|
|---|
| 57 | s_cErrors++;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | unsigned i = s_cChildren;
|
|---|
| 61 | while (i-- > 0)
|
|---|
| 62 | {
|
|---|
| 63 | if (s_apidChildren[i] == pid)
|
|---|
| 64 | {
|
|---|
| 65 | s_apidChildren[i] = s_apidChildren[--s_cChildren];
|
|---|
| 66 | break;
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | if (i >= sizeof(s_apidChildren) / sizeof(s_apidChildren[0]))
|
|---|
| 71 | {
|
|---|
| 72 | printf("fork-1: error! pid=%d not found. (s_cChildren=%d)\n", pid, s_cChildren);
|
|---|
| 73 | s_cErrors++;
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | int main(int argc, char **argv)
|
|---|
| 79 | {
|
|---|
| 80 | int i;
|
|---|
| 81 | hrtime_t StartTime = gethrtime();
|
|---|
| 82 | for (i = 0; i < 2000; i++)
|
|---|
| 83 | {
|
|---|
| 84 | int pid = fork();
|
|---|
| 85 | switch (pid)
|
|---|
| 86 | {
|
|---|
| 87 | case -1:
|
|---|
| 88 | printf("Error errno=%d %s\n", errno, strerror(errno));
|
|---|
| 89 | s_cErrors++;
|
|---|
| 90 | break;
|
|---|
| 91 | case 0:
|
|---|
| 92 | //printf("I'm child %#x i=%d.\n", getpid(), i);
|
|---|
| 93 | exit(0);
|
|---|
| 94 | return 0;
|
|---|
| 95 |
|
|---|
| 96 | default:
|
|---|
| 97 | s_apidChildren[s_cChildren++] = pid;
|
|---|
| 98 | break;
|
|---|
| 99 | }
|
|---|
| 100 | if (!(i % 100))
|
|---|
| 101 | {
|
|---|
| 102 | printf("fork-1: info: forked %d i=%d s_cChildren=%d\n", pid, i, s_cChildren);
|
|---|
| 103 | fflush(stdout); /* this is necessary if output is redirected, the child will inherit the buffer elsewise.. */
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /*
|
|---|
| 107 | * Reap until there are free slots again.
|
|---|
| 108 | */
|
|---|
| 109 | if (s_cChildren >= 42 || (i % 519) > 488)
|
|---|
| 110 | reap();
|
|---|
| 111 | while (s_cChildren >= sizeof(s_apidChildren) / sizeof(s_apidChildren[0]))
|
|---|
| 112 | {
|
|---|
| 113 | usleep(1000);
|
|---|
| 114 | reap();
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 | hrtime_t Elapsed = gethrtime() - StartTime;
|
|---|
| 118 | printf("fork-1: Elapsed: %lld (%llx)\n", (long long)Elapsed, (long long)Elapsed);
|
|---|
| 119 |
|
|---|
| 120 | /*
|
|---|
| 121 | * Wait for the rest of the children.
|
|---|
| 122 | */
|
|---|
| 123 | int cLoops = 5*20;
|
|---|
| 124 | while (s_cChildren > 0 && cLoops-- > 0)
|
|---|
| 125 | {
|
|---|
| 126 | usleep(50000);
|
|---|
| 127 | reap();
|
|---|
| 128 | }
|
|---|
| 129 | for (i = 0; i < s_cChildren; i++)
|
|---|
| 130 | {
|
|---|
| 131 | printf("fork-1: error! pid=%d did not complete in time.\n", s_apidChildren[i]);
|
|---|
| 132 | s_cErrors++;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | if (!s_cErrors)
|
|---|
| 136 | printf("fork-1: SUCCESS\n");
|
|---|
| 137 | else
|
|---|
| 138 | printf("fork-1: FAILURE - %d errors\n", s_cErrors);
|
|---|
| 139 | return !!s_cErrors;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|