Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <semaphore.h>
- #include <pthread.h>
- #include <time.h>
- #define SLEEP_TO 100
- #define SLEEP_FROM 20
- #define KAPACITA 3
- #define POCET_STUDENTOV 10
- sem_t prvy_semafor; //pocet aktualnych,buffer
- sem_t druhy_semafor; //pocet volnych,buffer
- sem_t sem_studentov[POCET_STUDENTOV];
- sem_t sem_sedacky;
- sem_t sem_spanku;
- pthread_mutex_t buffer_mutex;
- pthread_mutex_t sedacky_mutex;
- pthread_mutex_t spanok_mutex;
- pthread_mutex_t konzultacia_mutex;
- int buffer[KAPACITA];
- int na_rade = 0;
- int prve_volne = 0;
- int ucitel_spi = 1;
- int prebieha_konzultacia = 0;
- void init() {
- int i;
- sem_init(&prvy_semafor,0,0);
- sem_init(&druhy_semafor,0,KAPACITA);
- pthread_mutex_init(&buffer_mutex,NULL);
- pthread_mutex_init(&sedacky_mutex,NULL);
- sem_init(&sem_sedacky,0,KAPACITA);
- pthread_mutex_init(&spanok_mutex,NULL);
- sem_init(&sem_spanku,0,0);
- pthread_mutex_init(&konzultacia_mutex,NULL);
- for(i = 0; i < KAPACITA; i++) {
- buffer[i] = 0;
- }
- }
- void insert(int element) {
- sem_wait(&druhy_semafor);
- pthread_mutex_lock(&buffer_mutex);
- if(buffer == NULL) {
- pthread_mutex_unlock(&buffer_mutex);
- return;
- }
- buffer[prve_volne] = element;
- prve_volne++;
- if(prve_volne == KAPACITA) {
- prve_volne = 0;
- }
- sem_post(&prvy_semafor);
- pthread_mutex_unlock(&buffer_mutex);
- }
- int remove_a() {
- int ret;
- sem_wait(&prvy_semafor);
- pthread_mutex_lock(&buffer_mutex);
- if(buffer == NULL) {
- pthread_mutex_unlock(&buffer_mutex);
- return -1;
- }
- printf("vyberam si prvok: %d \n",buffer[na_rade]);
- ret = buffer[na_rade];
- buffer[na_rade] = 0;
- na_rade++;
- if(na_rade == KAPACITA) {
- na_rade = 0;
- }
- sem_post(&druhy_semafor);
- pthread_mutex_unlock(&buffer_mutex);
- return ret;
- }
- int isFull() { //ak je plne vratim jednotku
- pthread_mutex_lock(&buffer_mutex);
- int i;
- int flag = 0;
- if(buffer == NULL) {
- pthread_mutex_unlock(&buffer_mutex);
- return -1;
- }
- for(i = 0; i < KAPACITA; i++) {
- if(buffer[i] == 0) {
- flag = 1;
- break;
- }
- }
- if(flag == 0 ) {
- pthread_mutex_unlock(&buffer_mutex);
- return 1;
- }
- else {
- pthread_mutex_unlock(&buffer_mutex);
- return 0;
- }
- }
- int isEmpty() { //ak je prazdne vratim jednotku
- pthread_mutex_lock(&buffer_mutex);
- int i;
- int flag = 0;
- if(buffer == NULL) {
- pthread_mutex_unlock(&buffer_mutex);
- return -1;
- }
- for(i = 0; i < KAPACITA; i++) {
- if(buffer[i] != 0){
- flag = 1;
- }
- }
- if(flag == 0 ) {
- pthread_mutex_unlock(&buffer_mutex);
- return 1;
- }
- else {
- pthread_mutex_unlock(&buffer_mutex);
- return 0;
- }
- }
- void printOut() {
- int i;
- for(i = 0; i < KAPACITA; i++) {
- printf("buffer--- pozicia %d hodnota----%d \n",i,buffer[i]);
- }
- }
- int nik_neprisiel_konzultovat() {
- int ret = 0;
- printf("testujem ci prisiel niekto konzultovat \n");
- pthread_mutex_lock(&sedacky_mutex);
- if(isEmpty()) {
- ret = 1;
- printf("zistujem ze naozaj nik neprisiel \n");
- pthread_mutex_unlock(&sedacky_mutex);
- return ret;
- }
- printf("a niekto naozaj prisiel \n");
- pthread_mutex_unlock(&sedacky_mutex);
- return ret;
- }
- int cakaj_na_sedacke(int idcko) {
- int ret = 0;
- pthread_mutex_lock(&sedacky_mutex);
- if(isFull()) {
- ret = 1;
- pthread_mutex_unlock(&sedacky_mutex);
- return ret;
- }
- printf("Som student %d . a cakam na sedacke \n",idcko);
- insert(idcko);
- printf("______NA SEDACKE CAKAJU STUDENTI: _________ \n");
- printOut();
- printf("___________________________________________\n");
- pthread_mutex_unlock(&sedacky_mutex);
- sem_wait(&sem_sedacky);
- sem_wait(&sem_studentov[idcko]);
- return ret;
- }
- int pusti_do_kancelarie_dalsieho(int idcka[]) { //DOROBIT!
- int ret = 1;
- int pusti;
- pthread_mutex_lock(&sedacky_mutex);
- if(isEmpty()) {
- pthread_mutex_unlock(&sedacky_mutex);
- ret = 0;
- return ret;
- }
- pusti = remove_a();
- pthread_mutex_unlock(&sedacky_mutex);
- sem_post(&sem_sedacky);
- sem_post(&sem_studentov[pusti]);
- return ret;
- }
- void programuj(int idcko) {
- double time;
- time = random() % (SLEEP_TO - SLEEP_FROM) + SLEEP_FROM;
- printf("Som student %d . a programujem %lf milisekund \n", idcko,time);
- usleep(time);
- }
- void chod_spat() { //NEPOUZITA
- double time;
- time = random() % (SLEEP_TO - SLEEP_FROM) + SLEEP_FROM;
- printf("spim %lf milisekund \n", time);
- usleep(time);
- }
- void zobud_ak_spi(int idcko) {
- printf("Som student %d . a prave zobudzam ucitela \n",idcko);
- pthread_mutex_lock(&spanok_mutex);
- if(ucitel_spi == 1) {
- sem_post(&sem_spanku);
- ucitel_spi = 0;
- printf("Student: Ucitel zobudeny ... \n");
- }
- pthread_mutex_unlock(&spanok_mutex);
- }
- void chod_konzultovat(int idcko) {
- zobud_ak_spi(idcko);
- if(cakaj_na_sedacke(idcko) == 0) {
- return;
- }
- while(1) {
- pthread_mutex_lock(&konzultacia_mutex);
- if(prebieha_konzultacia == 0) {
- pthread_mutex_unlock(&konzultacia_mutex);
- printf("koniec konzultacie \n");
- //koniec id???
- break;
- }
- pthread_mutex_unlock(&konzultacia_mutex);
- }
- }
- void konzultuj() {
- double time;
- time = random() % (SLEEP_TO - SLEEP_FROM) + SLEEP_FROM;
- printf("konzultujem %lf milisekund \n",time);
- usleep(time);
- pthread_mutex_lock(&konzultacia_mutex);
- prebieha_konzultacia = 1;
- pthread_mutex_unlock(&konzultacia_mutex);
- }
- void zaspi() {
- pthread_mutex_lock(&spanok_mutex);
- ucitel_spi = 1;
- chod_spat();
- pthread_mutex_unlock(&spanok_mutex);
- printf("Ucitel: spim ..... \n");
- sem_wait(&sem_spanku);
- printf("Ucitel: bol som zobudeny \n");
- }
- void *student_main(void* p) {
- int* idcko = (int*)p;
- while(1) {
- programuj(*idcko);
- chod_konzultovat(*idcko);
- }
- }
- void *ucitel_main(void* p) {
- int *idcka = (void*)p;
- printf("som ucitel--------------\n");
- while(1) {
- if(nik_neprisiel_konzultovat() == 1) {
- printf("Ucitel: nik neprisiel konzultovat,idem spat \n");
- zaspi();
- }
- pthread_mutex_lock(&konzultacia_mutex);
- pusti_do_kancelarie_dalsieho(idcka);
- prebieha_konzultacia = 1;
- pthread_mutex_unlock(&konzultacia_mutex);
- konzultuj();
- }
- }
- int main() {
- int i;
- int id_studentov[POCET_STUDENTOV];
- pthread_t ucitel;
- pthread_t studenti[POCET_STUDENTOV];
- srandom(time(NULL));
- init();
- pthread_create(&ucitel, NULL,ucitel_main, (void*)id_studentov );
- for(i = 0; i < POCET_STUDENTOV; i++) {
- sem_init(&sem_studentov[i],0 ,0);
- id_studentov[i] = i;
- pthread_create(&studenti[i], NULL,student_main, (void*)(&id_studentov[i]) );
- }
- for(i = 0 ; i < POCET_STUDENTOV ; i++)
- {
- pthread_join(studenti[i],NULL);
- }
- pthread_join(ucitel,NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement