Advertisement
Guest User

Untitled

a guest
May 17th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. #define _SVID_SOURCE
  2. #include<stdio.h>
  3. #include<sys/types.h>
  4. #include<sys/ipc.h>
  5. #include<sys/sem.h>
  6. #include<unistd.h>
  7. #include<stdlib.h>
  8.  
  9.  
  10.  
  11. union semun
  12. { int val; //wartość SETVAL
  13. struct semid_ds *buf; //bufor dla IPC_stat/IPC_SET
  14. unsigned short *tab; //tablica dla GETALL,SETALL
  15. };
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. int tworzenie(key_t key)
  23. {
  24. int idZestawu; //id dla zbioru semaforów
  25. idZestawu=semget(key,1,IPC_CREAT|0666);
  26. if(idZestawu==-1)
  27. { perror("błąd tworzenia semafora\n");
  28. exit(1);
  29. } /*tworzy zbiór składający się z 1 semafora dla klucza key o*/ //flagach dostępu IPC_CREAT|IPC_EXCL|0666
  30. return idZestawu; //0666-mozliwosc wykonywania wszystkich operacji przez wszystkie procesy
  31.  
  32.  
  33. }
  34. key_t uzDostep()
  35. {
  36. key_t key;
  37. key=ftok("./semafory.exe",'B');
  38. return key;
  39.  
  40. }
  41. int kolejka(int idZestawu)
  42. {
  43. int licznik;
  44. licznik = semctl(idZestawu, 0, GETNCNT); //odczytanie liczby procesów czekających
  45. if(licznik==-1) perror("błąd GETNCNT");
  46. return licznik;
  47. }
  48. void inicjalizacja(int idZestawu)
  49. {
  50. union semun kontrola;
  51. kontrola.val=1;
  52. semctl(idZestawu,0,SETVAL,kontrola); /* argumenty identyfikator zbioru semaforow (zwracany przez semget),semnum: numer*/ fprintf(stdout,"semafor zainicjowany : podniesiony\n"); //semafora w zbiorze,cmd kodpolecenia,arg //parametry polecenia
  53. //ustawia SETVAL na 1
  54.  
  55. }
  56.  
  57. void podnies(int idZestawu)
  58. {
  59.  
  60. struct sembuf semOp;
  61. semOp.sem_num=0;
  62. semOp.sem_op = 1; //wartość sem_op= 1->semafor podniesiony
  63. semOp.sem_flg=0;
  64. semop(idZestawu,&semOp,1);
  65. printf("semafor podniesiony!\n");
  66.  
  67. }
  68. void opusc (int idZestawu)
  69. {
  70.  
  71. struct sembuf semOp;
  72. semOp.sem_num=0;
  73. semOp.sem_op=-1; //wartość semo_op=-1 ->semafor opuszczony
  74. semOp.sem_flg=0;
  75. semop(idZestawu,&semOp,1);
  76. printf("semafor opuszczony! \n");
  77.  
  78. }
  79. void usunSem(int idZestawu)
  80. {
  81.  
  82. semctl(idZestawu,0,IPC_RMID);
  83. fprintf(stdout,"semafor został usunięty!\n");
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement