Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mpi.h"
- #include <cstdio>
- #include <unistd.h>
- #include <ctime>
- #include <pthread.h>
- #include <cstdlib>
- #define QUESTION 100
- #define ANSWER 101
- int rank, size;
- MPI_Status status;
- int zegar=0;
- int ans[1] = {1};//zezwolenie na zajecie widelca przez konkurenta - tresc wiadomosci
- //widelce numerowane od 0 do 4
- int widelce[5];//0 - nie mam; 1 - nie mam, ale chce; 2 - mam;
- int pierwszyWidelec;
- int drugiWidelec;
- int konkurentPierwszegoWidelca;
- int konkurentDrugiegoWidelca;
- int prosbaKonkurentaOPierwszy;
- int prosbaKonkurentaODrugi;
- pthread_mutex_t mutex_widelce = PTHREAD_MUTEX_INITIALIZER;
- pthread_t thread;
- void mysl(){
- printf("Filozof nr %d mysli\n", rank );
- sleep(rand()%3+1);
- }
- void jedz(){
- printf("Filozof nr %d spozywa\n", rank );
- sleep(rand()%3+1);
- }
- void prosOWidelecNr(int nrWidelca, int konkurent){
- int msg[3];
- msg[0]=rank;
- msg[1]=zegar;
- msg[2]=nrWidelca;
- pthread_mutex_lock( &mutex_widelce );
- widelce[nrWidelca] = 1;
- //printf("rank = %d, miejsce 1\n", rank);
- MPI_Send( &msg, 3, MPI_INT, konkurent, QUESTION, MPI_COMM_WORLD );
- //printf("rank = %d, miejsce 1.1\n", rank);
- pthread_mutex_unlock( &mutex_widelce );
- //gdy otrzyma zgode to blokuje widelec
- //printf("rank = %d, miejsce 2\n", rank);
- MPI_Recv(ans, 1, MPI_INT, MPI_ANY_SOURCE, ANSWER, MPI_COMM_WORLD, &status);
- //printf("rank = %d, miejsce 2.2\n", rank);
- pthread_mutex_lock( &mutex_widelce );
- widelce[nrWidelca] = 2;
- pthread_mutex_unlock( &mutex_widelce );
- printf("Filozof nr %d zabral o widelec nr %d\n", rank, nrWidelca );
- }
- void prosOWidelce(){
- pthread_mutex_lock( &mutex_widelce );
- zegar++;
- pthread_mutex_unlock( &mutex_widelce );
- printf("Filozof nr %d prosi o widelec nr %d\n", rank, pierwszyWidelec );
- prosOWidelecNr(pierwszyWidelec, konkurentPierwszegoWidelca);
- printf("Filozof nr %d prosi o widelec nr %d\n", rank, drugiWidelec );
- prosOWidelecNr(drugiWidelec, konkurentDrugiegoWidelca);
- }
- void oddajWidelce(){
- //printf("Filozof nr %d oddaje widelce nr %d oraz %d\n", rank, pierwszyWidelec, drugiWidelec );
- //najpierw oddaje drugiWidelec
- pthread_mutex_lock( &mutex_widelce );
- if(prosbaKonkurentaODrugi){
- prosbaKonkurentaODrugi=0;
- //printf("rank = %d, miejsce 3\n", rank);
- MPI_Send( &ans, 1, MPI_INT, konkurentDrugiegoWidelca, ANSWER, MPI_COMM_WORLD );
- //printf("rank = %d, miejsce 3.3\n", rank);
- }
- widelce[drugiWidelec]=0;
- pthread_mutex_unlock( &mutex_widelce );
- //potem oddaje pierwszyWidelec
- pthread_mutex_lock( &mutex_widelce );
- if(prosbaKonkurentaOPierwszy){
- prosbaKonkurentaOPierwszy=0;
- //printf("rank = %d, miejsce 4\n", rank);
- MPI_Send( &ans, 1, MPI_INT, konkurentPierwszegoWidelca, ANSWER, MPI_COMM_WORLD );
- //printf("rank = %d, miejsce 4.4\n", rank);
- }
- widelce[pierwszyWidelec]=0;
- pthread_mutex_unlock( &mutex_widelce );
- }
- bool czyPozwolic(int *msg){
- if(widelce[msg[2]] == 0)
- return true;
- if(widelce[msg[2]] == 1)
- if(msg[1] < zegar || (msg[1]==zegar && msg[0] < rank))
- return true;
- return false;
- }
- //jak sie nie kompiluje to zmienic na: void* odbierz()
- void* odbierz(void *){
- int msg[3];
- while(1){
- //printf("rank = %d, miejsce 5\n", rank);
- MPI_Recv(&msg, 3, MPI_INT, MPI_ANY_SOURCE, QUESTION, MPI_COMM_WORLD, &status);
- //printf("rank = %d, miejsce 5.5\n", rank);
- pthread_mutex_lock( &mutex_widelce );
- //zezwolenie na wziece widelca przez inny proces
- if(czyPozwolic(msg)){
- printf("Filozof nr %d pozwala filozofowi %d na widelec %d\n", rank, msg[0], msg[2] );
- //printf("rank = %d, miejsce 6\n", rank);
- MPI_Send( &ans, 1, MPI_INT, msg[0], ANSWER, MPI_COMM_WORLD );
- //printf("rank = %d, miejsce 6.6\n", rank);
- }
- //w przypadku odmowy zapamietuje pytanie o dany widelec;
- else{
- //printf("Filozof nr %d NIE pozwala filozofowi %d na widelec %d\n", rank, msg[0], msg[2] );
- if(msg[2]==pierwszyWidelec)
- prosbaKonkurentaOPierwszy=1;
- else
- prosbaKonkurentaODrugi=1;
- }
- pthread_mutex_unlock( &mutex_widelce );
- }
- }
- void dopasujWidelce(){
- if(rank==4){
- pierwszyWidelec = 0;
- konkurentPierwszegoWidelca = 0;
- drugiWidelec = 4;
- konkurentDrugiegoWidelca = 3;
- }
- else{
- pierwszyWidelec = rank;
- konkurentPierwszegoWidelca = (rank + 4 )%5;
- drugiWidelec = rank+1;
- konkurentDrugiegoWidelca = rank + 1;
- }
- }
- int main(int argc, char** argv){
- MPI_Init( &argc, &argv );
- MPI_Comm_rank( MPI_COMM_WORLD, &rank );
- MPI_Comm_size( MPI_COMM_WORLD, &size );
- pthread_create(&thread, NULL, odbierz, NULL);
- srand(time(0));
- dopasujWidelce();
- while(1){
- mysl();
- prosOWidelce();
- jedz();
- oddajWidelce();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement