Changeset 2077


Ignore:
Timestamp:
Jun 26, 2005, 1:40:34 AM (20 years ago)
Author:
bird
Message:

hmm. wonder what was wrong. improoved the test...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/libctests/libc/smoketests/fork-1.c

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.2
    r2076 r2077  
    3535#include <sys/wait.h>
    3636
     37/*******************************************************************************
     38*   Global Variables                                                           *
     39*******************************************************************************/
     40static pid_t s_apidChildren[75];
     41static unsigned s_cChildren = 0;
     42static int s_cErrors = 0;
     43
     44static void reap(void)
     45{
     46    /*printf("fork-1: info: reaping s_cChildren=%d...\n", s_cChildren);*/
     47    int iStatus = -1;
     48    pid_t pid;
     49    while ((pid = wait4(-1, &iStatus, WNOHANG, NULL)) > 0)
     50    {
     51        if (!WIFEXITED(iStatus) || WEXITSTATUS(iStatus))
     52        {
     53            printf("fork-1: child %d -> iStatus=%#x\n", pid, iStatus);
     54            s_cErrors++;
     55        }
     56
     57        unsigned i = s_cChildren;
     58        while (i-- > 0)
     59        {
     60            if (s_apidChildren[i] == pid)
     61            {
     62                s_apidChildren[i] = s_apidChildren[--s_cChildren];
     63                break;
     64            }
     65        }
     66
     67        if (i >= sizeof(s_apidChildren) / sizeof(s_apidChildren[0]))
     68        {
     69            printf("fork-1: error! pid=%d not found. (s_cChildren=%d)\n", pid, s_cChildren);
     70            s_cErrors++;
     71        }
     72    }
     73}
     74
    3775int main(int argc, char **argv)
    3876{
    39     int cChildren = 0;
    40     int cErrors = 0;
    4177    int i;
    42     for (i = 0; i < 5000; i++)
     78    for (i = 0; i < 2000; i++)
    4379    {
    4480        int pid = fork();
     
    4783            case -1:
    4884                printf("Error errno=%d %s\n", errno, strerror(errno));
    49                 cErrors++;
     85                s_cErrors++;
    5086                break;
    5187            case 0:
    5288                //printf("I'm child %#x i=%d.\n", getpid(), i);
    5389                exit(0);
    54                 break;
     90                return 0;
    5591
    5692            default:
    57             {
    58                 cChildren++;
     93                s_apidChildren[s_cChildren++] = pid;
     94                break;
     95        }
     96        if (!(i % 100))
     97            printf("fork-1: info: forked %d i=%d s_cChildren=%d\n", pid, i, s_cChildren);
    5998
    60                 /* don't spawn for ever! */
    61                 if (!(i % 25))
    62                 {
    63                     usleep(1000); /* be kind to the host */
    64                     if (!(i % 100))
    65                         printf("forked %#x i=%d cChildren=%d\n", pid, i, cChildren);
    66                 }
    67 
    68                 if (i % 3)
    69                     break;
    70 
    71                 /* reap */
    72                 int iStatus = -1;
    73                 pid_t pid;
    74                 while ((pid = wait4(-1, &iStatus, WNOHANG, NULL)) > 0)
    75                 {
    76                     if (cChildren > 0)
    77                         cChildren--;
    78                     else
    79                     {
    80                         printf("wtf!?! no children officially running, but pid=%d arrived!\n", pid);
    81                         cErrors++;
    82                     }
    83                     if (!WIFEXITED(iStatus) || WEXITSTATUS(iStatus))
    84                     {
    85                         printf("child %d -> iStatus=%#x\n", pid, iStatus);
    86                         cErrors++;
    87                     }
    88                 }
    89                 break;
    90             }
     99        /*
     100         * Reap until there are free slots again.
     101         */
     102        if (s_cChildren >= 42 || (i % 519) > 488)
     103            reap();
     104        while (s_cChildren >= sizeof(s_apidChildren) / sizeof(s_apidChildren[0]))
     105        {
     106            usleep(1000);
     107            reap();
    91108        }
    92109    }
     
    95112     * Wait for the rest of the children.
    96113     */
    97     int cLoops = 30;
    98     while (cChildren > 0 && cLoops-- > 0)
     114    int cLoops = 5*20;
     115    while (s_cChildren > 0 && cLoops-- > 0)
    99116    {
    100         usleep(32000);
    101 
    102         /* reap */
    103         int iStatus = -1;
    104         pid_t pid;
    105         while ((pid = wait4(-1, &iStatus, WNOHANG, NULL)) > 0)
    106         {
    107             if (cChildren > 0)
    108                 cChildren--;
    109             else
    110             {
    111                 printf("wtf!?! no children officially running, but pid=%d arrived!\n", pid);
    112                 cErrors++;
    113             }
    114             if (!WIFEXITED(iStatus) || WEXITSTATUS(iStatus))
    115             {
    116                 printf("child %d -> iStatus=%#x\n", pid, iStatus);
    117                 cErrors++;
    118             }
    119         }
     117        usleep(50000);
     118        reap();
     119    }
     120    for (i = 0; i < s_cChildren; i++)
     121    {
     122        printf("fork-1: error! pid=%d did not complete in time.\n", s_apidChildren[i]);
     123        s_cErrors++;
    120124    }
    121125
    122     if (!cErrors)
     126    if (!s_cErrors)
    123127        printf("fork-1: SUCCESS\n");
    124128    else
    125         printf("fork-1: FAILURE - %d errors\n", cErrors);
    126     return !!cErrors;
     129        printf("fork-1: FAILURE - %d errors\n", s_cErrors);
     130    return !!s_cErrors;
    127131}
     132
Note: See TracChangeset for help on using the changeset viewer.