Ejemplo n.º 1
0
static void *philosopher(void *arg)
{
    pthread_detach(pthread_self());
    int i = (int)arg;
    while (1) {
        thinking(i);
        takeFolk(i);
        eating(i);
        putFolk(i);
    }
}
void* philosopher(void *arg)
{
	int i;
    int tid = *( ( int* ) arg );

	/*philosopher thread thinks, becomes hungry, ...*/

	for (i=0;i<10;i++) {
		thinking( tid );
		hungry( tid );
		eating( tid);
	}
	return 0;
}
Ejemplo n.º 3
0
void *philosopher(void *arg)
{
   // pthread_detach(pthread_self());
    int i = (int)arg;
    while(1)
    {
        thinking(i, nsecs);
        pthread_mutex_lock(&plock);
        while(!isAvailable(i)){
            pthread_cond_wait(&pwait, &plock);
        }
        takeFork(i);
        pthread_mutex_unlock(&plock);
        eating(i, nsecs);
        putFork(i);
        pthread_cond_broadcast(&pwait);
    }
}
Ejemplo n.º 4
0
// This function simulates the philosophers eating and thinking.
void* philosopher(void* arg){  

  int philo =   *((int*) arg);
  int eatingTime = 0;
  int totalEatingTime = 0;
  int thinkingTime = 0;
  int totalThinkingTime =0;
  free(arg);
  unsigned int state = philo;

  while(totalEatingTime <= MAX_EATING_TIME){
    // try to acquire chopsticks if available.
    while (1) {
    	pthread_mutex_lock(&chopstiks[philo]);
    	int rtrn = pthread_mutex_trylock(&chopstiks[(philo+1) % 5]);
    
    	if (rtrn == EBUSY)
    		pthread_mutex_unlock(&chopstiks[philo]);
    	else break;
    	sleep(1);
    }
    	
   	/******* philosopher i is eating for a random amount of time.*******/
    if((eatingTime = randomGaussian_r(EATING_MEAN, EATING_STD, &state)) < 0)
    	eatingTime = 0;

	eating(philo, eatingTime, totalEatingTime);
    totalEatingTime += eatingTime;
   	 
    // done eating, Release the chopsticks.
    pthread_mutex_unlock(&chopstiks[philo]);
	pthread_mutex_unlock(&chopstiks[(philo+1) % 5]);

   	 /****** Philo is thinking for a random amount of time. ********/
   	if((thinkingTime = randomGaussian_r(THINKING_MEAN, THINKING_STD, &state)) < 0) 
    	thinkingTime = 0;
	totalThinkingTime += thinkingTime;
 	thinking(philo, thinkingTime, totalThinkingTime);  
  } 
  printf("Philo %i has finished eating. [Total time spent eating = %i]\n", philo, totalEatingTime);
  philoTimeEating[philo] = totalEatingTime;
  philoTimeThinking[philo] = totalThinkingTime;
  return NULL;
}
Ejemplo n.º 5
0
void big_bang(vector<planet>& V)
{
    for(int i=0;i<V.size();i++){      // put the circles together add radius
        for(int j=i+1; j< V.size();j++){
            eating(V[i],V[j]);
        }
    }

    for(int i=0;i<V.size();i++)    //delete who radius is 0   >>delete
    {
        if(V[i].get_radius()==0){
            V.erase(V.begin()+i);   // note : for delete
        }
    }

    for(int i=0;i<V.size();i++){           //  dont let the circles exit the screen
        if(V[i].get_x()+2*V[i].get_radius()>=SCREEN_WIDTH||V[i].get_x()<=0){
            V[i].velocity_x= -V[i].get_velocity_x();
        }
        if(V[i].get_y()+2*V[i].get_radius()>=SCREEN_HEIGHT||V[i].get_y()<=0){
            V[i].velocity_y= -V[i].get_velocity_y();
        }
    }
}
Ejemplo n.º 6
0
/*
 * try_eating - thread function
 * @arg: the pointer which point to an struct
 *
 * it seems should return a void pointer, but it's not actually.
 * This function is going be a dead loop unless you interrupt it,
 * manually.
 */
