Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <sys/ipc.h>
  5. #include <sys/msg.h>
  6. #include <sys/shm.h>
  7. #include <sys/wait.h>
  8. #include <unistd.h>
  9.  
  10. #define WAIT 10
  11. #define THINKING 0
  12. #define N 5
  13. void signal_handler(int);
  14.  
  15. struct Message{
  16. long mtype;
  17. char *mvalue;
  18. };
  19. int forkID, philosophID, shmID, waitID;
  20.  
  21. int main(){
  22. key_t keyFork, keyWait, keyPhilosoph, keyshm;
  23. int philoshopNumber = N;
  24. int i;
  25. int *stan;
  26. char bufor[2];
  27. struct Message message;
  28.  
  29. signal(SIGINT, signal_handler);
  30. printf("Start!\n");
  31.  
  32. if((keyPhilosoph = ftok(".", 'P')) == -1){
  33. fprintf(stderr, "Error ftok message!1\n");
  34. exit(1);
  35. }
  36. philosophID = msgget(keyPhilosoph, IPC_CREAT | IPC_EXCL | 0666);
  37. if(philosophID == -1){
  38. fprintf(stderr, "Error message queue!1\n");
  39. msgctl(philosophID, IPC_RMID, NULL);
  40. exit(1);
  41. }
  42. if((keyFork = ftok(".", 'F')) == -1){
  43. fprintf(stderr, "Error ftok message!1\n");
  44. exit(1);
  45. }
  46. forkID = msgget(keyFork, IPC_CREAT | IPC_EXCL | 0666);
  47. if(forkID == -1){
  48. fprintf(stderr, "Error message queue!1\n");
  49. msgctl(philosophID, IPC_RMID, NULL);
  50. exit(1);
  51. }
  52. for(i = 1; i<=N; i++){
  53. message.mtype=i;
  54. msgsnd(forkID, &message, sizeof(message.mvalue), 0);
  55. }
  56. if((keyWait = ftok(".", 'D')) == -1){
  57. fprintf(stderr, "Error ftok message!1\n");
  58. exit(1);
  59. }
  60. waitID = msgget(keyWait, IPC_CREAT | IPC_EXCL | 0666);
  61. if(waitID == -1){
  62. fprintf(stderr, "Error message queue!1\n");
  63. msgctl(waitID, IPC_RMID, NULL);
  64. msgctl(philosophID,IPC_RMID,NULL);
  65. exit(1);
  66. }
  67. message.mtype = WAIT;
  68. msgsnd(waitID, &message, sizeof(message.mvalue), 0);
  69. //USTAWIENIE PAMIECI DZIELONEJ
  70. if((keyshm = ftok(".", 'M')) == -1){
  71. fprintf(stderr, "Error ftok shared memory!\n");
  72. msgctl(philosophID, IPC_RMID, NULL);
  73. msgctl(waitID, IPC_RMID, NULL);
  74. shmctl(shmID, IPC_RMID, NULL);
  75. exit(1);
  76. }
  77. shmID = shmget(keyshm, philoshopNumber * sizeof(int), IPC_CREAT| IPC_EXCL | 0666);
  78. if(shmID == -1){
  79. fprintf(stderr, "Sharred memory error!\n");
  80. msgctl(philosophID, IPC_RMID, NULL);
  81. msgctl(waitID, IPC_RMID, NULL);
  82. shmctl(shmID, IPC_RMID, NULL);
  83. exit(1);
  84. }
  85.  
  86. stan = (int*)shmat(shmID, NULL, 0);
  87. for( i = 0; i<N; i++){
  88. stan[i] = THINKING;
  89. }
  90. //TWORZENIE PROCESÓW FILOZOFÓW
  91. for(i = 0; i<5; i++){
  92. switch (fork()){
  93. case -1:
  94. fprintf(stderr, "Error fork main.\n");
  95. msgctl(philosophID, IPC_RMID, NULL);
  96. msgctl(waitID, IPC_RMID, NULL);
  97. shmctl(shmID, IPC_RMID, NULL);
  98. printf("NOOOO!!");
  99. exit(1);
  100. case 0:
  101. sprintf(bufor, "%d", i);
  102. execl("./filozof", "filozof", bufor, NULL);
  103. }
  104. }
  105. //ZAMYKANIE MECHANIZMÓW IPC
  106. for(i = 0; i<5; i++){
  107. wait(NULL);
  108. }
  109. printf("what?");
  110. msgctl(waitID, IPC_RMID, NULL);
  111. msgctl(forkID, IPC_RMID, NULL);
  112. msgctl(philosophID, IPC_RMID, NULL);
  113. shmctl(shmID, IPC_RMID, NULL);
  114. printf("MAIN: Koniec.\n");
  115. return 0;
  116. }
  117.  
  118. void signal_handler(int sig){
  119. if(sig != SIGINT){
  120. return;
  121. }
  122. printf("SIGINT!\n");
  123. msgctl(waitID, IPC_RMID, NULL);
  124. msgctl(forkID, IPC_RMID, NULL);
  125. msgctl(philosophID, IPC_RMID, NULL);
  126. shmctl(shmID, IPC_RMID, NULL);
  127. exit(3);
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement