void philosoper(int i) { // i: philospher number, from 0 to 4 while (true) { think(); // philisopher is thinking take_fork(i); // take left fork take_for((i + 1) % N); // take right fork; % is modulo operator eat(); // yum-yum, spaghetti put_fork(i); // put left fork back on the table put_fork((i + 1) % N); // put right fork back on the table } }
void *philospher(void *num){ while(1){ int *i =(int*)num; sleep(1); take_fork(*i); sleep(0); put_fork(*i); } }
void *philospher(void *num){ while(1){ int *i = num; sleep(1); take_fork(*i); sleep(0); put_fork(*i); } }
void *philosopher(void *arg) { int num=*((int *)arg); while(1) { sleep(1); take_fork(num); put_fork(num); } }
void philosphar(int i, int * buf){ printf("philosphar %d started.\n", i); int j=0; while(j<5){ pick_fork(i, buf); sleep(rand()%3); put_fork(i, buf); j++; } exit(i); }
void *philospher_activity(void *a_philo_pointer) { philo *this_philo = (philo*)a_philo_pointer; int iter=0,num_eat = 0; while( iter < M) { int *i = &(this_philo->phil_num); // address of this_pil_id sleep(1); take_fork(*i); sleep(0); printf("%s has eaten %d times till now \n",this_philo->phil_name,++num_eat); put_fork(*i); iter++; } }
// Eating void Philosopher::eat(){ take_fork(); sleep(2); // eating put_fork(); }