#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>

pthread_mutex_t resource[5];
char res_str[] = "00000";

int pick(){return rand()%5;}
int pick2(){return rand()%2+1;}

void* t1 (void * p) {
  int r1, r2;
  while(1) {
    r1=pick(); r2=pick();
    pthread_mutex_lock(&resource[r1]);
    res_str[r1]='1';
    sleep(pick2());
    if(pthread_mutex_trylock(&resource[r2])){
        res_str[r2]='1';
        sleep(pick2());
    } else {
        pthread_mutex_unlock(&resource[r1]);
        res_str[r1]='0';

    }
    pthread_mutex_unlock(&resource[r1]);
    pthread_mutex_unlock(&resource[r2]);
    res_str[r1]='0';
    res_str[r2]='0';
  }
}

void* t2 (void * p) {
  int r1, r2;
  while(1) {
    r1=pick(); r2=pick();
    pthread_mutex_lock(&resource[r1]);
    res_str[r1]='2';
    sleep(pick2());
    if(pthread_mutex_trylock(&resource[r2])){
        res_str[r2]='2';
        sleep(pick2());
    } else {
        pthread_mutex_unlock(&resource[r1]);
        res_str[r1]='0';

    }
    pthread_mutex_unlock(&resource[r1]);
    pthread_mutex_unlock(&resource[r2]);
    res_str[r1]='0';
    res_str[r2]='0';
  }
}

main() {
  int i=0,n;
  pthread_t id1, id2;
  pthread_create(&id1,NULL,&t1,NULL);
  pthread_create(&id2,NULL,&t2,NULL);
  while(i++<30)
  {
    sleep(1);
    printf("%2d: %s\n", i, res_str);
  }
}
