#include #include #include #include #include #include #include #define NUM_THREADS 10 #define NUM1 1000000 #define NUM2 5000000 #define NUM3 100000 pthread_t td[NUM_THREADS]; pthread_t mt; pthread_attr_t attr; pthread_mutex_t t_mtx = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t t_cond = PTHREAD_COND_INITIALIZER; void *malloc_exec(void *parm) { while (1) { malloc_stats(); sleep(1); } } void *empty_exec(void *parm) { while(1); } void *array_exec(void *parm) { char **aa, **bb, **cc; static int tt = 50; while (1) { aa = new char*[NUM1]; bb = new char*[NUM2]; cc = new char*[NUM3]; unsigned int xx = time(0); for (int i=0; i < NUM1; i++) aa[i] = new char[90]; for (int i=0; i < NUM2; i++) bb[i] = new char[15]; for (int i=0; i < NUM3; i++) cc[i] = new char[1000]; fprintf(stderr, "Thread %ld Allocation ended %ld\n", pthread_self(), time(0) - xx); fflush(stderr); tt = tt - 5; if (tt <= 0) tt = 50; sleep(tt); fprintf(stderr, "Thread %ld Deallocation started %ld\n", pthread_self(), time(0)); fflush(stderr); for (int i=0; i < NUM3; i++) delete[] cc[i]; for (int i=0; i < NUM2; i++) delete[] bb[i]; for (int i=0; i < NUM1; i++) delete[] aa[i]; delete[] cc; delete[] bb; delete[] aa; fprintf(stderr, "Thread %ld Deallocation ended %ld\n", pthread_self(), time(0)); fflush(stderr); } pthread_exit((void *)0); } int main( int argc, char **argv) { int tid; int pri; int stat; int tstatus; pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); pthread_create(&mt, NULL, malloc_exec, NULL); for (tid = 0; tid < NUM_THREADS; tid++) { pthread_create(&td[tid], NULL, array_exec, NULL); sleep(5); } for (tid = 0; tid < NUM_THREADS; tid++) { stat = pthread_join(td[tid], NULL); printf("Thread %d done \n", tid); } pthread_join(mt, NULL); }