Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 KB | None | 0 0
  1. //filozof-----------------------------------
  2. #include <stdio.h>
  3. #include <sys/shm.h>
  4. #include <sys/ipc.h>
  5. #include <sys/msg.h>
  6. #include <stdlib.h>
  7.  
  8. struct message{
  9.     int mtype;
  10.     int mvalue;
  11. };
  12. void wypisz_widelce(int *w){
  13.     int i;
  14.     for(i = 0;i<5;i++){
  15.         printf("Widelec[%d] = %d\n", i+1, w[i]);
  16.     }
  17. }
  18.  
  19. int main(int argc, char* argv[])
  20. {
  21.     //const char *names[5] = {"Kant","Sokrates","Lao Tse","Platon","Tomasz z Akwinu"};
  22.     key_t key_message, key_memory;
  23.     int F = 5;
  24.     int msgID, shmID;
  25.     int i;
  26.     int numer;      //numer filozofa
  27.     int *widelec;   //tablica widelcow
  28.     struct message kom;
  29.  
  30.     if( (key_message = ftok(".",'A')) == -1){       //tworzenie kolejki
  31.         printf("Ftok key_message error(filozof)\n");
  32.         exit(1);
  33.     }
  34.     if( (msgID = msgget(key_message,IPC_CREAT|0666)) == -1){
  35.         printf("Msget error (filozof)\n");
  36.         exit(1);
  37.     }
  38.  
  39.     if( (key_memory = ftok(".",'B')) == -1){        //klucz shared memory
  40.         printf("Ftok key_memory error(filozof)\n");
  41.         exit(1);
  42.     }
  43.     if( (shmID = shmget(key_memory,5 * sizeof(int),IPC_CREAT|0666)) == -1){
  44.         printf("Shmget error (filozof)\n");
  45.         exit(1);
  46.     }
  47.     fflush(stdout);
  48.    
  49.     widelec = (int*)shmat(shmID,0,0);   //przylaczenie pam.dziel;
  50.  
  51.     numer = atoi(argv[1]);          //pobieranie numeru filozofa
  52.  
  53.     for(i = 0;i < 1; i++){
  54.         printf("Filozof %d mysli\n", numer+1);
  55.        
  56.         if(msgrcv(msgID,&kom,sizeof(kom.mvalue),numer+1,0) == -1){
  57.             printf("blad odebrania\n");
  58.             exit(1);
  59.         }
  60.        
  61.         if(msgrcv(msgID,&kom,sizeof(kom.mvalue),(numer+1)%5 +1,0) == -1){
  62.             printf("blad odebrania\n");
  63.             exit(1);
  64.         }
  65.    
  66.         widelec[numer] = numer+1;       //podniesienie widelcow
  67.         widelec[(numer + 1)%5] = numer + 1;
  68.        
  69.         printf("Filozof %d je\n",numer + 1);    //jedzenie
  70.         wypisz_widelce(widelec);
  71.         widelec[numer] = 0;
  72.         widelec[(numer+1)%5] = 0;//zwolnienie widelcow 
  73.        
  74.         kom.mtype = numer +1;
  75.         if(msgsnd(msgID,&kom,sizeof(kom.mvalue),0) == -1){
  76.             printf("Blad wyslania \n");
  77.             exit(1);
  78.         }  
  79.        
  80.         kom.mtype = (numer+1)%5 + 1;
  81.         if(msgsnd(msgID,&kom,sizeof(kom.mvalue),0) == -1){
  82.             printf("Blad wyslania\n");
  83.             exit(1);
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement