#include #include #include int mode; static void *thr_function(void *data) { int i; pthread_t *p_thrID = data; void *mem; if (mode == 'd') pthread_detach(*p_thrID); for (i = 0; i < 10; ++i) { mem = malloc(1024*1024); sleep(1); free(mem); } } #define N_THR 10 int main(int argc, char *argv[]) { pthread_attr_t thrATTR; pthread_t thrID[N_THR]; int i, n; if (argc != 2) { printf("Usage:\n ./pth_mem \n"); exit(0); } mode = argv[1][0]; pthread_attr_init(&thrATTR); pthread_attr_setdetachstate(&thrATTR, PTHREAD_CREATE_DETACHED); sleep(5); for (n = 0; n < 8; ++n) { for (i = 0; i < N_THR; ++i) { if (mode == 'c') pthread_create(&thrID[i], &thrATTR, thr_function, &thrID[i]); else pthread_create(&thrID[i], NULL, thr_function, &thrID[i]); } if (mode == 'j') { for (i = 0; i < N_THR; ++i) pthread_join(thrID[i], NULL); sleep(5); } else { sleep(15); } } pthread_attr_destroy(&thrATTR); return 0; }