如何将幼儿园班与条件变量同步

时间:2019-05-04 16:00:12

标签: c synchronization

作业要求我们两次编写代码 1)仅使用互斥锁/锁,共享变量和信号量✅ 2)仅使用互斥锁/锁,共享变量和条件变量。

我和我的小组成员对信号量没有任何问题,但都在努力将它们换成条件变量

我已经实现了信号量,但是无法找出条件变量

void teacher_enter(){
    sem_post(&teacher);
    printf("Teacher number %u arrived...", (int) pthread_self());
}

void teach(){
    printf("Teacher number %u started teaching. Teachin will take 5 seconds...", (int) pthread_self());
    sleep(5);
}

void child_enter(){
    sem_post(&child);
    printf("Child number %u enteredthe classroom...", (int) pthread_self());
}

void child_exit(){
    printf("Child number %u left...", (int) pthread_self());
}

void learn(){
    printf("Child number %u is learning.Learning will take 5 seconds...",  (int) pthread_self());
    sleep(5);
}

void go_home(){
    printf("Teacher number %u is going gome...", (int) pthread_self());
    pthread_exit(0);
}

void parent_enter(){
    printf("Parent number %u arrived...", (int) pthread_self());
}

void parent_exit(){
    printf("Parent number %u left...",  (int) pthread_self());
}

void verify_compilance(){

    int currentTeachers;
    sem_getvalue(&teacher, &currentTeachers);

    int currentChildren;
    sem_getvalue(&child, &currentChildren);

    float currentRatio;

    if(currentTeachers != 0){
        currentRatio = currentChildren/currentTeachers;
    }
    else{
        if(currentChildren == 0){
            currentRatio = 0;
        }
        else{
            currentRatio = 999999;
        }
    }
    sem_post(&ratio_mutex);
    if(currentRatio <= ratio){
        printf("Parents number %u is happy compilance is met...", (int) pthread_self());
        parent_exit();
    }
    else{
        printf("Parents number %u is not satisfied with compilance...", (int) pthread_self());
        parent_exit();
    }
}

void teacher_exit(){
    int currentTeachers;
    sem_getvalue(&teacher, &currentTeachers);

    int currentChildren;
    sem_getvalue(&child, &currentChildren);

    float currentRatio;

    if(currentTeachers != 0){
        currentRatio = currentChildren/currentTeachers;
    }
    else{
        if(currentChildren == 0){
            currentRatio = 0;
        }
        else{
            currentRatio = 999999;
        }
    }

    if(currentRatio <= ratio){
        sem_wait(&teacher);
        printf("The required ratio is met so teacher %u can leave...", (int) pthread_self());
    }
    else{
        printf("Teacher number %u can't leave because the required ratio is not met...", (int) pthread_self());
        teach();
        teacher_exit(); //teacher will try leaving again...
    }

}

void Teacher(){
    for(;;){
        teacher_enter();
        sem_wait(&teacher_mutex);
        teach();
        sem_post(&teacher_mutex);
        go_home();
    }
}


void Parent(){
    for(;;){
        parent_enter();
        //Critical section...
        verify_compilance();
        parent_exit();
        go_home();
    }
}

void Child(){
    for(;;){
        child_enter();
        //Critical section...
        learn();
        child_exit();
        go_home();
    }
}

0 个答案:

没有答案