解题思路:
为了保证调度顺序,我们需要让要求先运行的线程先运行,而其他的线程先挂起,等待第一线程运行后,唤醒第二线程,这里采用c的信号量来实现。
c详细代码:
typedef struct {
// User defined data may be declared here.
} Foo;
sem_t b,c;
Foo* fooCreate() {
Foo* obj = (Foo*) malloc(sizeof(Foo));
sem_init(&b,1,0);
sem_init(&c,1,0);
// Initialize user defined data here.
return obj;
}
void first(Foo* obj) {
// printFirst() outputs "first". Do not change or remove this line.
printFirst();
sem_post(&b);
}
void second(Foo* obj) {
sem_wait(&b);
// printSecond() outputs "second". Do not change or remove this line.
printSecond();
sem_post(&c);
}
void third(Foo* obj) {
sem_wait(&c);
// printThird() outputs "third". Do not change or remove this line.
printThird();
}
void fooFree(Foo* obj) {
// User defined data may be cleaned up here.
}