void *try_eating(void *arg)
{
    /* get which philosopher reaching here */
    struct philosopher *ph = (struct philosopher *) arg;
    int i;

    /* time seconds as our paramaters for random number */
    srand(time(0));
    while (1) {
      if (count >= NR)
	    count = 0;

      /* lock it up */
      pthread_mutex_lock(&cp_mutex[count]);
      i = rand()%NR;

      thinking(ph+i);
      pthread_mutex_unlock(&cp_mutex[count]);

      phv_index[i] = i;

      /* check the philosopher's location */
      if ((ph + phv_index[i])->ith == 0) {

	pthread_mutex_lock(&cp_mutex[count]);
          if (cs_shared[NR - 1] == 0 && cs_shared[(ph + phv_index[i])->ith] == 0) {

		    cs_shared[NR - 1] = 1;
		    cs_shared[0] = 1;
		    pthread_mutex_unlock(&cp_mutex[count]);

		    eating(ph + phv_index[i]);

		    pthread_mutex_lock(&cp_mutex[count]);
		    cs_shared[NR - 1] = 0;
		    cs_shared[0] = 0;
		    pthread_mutex_unlock(&cp_mutex[count]);
		
          } 
	  else {
	    hungry(ph + phv_index[i]);
	    cs_shared[NR - 1] = 0;
	    cs_shared[0] = 0;

	    pthread_mutex_unlock(&cp_mutex[count]);
          }
      }
        else {
	  pthread_mutex_lock(&cp_mutex[1]);
          if (cs_shared[(ph + phv_index[i])->ith - 1] == 0 && cs_shared[(ph + phv_index[i])->ith] == 0) {

		    cs_shared[(ph + phv_index[i])->ith - 1] = 1;
		    cs_shared[(ph + phv_index[i])->ith] = 1;
		    eating(ph + phv_index[i]);

		    pthread_mutex_unlock(&cp_mutex[1]);

		    pthread_mutex_lock(&cp_mutex[1]);
		    cs_shared[(ph + phv_index[i])->ith - 1] = 0;
		    cs_shared[(ph + phv_index[i])->ith] = 0;
		  
		    pthread_mutex_unlock(&cp_mutex[1]);
	
		} 
          else {
	    hungry(ph + phv_index[i]);
	    cs_shared[(ph + phv_index[i])->ith - 1] = 0;
	    cs_shared[(ph + phv_index[i])->ith] = 0;

	    pthread_mutex_unlock(&cp_mutex[1]);
          }
        }
      sleep(1);
    }
}
Ejemplo n.º 7
0
int main()
{
	//Calka z sinusa
	cout << "CALKA Z SINUSA" << endl;
	double result = 0.0;
	TworzenieWatkow calka(0, M_PI, &result);
	boost::thread thread(calka);
	cout << "Hardware concurrency: " << boost::thread::hardware_concurrency
		<< endl;
	cout << "Thread ID" << thread.get_id() << endl;
	thread.join();
	cout << "result" << result << endl;
	cout << "DONE!";
	getchar();
	//-------------------------------------
	//Synchronizacja
	cout << "SYNCHRONIZACJA" << endl;
	mutex characterMutex;
	string lancuch;
	lancuch.resize(CHARACTERS_TO_WRITE);
	thread_group threads;
	for (unsigned int i = 0; i < NUMBER_OF_THREADS; ++i)
		threads.create_thread(Watek('a'+i,lancuch,characterMutex));
	threads.join_all();
	cout << lancuch << endl;
	cout << "DONE!";
	getchar();
	//----------------------------------------
	//Zmienne warunkowe
	cout << "ZMIENNE WARUNKOWE"<<endl;
	mutex dataMutex;
	Monitor<char>container(dataMutex);
	thread_group conditional;
	mutex consoleMutex;
	for (int i = 0; i < 5; i++) {
		conditional.create_thread(Consumer(container,i,consoleMutex));
		conditional.create_thread(Producer(container,i,consoleMutex));
	}
	conditional.join_all();
	cout << "DONE!";
	getchar();
	//------------------------------------
	//Pociagi/semafory
	cout << "STACJA KOLEJOWA"<<endl;
	Peron p1("p1"), p2("p2"), p3("p3");
	Train tr1 = Train("T1", p2, p1);
	boost::thread t1(tr1);
	boost::thread t2(Train("T2", p2, p3));
	boost::thread t3(Train("T3", p2, p1));
	boost::thread t4(Train("T4", p2, p3));
	t1.join();
	t2.join();
	t3.join();
	t4.join();
	cout << "DONE"<<endl;
	getchar();
	cout << "FILOZOFOWIE" << endl;
	Semafor eating(PHILOSOPHERS - 1);

	Fork* forks[5];
	thread_group philosophers;
	for (int i = 0; i<PHILOSOPHERS; i++)
	{
		forks[i] = new Fork(i);
	}
	for (int i = 0; i<PHILOSOPHERS; i++)
	{
		philosophers.create_thread(Philosopher(i, forks[i], forks[(i + (PHILOSOPHERS -1)) % PHILOSOPHERS], eating));
	}
	philosophers.join_all();
	cout << "DONE"<<endl;
	getchar();
	cout << "TASK 1" << endl;
	mutex tConsoleMutex;
	Task1 taskOne(tConsoleMutex,0);
	Task1 taskTwo(tConsoleMutex,1);
	boost::thread th1(taskOne);
	boost::thread th2(taskTwo);
	th1.join();
	th2.join();
	cout << "DONE" << endl;
	getchar();
	cout << "TASK 2" << endl;
	boost::mutex task2Mutex;
	Task2 task2DataGenerator(100);
	boost::thread task2Thread(task2DataGenerator);
	boost::unique_lock<mutex>task2Lock(task2Mutex);
	task2DataGenerator.task2isGeneratingData->wait(task2Lock);
	for (int i = 0; i < 100; i++)cout << task2Array[i] << endl;
	cout << "DONE" << endl; 
	getchar();
	cout << "TASK 3" << endl;
	thread_group task3Group;
	Semafor task3Semafor(1);
	Semafor task3Max3ThreadsSemafor(3);
	for (int i = 0; i < 10; i++) {
		task3Group.create_thread(Task3(task3Max3ThreadsSemafor,task3Semafor,i));
	}
	task3Group.join_all();
	cout << "DONE" << endl;
	getchar();
	cout << "SMOLKA EXAM"<<endl;
	cout << "TASK 1"<<endl;
	boost::mutex sConsoleMutex;
	STask1 st1(sConsoleMutex);
	STask1 st2(sConsoleMutex);
	boost::thread sTask1Thread(st1);
	boost::thread sTask1Thread2(st2);
	sTask1Thread.join();
	sTask1Thread2.join();
	cout << "DONE" << endl;
	getchar();
	cout << "TASK 2" << endl;
	sN = 75;
	boost::thread sTask2Thread(sTask2);
	boost::mutex sTask2Mutex;
	boost::unique_lock<mutex>sTask2Lock(sTask2Mutex);
	sTask2Condition.wait(sTask2Lock);
	for (int i = 0; i < 75; i++) {
		cout<< sTask2Array[i]<<endl;
	}
	getchar();
    return 0;
}