Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <semaphore.h>
  5. #include <pthread.h>
  6. #include <time.h>
  7.  
  8. #define SLEEP_TO 100
  9. #define SLEEP_FROM 20
  10.  
  11. #define KAPACITA 3
  12. #define POCET_STUDENTOV 10
  13.  
  14. sem_t prvy_semafor; //pocet aktualnych,buffer
  15. sem_t druhy_semafor; //pocet volnych,buffer
  16.  
  17. sem_t sem_studentov[POCET_STUDENTOV];
  18. sem_t sem_sedacky;
  19. sem_t sem_spanku;
  20.  
  21. pthread_mutex_t buffer_mutex;
  22. pthread_mutex_t sedacky_mutex;
  23. pthread_mutex_t spanok_mutex;
  24. pthread_mutex_t konzultacia_mutex;
  25.  
  26. int buffer[KAPACITA];
  27.  
  28. int na_rade = 0;
  29.  
  30. int prve_volne = 0;
  31.  
  32. int ucitel_spi = 1;
  33.  
  34. int prebieha_konzultacia = 0;
  35.  
  36. void init() {
  37. int i;
  38.  
  39. sem_init(&prvy_semafor,0,0);
  40. sem_init(&druhy_semafor,0,KAPACITA);
  41. pthread_mutex_init(&buffer_mutex,NULL);
  42. pthread_mutex_init(&sedacky_mutex,NULL);
  43. sem_init(&sem_sedacky,0,KAPACITA);
  44. pthread_mutex_init(&spanok_mutex,NULL);
  45. sem_init(&sem_spanku,0,0);
  46. pthread_mutex_init(&konzultacia_mutex,NULL);
  47.  
  48. for(i = 0; i < KAPACITA; i++) {
  49. buffer[i] = 0;
  50. }
  51. }
  52.  
  53. void insert(int element) {
  54. sem_wait(&druhy_semafor);
  55.  
  56. pthread_mutex_lock(&buffer_mutex);
  57.  
  58. if(buffer == NULL) {
  59. pthread_mutex_unlock(&buffer_mutex);
  60.  
  61. return;
  62. }
  63. buffer[prve_volne] = element;
  64. prve_volne++;
  65.  
  66. if(prve_volne == KAPACITA) {
  67. prve_volne = 0;
  68. }
  69. sem_post(&prvy_semafor);
  70. pthread_mutex_unlock(&buffer_mutex);
  71. }
  72.  
  73. int remove_a() {
  74. int ret;
  75.  
  76. sem_wait(&prvy_semafor);
  77.  
  78. pthread_mutex_lock(&buffer_mutex);
  79.  
  80. if(buffer == NULL) {
  81. pthread_mutex_unlock(&buffer_mutex);
  82.  
  83. return -1;
  84. }
  85. printf("vyberam si prvok: %d \n",buffer[na_rade]);
  86. ret = buffer[na_rade];
  87. buffer[na_rade] = 0;
  88. na_rade++;
  89.  
  90. if(na_rade == KAPACITA) {
  91. na_rade = 0;
  92. }
  93.  
  94. sem_post(&druhy_semafor);
  95. pthread_mutex_unlock(&buffer_mutex);
  96.  
  97. return ret;
  98. }
  99.  
  100. int isFull() { //ak je plne vratim jednotku
  101. pthread_mutex_lock(&buffer_mutex);
  102. int i;
  103. int flag = 0;
  104.  
  105. if(buffer == NULL) {
  106. pthread_mutex_unlock(&buffer_mutex);
  107.  
  108. return -1;
  109. }
  110.  
  111. for(i = 0; i < KAPACITA; i++) {
  112. if(buffer[i] == 0) {
  113. flag = 1;
  114. break;
  115. }
  116. }
  117. if(flag == 0 ) {
  118. pthread_mutex_unlock(&buffer_mutex);
  119.  
  120. return 1;
  121. }
  122. else {
  123. pthread_mutex_unlock(&buffer_mutex);
  124.  
  125. return 0;
  126. }
  127. }
  128.  
  129. int isEmpty() { //ak je prazdne vratim jednotku
  130. pthread_mutex_lock(&buffer_mutex);
  131. int i;
  132. int flag = 0;
  133.  
  134. if(buffer == NULL) {
  135. pthread_mutex_unlock(&buffer_mutex);
  136.  
  137. return -1;
  138. }
  139. for(i = 0; i < KAPACITA; i++) {
  140. if(buffer[i] != 0){
  141. flag = 1;
  142. }
  143. }
  144. if(flag == 0 ) {
  145. pthread_mutex_unlock(&buffer_mutex);
  146.  
  147. return 1;
  148. }
  149. else {
  150. pthread_mutex_unlock(&buffer_mutex);
  151.  
  152. return 0;
  153. }
  154. }
  155.  
  156.  
  157. int nik_neprisiel_konzultovat() {
  158. int ret = 0;
  159.  
  160. printf("testujem ci prisiel niekto konzultovat \n");
  161. pthread_mutex_lock(&sedacky_mutex);
  162.  
  163. if(isEmpty()) {
  164. ret = 1;
  165. printf("zistujem ze naozaj nik neprisiel \n");
  166. pthread_mutex_unlock(&sedacky_mutex);
  167.  
  168. return ret;
  169. }
  170. printf("a niekto naozaj prisiel \n");
  171. pthread_mutex_unlock(&sedacky_mutex);
  172.  
  173. return ret;
  174. }
  175.  
  176. int cakaj_na_sedacke(int idcko) {
  177. int ret = 0;
  178.  
  179. pthread_mutex_lock(&sedacky_mutex);
  180.  
  181. if(isFull()) {
  182. ret = 1;
  183. pthread_mutex_unlock(&sedacky_mutex);
  184.  
  185. return ret;
  186. }
  187. printf("Som student %d . a cakam na sedacke \n",idcko);
  188. insert(idcko);
  189. pthread_mutex_unlock(&sedacky_mutex);
  190. sem_wait(&sem_sedacky);
  191. return ret;
  192. }
  193.  
  194. int pusti_do_kancelarie_dalsieho(int idcka[]) { //DOROBIT!
  195.  
  196. int ret = 1;
  197. int pusti;
  198.  
  199. pthread_mutex_lock(&sedacky_mutex);
  200.  
  201. if(isEmpty()) {
  202. pthread_mutex_unlock(&sedacky_mutex);
  203. ret = 0;
  204.  
  205. return ret;
  206. }
  207. pusti = remove_a();
  208. pthread_mutex_unlock(&sedacky_mutex);
  209. sem_post(&sem_sedacky);
  210. sem_post(&sem_studentov[pusti]);
  211.  
  212. return ret;
  213. }
  214.  
  215. void programuj(int idcko) {
  216. double time;
  217.  
  218. time = random() % (SLEEP_TO - SLEEP_FROM) + SLEEP_FROM;
  219. printf("Som student %d . a programujem %lf milisekund \n", idcko,time);
  220. usleep(time);
  221. }
  222.  
  223. void chod_spat() { //NEPOUZITA
  224. double time;
  225.  
  226. time = random() % (SLEEP_TO - SLEEP_FROM) + SLEEP_FROM;
  227. printf("spim %lf milisekund \n", time);
  228. usleep(time);
  229. }
  230.  
  231. void zobud_ak_spi(int idcko) {
  232. printf("Som student %d . a prave zobudzam ucitela \n",idcko);
  233. pthread_mutex_lock(&spanok_mutex);
  234.  
  235. if(ucitel_spi == 1) {
  236. sem_post(&sem_spanku);
  237. ucitel_spi = 0;
  238. printf("Student: Ucitel zobudeny ... \n");
  239. }
  240. pthread_mutex_unlock(&spanok_mutex);
  241. }
  242.  
  243.  
  244. void chod_konzultovat(int idcko) {
  245. zobud_ak_spi(idcko);
  246. if(cakaj_na_sedacke(idcko) == 0) {
  247. return;
  248. }
  249.  
  250. while(1) {
  251. pthread_mutex_lock(&konzultacia_mutex);
  252. if(prebieha_konzultacia == 0) {
  253. pthread_mutex_unlock(&konzultacia_mutex);
  254. printf("koniec konzultacie \n");
  255. //koniec id???
  256. break;
  257. }
  258. pthread_mutex_unlock(&konzultacia_mutex);
  259. }
  260. }
  261.  
  262. void konzultuj() {
  263. double time;
  264.  
  265. time = random() % (SLEEP_TO - SLEEP_FROM) + SLEEP_FROM;
  266. printf("konzultujem %lf milisekund \n",time);
  267. usleep(time);
  268.  
  269. pthread_mutex_lock(&konzultacia_mutex);
  270. prebieha_konzultacia = 1;
  271. pthread_mutex_unlock(&konzultacia_mutex);
  272. }
  273.  
  274. void zaspi() {
  275. pthread_mutex_lock(&spanok_mutex);
  276. ucitel_spi = 1;
  277. chod_spat();
  278. pthread_mutex_unlock(&spanok_mutex);
  279. printf("Ucitel: spim ..... \n");
  280. sem_wait(&sem_spanku);
  281. printf("Ucitel: bol som zobudeny \n");
  282. }
  283.  
  284. void *student_main(void* p) {
  285. int* idcko = (int*)p;
  286.  
  287. while(1) {
  288. programuj(*idcko);
  289. chod_konzultovat(*idcko);
  290. }
  291. }
  292.  
  293. void *ucitel_main(void* p) {
  294. int *idcka = (void*)p;
  295.  
  296. printf("som ucitel--------------\n");
  297. while(1) {
  298. if(nik_neprisiel_konzultovat() == 1) {
  299. printf("Ucitel: nik neprisiel konzultovat,idem spat \n");
  300. zaspi();
  301. }
  302. pthread_mutex_lock(&konzultacia_mutex);
  303. pusti_do_kancelarie_dalsieho(idcka);
  304. prebieha_konzultacia = 1;
  305. pthread_mutex_unlock(&konzultacia_mutex);
  306. konzultuj();
  307. }
  308. }
  309.  
  310. int main() {
  311. int i;
  312. int id_studentov[POCET_STUDENTOV];
  313.  
  314. pthread_t ucitel;
  315. pthread_t studenti[POCET_STUDENTOV];
  316.  
  317. srandom(time(NULL));
  318.  
  319. init();
  320.  
  321. pthread_create(&ucitel, NULL,ucitel_main, (void*)id_studentov );
  322. for(i = 0; i < POCET_STUDENTOV; i++) {
  323. sem_init(&sem_studentov[i],0 ,0);
  324. id_studentov[i] = i;
  325. pthread_create(&studenti[i], NULL,student_main, (void*)(&id_studentov[i]) );
  326. }
  327. for(i = 0 ; i < POCET_STUDENTOV ; i++)
  328. {
  329. pthread_join(studenti[i],NULL);
  330. }
  331. pthread_join(ucitel,NULL);
  332.  
  333. return 0;
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement