我正在尝试模拟 FCFS 调度程序,我这样做的方式是当一个线程进入它时,如果它不在队列中,我将它推送到队列中,但如果它是然后检查线程是否是在队列的头部(先入)并且作业的剩余时间> 0。我的问题是如何将线程置于等待状态,直到它成为队列的头部?我听说条件变量可能会有所帮助,但不确定它们是如何工作的。
if (!(is_in_queue(ready_queue, tid))) { //thread is not in the queue
pthread_mutex_lock(&schedule_lock);
ready_queue = enqueue(ready_queue, currentTime, tid, remainingTime, tprio);
pthread_mutex_unlock(&schedule_lock);
}
else{ //thread is in queue
if (tid == head_of_queue(ready_queue) && remainingTime >0) { //need to schedule this thread for full time
return currentTime +1; //schedule on the next unit of "time"
}
else if (tid == head_of_queue(ready_queue) && remainingTime == 0){ //need to go to next task
pthread_mutex_lock(&schedule_lock);
ready_queue = dequeue(ready_queue);
pthread_mutex_unlock(&schedule_lock);
}
else{ //not at the head of the queue
//How do i wait until it is at the head??
}
}