void *bank_client(void *args) { int client_id = *(int*)args; Answer *answer = &answers[client_id]; for(int i = 0; i < CALL_COUNT; ++i) { OpCode call_code = i + 1 != CALL_COUNT ? (OpCode)(rand() % 5) : OP_UNLOCK; int value = rand() % 100; while(operation.status != 0) { } pthread_mutex_lock(&operation.mutex); fill_op(&operation, answer, call_code, client_id, value); pthread_mutex_unlock(&operation.mutex); pthread_mutex_lock(&answer->mutex); while(answer->status == 0) { pthread_cond_wait(&answer->cond, &answer->mutex); } pthread_mutex_unlock(&answer->mutex); pthread_mutex_lock(&print_mutex); if(answer->status == 1) { switch(call_code) { case OP_LOOK: printf( "[Thread %d]%s current balance %d\n", client_id, answer->comment, answer->value); break; case OP_LOCK: printf("[Thread %d] %s\n", client_id, answer->comment); break; case OP_UNLOCK: printf("[Thread %d] %s\n", client_id, answer->comment); break; case OP_INC: case OP_DEC: printf( "[Thread %d] %s %d, current balance %d\n", client_id, answer->comment, value, answer->value); break; } } else { printf("[Thread %d] Error: %s (tried %s)\n", client_id, answer->comment, code_to_text(call_code)); } fflush(stdout); pthread_mutex_unlock(&print_mutex); answer->status = 0; } return NULL; }
int check_opt(int (*opt)[], char **av) { int i; int j; i = 1; (*opt)[0] = 0; (*opt)[1] = 0; while (av[i]) { j = 1; if (av[i][0] == '-') while (av[i][j]) { if (av[i][j] != 'n' && av[i][j] != 'e' && av[i][j] != 'E') return (i); j++; } else return (i); fill_op(opt, av, &i); } return (i); }