Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.57 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. void printOut() {
  157. int i;
  158.  
  159. for(i = 0; i < KAPACITA; i++) {
  160. printf("buffer--- pozicia %d hodnota----%d \n",i,buffer[i]);
  161. }
  162. }
  163.  
  164. int nik_neprisiel_konzultovat() {
  165. int ret = 0;
  166.  
  167. printf("testujem ci prisiel niekto konzultovat \n");
  168. pthread_mutex_lock(&sedacky_mutex);
  169.  
  170. if(isEmpty()) {
  171. ret = 1;
  172. printf("zistujem ze naozaj nik neprisiel \n");
  173. pthread_mutex_unlock(&sedacky_mutex);
  174.  
  175. return ret;
  176. }
  177. printf("a niekto naozaj prisiel \n");
  178. pthread_mutex_unlock(&sedacky_mutex);
  179.  
  180. return ret;
  181. }
  182.  
  183. int cakaj_na_sedacke(int idcko) {
  184. int ret = 0;
  185.  
  186. pthread_mutex_lock(&sedacky_mutex);
  187.  
  188. if(isFull()) {
  189. ret = 1;
  190. pthread_mutex_unlock(&sedacky_mutex);
  191.  
  192. return ret;
  193. }
  194. printf("Som student %d . a cakam na sedacke \n",idcko);
  195. insert(idcko);
  196. printf("______NA SEDACKE CAKAJU STUDENTI: _________ \n");
  197. printOut();
  198. printf("___________________________________________\n");
  199. pthread_mutex_unlock(&sedacky_mutex);
  200. sem_wait(&sem_sedacky);
  201. sem_wait(&sem_studentov[idcko]);
  202. return ret;
  203. }
  204.  
  205. int pusti_do_kancelarie_dalsieho(int idcka[]) { //DOROBIT!
  206.  
  207. int ret = 1;
  208. int pusti;
  209.  
  210. pthread_mutex_lock(&sedacky_mutex);
  211.  
  212. if(isEmpty()) {
  213. pthread_mutex_unlock(&sedacky_mutex);
  214. ret = 0;
  215.  
  216. return ret;
  217. }
  218. pusti = remove_a();
  219. pthread_mutex_unlock(&sedacky_mutex);
  220. sem_post(&sem_sedacky);
  221. sem_post(&sem_studentov[pusti]);
  222.  
  223. return ret;
  224. }
  225.  
  226. void programuj(int idcko) {
  227. double time;
  228.  
  229. time = random() % (SLEEP_TO - SLEEP_FROM) + SLEEP_FROM;
  230. printf("Som student %d . a programujem %lf milisekund \n", idcko,time);
  231. usleep(time);
  232. }
  233.  
  234. void chod_spat() { //NEPOUZITA
  235. double time;
  236.  
  237. time = random() % (SLEEP_TO - SLEEP_FROM) + SLEEP_FROM;
  238. printf("spim %lf milisekund \n", time);
  239. usleep(time);
  240. }
  241.  
  242. void zobud_ak_spi(int idcko) {
  243. printf("Som student %d . a prave zobudzam ucitela \n",idcko);
  244. pthread_mutex_lock(&spanok_mutex);
  245.  
  246. if(ucitel_spi == 1) {
  247. sem_post(&sem_spanku);
  248. ucitel_spi = 0;
  249. printf("Student: Ucitel zobudeny ... \n");
  250. }
  251. pthread_mutex_unlock(&spanok_mutex);
  252. }
  253.  
  254.  
  255. void chod_konzultovat(int idcko) {
  256. zobud_ak_spi(idcko);
  257. if(cakaj_na_sedacke(idcko) == 0) {
  258. return;
  259. }
  260.  
  261. while(1) {
  262. pthread_mutex_lock(&konzultacia_mutex);
  263. if(prebieha_konzultacia == 0) {
  264. pthread_mutex_unlock(&konzultacia_mutex);
  265. printf("koniec konzultacie \n");
  266. //koniec id???
  267. break;
  268. }
  269. pthread_mutex_unlock(&konzultacia_mutex);
  270. }
  271. }
  272.  
  273. void konzultuj() {
  274. double time;
  275.  
  276. time = random() % (SLEEP_TO - SLEEP_FROM) + SLEEP_FROM;
  277. printf("konzultujem %lf milisekund \n",time);
  278. usleep(time);
  279.  
  280. pthread_mutex_lock(&konzultacia_mutex);
  281. prebieha_konzultacia = 1;
  282. pthread_mutex_unlock(&konzultacia_mutex);
  283. }
  284.  
  285. void zaspi() {
  286. pthread_mutex_lock(&spanok_mutex);
  287. ucitel_spi = 1;
  288. chod_spat();
  289. pthread_mutex_unlock(&spanok_mutex);
  290. printf("Ucitel: spim ..... \n");
  291. sem_wait(&sem_spanku);
  292. printf("Ucitel: bol som zobudeny \n");
  293. }
  294.  
  295. void *student_main(void* p) {
  296. int* idcko = (int*)p;
  297.  
  298. while(1) {
  299. programuj(*idcko);
  300. chod_konzultovat(*idcko);
  301. }
  302. }
  303.  
  304. void *ucitel_main(void* p) {
  305. int *idcka = (void*)p;
  306.  
  307. printf("som ucitel--------------\n");
  308. while(1) {
  309. if(nik_neprisiel_konzultovat() == 1) {
  310. printf("Ucitel: nik neprisiel konzultovat,idem spat \n");
  311. zaspi();
  312. }
  313. pthread_mutex_lock(&konzultacia_mutex);
  314. pusti_do_kancelarie_dalsieho(idcka);
  315. prebieha_konzultacia = 1;
  316. pthread_mutex_unlock(&konzultacia_mutex);
  317. konzultuj();
  318. }
  319. }
  320.  
  321. int main() {
  322. int i;
  323. int id_studentov[POCET_STUDENTOV];
  324.  
  325. pthread_t ucitel;
  326. pthread_t studenti[POCET_STUDENTOV];
  327.  
  328. srandom(time(NULL));
  329.  
  330. init();
  331.  
  332. pthread_create(&ucitel, NULL,ucitel_main, (void*)id_studentov );
  333. for(i = 0; i < POCET_STUDENTOV; i++) {
  334. sem_init(&sem_studentov[i],0 ,0);
  335. id_studentov[i] = i;
  336. pthread_create(&studenti[i], NULL,student_main, (void*)(&id_studentov[i]) );
  337. }
  338. for(i = 0 ; i < POCET_STUDENTOV ; i++)
  339. {
  340. pthread_join(studenti[i],NULL);
  341. }
  342. pthread_join(ucitel,NULL);
  343.  
  344. return 0;
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement