Guest User

Untitled

a guest
Jan 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/ipc.h>
  4. #include <sys/msg.h>
  5. #include <sys/shm.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8.  
  9.  
  10. struct bufor {
  11. int mtype;
  12. int mvalue;
  13. };
  14.  
  15. int *pam;
  16. #define MAX2 12
  17. #define MAX 10
  18. #define SP 1
  19. #define W 2
  20. #define zapis pam[MAX+1]
  21. #define odczyt pam[MAX]
  22.  
  23.  
  24. int main() {
  25. key_t klucz, kluczm;
  26. int msgID, shmID;
  27. int i;
  28. int pamm[12];
  29. struct bufor komunikat;
  30.  
  31. if ((klucz = ftok(".", 'A')) == -1 ) {
  32. printf("Blad ftok (main)\n");
  33. exit(1);
  34. }
  35.  
  36. msgID=msgget(klucz,IPC_CREAT|0666); //uzyskanie dosepu do kolejki komunikatow
  37. if (msgID==-1) {
  38. printf("blad kolejki komunikatow\n");
  39. exit(1);
  40. }
  41.  
  42. kluczm=ftok(".",'B'); //uzyskanie dosepu do pamieci dzielonej
  43. shmID=shmget(kluczm, MAX2*sizeof(int),IPC_CREAT|0666);
  44.  
  45. if((pam = shmat(shmID, NULL, 0)) == (int *)-1) { //przylaczenie pam. dzielonej
  46. printf("Blad pamieci wspoldzielonej\n");
  47. exit(EXIT_FAILURE);
  48. }
  49.  
  50. msgrcv(msgID, &komunikat, sizeof(int), W, 0);
  51. pam[13] = pam[13] + 1;
  52. if(pam[13]==1)
  53. msgrcv(msgID, &komunikat, sizeof(int), SP, 0);
  54.  
  55. komunikat.mtype = W;
  56. msgsnd(msgID,&komunikat,sizeof(komunikat.mvalue),0);
  57.  
  58. for(i=0; i<MAX; i++)
  59. fprintf(stderr,"czytam %d \n",pam[i]);
  60. msgrcv(msgID, &komunikat, sizeof(int), W, 0);
  61.  
  62. pam[13] = pam[13] - 1;
  63.  
  64. if(pam[13]==0) {
  65. komunikat.mtype = SP;
  66. msgsnd(msgID,&komunikat,sizeof(komunikat.mvalue),0);
  67. }
  68.  
  69. komunikat.mtype = W;
  70. msgsnd(msgID,&komunikat,sizeof(komunikat.mvalue),0);
  71. }
Add Comment
Please, Sign In to add comment