Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
- void *downtime(){
- sleep(1);
- pthread_rwlock_wrlock(&lock);
- root_balanced = balanceTree(root);
- freeSubtree(root);
- root = root_balanced;
- root_balanced=NULL;
- pthread_rwlock_unlock(&lock);
- sleep(1);
- pthread_rwlock_wrlock(&lock);
- root_balanced = balanceTree(root);
- freeSubtree(root);
- root = root_balanced;
- root_balanced=NULL;
- pthread_rwlock_unlock(&lock);
- sleep(1);
- pthread_rwlock_wrlock(&lock);
- root_balanced = balanceTree(root);
- freeSubtree(root);
- root = root_balanced;
- root_balanced=NULL;
- pthread_rwlock_unlock(&lock);
- return NULL;
- }
- void* ServeClient(char *client){
- FILE *fp;
- char str[80];
- char delim[] = " ";
- int n, number = 0;
- fp = fopen(client, "r");
- while (fgets(str, sizeof(str), fp) != NULL) {
- pthread_rwlock_wrlock(&lock);
- char *ptr = strtok(str, delim);
- printf("[%s]%s", client, ptr);
- ptr = strtok(NULL, delim);
- switch (str[0])
- {
- case 'i':
- n = number = 0;
- while (ptr[n] != '\r' && ptr[n] != '\n') {
- number = number * 10 + (int)ptr[n] - 48;
- n++;
- }
- printf(" %d\n", number);
- root = insertNode(root, number);
- break;
- case 'd':
- n = number = 0;
- while (ptr[n] != '\r' && ptr[n] != '\n') {
- number = number * 10 + (int)ptr[n] - 48;
- n++;
- }
- printf(" %d\n", number);
- root = deleteNode(root, number);
- break;
- case 'c':
- printf(" = %d\n", countNodes(root));
- break;
- case 's':
- printf(" = %d\n", sumSubtree(root));
- break;
- }
- pthread_rwlock_unlock(&lock);
- }
- fclose(fp);
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement