Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/shm.h>
- #include <time.h>
- #define LICZBA_FILOZOFOW 5
- #define W 7
- struct bufor
- {
- long mtype;
- int mvalue;
- };
- void thinking();
- void eating();
- void test(int nr_filozofa);
- void showForks();
- int msgID,shmID;
- int numer;
- int *pam;
- key_t kluczm, klucz;
- struct bufor komunikat;
- int main(int argc,char *argv[])
- {
- srand(time(NULL));
- if ( (kluczm = ftok(".", 'A')) == -1 )
- {
- printf("Blad kluczs (main)\n");
- exit(1);
- }
- shmID=shmget(kluczm, LICZBA_FILOZOFOW*sizeof(int), IPC_CREAT|0666);//tworzenie pam. dz.
- if (shmID==-1){
- printf("blad pam dzielonej\n");
- exit(1);
- }
- if ((klucz = ftok(".", 'B')) == -1)
- {
- printf("Blad ftok (main)\n");
- exit(1);
- }
- msgID = msgget(klucz, IPC_CREAT | 0666);
- if (msgID == -1)
- {
- printf("blad kolejki komunikatow\n");
- exit(1);
- }
- pam=(int*)shmat(shmID,NULL,0);
- if(*pam==-1){
- printf("Problem z przydzieleniem adresu.\n");
- exit(1);
- }
- numer = atoi(argv[1]);
- int pp=10;
- while(pp) {
- thinking(); //myslenie
- printf("Filozof %d czeka na komunikat 1W\n",numer);
- msgrcv(msgID, &komunikat, sizeof(komunikat.mvalue), W, 0); //wait(w)
- printf("Filozof %d odebral komunikat 1W\n",numer);
- pam[numer-1] = 1; //stan[name] := 1
- test(numer); //test(name)
- komunikat.mtype = W;
- if (msgsnd(msgID, &komunikat, sizeof(komunikat.mvalue),0) == -1) { //signal(w)
- printf("blad wyslania kom. pustego\n");
- exit(1);
- }
- printf("Filozof %d wyslal komunikat typu W\n",numer);
- printf("Filozof %d czeka na komunikat %d\n",numer,numer);
- msgrcv(msgID, &komunikat, sizeof(komunikat.mvalue), numer, 0); //wait(sem[name])
- printf("Filozof %d odebral komunikat %d\n",numer,numer);
- //msgrcv(msgID, &komunikat, sizeof(komunikat.mvalue), (numer+1)%5, 0);
- printf("filozof %d zaczal je\n", numer); //jedzenie
- pam[numer] = 0;
- printf("Filozof %d czeka na komunikat 2W\n",numer);
- msgrcv(msgID, &komunikat, sizeof(komunikat.mvalue), W, 0); //wait(w)
- printf("Filozof %d odebral komunikat 2W\n",numer);
- //msgrcv(msgID, &komunikat, sizeof(komunikat.mvalue), (numer+1)%5, 0); //wait(w)
- int filp,fill;
- fill=(numer-1)%5;
- if(fill==0)fill=5;
- filp=(numer+1)%5;
- if(filp==0) filp=1;
- test(filp); //test((name+1)mod5)
- test(fill); //test((name-1)mod5)
- komunikat.mtype = W;
- if (msgsnd(msgID, &komunikat, sizeof(komunikat.mvalue),0) == -1) { //signal(w)
- printf("blad wyslania kom. pustego\n");
- exit(1);
- }
- sleep(1);
- pp--;
- }
- /*printf("%d filozof mysli\n",numer+1);
- msgrcv(msgID, &komunikat, sizeof(komunikat.mvalue), numer + 1, 0);
- msgrcv(msgID, &komunikat, sizeof(komunikat.mvalue), (numer+1)%5 + 1, 0);
- pam[numer]=numer+1;
- pam[(numer+1)%5]=numer+1;
- printf("%d filozof je, uzywa widelca %d i %d \n",numer+1,numer+1,(numer+1)%5+1);
- for(int i=0;i<LICZBA_FILOZOFOW;i++)
- printf("%d ",pam[i]);
- printf("\n");
- pam[numer]=0;
- pam[(numer+1)%5]=0;
- komunikat.mtype = numer+1;
- if (msgsnd(msgID, &komunikat, sizeof(komunikat.mvalue),0) == -1)
- {
- // printf("blad wyslania kom. pustego\n");
- exit(1);
- }
- komunikat.mtype = ((numer+1)%5)+1;
- if (msgsnd(msgID, &komunikat, sizeof(komunikat.mvalue),0 ) == -1)
- {
- // printf("blad wyslania kom. pustego\n");
- exit(1);
- } */
- }
- void thinking()
- {
- printf("Filozof %d mysli...\n", numer);
- //sleep(rand()%3);
- }
- void showForks()
- {
- int i;
- printf("\ntablica stanow: \n");
- for(i = 0; i < LICZBA_FILOZOFOW; i++) {
- printf("%d", pam[i]);
- }
- printf("\n");
- }
- void eating()
- {
- printf("----------- filozof %d je... \n", numer);
- // sleep(rand()%4);
- showForks();
- printf("Filozof %d skonczyl jesc \n", numer);
- }
- void test(int nr_filozofa)
- {
- int fill, filp;
- fill=(nr_filozofa-1)%5;
- if(fill==0)fill=5;
- filp=(nr_filozofa+1)%5;
- if(filp==0) filp=1;
- if(pam[fill] != 2 && pam[nr_filozofa] == 1 && pam[filp] != 2)
- {
- pam[nr_filozofa] = 2;
- komunikat.mtype = nr_filozofa;}
- msgsnd(msgID, &komunikat, sizeof(komunikat.mvalue), 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement