SHARE
TWEET

Untitled

a guest Nov 17th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
  2.  
  3. void *downtime(){
  4.     sleep(1);
  5.     pthread_rwlock_wrlock(&lock);
  6.     root_balanced = balanceTree(root);
  7.     freeSubtree(root);
  8.     root = root_balanced;
  9.     root_balanced=NULL;
  10.     pthread_rwlock_unlock(&lock);
  11.  
  12.     sleep(1);
  13.     pthread_rwlock_wrlock(&lock);
  14.     root_balanced = balanceTree(root);
  15.     freeSubtree(root);
  16.     root = root_balanced;
  17.     root_balanced=NULL;
  18.     pthread_rwlock_unlock(&lock);
  19.  
  20.     sleep(1);
  21.     pthread_rwlock_wrlock(&lock);
  22.     root_balanced = balanceTree(root);
  23.     freeSubtree(root);
  24.     root = root_balanced;
  25.     root_balanced=NULL;
  26.     pthread_rwlock_unlock(&lock);
  27.  
  28.     return NULL;
  29. }
  30.  
  31. void* ServeClient(char *client){
  32.     FILE *fp;
  33.     char str[80];
  34.     char delim[] = " ";
  35.     int n, number = 0;
  36.  
  37.     fp = fopen(client, "r");
  38.  
  39.     while (fgets(str, sizeof(str), fp) != NULL) {
  40.         pthread_rwlock_wrlock(&lock);
  41.  
  42.         char *ptr = strtok(str, delim);
  43.         printf("[%s]%s", client, ptr);
  44.  
  45.         ptr = strtok(NULL, delim);
  46.  
  47.         switch (str[0])
  48.             {
  49.                 case 'i':
  50.                     n = number = 0;
  51.                     while (ptr[n] != '\r' && ptr[n] != '\n') {
  52.                         number = number * 10 + (int)ptr[n] - 48;
  53.                         n++;
  54.                     }
  55.  
  56.                     printf(" %d\n", number);
  57.                     root = insertNode(root, number);
  58.                     break;
  59.  
  60.                 case 'd':
  61.                     n = number = 0;
  62.                     while (ptr[n] != '\r' && ptr[n] != '\n') {
  63.                         number = number * 10 + (int)ptr[n] - 48;
  64.                         n++;
  65.                     }
  66.  
  67.                     printf(" %d\n", number);
  68.                     root = deleteNode(root, number);
  69.                     break;
  70.  
  71.                 case 'c':
  72.                     printf(" = %d\n", countNodes(root));
  73.                     break;
  74.  
  75.                 case 's':
  76.                     printf(" = %d\n", sumSubtree(root));
  77.                     break;
  78.             }
  79.  
  80.  
  81.         pthread_rwlock_unlock(&lock);
  82.     }
  83.  
  84.     fclose(fp);
  85.     return NULL;
  86. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top