Changeset 2077
- Timestamp:
- Jun 26, 2005, 1:40:34 AM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/libctests/libc/smoketests/fork-1.c
-
Property cvs2svn:cvs-rev
changed from
1.1
to1.2
r2076 r2077 35 35 #include <sys/wait.h> 36 36 37 /******************************************************************************* 38 * Global Variables * 39 *******************************************************************************/ 40 static pid_t s_apidChildren[75]; 41 static unsigned s_cChildren = 0; 42 static int s_cErrors = 0; 43 44 static 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 37 75 int main(int argc, char **argv) 38 76 { 39 int cChildren = 0;40 int cErrors = 0;41 77 int i; 42 for (i = 0; i < 5000; i++)78 for (i = 0; i < 2000; i++) 43 79 { 44 80 int pid = fork(); … … 47 83 case -1: 48 84 printf("Error errno=%d %s\n", errno, strerror(errno)); 49 cErrors++;85 s_cErrors++; 50 86 break; 51 87 case 0: 52 88 //printf("I'm child %#x i=%d.\n", getpid(), i); 53 89 exit(0); 54 break;90 return 0; 55 91 56 92 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); 59 98 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(); 91 108 } 92 109 } … … 95 112 * Wait for the rest of the children. 96 113 */ 97 int cLoops = 30;98 while ( cChildren > 0 && cLoops-- > 0)114 int cLoops = 5*20; 115 while (s_cChildren > 0 && cLoops-- > 0) 99 116 { 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++; 120 124 } 121 125 122 if (! cErrors)126 if (!s_cErrors) 123 127 printf("fork-1: SUCCESS\n"); 124 128 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; 127 131 } 132 -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.