mallocstress  return fail sometimes.
[EMAIL PROTECTED] mtest07]# ./mallocstress -t 30
main(): pthread_create(): Cannot allocate memory
[EMAIL PROTECTED] mtest07]# echo $?
255

mallocstress stresses the VMM and C library  by spawning N threads which
malloc blocks of increasing size until malloc returns NULL.

It occur because test doesn't wait for all thread started.
already started threads begin allocate memory, so pthread_create can return
ENOMEM.
my patch added this synchronizing.
I use semaphore, because it can wait-for-zero unlike pthread_mutex
--- tmp/ltp-base-20080531/testcases/kernel/mem/mtest07/mallocstress.c	2008-06-01 14:37:03.000000000 +0400
+++ ltp-base-20080531/testcases/kernel/mem/mtest07/mallocstress.c	2008-06-07 13:51:01.000000000 +0400
@@ -69,6 +69,9 @@
 #include <math.h>
 #include <assert.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/sem.h>
 
 #define MAXL    100     /* default number of loops to do malloc and free      */
 #define MAXT     60     /* default number of threads to create.               */
@@ -86,7 +89,7 @@
                                    } while (0)
 
 int num_loop = MAXL;/* number of loops to perform		      */
-
+int semid;
 
 /* Define SPEW_SIGNALS to tickle thread_create bug (it fails if interrupted). */
 #define SPEW_SIGNALS
@@ -254,6 +257,17 @@ allocate_free(int    repeat,	/* number o
 void *
 alloc_mem(void * threadnum)
 {
+    struct sembuf sop[1];
+    sop[0].sem_num = 0;
+    sop[0].sem_op = 0;
+    sop[0].sem_flg = 0;
+    /* waiting for other threads starting */
+    if (semop(semid, sop, 1) == -1) {
+        if (errno != EIDRM)
+            perror("semop");
+        return (void *) -1;
+    }
+
     /* thread N will use growth scheme N mod 4 */
     int err = allocate_free(num_loop, ((int)threadnum) % 4);
     fprintf(stdout, 
@@ -288,6 +302,8 @@ main(int	argc,		/* number of input param
     int		thrd_ndx;	/* index into the array of thread ids         */
     pthread_t	*thrdid;	/* the threads                                */
     extern int	 optopt;	/* options to the program		      */
+    struct sembuf sop[1];
+    int ret = 0;
 
     while ((c =  getopt(argc, argv, "hl:t:")) != -1)
     {
@@ -332,6 +348,19 @@ main(int	argc,		/* number of input param
 	perror("main(): allocating space for thrdid[] malloc()");
 	return 1;
     }
+    semid=semget(IPC_PRIVATE,  1,IPC_CREAT);
+    if  (semid<0) {
+        perror("Semaphore creation failed  Reason:");
+    }
+
+    sop[0].sem_num = 0;
+    sop[0].sem_op = 1;
+    sop[0].sem_flg = 0;
+    if (semop(semid, sop, 1) == -1) {
+        perror("semop");
+        ret = -1;
+	goto out;
+    }
 
     for (thrd_ndx = 0; thrd_ndx < num_thrd; thrd_ndx++)
     {
@@ -340,22 +369,31 @@ main(int	argc,		/* number of input param
 	    int err = errno;
 	    if (err == EINTR) {
 		fprintf(stderr, "main(): pthread_create failed with EINTR!\n");
-		exit(-1);
+		ret = -1;
+		goto out;
 	    }
             perror("main(): pthread_create()");
-            exit(-1);
+            ret = -11;
+            goto out;
         }
     }
     my_yield();
-    
-
+ 
+    sop[0].sem_op = -1;
+    if (semop(semid, sop, 1) == -1) {
+        perror("semop");
+        ret = -1;
+        goto out;
+    }
+   
     for (thrd_ndx = 0; thrd_ndx < num_thrd; thrd_ndx++)
     {
         void *th_status;	/* exit status of LWP */
         if (pthread_join(thrdid[thrd_ndx], &th_status) != 0)
         {
             perror("main(): pthread_join()");
-            exit(-1);
+            ret = -1;
+            goto out;
         }
         else
         {
@@ -363,12 +401,18 @@ main(int	argc,		/* number of input param
             {
                 fprintf(stderr,
                         "main(): thread [%d] - exited with errors\n", thrd_ndx);
-                exit(-1);
+                ret = -1;
+                goto out;
             }
             dprt(("main(): thread [%d]: exited without errors\n", thrd_ndx));
         }
         my_yield();
     }
     printf("main(): test passed.\n");
-    exit(0);
+out:
+    if(semctl(semid, 0, IPC_RMID) == -1) {
+        perror("semctl\n");
+	ret = -1;
+    }
+    exit(ret);
 }
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to