Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. #include <string.h>
  6. #include <sys/time.h>
  7.  
  8. #define MAIN_THPOOL_NUM     4
  9. #define WHILE_FOR_NUM       10
  10. #define MALLOC_SIZE         4096
  11.  
  12. int     g_done[MAIN_THPOOL_NUM];
  13.  
  14. pthread_t pthreads[MAIN_THPOOL_NUM];
  15.  
  16. void sleepmsec(long msec)
  17. {
  18.     struct timeval stTimeVal;
  19.  
  20.     stTimeVal.tv_sec = msec / 1000;
  21.     msec -= stTimeVal.tv_sec * 1000;
  22.     stTimeVal.tv_usec = msec * 1000;
  23.  
  24.     select(0, 0, 0, 0, &stTimeVal);
  25. }
  26.  
  27. void alloc_free(int ri)
  28. {
  29.     void    *test_array[WHILE_FOR_NUM];
  30.     int     i;
  31.  
  32.     for (i = 0; i < WHILE_FOR_NUM; i++) {
  33.         test_array[i] = NULL;
  34.         test_array[i] = malloc(MALLOC_SIZE);
  35.  
  36.         if (test_array[i]) {
  37.             memset(test_array[i],i,MALLOC_SIZE);
  38.         } else {
  39.             printf("alloc error %d\n",ri);
  40.         }
  41.     }
  42.  
  43.     for(i=0; i<WHILE_FOR_NUM; i++) {
  44.         if (test_array[i]) {
  45.             free(test_array[i]);
  46.             test_array[i]=NULL;
  47.         }
  48.     }
  49. }
  50.  
  51. void *polling_task(void *arg)
  52. {
  53.     void    *test_array[WHILE_FOR_NUM];
  54.     int     i = *(int*)arg;
  55.  
  56.     printf("i=%d\n",i);
  57.  
  58.     while ( 1 ) {
  59.         alloc_free(i);
  60.         sleepmsec(10);
  61.     }
  62.  
  63.     return NULL;
  64. }
  65.  
  66. int main(int argc, char *argv[])
  67. {
  68.     int     i;
  69.     int     status[MAIN_THPOOL_NUM];
  70.     //int     cnt = 1000000;
  71.  
  72.     for (i = 0; i < MAIN_THPOOL_NUM; i++) {
  73.         pthread_create(&pthreads[i], NULL, (void *)polling_task, &i);
  74.         sleepmsec(1);
  75.     }
  76.  
  77.     while (1) {
  78.         sleepmsec(10);
  79.     }
  80.  
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement